001: package net.suberic.util.swing;
002:
003: import java.awt.event.*;
004: import javax.swing.event.*;
005: import javax.swing.*;
006: import javax.swing.undo.*;
007: import javax.swing.text.*;
008: import java.beans.*;
009:
010: /**
011: * An extension of JTextPane with some additional actions.
012: *
013: * Most of the Undo and Redo action code was taken from the sample
014: * Notepad and Stylepad demos included in the Sun JDK's.
015: */
016: public class ExtendedEditorPane extends JTextPane {
017:
018: private UndoManager undoManager = new UndoManager();
019:
020: private UndoableEditListener undoHandler = new UndoHandler();
021:
022: private UndoAction undoAction = new UndoAction();
023:
024: private RedoAction redoAction = new RedoAction();
025:
026: public ExtendedEditorPane() {
027: super ();
028: this .addPropertyChangeListener("document",
029: new PropertyChangeListener() {
030: public void propertyChange(PropertyChangeEvent evt) {
031: if (evt.getPropertyName().equalsIgnoreCase(
032: "document")) {
033: Document oldDoc = (Document) evt
034: .getOldValue();
035: Document doc = (Document) evt.getNewValue();
036:
037: if (oldDoc != null)
038: oldDoc
039: .removeUndoableEditListener(undoHandler);
040: if (doc != null)
041: doc
042: .addUndoableEditListener(undoHandler);
043:
044: if (doc != oldDoc) {
045: undoManager.discardAllEdits();
046: undoAction.update();
047: redoAction.update();
048: }
049:
050: }
051: }
052: });
053:
054: }
055:
056: public Action[] getActions() {
057: Action[] defaultActions = super .getActions();
058: if (defaultActions == null)
059: return defaultActions;
060:
061: if (undoAction != null) {
062: Action[] additionalActions = new Action[] { undoAction,
063: redoAction };
064: return TextAction.augmentList(defaultActions,
065: additionalActions);
066: } else {
067: return defaultActions;
068: }
069: }
070:
071: class UndoHandler implements UndoableEditListener {
072:
073: /**
074: * Messaged when the Document has created an edit, the edit is
075: * added to <code>undo</code>, an instance of UndoManager.
076: */
077: public void undoableEditHappened(UndoableEditEvent e) {
078: undoManager.addEdit(e.getEdit());
079: undoAction.update();
080: redoAction.update();
081: }
082: }
083:
084: class UndoAction extends AbstractAction {
085: public UndoAction() {
086: super ("edit-undo");
087: setEnabled(false);
088: }
089:
090: public void actionPerformed(ActionEvent e) {
091: try {
092: undoManager.undo();
093: } catch (CannotUndoException ex) {
094:
095: }
096: update();
097: redoAction.update();
098: }
099:
100: protected void update() {
101: if (undoManager.canUndo()) {
102: setEnabled(true);
103: //putValue(Action.NAME, undo.getUndoPresentationName());
104: } else {
105: setEnabled(false);
106: //putValue(Action.NAME, "Undo");
107: }
108: }
109: }
110:
111: class RedoAction extends AbstractAction {
112: public RedoAction() {
113: super ("edit-redo");
114: setEnabled(false);
115: }
116:
117: public void actionPerformed(ActionEvent e) {
118: try {
119: undoManager.redo();
120: } catch (CannotRedoException ex) {
121:
122: }
123: update();
124: undoAction.update();
125: }
126:
127: protected void update() {
128: if (undoManager.canRedo()) {
129: setEnabled(true);
130: //putValue(Action.NAME, undo.getRedoPresentationName());
131: } else {
132: setEnabled(false);
133: //putValue(Action.NAME, "Redo");
134: }
135: }
136: }
137:
138: }
|