TextComponentDemopublic class TextComponentDemo extends JFrame
Fields Summary |
---|
JTextPane | textPane | LimitedStyledDocument | lsd | JTextArea | changeLog | String | newline | static final int | MAX_CHARACTERS | Hashtable | actions | protected UndoAction | undoAction | protected RedoAction | redoAction | protected UndoManager | undo |
Constructors Summary |
---|
public TextComponentDemo()
//some initial setup
super("TextComponentDemo");
//Create the document for the text area.
lsd = new LimitedStyledDocument(MAX_CHARACTERS);
//Create the text pane and configure it.
textPane = new JTextPane(lsd); //All right! No 60's jokes.
textPane.setCaretPosition(0);
textPane.setMargin(new Insets(5,5,5,5));
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setPreferredSize(new Dimension(200, 200));
//Create the text area for the status log and configure it.
changeLog = new JTextArea(5, 30);
changeLog.setEditable(false);
JScrollPane scrollPaneForLog = new JScrollPane(changeLog);
//Create a split pane for the change log and the text area.
JSplitPane splitPane = new JSplitPane(
JSplitPane.VERTICAL_SPLIT,
scrollPane, scrollPaneForLog);
splitPane.setOneTouchExpandable(true);
//Create the status area.
JPanel statusPane = new JPanel(new GridLayout(1, 1));
CaretListenerLabel caretListenerLabel =
new CaretListenerLabel("Caret Status");
statusPane.add(caretListenerLabel);
//Add the components to the frame.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.add(splitPane, BorderLayout.CENTER);
contentPane.add(statusPane, BorderLayout.SOUTH);
setContentPane(contentPane);
//Set up the menu bar.
createActionTable(textPane);
JMenu editMenu = createEditMenu();
JMenu styleMenu = createStyleMenu();
JMenuBar mb = new JMenuBar();
mb.add(editMenu);
mb.add(styleMenu);
setJMenuBar(mb);
//Add some key bindings to the keymap.
addKeymapBindings();
//Put the initial text into the text pane.
initDocument();
//Start watching for undoable edits and caret changes.
lsd.addUndoableEditListener(new MyUndoableEditListener());
textPane.addCaretListener(caretListenerLabel);
lsd.addDocumentListener(new MyDocumentListener());
|
Methods Summary |
---|
protected void | addKeymapBindings()
//Add a new key map to the keymap hierarchy.
Keymap keymap = textPane.addKeymap("MyEmacsBindings",
textPane.getKeymap());
//Ctrl-b to go backward one character
Action action = getActionByName(DefaultEditorKit.backwardAction);
KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_B, Event.CTRL_MASK);
keymap.addActionForKeyStroke(key, action);
//Ctrl-f to go forward one character
action = getActionByName(DefaultEditorKit.forwardAction);
key = KeyStroke.getKeyStroke(KeyEvent.VK_F, Event.CTRL_MASK);
keymap.addActionForKeyStroke(key, action);
//Ctrl-p to go up one line
action = getActionByName(DefaultEditorKit.upAction);
key = KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK);
keymap.addActionForKeyStroke(key, action);
//Ctrl-n to go down one line
action = getActionByName(DefaultEditorKit.downAction);
key = KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK);
keymap.addActionForKeyStroke(key, action);
textPane.setKeymap(keymap);
| private void | createActionTable(javax.swing.text.JTextComponent textComponent)
actions = new Hashtable();
Action[] actionsArray = textComponent.getActions();
for (int i = 0; i < actionsArray.length; i++) {
Action a = actionsArray[i];
actions.put(a.getValue(Action.NAME), a);
}
| protected javax.swing.JMenu | createEditMenu()
JMenu menu = new JMenu("Edit");
//Undo and redo are actions of our own creation.
undoAction = new UndoAction();
menu.add(undoAction);
redoAction = new RedoAction();
menu.add(redoAction);
menu.addSeparator();
//These actions come from the default editor kit.
//Get the ones we want and stick them in the menu.
menu.add(getActionByName(DefaultEditorKit.cutAction));
menu.add(getActionByName(DefaultEditorKit.copyAction));
menu.add(getActionByName(DefaultEditorKit.pasteAction));
menu.addSeparator();
menu.add(getActionByName(DefaultEditorKit.selectAllAction));
return menu;
| protected javax.swing.JMenu | createStyleMenu()
JMenu menu = new JMenu("Style");
Action action = new StyledEditorKit.BoldAction();
action.putValue(Action.NAME, "Bold");
menu.add(action);
action = new StyledEditorKit.ItalicAction();
action.putValue(Action.NAME, "Italic");
menu.add(action);
action = new StyledEditorKit.UnderlineAction();
action.putValue(Action.NAME, "Underline");
menu.add(action);
menu.addSeparator();
menu.add(new StyledEditorKit.FontSizeAction("12", 12));
menu.add(new StyledEditorKit.FontSizeAction("14", 14));
menu.add(new StyledEditorKit.FontSizeAction("18", 18));
menu.addSeparator();
menu.add(new StyledEditorKit.FontFamilyAction("Serif",
"Serif"));
menu.add(new StyledEditorKit.FontFamilyAction("SansSerif",
"SansSerif"));
menu.addSeparator();
menu.add(new StyledEditorKit.ForegroundAction("Red",
Color.red));
menu.add(new StyledEditorKit.ForegroundAction("Green",
Color.green));
menu.add(new StyledEditorKit.ForegroundAction("Blue",
Color.blue));
menu.add(new StyledEditorKit.ForegroundAction("Black",
Color.black));
return menu;
| private javax.swing.Action | getActionByName(java.lang.String name)
return (Action)(actions.get(name));
| protected javax.swing.text.SimpleAttributeSet[] | initAttributes(int length)
//Hard-code some attributes.
SimpleAttributeSet[] attrs = new SimpleAttributeSet[length];
attrs[0] = new SimpleAttributeSet();
StyleConstants.setFontFamily(attrs[0], "SansSerif");
StyleConstants.setFontSize(attrs[0], 16);
attrs[1] = new SimpleAttributeSet(attrs[0]);
StyleConstants.setBold(attrs[1], true);
attrs[2] = new SimpleAttributeSet(attrs[0]);
StyleConstants.setItalic(attrs[2], true);
attrs[3] = new SimpleAttributeSet(attrs[0]);
StyleConstants.setFontSize(attrs[3], 20);
attrs[4] = new SimpleAttributeSet(attrs[0]);
StyleConstants.setFontSize(attrs[4], 12);
attrs[5] = new SimpleAttributeSet(attrs[0]);
StyleConstants.setForeground(attrs[5], Color.red);
return attrs;
| protected void | initDocument()
String initString[] =
{ "Use the mouse to place the caret.",
"Use the edit menu to cut, copy, paste, and select text.",
"Also to undo and redo changes.",
"Use the style menu to change the style of the text.",
"Use these emacs key bindings to move the caret:",
"ctrl-f, ctrl-b, ctrl-n, ctrl-p." };
SimpleAttributeSet[] attrs = initAttributes(initString.length);
try {
for (int i = 0; i < initString.length; i ++) {
lsd.insertString(lsd.getLength(), initString[i] + newline,
attrs[i]);
}
} catch (BadLocationException ble) {
System.err.println("Couldn't insert initial text.");
}
| public static void | main(java.lang.String[] args)
final TextComponentDemo frame = new TextComponentDemo();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowActivated(WindowEvent e) {
frame.textPane.requestFocus();
}
});
frame.pack();
frame.setVisible(true);
|
|