langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/EditPad.java
changeset 41939 4e7ef9667ea6
parent 41930 394bc7b2237f
parent 41938 8e66bf10fcec
child 41940 048d559e9da7
equal deleted inserted replaced
41930:394bc7b2237f 41939:4e7ef9667ea6
     1 /*
       
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.internal.jshell.tool;
       
    27 
       
    28 import java.awt.BorderLayout;
       
    29 import java.awt.FlowLayout;
       
    30 import java.awt.event.KeyEvent;
       
    31 import java.awt.event.WindowAdapter;
       
    32 import java.awt.event.WindowEvent;
       
    33 import java.util.concurrent.CountDownLatch;
       
    34 import java.util.function.Consumer;
       
    35 import javax.swing.JButton;
       
    36 import javax.swing.JFrame;
       
    37 import javax.swing.JPanel;
       
    38 import javax.swing.JScrollPane;
       
    39 import javax.swing.JTextArea;
       
    40 import javax.swing.SwingUtilities;
       
    41 
       
    42 /**
       
    43  * A minimal Swing editor as a fallback when the user does not specify an
       
    44  * external editor.
       
    45  */
       
    46 @SuppressWarnings("serial")             // serialVersionUID intentionally omitted
       
    47 public class EditPad extends JFrame implements Runnable {
       
    48     private final Consumer<String> errorHandler; // For possible future error handling
       
    49     private final String initialText;
       
    50     private final CountDownLatch closeLock;
       
    51     private final Consumer<String> saveHandler;
       
    52 
       
    53     EditPad(Consumer<String> errorHandler, String initialText,
       
    54             CountDownLatch closeLock, Consumer<String> saveHandler) {
       
    55         super("JShell Edit Pad");
       
    56         this.errorHandler = errorHandler;
       
    57         this.initialText = initialText;
       
    58         this.closeLock = closeLock;
       
    59         this.saveHandler = saveHandler;
       
    60     }
       
    61 
       
    62     @Override
       
    63     public void run() {
       
    64         addWindowListener(new WindowAdapter() {
       
    65             @Override
       
    66             public void windowClosing(WindowEvent e) {
       
    67                 EditPad.this.dispose();
       
    68                 closeLock.countDown();
       
    69             }
       
    70         });
       
    71         setLocationRelativeTo(null);
       
    72         setLayout(new BorderLayout());
       
    73         JTextArea textArea = new JTextArea(initialText);
       
    74         add(new JScrollPane(textArea), BorderLayout.CENTER);
       
    75         add(buttons(textArea), BorderLayout.SOUTH);
       
    76 
       
    77         setSize(800, 600);
       
    78         setVisible(true);
       
    79     }
       
    80 
       
    81     private JPanel buttons(JTextArea textArea) {
       
    82         FlowLayout flow = new FlowLayout();
       
    83         flow.setHgap(35);
       
    84         JPanel buttons = new JPanel(flow);
       
    85         JButton cancel = new JButton("Cancel");
       
    86         cancel.setMnemonic(KeyEvent.VK_C);
       
    87         JButton accept = new JButton("Accept");
       
    88         accept.setMnemonic(KeyEvent.VK_A);
       
    89         JButton exit = new JButton("Exit");
       
    90         exit.setMnemonic(KeyEvent.VK_X);
       
    91         buttons.add(cancel);
       
    92         buttons.add(accept);
       
    93         buttons.add(exit);
       
    94 
       
    95         cancel.addActionListener(e -> {
       
    96             close();
       
    97         });
       
    98         accept.addActionListener(e -> {
       
    99             saveHandler.accept(textArea.getText());
       
   100         });
       
   101         exit.addActionListener(e -> {
       
   102             saveHandler.accept(textArea.getText());
       
   103             close();
       
   104         });
       
   105 
       
   106         return buttons;
       
   107     }
       
   108 
       
   109     private void close() {
       
   110         setVisible(false);
       
   111         dispose();
       
   112         closeLock.countDown();
       
   113     }
       
   114 
       
   115     public static void edit(Consumer<String> errorHandler, String initialText,
       
   116             Consumer<String> saveHandler) {
       
   117         CountDownLatch closeLock = new CountDownLatch(1);
       
   118         SwingUtilities.invokeLater(
       
   119                 new EditPad(errorHandler, initialText, closeLock, saveHandler));
       
   120         do {
       
   121             try {
       
   122                 closeLock.await();
       
   123                 break;
       
   124             } catch (InterruptedException ex) {
       
   125                 // ignore and loop
       
   126             }
       
   127         } while (true);
       
   128     }
       
   129 }