8168972: Editor support: move built-in and external editor support to the jdk repo
8167639: jshell tool: Edit Pad has readability issues
Reviewed-by: jlahoda
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/jdk.editpad/share/classes/jdk/editpad/EditPad.java Wed Nov 02 16:24:43 2016 -0700
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2015, 2016, 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.editpad;
+
+import java.awt.BorderLayout;
+import java.awt.FlowLayout;
+import java.awt.Font;
+import java.awt.event.ActionListener;
+import java.awt.event.KeyEvent;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+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;
+
+/**
+ * A minimal Swing editor as a fallback when the user does not specify an
+ * external editor.
+ */
+class EditPad implements Runnable {
+
+ private static final String L10N_RB_NAME = "jdk.editpad.resources.l10n";
+ private ResourceBundle rb = null;
+ private final String windowLabel;
+ private final Consumer<String> errorHandler;
+ private final String initialText;
+ private final Runnable closeMark;
+ private final Consumer<String> saveHandler;
+
+ /**
+ * Create an Edit Pad minimal editor.
+ *
+ * @param windowLabel the label string for the Edit Pad window
+ * @param errorHandler a handler for unexpected errors
+ * @param initialText the source to load in the Edit Pad
+ * @param closeMark a Runnable that is run when Edit Pad closes
+ * @param saveHandler a handler for changed source (sent the full source)
+ */
+ EditPad(String windowLabel, Consumer<String> errorHandler, String initialText,
+ Runnable closeMark, Consumer<String> saveHandler) {
+ this.windowLabel = windowLabel;
+ this.errorHandler = errorHandler;
+ this.initialText = initialText;
+ this.closeMark = closeMark;
+ this.saveHandler = saveHandler;
+ }
+
+ @Override
+ public void run() {
+ JFrame jframe = new JFrame(windowLabel == null
+ ? getResourceString("editpad.name")
+ : windowLabel);
+ Runnable closer = () -> {
+ jframe.setVisible(false);
+ jframe.dispose();
+ closeMark.run();
+ };
+ jframe.addWindowListener(new WindowAdapter() {
+ @Override
+ public void windowClosing(WindowEvent e) {
+ closer.run();
+ }
+ });
+ jframe.setLocationRelativeTo(null);
+ jframe.setLayout(new BorderLayout());
+ JTextArea textArea = new JTextArea(initialText);
+ textArea.setFont(new Font("monospaced", Font.PLAIN, 13));
+ jframe.add(new JScrollPane(textArea), BorderLayout.CENTER);
+ jframe.add(buttons(closer, textArea), BorderLayout.SOUTH);
+
+ jframe.setSize(800, 600);
+ jframe.setVisible(true);
+ }
+
+ private JPanel buttons(Runnable closer, JTextArea textArea) {
+ FlowLayout flow = new FlowLayout();
+ flow.setHgap(35);
+ JPanel buttons = new JPanel(flow);
+ addButton(buttons, "editpad.cancel", KeyEvent.VK_C, e -> {
+ closer.run();
+ });
+ addButton(buttons, "editpad.accept", KeyEvent.VK_A, e -> {
+ saveHandler.accept(textArea.getText());
+ });
+ addButton(buttons, "editpad.exit", KeyEvent.VK_X, e -> {
+ saveHandler.accept(textArea.getText());
+ closer.run();
+ });
+ return buttons;
+ }
+
+ private void addButton(JPanel buttons, String rkey, int mnemonic, ActionListener action) {
+ JButton but = new JButton(getResourceString(rkey));
+ but.setMnemonic(mnemonic);
+ buttons.add(but);
+ but.addActionListener(action);
+ }
+
+ private String getResourceString(String key) {
+ if (rb == null) {
+ try {
+ rb = ResourceBundle.getBundle(L10N_RB_NAME);
+ } catch (MissingResourceException mre) {
+ error("Cannot find ResourceBundle: %s", L10N_RB_NAME);
+ return "";
+ }
+ }
+ String s;
+ try {
+ s = rb.getString(key);
+ } catch (MissingResourceException mre) {
+ error("Missing resource: %s in %s", key, L10N_RB_NAME);
+ return "";
+ }
+ return s;
+ }
+
+ private void error(String fmt, Object... args) {
+ errorHandler.accept(String.format(fmt, args));
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/jdk.editpad/share/classes/jdk/editpad/EditPadProvider.java Wed Nov 02 16:24:43 2016 -0700
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2016, 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.editpad;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.function.Consumer;
+import javax.swing.SwingUtilities;
+import jdk.internal.editor.spi.BuildInEditorProvider;
+
+/**
+ * Defines the provider of an Edit Pad implementation.
+ *
+ * @author Robert Field
+ */
+public class EditPadProvider implements BuildInEditorProvider {
+
+ /**
+ * @return the rank of a provider, greater is better.
+ */
+ @Override
+ public int rank() {
+ return 5;
+ }
+
+ /**
+ * Create an Edit Pad minimal editor.
+ *
+ * @param windowLabel the label string for the Edit Pad window, or null,
+ * for default window label
+ * @param initialText the source to load in the Edit Pad
+ * @param saveHandler a handler for changed source (can be sent the full source)
+ * @param errorHandler a handler for unexpected errors
+ */
+ @Override
+ public void edit(String windowLabel, String initialText,
+ Consumer<String> saveHandler, Consumer<String> errorHandler) {
+ CountDownLatch closeLock = new CountDownLatch(1);
+ SwingUtilities.invokeLater(
+ new EditPad(windowLabel, errorHandler, initialText, closeLock::countDown, saveHandler));
+ do {
+ try {
+ closeLock.await();
+ break;
+ } catch (InterruptedException ex) {
+ // ignore and loop
+ }
+ } while (true);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/jdk.editpad/share/classes/jdk/editpad/resources/l10n.properties Wed Nov 02 16:24:43 2016 -0700
@@ -0,0 +1,29 @@
+#
+# Copyright (c) 2016, 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.
+#
+
+editpad.name = Edit Pad
+editpad.cancel = Cancel
+editpad.accept = Accept
+editpad.exit = Exit
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/jdk.editpad/share/classes/module-info.java Wed Nov 02 16:24:43 2016 -0700
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2016, 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.
+ */
+
+/**
+ * Implementation of the edit pad service.
+ */
+module jdk.editpad {
+ requires jdk.internal.ed;
+ requires java.desktop;
+ provides jdk.internal.editor.spi.BuildInEditorProvider
+ with jdk.editpad.EditPadProvider;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/jdk.internal.ed/share/classes/jdk/internal/editor/external/ExternalEditor.java Wed Nov 02 16:24:43 2016 -0700
@@ -0,0 +1,192 @@
+/*
+ * Copyright (c) 2015, 2016, 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.internal.editor.external;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.file.ClosedWatchServiceException;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.WatchKey;
+import java.nio.file.WatchService;
+import java.util.Arrays;
+import java.util.Scanner;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
+import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
+import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
+
+/**
+ * Wrapper for controlling an external editor.
+ */
+public class ExternalEditor {
+ private final Consumer<String> errorHandler;
+ private final Consumer<String> saveHandler;
+ private final boolean wait;
+
+ private final Runnable suspendInteractiveInput;
+ private final Runnable resumeInteractiveInput;
+ private final Runnable promptForNewLineToEndWait;
+
+ private WatchService watcher;
+ private Thread watchedThread;
+ private Path dir;
+ private Path tmpfile;
+
+ /**
+ * Launch an external editor.
+ *
+ * @param cmd the command to launch (with parameters)
+ * @param initialText initial text in the editor buffer
+ * @param errorHandler handler for error messages
+ * @param saveHandler handler sent the buffer contents on save
+ * @param suspendInteractiveInput a callback to suspend caller (shell) input
+ * @param resumeInteractiveInput a callback to resume caller input
+ * @param wait true, if editor process termination cannot be used to
+ * determine when done
+ * @param promptForNewLineToEndWait a callback to prompt for newline if
+ * wait==true
+ */
+ public static void edit(String[] cmd, String initialText,
+ Consumer<String> errorHandler,
+ Consumer<String> saveHandler,
+ Runnable suspendInteractiveInput,
+ Runnable resumeInteractiveInput,
+ boolean wait,
+ Runnable promptForNewLineToEndWait) {
+ ExternalEditor ed = new ExternalEditor(errorHandler, saveHandler, suspendInteractiveInput,
+ resumeInteractiveInput, wait, promptForNewLineToEndWait);
+ ed.edit(cmd, initialText);
+ }
+
+ ExternalEditor(Consumer<String> errorHandler,
+ Consumer<String> saveHandler,
+ Runnable suspendInteractiveInput,
+ Runnable resumeInteractiveInput,
+ boolean wait,
+ Runnable promptForNewLineToEndWait) {
+ this.errorHandler = errorHandler;
+ this.saveHandler = saveHandler;
+ this.wait = wait;
+ this.suspendInteractiveInput = suspendInteractiveInput;
+ this.resumeInteractiveInput = resumeInteractiveInput;
+ this.promptForNewLineToEndWait = promptForNewLineToEndWait;
+ }
+
+ private void edit(String[] cmd, String initialText) {
+ try {
+ setupWatch(initialText);
+ launch(cmd);
+ } catch (IOException ex) {
+ errorHandler.accept(ex.getMessage());
+ }
+ }
+
+ /**
+ * Creates a WatchService and registers the given directory
+ */
+ private void setupWatch(String initialText) throws IOException {
+ this.watcher = FileSystems.getDefault().newWatchService();
+ this.dir = Files.createTempDirectory("extedit");
+ this.tmpfile = Files.createTempFile(dir, null, ".java");
+ Files.write(tmpfile, initialText.getBytes(Charset.forName("UTF-8")));
+ dir.register(watcher,
+ ENTRY_CREATE,
+ ENTRY_DELETE,
+ ENTRY_MODIFY);
+ watchedThread = new Thread(() -> {
+ for (;;) {
+ WatchKey key;
+ try {
+ key = watcher.take();
+ } catch (ClosedWatchServiceException ex) {
+ // The watch service has been closed, we are done
+ break;
+ } catch (InterruptedException ex) {
+ // tolerate an interrupt
+ continue;
+ }
+
+ if (!key.pollEvents().isEmpty()) {
+ saveFile();
+ }
+
+ boolean valid = key.reset();
+ if (!valid) {
+ // The watch service has been closed, we are done
+ break;
+ }
+ }
+ });
+ watchedThread.start();
+ }
+
+ private void launch(String[] cmd) throws IOException {
+ String[] params = Arrays.copyOf(cmd, cmd.length + 1);
+ params[cmd.length] = tmpfile.toString();
+ ProcessBuilder pb = new ProcessBuilder(params);
+ pb = pb.inheritIO();
+
+ try {
+ suspendInteractiveInput.run();
+ Process process = pb.start();
+ // wait to exit edit mode in one of these ways...
+ if (wait) {
+ // -wait option -- ignore process exit, wait for carriage-return
+ Scanner scanner = new Scanner(System.in);
+ promptForNewLineToEndWait.run();
+ scanner.nextLine();
+ } else {
+ // wait for process to exit
+ process.waitFor();
+ }
+ } catch (IOException ex) {
+ errorHandler.accept("process IO failure: " + ex.getMessage());
+ } catch (InterruptedException ex) {
+ errorHandler.accept("process interrupt: " + ex.getMessage());
+ } finally {
+ try {
+ watcher.close();
+ watchedThread.join(); //so that saveFile() is finished.
+ saveFile();
+ } catch (InterruptedException ex) {
+ errorHandler.accept("process interrupt: " + ex.getMessage());
+ } finally {
+ resumeInteractiveInput.run();
+ }
+ }
+ }
+
+ private void saveFile() {
+ try {
+ saveHandler.accept(Files.lines(tmpfile).collect(Collectors.joining("\n", "", "\n")));
+ } catch (IOException ex) {
+ errorHandler.accept("Failure in read edit file: " + ex.getMessage());
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/jdk.internal.ed/share/classes/jdk/internal/editor/spi/BuildInEditorProvider.java Wed Nov 02 16:24:43 2016 -0700
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2016, 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.internal.editor.spi;
+
+import java.util.function.Consumer;
+
+/**
+ * Defines the provider of a built-in editor.
+ */
+public interface BuildInEditorProvider {
+
+ /**
+ * @return the rank of a provider, greater is better.
+ */
+ int rank();
+
+ /**
+ * Create a simple built-in editor.
+ *
+ * @param windowLabel the label string for the Edit Pad window, or null,
+ * for default window label
+ * @param initialText the source to load in the Edit Pad
+ * @param saveHandler a handler for changed source (can be sent the full source)
+ * @param errorHandler a handler for unexpected errors
+ */
+ void edit(String windowLabel, String initialText,
+ Consumer<String> saveHandler, Consumer<String> errorHandler);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/jdk.internal.ed/share/classes/module-info.java Wed Nov 02 16:24:43 2016 -0700
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2016, 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.
+ */
+
+/**
+ * Internal editor support for JDK tools. Includes the Service Provider
+ * Interface to built-in editors.
+ */
+module jdk.internal.ed {
+
+ exports jdk.internal.editor.spi to jdk.editpad, jdk.jshell, jdk.scripting.nashorn.shell;
+ exports jdk.internal.editor.external to jdk.jshell, jdk.scripting.nashorn.shell;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/jdk/editpad/EditPadTest.java Wed Nov 02 16:24:43 2016 -0700
@@ -0,0 +1,342 @@
+/*
+ * 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.
+ *
+ * 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.
+ */
+
+/*
+ * @test
+ * @bug 8167636 8167639 8168972
+ * @summary Testing built-in editor.
+ * @modules java.desktop/java.awt
+ * jdk.internal.ed/jdk.internal.editor.spi
+ * jdk.editpad/jdk.editpad
+ * @run testng EditPadTest
+ */
+
+import java.awt.AWTException;
+import java.awt.Component;
+import java.awt.Container;
+import java.awt.Dimension;
+import java.awt.Frame;
+import java.awt.GraphicsEnvironment;
+import java.awt.Point;
+import java.awt.Robot;
+import java.awt.event.InputEvent;
+import java.awt.event.WindowEvent;
+import java.lang.reflect.InvocationTargetException;
+import java.util.ServiceLoader;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+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.JViewport;
+import javax.swing.SwingUtilities;
+
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import jdk.internal.editor.spi.BuildInEditorProvider;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+@Test
+public class EditPadTest {
+
+ private static final int DELAY = 500;
+ private static final String WINDOW_LABEL = "Test Edit Pad";
+
+ private static ExecutorService executor;
+ private static Robot robot;
+ private static JFrame frame = null;
+ private static JTextArea area = null;
+ private static JButton cancel = null;
+ private static JButton accept = null;
+ private static JButton exit = null;
+
+ @BeforeClass
+ public static void setUpEditorPadTest() {
+ if (!GraphicsEnvironment.isHeadless()) {
+ try {
+ robot = new Robot();
+ robot.setAutoWaitForIdle(true);
+ robot.setAutoDelay(DELAY);
+ } catch (AWTException e) {
+ throw new ExceptionInInitializerError(e);
+ }
+ }
+ }
+
+ @AfterClass
+ public static void shutdown() {
+ executorShutdown();
+ }
+
+ public void testSimple() {
+ testEdit("abcdef", 1, "xyz",
+ () -> assertSource("abcdef"),
+ () -> writeSource("xyz"),
+ () -> accept(),
+ () -> assertSource("xyz"),
+ () -> shutdownEditor());
+ }
+
+ public void testCancel() {
+ testEdit("abcdef", 0, "abcdef",
+ () -> assertSource("abcdef"),
+ () -> writeSource("xyz"),
+ () -> cancel());
+ }
+
+ public void testAbort() {
+ testEdit("abcdef", 0, "abcdef",
+ () -> assertSource("abcdef"),
+ () -> writeSource("xyz"),
+ () -> shutdownEditor());
+ }
+
+ public void testAcceptCancel() {
+ testEdit("abcdef", 1, "xyz",
+ () -> assertSource("abcdef"),
+ () -> writeSource("xyz"),
+ () -> accept(),
+ () -> assertSource("xyz"),
+ () -> writeSource("!!!!!!!!!"),
+ () -> cancel());
+ }
+
+ public void testAcceptEdit() {
+ testEdit("abcdef", 2, "xyz",
+ () -> assertSource("abcdef"),
+ () -> writeSource("NoNo"),
+ () -> accept(),
+ () -> assertSource("NoNo"),
+ () -> writeSource("xyz"),
+ () -> exit());
+ }
+
+ private void testEdit(String initialText,
+ int savedCount, String savedText, Runnable... actions) {
+ class Handler {
+
+ String text = null;
+ int count = 0;
+
+ void handle(String s) {
+ ++count;
+ text = s;
+ }
+ }
+ Handler save = new Handler();
+ Handler error = new Handler();
+
+ if (GraphicsEnvironment.isHeadless()) {
+ // Do not actually run if we are headless
+ return;
+ }
+ Future<?> task = doActions(actions);
+ builtInEdit(initialText, save::handle, error::handle);
+ complete(task);
+ assertEquals(error.count, 0, "Error: " + error.text);
+ assertTrue(save.count != savedCount
+ || save.text == null
+ ? savedText != null
+ : savedText.equals(save.text),
+ "Expected " + savedCount + " saves, got " + save.count
+ + ", expected \"" + savedText + "\" got \"" + save.text + "\"");
+ }
+
+ private static ExecutorService getExecutor() {
+ if (executor == null) {
+ executor = Executors.newSingleThreadExecutor();
+ }
+ return executor;
+ }
+
+ private static void executorShutdown() {
+ if (executor != null) {
+ executor.shutdown();
+ executor = null;
+ }
+ }
+
+ private void builtInEdit(String initialText,
+ Consumer<String> saveHandler, Consumer<String> errorHandler) {
+ 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(WINDOW_LABEL,
+ initialText, saveHandler, errorHandler);
+ } else {
+ throw new InternalError("Cannot find provider");
+ }
+ }
+
+ private Future<?> doActions(Runnable... actions) {
+ return getExecutor().submit(() -> {
+ try {
+ waitForIdle();
+ SwingUtilities.invokeLater(this::seekElements);
+ waitForIdle();
+ for (Runnable act : actions) {
+ act.run();
+ }
+ } catch (Throwable e) {
+ shutdownEditor();
+ if (e instanceof AssertionError) {
+ throw (AssertionError) e;
+ }
+ throw new RuntimeException(e);
+ }
+ });
+ }
+
+ private void complete(Future<?> task) {
+ try {
+ task.get();
+ waitForIdle();
+ } catch (ExecutionException e) {
+ if (e.getCause() instanceof AssertionError) {
+ throw (AssertionError) e.getCause();
+ }
+ throw new RuntimeException(e);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ } finally {
+ shutdownEditor();
+ }
+ }
+
+ private void writeSource(String s) {
+ SwingUtilities.invokeLater(() -> area.setText(s));
+ }
+
+ private void assertSource(String expected) {
+ String[] s = new String[1];
+ try {
+ SwingUtilities.invokeAndWait(() -> s[0] = area.getText());
+ } catch (InvocationTargetException | InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ assertEquals(s[0], expected);
+ }
+
+ private void accept() {
+ clickOn(accept);
+ }
+
+ private void exit() {
+ clickOn(exit);
+ }
+
+ private void cancel() {
+ clickOn(cancel);
+ }
+
+ private void shutdownEditor() {
+ SwingUtilities.invokeLater(this::clearElements);
+ waitForIdle();
+ }
+
+ private void waitForIdle() {
+ robot.waitForIdle();
+ robot.delay(DELAY);
+ }
+
+ private void seekElements() {
+ for (Frame f : Frame.getFrames()) {
+ if (f.getTitle().equals(WINDOW_LABEL)) {
+ frame = (JFrame) f;
+ // workaround
+ frame.setLocation(0, 0);
+ Container root = frame.getContentPane();
+ for (Component c : root.getComponents()) {
+ if (c instanceof JScrollPane) {
+ JScrollPane scrollPane = (JScrollPane) c;
+ for (Component comp : scrollPane.getComponents()) {
+ if (comp instanceof JViewport) {
+ JViewport view = (JViewport) comp;
+ area = (JTextArea) view.getComponent(0);
+ }
+ }
+ }
+ if (c instanceof JPanel) {
+ JPanel p = (JPanel) c;
+ for (Component comp : p.getComponents()) {
+ if (comp instanceof JButton) {
+ JButton b = (JButton) comp;
+ switch (b.getText()) {
+ case "Cancel":
+ cancel = b;
+ break;
+ case "Exit":
+ exit = b;
+ break;
+ case "Accept":
+ accept = b;
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ private void clearElements() {
+ if (frame != null) {
+ frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
+ frame = null;
+ }
+ area = null;
+ accept = null;
+ cancel = null;
+ exit = null;
+ }
+
+ private void clickOn(JButton button) {
+ waitForIdle();
+ waitForIdle();
+ waitForIdle();
+ waitForIdle();
+ waitForIdle();
+ waitForIdle();
+ Point p = button.getLocationOnScreen();
+ Dimension d = button.getSize();
+ robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);
+ robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
+ robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
+ }
+}