8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
authorsdama
Wed, 26 Jul 2017 04:25:23 +0530
changeset 45950 720bc0f5ba9d
parent 45949 d5425a7c270e
child 45951 e5fbaa96e81b
8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class Summary: Added support for using BuildInEditorProvider service implemenation provided by jdk.editpad module Reviewed-by: hannesw, jlaskey Contributed-by: srinivas.dama@oracle.com
nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/EditObject.java
nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/EditPad.java
nashorn/src/jdk.scripting.nashorn.shell/share/classes/module-info.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/tools/resources/Shell.properties
--- a/nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/EditObject.java	Thu Jul 20 18:17:12 2017 +0000
+++ b/nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/EditObject.java	Wed Jul 26 04:25:23 2017 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -29,7 +29,9 @@
 import java.util.HashSet;
 import java.util.Set;
 import java.util.function.Consumer;
+import java.util.ServiceLoader;
 import jdk.nashorn.api.scripting.AbstractJSObject;
+import jdk.internal.editor.spi.BuildInEditorProvider;
 import jdk.nashorn.internal.runtime.JSType;
 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
 
@@ -116,7 +118,24 @@
         if (editor != null && !editor.isEmpty()) {
             ExternalEditor.edit(editor, errorHandler, initText, saveHandler, console);
         } else if (! Main.HEADLESS) {
-            EditPad.edit(errorHandler, initText, saveHandler);
+            try {
+                ServiceLoader<BuildInEditorProvider> sl
+                        = ServiceLoader.load(BuildInEditorProvider.class);
+                //find the highest ranking provider
+                BuildInEditorProvider provider = null;
+                for (BuildInEditorProvider p : sl){
+                    if (provider == null || p.rank() > provider.rank()) {
+                        provider = p;
+                    }
+                }
+                if (provider != null) {
+                    provider.edit(null, initText, saveHandler, errorHandler);
+                } else {
+                    errorHandler.accept(Main.getMessage("jjs.err.no.builtin.editor"));
+                }
+            } catch (RuntimeException ex) {
+                errorHandler.accept(Main.getMessage("jjs.err.cant.launch.editor"));
+            }
         } else {
             errorHandler.accept(Main.getMessage("no.editor"));
         }
--- a/nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/EditPad.java	Thu Jul 20 18:17:12 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,136 +0,0 @@
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package jdk.nashorn.tools.jjs;
-
-import java.awt.BorderLayout;
-import java.awt.FlowLayout;
-import java.awt.event.KeyEvent;
-import java.awt.event.WindowAdapter;
-import java.awt.event.WindowEvent;
-import java.util.function.Consumer;
-import javax.swing.JButton;
-import javax.swing.JFrame;
-import javax.swing.JPanel;
-import javax.swing.JScrollPane;
-import javax.swing.JTextArea;
-import javax.swing.SwingUtilities;
-
-/**
- * A minimal Swing editor as a fallback when the user does not specify an
- * external editor.
- */
-final class EditPad extends JFrame implements Runnable {
-    private static final long serialVersionUID = 1;
-    private final Consumer<String> errorHandler;
-    private final String initialText;
-    private final boolean[] closeLock;
-    private final Consumer<String> saveHandler;
-
-    EditPad(final Consumer<String> errorHandler, final String initialText,
-            final boolean[] closeLock, final Consumer<String> saveHandler) {
-        super("Edit Pad (Experimental)");
-        this.errorHandler = errorHandler;
-        this.initialText = initialText;
-        this.closeLock = closeLock;
-        this.saveHandler = saveHandler;
-    }
-
-    @Override
-    public void run() {
-        addWindowListener(new WindowAdapter() {
-            @Override
-            public void windowClosing(final WindowEvent e) {
-                EditPad.this.dispose();
-                notifyClose();
-            }
-        });
-        setLocationRelativeTo(null);
-        setLayout(new BorderLayout());
-        JTextArea textArea = new JTextArea(initialText);
-        add(new JScrollPane(textArea), BorderLayout.CENTER);
-        add(buttons(textArea), BorderLayout.SOUTH);
-
-        setSize(800, 600);
-        setVisible(true);
-    }
-
-    private JPanel buttons(final JTextArea textArea) {
-        FlowLayout flow = new FlowLayout();
-        flow.setHgap(35);
-        JPanel buttons = new JPanel(flow);
-        JButton cancel = new JButton("Cancel");
-        cancel.setMnemonic(KeyEvent.VK_C);
-        JButton accept = new JButton("Accept");
-        accept.setMnemonic(KeyEvent.VK_A);
-        JButton exit = new JButton("Exit");
-        exit.setMnemonic(KeyEvent.VK_X);
-        buttons.add(cancel);
-        buttons.add(accept);
-        buttons.add(exit);
-
-        cancel.addActionListener(e -> {
-            close();
-        });
-        accept.addActionListener(e -> {
-            saveHandler.accept(textArea.getText());
-        });
-        exit.addActionListener(e -> {
-            saveHandler.accept(textArea.getText());
-            close();
-        });
-
-        return buttons;
-    }
-
-    private void close() {
-        setVisible(false);
-        dispose();
-        notifyClose();
-    }
-
-    private void notifyClose() {
-        synchronized (closeLock) {
-            closeLock[0] = true;
-            closeLock.notify();
-        }
-    }
-
-    static void edit(final Consumer<String> errorHandler, final String initialText,
-            final Consumer<String> saveHandler) {
-        boolean[] closeLock = new boolean[1];
-        SwingUtilities.invokeLater(
-                new EditPad(errorHandler, initialText, closeLock, saveHandler));
-        synchronized (closeLock) {
-            while (!closeLock[0]) {
-                try {
-                    closeLock.wait();
-                } catch (final InterruptedException ex) {
-                    // ignore and loop
-                }
-            }
-        }
-    }
-}
--- a/nashorn/src/jdk.scripting.nashorn.shell/share/classes/module-info.java	Thu Jul 20 18:17:12 2017 +0000
+++ b/nashorn/src/jdk.scripting.nashorn.shell/share/classes/module-info.java	Wed Jul 26 04:25:23 2017 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -42,5 +42,7 @@
     requires java.desktop;
     requires jdk.internal.le;
     requires jdk.scripting.nashorn;
+    requires jdk.internal.ed;
+    uses jdk.internal.editor.spi.BuildInEditorProvider;
 }
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/tools/resources/Shell.properties	Thu Jul 20 18:17:12 2017 +0000
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/tools/resources/Shell.properties	Wed Jul 26 04:25:23 2017 +0530
@@ -32,3 +32,7 @@
 shell.prompt2=...> 
 
 no.editor=AWT Headless mode set and no external editor is configured!
+
+jjs.err.no.builtin.editor=Built-in editor not available.
+
+jjs.err.cant.launch.editor=Cannot launch built-in editor -- unexpected exception: {0}