/* * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* * This source code is provided to illustrate the usage of a given feature * or technique and has been deliberately simplified. Additional steps * required for a production-quality application, such as security checks, * input validation and proper error handling, might not be present in * this sample code.
*/
/** * Sample application using the simple text editor component that * supports only one font. * * @author Timothy Prinzing
*/
@SuppressWarnings("serial") publicclass Notepad extends JPanel {
// Trying to set Nimbus look and feel try { for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName()); break;
}
}
} catch (Exception ignored) {
}
// create the embedded JTextComponent
editor = createEditor(); // Add this as a listener for undoable edits.
editor.getDocument().addUndoableEditListener(undoHandler);
// install the command table
commands = new HashMap<Object, Action>();
Action[] actions = getActions(); for (Action a : actions) {
commands.put(a.getValue(Action.NAME), a);
}
JScrollPane scroller = new JScrollPane();
JViewport port = scroller.getViewport();
port.add(editor);
/** * Fetch the list of actions supported by this * editor. It is implemented to return the list * of actions supported by the embedded JTextComponent * augmented with the actions defined locally.
*/ public Action[] getActions() { return TextAction.augmentList(editor.getActions(), defaultActions);
}
/** * Create an editor to represent the given document.
*/ protected JTextComponent createEditor() {
JTextComponent c = new JTextArea();
c.setDragEnabled(true);
c.setFont(new Font("monospaced", Font.PLAIN, 12)); return c;
}
/** * Fetch the editor contained in this panel
*/ protected JTextComponent getEditor() { return editor;
}
/** * To shutdown when run as an application. This is a * fairly lame implementation. A more self-respecting * implementation would at least check to see if a save * was needed.
*/ protectedstaticfinalclass AppCloser extends WindowAdapter {
/** * Find the hosting frame, for the file-chooser dialog.
*/ protected Frame getFrame() { for (Container p = getParent(); p != null; p = p.getParent()) { if (p instanceof Frame) { return (Frame) p;
}
} returnnull;
}
/** * This is the hook through which all menu items are * created.
*/ protected JMenuItem createMenuItem(String cmd) {
JMenuItem mi = new JMenuItem(getResourceString(cmd + labelSuffix));
URL url = getResource(cmd + imageSuffix); if (url != null) {
mi.setHorizontalTextPosition(JButton.RIGHT);
mi.setIcon(new ImageIcon(url));
}
String astr = getProperty(cmd + actionSuffix); if (astr == null) {
astr = cmd;
}
mi.setActionCommand(astr);
Action a = getAction(astr); if (a != null) {
mi.addActionListener(a);
a.addPropertyChangeListener(createActionChangeListener(mi));
mi.setEnabled(a.isEnabled());
} else {
mi.setEnabled(false);
} return mi;
}
protected URL getResource(String key) {
String name = getResourceString(key); if (name != null) { returnthis.getClass().getResource(name);
} returnnull;
}
/** * Create a status bar
*/ protected Component createStatusbar() { // need to do something reasonable here
status = new StatusBar(); return status;
}
/** * Create the toolbar. By default this reads the * resource file for the definition of the toolbar.
*/ private Component createToolbar() {
toolbar = new JToolBar(); for (String toolKey: getToolBarKeys()) { if (toolKey.equals("-")) {
toolbar.add(Box.createHorizontalStrut(5));
} else {
toolbar.add(createTool(toolKey));
}
}
toolbar.add(Box.createHorizontalGlue()); return toolbar;
}
/** * Hook through which every toolbar item is created.
*/ protected Component createTool(String key) { return createToolbarButton(key);
}
/** * Create a button to go inside of the toolbar. By default this * will load an image resource. The image filename is relative to * the classpath (including the '.' directory if its a part of the * classpath), and may either be in a JAR file or a separate file. * * @param key The key in the resource file to serve as the basis * of lookups.
*/ protected JButton createToolbarButton(String key) {
URL url = getResource(key + imageSuffix);
JButton b = new JButton(new ImageIcon(url)) {
String astr = getProperty(key + actionSuffix); if (astr == null) {
astr = key;
}
Action a = getAction(astr); if (a != null) {
b.setActionCommand(astr);
b.addActionListener(a);
} else {
b.setEnabled(false);
}
String tip = getResourceString(key + tipSuffix); if (tip != null) {
b.setToolTipText(tip);
}
return b;
}
/** * Create the menubar for the app. By default this pulls the * definition of the menu from the associated resource file.
*/ protected JMenuBar createMenubar() {
JMenuBar mb = new JMenuBar(); for(String menuKey: getMenuBarKeys()){
JMenu m = createMenu(menuKey); if (m != null) {
mb.add(m);
}
} return mb;
}
/** * Create a menu for the app. By default this pulls the * definition of the menu from the associated resource file.
*/ protected JMenu createMenu(String key) {
JMenu menu = new JMenu(getResourceString(key + labelSuffix)); for (String itemKey: getItemKeys(key)) { if (itemKey.equals("-")) {
menu.addSeparator();
} else {
JMenuItem mi = createMenuItem(itemKey);
menu.add(mi);
}
} return menu;
}
// Yarked from JMenu, ideally this would be public. protected PropertyChangeListener createActionChangeListener(JMenuItem b) { returnnew ActionChangedListener(b);
}
// Yarked from JMenu, ideally this would be public.
/** * Listener for the edits on the current document.
*/ protected UndoableEditListener undoHandler = new UndoHandler(); /** UndoManager that we add edits to. */ protected UndoManager undo = new UndoManager(); /** * Suffix applied to the key used in resource file * lookups for an image.
*/ publicstaticfinal String imageSuffix = "Image"; /** * Suffix applied to the key used in resource file * lookups for a label.
*/ publicstaticfinal String labelSuffix = "Label"; /** * Suffix applied to the key used in resource file * lookups for an action.
*/ publicstaticfinal String actionSuffix = "Action"; /** * Suffix applied to the key used in resource file * lookups for tooltip text.
*/ publicstaticfinal String tipSuffix = "Tooltip"; publicstaticfinal String openAction = "open"; publicstaticfinal String newAction = "new"; publicstaticfinal String saveAction = "save"; publicstaticfinal String exitAction = "exit"; publicstaticfinal String showElementTreeAction = "showElementTree";
class UndoHandler implements UndoableEditListener {
/** * Messaged when the Document has created an edit, the edit is * added to <code>undo</code>, an instance of UndoManager.
*/ publicvoid undoableEditHappened(UndoableEditEvent e) {
undo.addEdit(e.getEdit());
undoAction.update();
redoAction.update();
}
}
/** * FIXME - I'm not very useful yet
*/ class StatusBar extends JComponent {
public StatusBar() { super();
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
}
@Override publicvoid paint(Graphics g) { super.paint(g);
}
} // --- action implementations ----------------------------------- private UndoAction undoAction = new UndoAction(); private RedoAction redoAction = new RedoAction(); /** * Actions defined by the Notepad class
*/ private Action[] defaultActions = { new NewAction(), new OpenAction(), new SaveAction(), new ExitAction(), new ShowElementTreeAction(),
undoAction,
redoAction
};
class UndoAction extends AbstractAction {
public UndoAction() { super("Undo");
setEnabled(false);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.