src/demo/share/jfc/Stylepad/Stylepad.java
author zgu
Fri, 12 Apr 2019 09:55:30 -0400
changeset 54524 33fda525ad41
parent 50830 5500595d8ebf
permissions -rw-r--r--
8222403: Shenandoah: Remove ShenandoahAlwaysTrueClosure, use AlwaysTrueClosure instead Reviewed-by: shade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50830
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
     1
/*
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
     2
 *
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
     3
 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
     4
 *
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
     5
 * Redistribution and use in source and binary forms, with or without
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
     6
 * modification, are permitted provided that the following conditions
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
     7
 * are met:
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
     8
 *
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
     9
 *   - Redistributions of source code must retain the above copyright
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    10
 *     notice, this list of conditions and the following disclaimer.
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    11
 *
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    12
 *   - Redistributions in binary form must reproduce the above copyright
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    13
 *     notice, this list of conditions and the following disclaimer in the
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    14
 *     documentation and/or other materials provided with the distribution.
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    15
 *
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    16
 *   - Neither the name of Oracle nor the names of its
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    17
 *     contributors may be used to endorse or promote products derived
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    18
 *     from this software without specific prior written permission.
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    19
 *
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    21
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    22
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    23
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    24
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    25
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    26
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    27
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    28
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    29
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    30
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    31
 */
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    32
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    33
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    34
import java.awt.BorderLayout;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    35
import java.awt.Color;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    36
import java.awt.Component;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    37
import java.awt.FileDialog;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    38
import java.awt.Frame;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    39
import java.awt.Graphics;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    40
import java.awt.GraphicsEnvironment;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    41
import java.awt.event.ActionEvent;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    42
import java.awt.event.ActionListener;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    43
import java.io.File;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    44
import java.io.FileInputStream;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    45
import java.io.FileOutputStream;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    46
import java.io.IOException;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    47
import java.io.ObjectInputStream;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    48
import java.io.ObjectOutput;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    49
import java.io.ObjectOutputStream;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    50
import java.lang.reflect.InvocationTargetException;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    51
import java.util.MissingResourceException;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    52
import java.util.ResourceBundle;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    53
import java.util.logging.Level;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    54
import java.util.logging.Logger;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    55
import javax.swing.AbstractAction;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    56
import javax.swing.Action;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    57
import javax.swing.Icon;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    58
import javax.swing.JButton;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    59
import javax.swing.JComboBox;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    60
import javax.swing.JFrame;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    61
import javax.swing.JMenu;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    62
import javax.swing.JMenuItem;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    63
import javax.swing.JTextPane;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    64
import javax.swing.SwingUtilities;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    65
import javax.swing.text.DefaultStyledDocument;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    66
import javax.swing.text.Document;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    67
import javax.swing.text.JTextComponent;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    68
import javax.swing.text.StyleContext;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    69
import javax.swing.text.StyledEditorKit;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    70
import javax.swing.text.TextAction;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    71
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    72
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    73
/**
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    74
 * Sample application using JTextPane.
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    75
 *
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    76
 * @author Timothy Prinzing
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    77
 */
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    78
@SuppressWarnings("serial")
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    79
public class Stylepad extends Notepad {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    80
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    81
    private static ResourceBundle resources;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    82
    private FileDialog fileDialog;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    83
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    84
    private static final String[] MENUBAR_KEYS = {"file", "edit", "color",
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    85
        "font", "debug"};
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    86
    private static final String[] FONT_KEYS = {"family1", "family2", "family3",
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    87
        "family4", "-", "size1", "size2", "size3", "size4", "size5", "-",
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    88
        "bold", "italic", "underline"};
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    89
    private static final String[] TOOLBAR_KEYS = {"new", "open", "save", "-",
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    90
        "cut", "copy", "paste", "-", "bold", "italic", "underline", "-",
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    91
        "left", "center", "right"};
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    92
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    93
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    94
    static {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    95
        try {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    96
            properties.load(Stylepad.class.getResourceAsStream(
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    97
                    "resources/StylepadSystem.properties"));
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    98
            resources = ResourceBundle.getBundle("resources.Stylepad");
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
    99
        } catch (MissingResourceException | IOException  mre) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   100
            System.err.println("Stylepad.properties or StylepadSystem.properties not found");
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   101
            System.exit(0);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   102
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   103
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   104
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   105
    public Stylepad() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   106
        super();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   107
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   108
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   109
    public static void main(String[] args) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   110
        try {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   111
            SwingUtilities.invokeAndWait(new Runnable() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   112
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   113
                public void run() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   114
                    JFrame frame = new JFrame();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   115
                    frame.setTitle(resources.getString("Title"));
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   116
                    frame.setBackground(Color.lightGray);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   117
                    frame.getContentPane().
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   118
                            setLayout(new BorderLayout());
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   119
                    Stylepad stylepad = new Stylepad();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   120
                    frame.getContentPane().add("Center", stylepad);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   121
                    frame.setJMenuBar(stylepad.createMenubar());
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   122
                    frame.addWindowListener(new AppCloser());
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   123
                    frame.pack();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   124
                    frame.setSize(600, 480);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   125
                    frame.setVisible(true);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   126
                }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   127
            });
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   128
        } catch (InterruptedException ex) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   129
            Logger.getLogger(Stylepad.class.getName()).log(Level.SEVERE, null,
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   130
                    ex);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   131
        } catch (InvocationTargetException ex) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   132
            Logger.getLogger(Stylepad.class.getName()).log(Level.SEVERE, null,
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   133
                    ex);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   134
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   135
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   136
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   137
    /**
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   138
     * Fetch the list of actions supported by this
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   139
     * editor.  It is implemented to return the list
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   140
     * of actions supported by the superclass
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   141
     * augmented with the actions defined locally.
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   142
     */
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   143
    @Override
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   144
    public Action[] getActions() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   145
        Action[] defaultActions = {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   146
            new NewAction(),
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   147
            new OpenAction(),
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   148
            new SaveAction(),
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   149
            new StyledEditorKit.FontFamilyAction("font-family-SansSerif",
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   150
            "SansSerif"), };
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   151
        Action[] a = TextAction.augmentList(super.getActions(), defaultActions);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   152
        return a;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   153
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   154
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   155
    /**
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   156
     * Try and resolve the resource name in the local
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   157
     * resource file, and if not found fall back to
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   158
     * the superclass resource file.
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   159
     */
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   160
    @Override
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   161
    protected String getResourceString(String nm) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   162
        String str;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   163
        try {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   164
            str = Stylepad.resources.getString(nm);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   165
        } catch (MissingResourceException mre) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   166
            str = super.getResourceString(nm);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   167
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   168
        return str;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   169
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   170
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   171
    /**
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   172
     * Create an editor to represent the given document.
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   173
     */
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   174
    @Override
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   175
    protected JTextComponent createEditor() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   176
        StyleContext sc = new StyleContext();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   177
        DefaultStyledDocument doc = new DefaultStyledDocument(sc);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   178
        initDocument(doc, sc);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   179
        JTextPane p = new JTextPane(doc);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   180
        p.setDragEnabled(true);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   181
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   182
        //p.getCaret().setBlinkRate(0);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   183
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   184
        return p;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   185
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   186
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   187
    /**
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   188
     * Create a menu for the app.  This is redefined to trap
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   189
     * a couple of special entries for now.
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   190
     */
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   191
    @Override
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   192
    protected JMenu createMenu(String key) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   193
        if (key.equals("color")) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   194
            return createColorMenu();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   195
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   196
        return super.createMenu(key);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   197
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   198
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   199
    @Override
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   200
    protected String[] getItemKeys(String key) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   201
        switch (key) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   202
            case "font":
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   203
                return FONT_KEYS;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   204
            default:
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   205
                return super.getItemKeys(key);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   206
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   207
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   208
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   209
    @Override
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   210
    protected String[] getMenuBarKeys() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   211
        return MENUBAR_KEYS;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   212
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   213
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   214
    @Override
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   215
    protected String[] getToolBarKeys() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   216
        return TOOLBAR_KEYS;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   217
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   218
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   219
    // this will soon be replaced
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   220
    JMenu createColorMenu() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   221
        ActionListener a;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   222
        JMenuItem mi;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   223
        JMenu menu = new JMenu(getResourceString("color" + labelSuffix));
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   224
        mi = new JMenuItem(resources.getString("Red"));
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   225
        mi.setHorizontalTextPosition(JButton.RIGHT);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   226
        mi.setIcon(new ColoredSquare(Color.red));
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   227
        a =
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   228
                new StyledEditorKit.ForegroundAction("set-foreground-red",
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   229
                Color.red);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   230
        //a = new ColorAction(se, Color.red);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   231
        mi.addActionListener(a);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   232
        menu.add(mi);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   233
        mi = new JMenuItem(resources.getString("Green"));
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   234
        mi.setHorizontalTextPosition(JButton.RIGHT);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   235
        mi.setIcon(new ColoredSquare(Color.green));
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   236
        a = new StyledEditorKit.ForegroundAction("set-foreground-green",
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   237
                Color.green);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   238
        //a = new ColorAction(se, Color.green);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   239
        mi.addActionListener(a);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   240
        menu.add(mi);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   241
        mi = new JMenuItem(resources.getString("Blue"));
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   242
        mi.setHorizontalTextPosition(JButton.RIGHT);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   243
        mi.setIcon(new ColoredSquare(Color.blue));
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   244
        a = new StyledEditorKit.ForegroundAction("set-foreground-blue",
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   245
                Color.blue);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   246
        //a = new ColorAction(se, Color.blue);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   247
        mi.addActionListener(a);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   248
        menu.add(mi);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   249
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   250
        return menu;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   251
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   252
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   253
    void initDocument(DefaultStyledDocument doc, StyleContext sc) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   254
        Wonderland w = new Wonderland(doc, sc);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   255
        w.loadDocument();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   256
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   257
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   258
    JComboBox createFamilyChoices() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   259
        JComboBox b = new JComboBox();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   260
        String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   261
                getAvailableFontFamilyNames();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   262
        for (String fontName : fontNames) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   263
            b.addItem(fontName);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   264
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   265
        return b;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   266
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   267
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   268
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   269
    /**
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   270
     * Trys to read a file which is assumed to be a
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   271
     * serialization of a document.
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   272
     */
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   273
    class OpenAction extends AbstractAction {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   274
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   275
        OpenAction() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   276
            super(openAction);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   277
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   278
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   279
        @Override
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   280
        public void actionPerformed(ActionEvent e) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   281
            Frame frame = getFrame();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   282
            if (fileDialog == null) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   283
                fileDialog = new FileDialog(frame);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   284
            }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   285
            fileDialog.setMode(FileDialog.LOAD);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   286
            fileDialog.setVisible(true);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   287
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   288
            String file = fileDialog.getFile();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   289
            if (file == null) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   290
                return;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   291
            }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   292
            String directory = fileDialog.getDirectory();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   293
            File f = new File(directory, file);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   294
            if (f.exists()) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   295
                try {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   296
                    FileInputStream fin = new FileInputStream(f);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   297
                    ObjectInputStream istrm = new ObjectInputStream(fin);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   298
                    Document doc = (Document) istrm.readObject();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   299
                    if (getEditor().getDocument() != null) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   300
                        getEditor().getDocument().removeUndoableEditListener(
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   301
                                undoHandler);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   302
                    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   303
                    getEditor().setDocument(doc);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   304
                    doc.addUndoableEditListener(undoHandler);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   305
                    resetUndoManager();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   306
                    frame.setTitle(file);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   307
                    validate();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   308
                } catch (IOException io) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   309
                    // should put in status panel
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   310
                    System.err.println("IOException: " + io.getMessage());
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   311
                } catch (ClassNotFoundException cnf) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   312
                    // should put in status panel
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   313
                    System.err.println("Class not found: " + cnf.getMessage());
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   314
                }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   315
            } else {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   316
                // should put in status panel
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   317
                System.err.println("No such file: " + f);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   318
            }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   319
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   320
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   321
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   322
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   323
    /**
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   324
     * Trys to write the document as a serialization.
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   325
     */
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   326
    class SaveAction extends AbstractAction {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   327
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   328
        SaveAction() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   329
            super(saveAction);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   330
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   331
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   332
        @Override
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   333
        public void actionPerformed(ActionEvent e) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   334
            Frame frame = getFrame();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   335
            if (fileDialog == null) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   336
                fileDialog = new FileDialog(frame);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   337
            }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   338
            fileDialog.setMode(FileDialog.SAVE);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   339
            fileDialog.setVisible(true);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   340
            String file = fileDialog.getFile();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   341
            if (file == null) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   342
                return;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   343
            }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   344
            String directory = fileDialog.getDirectory();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   345
            File f = new File(directory, file);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   346
            try {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   347
                FileOutputStream fstrm = new FileOutputStream(f);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   348
                ObjectOutput ostrm = new ObjectOutputStream(fstrm);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   349
                ostrm.writeObject(getEditor().getDocument());
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   350
                ostrm.flush();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   351
                frame.setTitle(f.getName());
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   352
            } catch (IOException io) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   353
                // should put in status panel
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   354
                System.err.println("IOException: " + io.getMessage());
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   355
            }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   356
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   357
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   358
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   359
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   360
    /**
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   361
     * Creates an empty document.
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   362
     */
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   363
    class NewAction extends AbstractAction {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   364
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   365
        NewAction() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   366
            super(newAction);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   367
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   368
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   369
        @Override
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   370
        public void actionPerformed(ActionEvent e) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   371
            if (getEditor().getDocument() != null) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   372
                getEditor().getDocument().removeUndoableEditListener(undoHandler);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   373
            }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   374
            getEditor().setDocument(new DefaultStyledDocument());
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   375
            getEditor().getDocument().addUndoableEditListener(undoHandler);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   376
            resetUndoManager();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   377
            getFrame().setTitle(resources.getString("Title"));
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   378
            validate();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   379
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   380
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   381
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   382
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   383
    class ColoredSquare implements Icon {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   384
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   385
        Color color;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   386
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   387
        public ColoredSquare(Color c) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   388
            this.color = c;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   389
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   390
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   391
        @Override
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   392
        public void paintIcon(Component c, Graphics g, int x, int y) {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   393
            Color oldColor = g.getColor();
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   394
            g.setColor(color);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   395
            g.fill3DRect(x, y, getIconWidth(), getIconHeight(), true);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   396
            g.setColor(oldColor);
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   397
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   398
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   399
        @Override
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   400
        public int getIconWidth() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   401
            return 12;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   402
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   403
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   404
        @Override
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   405
        public int getIconHeight() {
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   406
            return 12;
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   407
        }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   408
    }
5500595d8ebf 8205136: Move StylePad demo to the open repository
prr
parents:
diff changeset
   409
}