8134381: load completion should not use swing from non UI thread
authorsundar
Tue, 25 Aug 2015 14:47:42 +0530
changeset 32319 3f22ddeabc0e
parent 32318 3279b026c98a
child 32320 3e408f16c1c9
8134381: load completion should not use swing from non UI thread Reviewed-by: lagergren, attila, hannesw
nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/NashornCompleter.java
--- a/nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/NashornCompleter.java	Mon Aug 24 18:25:22 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/NashornCompleter.java	Tue Aug 25 14:47:42 2015 +0530
@@ -29,9 +29,12 @@
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.FutureTask;
 import java.util.regex.Pattern;
 import javax.swing.JFileChooser;
 import javax.swing.filechooser.FileNameExtensionFilter;
+import javax.swing.SwingUtilities;
 import jdk.internal.jline.console.completer.Completer;
 import jdk.internal.jline.console.UserInterruptException;
 import jdk.nashorn.api.tree.AssignmentTree;
@@ -219,12 +222,8 @@
         if (topExpr == null) {
             // special case for load call that looks like "load(" with optional whitespaces
             if (LOAD_CALL.matcher(test).matches()) {
-                // throw a file dialog box
-                final JFileChooser chooser = new JFileChooser();
-                chooser.setFileFilter(new FileNameExtensionFilter("JavaScript Files", "js"));
-                int retVal = chooser.showOpenDialog(null);
-                if (retVal == JFileChooser.APPROVE_OPTION) {
-                    String name = chooser.getSelectedFile().getAbsolutePath();
+                String name = readFileName(context.getErr());
+                if (name != null) {
                     // handle '\' file separator
                     if (BACKSLASH_FILE_SEPARATOR) {
                         name = name.replace("\\", "\\\\");
@@ -253,6 +252,30 @@
 
     // Internals only below this point
 
+    // read file name from the user using by showing a swing file chooser diablog
+    private static String readFileName(final PrintWriter err) {
+        final FutureTask<String> fileChooserTask = new FutureTask<String>(() -> {
+            // show a file chooser dialog box
+            final JFileChooser chooser = new JFileChooser();
+            chooser.setFileFilter(new FileNameExtensionFilter("JavaScript Files", "js"));
+            final int retVal = chooser.showOpenDialog(null);
+            return retVal == JFileChooser.APPROVE_OPTION ?
+                chooser.getSelectedFile().getAbsolutePath() : null;
+        });
+
+        SwingUtilities.invokeLater(fileChooserTask);
+
+        try {
+            return fileChooserTask.get();
+        } catch (final ExecutionException | InterruptedException e) {
+            err.println(e);
+            if (Main.DEBUG) {
+                e.printStackTrace();
+            }
+        }
+        return null;
+    }
+
     // fill properties of the incomplete member expression
     private int completeMemberSelect(final String exprStr, final int cursor, final List<CharSequence> result,
                 final MemberSelectTree select, final boolean endsWithDot) {