KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > swing > ExtendedEditorPane


1 package net.suberic.util.swing;
2
3 import java.awt.event.*;
4 import javax.swing.event.*;
5 import javax.swing.*;
6 import javax.swing.undo.*;
7 import javax.swing.text.*;
8 import java.beans.*;
9
10 /**
11  * An extension of JTextPane with some additional actions.
12  *
13  * Most of the Undo and Redo action code was taken from the sample
14  * Notepad and Stylepad demos included in the Sun JDK's.
15  */

16 public class ExtendedEditorPane extends JTextPane {
17   
18   private UndoManager undoManager = new UndoManager();
19
20   private UndoableEditListener undoHandler = new UndoHandler();
21   
22   private UndoAction undoAction = new UndoAction();
23
24   private RedoAction redoAction = new RedoAction();
25
26   public ExtendedEditorPane() {
27     super();
28     this.addPropertyChangeListener("document", new PropertyChangeListener() {
29     public void propertyChange(PropertyChangeEvent evt) {
30       if (evt.getPropertyName().equalsIgnoreCase("document")) {
31         Document oldDoc = (Document) evt.getOldValue();
32         Document doc = (Document) evt.getNewValue();
33
34         if (oldDoc != null)
35           oldDoc.removeUndoableEditListener(undoHandler);
36         if (doc != null)
37           doc.addUndoableEditListener(undoHandler);
38         
39         if (doc != oldDoc) {
40           undoManager.discardAllEdits();
41           undoAction.update();
42           redoAction.update();
43         }
44         
45       }
46     }
47       });
48     
49   }
50
51   public Action[] getActions() {
52     Action[] defaultActions = super.getActions();
53     if (defaultActions == null)
54       return defaultActions;
55
56     if (undoAction != null) {
57       Action[] additionalActions = new Action[] {
58     undoAction,
59     redoAction
60       };
61       return TextAction.augmentList(defaultActions, additionalActions);
62     } else {
63       return defaultActions;
64     }
65   }
66
67   class UndoHandler implements UndoableEditListener {
68     
69     /**
70      * Messaged when the Document has created an edit, the edit is
71      * added to <code>undo</code>, an instance of UndoManager.
72      */

73     public void undoableEditHappened(UndoableEditEvent e) {
74       undoManager.addEdit(e.getEdit());
75       undoAction.update();
76       redoAction.update();
77     }
78   }
79
80   class UndoAction extends AbstractAction {
81     public UndoAction() {
82       super("edit-undo");
83       setEnabled(false);
84     }
85     
86     public void actionPerformed(ActionEvent e) {
87       try {
88     undoManager.undo();
89       } catch (CannotUndoException ex) {
90
91       }
92       update();
93       redoAction.update();
94     }
95     
96     protected void update() {
97       if(undoManager.canUndo()) {
98     setEnabled(true);
99     //putValue(Action.NAME, undo.getUndoPresentationName());
100
}
101       else {
102     setEnabled(false);
103     //putValue(Action.NAME, "Undo");
104
}
105     }
106   }
107   
108   class RedoAction extends AbstractAction {
109     public RedoAction() {
110       super("edit-redo");
111       setEnabled(false);
112     }
113     
114     public void actionPerformed(ActionEvent e) {
115       try {
116     undoManager.redo();
117       } catch (CannotRedoException ex) {
118
119       }
120       update();
121       undoAction.update();
122     }
123     
124     protected void update() {
125       if(undoManager.canRedo()) {
126     setEnabled(true);
127     //putValue(Action.NAME, undo.getRedoPresentationName());
128
}
129       else {
130     setEnabled(false);
131     //putValue(Action.NAME, "Redo");
132
}
133     }
134   }
135
136 }
137
Popular Tags