Path: blob/master/src/jdk.editpad/share/classes/jdk/editpad/EditPad.java
41152 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.editpad;2627import java.awt.BorderLayout;28import java.awt.FlowLayout;29import java.awt.Font;30import java.awt.event.ActionListener;31import java.awt.event.KeyEvent;32import java.awt.event.WindowAdapter;33import java.awt.event.WindowEvent;34import java.util.MissingResourceException;35import java.util.ResourceBundle;36import java.util.function.Consumer;37import javax.swing.JButton;38import javax.swing.JFrame;39import javax.swing.JPanel;40import javax.swing.JScrollPane;41import javax.swing.JTextArea;4243/**44* A minimal Swing editor as a fallback when the user does not specify an45* external editor.46*/47class EditPad implements Runnable {4849private static final String L10N_RB_NAME = "jdk.editpad.resources.l10n";50private ResourceBundle rb = null;51private final String windowLabel;52private final Consumer<String> errorHandler;53private final String initialText;54private final Runnable closeMark;55private final Consumer<String> saveHandler;5657/**58* Create an Edit Pad minimal editor.59*60* @param windowLabel the label string for the Edit Pad window61* @param errorHandler a handler for unexpected errors62* @param initialText the source to load in the Edit Pad63* @param closeMark a Runnable that is run when Edit Pad closes64* @param saveHandler a handler for changed source (sent the full source)65*/66EditPad(String windowLabel, Consumer<String> errorHandler, String initialText,67Runnable closeMark, Consumer<String> saveHandler) {68this.windowLabel = windowLabel;69this.errorHandler = errorHandler;70this.initialText = initialText;71this.closeMark = closeMark;72this.saveHandler = saveHandler;73}7475@Override76public void run() {77JFrame jframe = new JFrame(windowLabel == null78? getResourceString("editpad.name")79: windowLabel);80Runnable closer = () -> {81jframe.setVisible(false);82jframe.dispose();83closeMark.run();84};85jframe.addWindowListener(new WindowAdapter() {86@Override87public void windowClosing(WindowEvent e) {88closer.run();89}90});91jframe.setLocationRelativeTo(null);92jframe.setLayout(new BorderLayout());93JTextArea textArea = new JTextArea(initialText);94textArea.setFont(new Font("monospaced", Font.PLAIN, 13));95jframe.add(new JScrollPane(textArea), BorderLayout.CENTER);96jframe.add(buttons(closer, textArea), BorderLayout.SOUTH);9798jframe.setSize(800, 600);99jframe.setVisible(true);100}101102private JPanel buttons(Runnable closer, JTextArea textArea) {103FlowLayout flow = new FlowLayout();104flow.setHgap(35);105JPanel buttons = new JPanel(flow);106addButton(buttons, "editpad.cancel", KeyEvent.VK_C, e -> {107closer.run();108});109addButton(buttons, "editpad.accept", KeyEvent.VK_A, e -> {110saveHandler.accept(textArea.getText());111});112addButton(buttons, "editpad.exit", KeyEvent.VK_X, e -> {113saveHandler.accept(textArea.getText());114closer.run();115});116return buttons;117}118119private void addButton(JPanel buttons, String rkey, int mnemonic, ActionListener action) {120JButton but = new JButton(getResourceString(rkey));121but.setMnemonic(mnemonic);122buttons.add(but);123but.addActionListener(action);124}125126private String getResourceString(String key) {127if (rb == null) {128try {129rb = ResourceBundle.getBundle(L10N_RB_NAME);130} catch (MissingResourceException mre) {131error("Cannot find ResourceBundle: %s", L10N_RB_NAME);132return "";133}134}135String s;136try {137s = rb.getString(key);138} catch (MissingResourceException mre) {139error("Missing resource: %s in %s", key, L10N_RB_NAME);140return "";141}142return s;143}144145private void error(String fmt, Object... args) {146errorHandler.accept(String.format(fmt, args));147}148}149150151