/* * * Copyright (c) 2007, 2011, 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.
*/
/** * Fetch the list of actions supported by this * editor. It is implemented to return the list * of actions supported by the superclass * augmented with the actions defined locally.
*/
@Override public Action[] getActions() {
Action[] defaultActions = { new NewAction(), new OpenAction(), new SaveAction(), new StyledEditorKit.FontFamilyAction("font-family-SansSerif", "SansSerif"), };
Action[] a = TextAction.augmentList(super.getActions(), defaultActions); return a;
}
/** * Try and resolve the resource name in the local * resource file, and if not found fall back to * the superclass resource file.
*/
@Override protected String getResourceString(String nm) {
String str; try {
str = Stylepad.resources.getString(nm);
} catch (MissingResourceException mre) {
str = super.getResourceString(nm);
} return str;
}
/** * Create an editor to represent the given document.
*/
@Override protected JTextComponent createEditor() {
StyleContext sc = new StyleContext();
DefaultStyledDocument doc = new DefaultStyledDocument(sc);
initDocument(doc, sc);
JTextPane p = new JTextPane(doc);
p.setDragEnabled(true);
//p.getCaret().setBlinkRate(0);
return p;
}
/** * Create a menu for the app. This is redefined to trap * a couple of special entries for now.
*/
@Override protected JMenu createMenu(String key) { if (key.equals("color")) { return createColorMenu();
} returnsuper.createMenu(key);
}
// this will soon be replaced
JMenu createColorMenu() {
ActionListener a;
JMenuItem mi;
JMenu menu = new JMenu(getResourceString("color" + labelSuffix));
mi = new JMenuItem(resources.getString("Red"));
mi.setHorizontalTextPosition(JButton.RIGHT);
mi.setIcon(new ColoredSquare(Color.red));
a = new StyledEditorKit.ForegroundAction("set-foreground-red",
Color.red); //a = new ColorAction(se, Color.red);
mi.addActionListener(a);
menu.add(mi);
mi = new JMenuItem(resources.getString("Green"));
mi.setHorizontalTextPosition(JButton.RIGHT);
mi.setIcon(new ColoredSquare(Color.green));
a = new StyledEditorKit.ForegroundAction("set-foreground-green",
Color.green); //a = new ColorAction(se, Color.green);
mi.addActionListener(a);
menu.add(mi);
mi = new JMenuItem(resources.getString("Blue"));
mi.setHorizontalTextPosition(JButton.RIGHT);
mi.setIcon(new ColoredSquare(Color.blue));
a = new StyledEditorKit.ForegroundAction("set-foreground-blue",
Color.blue); //a = new ColorAction(se, Color.blue);
mi.addActionListener(a);
menu.add(mi);
return menu;
}
void initDocument(DefaultStyledDocument doc, StyleContext sc) {
Wonderland w = new Wonderland(doc, sc);
w.loadDocument();
}
JComboBox<String> createFamilyChoices() {
JComboBox<String> b = new JComboBox<>();
String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().
getAvailableFontFamilyNames(); for (String fontName : fontNames) {
b.addItem(fontName);
} return b;
}
/** * Trys to read a file which is assumed to be a * serialization of a document.
*/ class OpenAction extends AbstractAction {
String file = fileDialog.getFile(); if (file == null) { return;
}
String directory = fileDialog.getDirectory();
File f = new File(directory, file); if (f.exists()) { try {
FileInputStream fin = new FileInputStream(f);
ObjectInputStream istrm = new ObjectInputStream(fin);
Document doc = (Document) istrm.readObject(); if (getEditor().getDocument() != null) {
getEditor().getDocument().removeUndoableEditListener(
undoHandler);
}
getEditor().setDocument(doc);
doc.addUndoableEditListener(undoHandler);
resetUndoManager();
frame.setTitle(file);
validate();
} catch (IOException io) { // should put in status panel
System.err.println("IOException: " + io.getMessage());
} catch (ClassNotFoundException cnf) { // should put in status panel
System.err.println("Class not found: " + cnf.getMessage());
}
} else { // should put in status panel
System.err.println("No such file: " + f);
}
}
}
/** * Trys to write the document as a serialization.
*/ class SaveAction extends AbstractAction {
SaveAction() { super(saveAction);
}
@Override publicvoid actionPerformed(ActionEvent e) {
Frame frame = getFrame(); if (fileDialog == null) {
fileDialog = new FileDialog(frame);
}
fileDialog.setMode(FileDialog.SAVE);
fileDialog.setVisible(true);
String file = fileDialog.getFile(); if (file == null) { return;
}
String directory = fileDialog.getDirectory();
File f = new File(directory, file); try {
FileOutputStream fstrm = new FileOutputStream(f);
ObjectOutput ostrm = new ObjectOutputStream(fstrm);
ostrm.writeObject(getEditor().getDocument());
ostrm.flush();
frame.setTitle(f.getName());
} catch (IOException io) { // should put in status panel
System.err.println("IOException: " + io.getMessage());
}
}
}
/** * Creates an empty document.
*/ class NewAction extends AbstractAction {
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.