changeset 1389:fa839eaa06b0

8134381: load completion should not use swing from non UI thread Reviewed-by: lagergren, attila, hannesw
author sundar
date Tue, 25 Aug 2015 14:47:42 +0530
parents 9379e33273fe
children 4577d801c522
files src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/NashornCompleter.java
diffstat 1 files changed, 29 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/NashornCompleter.java	Mon Aug 24 18:25:22 2015 +0530
+++ b/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) {