1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support.components;
14
15 import java.awt.Dimension;
16 import java.awt.Toolkit;
17 import java.awt.event.FocusEvent;
18 import java.awt.event.FocusListener;
19 import java.awt.event.KeyAdapter;
20 import java.awt.event.KeyEvent;
21
22 import javax.swing.JFormattedTextField;
23 import javax.swing.KeyStroke;
24 import javax.swing.event.UndoableEditEvent;
25 import javax.swing.event.UndoableEditListener;
26 import javax.swing.undo.CannotRedoException;
27 import javax.swing.undo.CannotUndoException;
28 import javax.swing.undo.UndoManager;
29
30 import com.eviware.soapui.support.UISupport;
31
32 /***
33 * JTextArea with Undo/Redo keyboard/popup support
34 *
35 * @author Ole.Matzura
36 */
37
38 public class JUndoableFormattedTextField extends JFormattedTextField implements Undoable, UndoableEditListener,
39 FocusListener
40 {
41 public static final int UNDO_LIMIT = 100;
42
43 private UndoManager undoManager;
44 private boolean discardEditsOnSet = false;
45
46 public JUndoableFormattedTextField()
47 {
48 super();
49 init();
50 }
51
52 private void init()
53 {
54 getDocument().addUndoableEditListener( this );
55 addFocusListener( this );
56
57 setMinimumSize( new Dimension( 50, 50 ) );
58 addKeyListener( new KeyAdapter()
59 {
60
61 public void keyPressed( KeyEvent e )
62 {
63 if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu Z" ) ) )
64 undo();
65 else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu Y" ) ) )
66 redo();
67 else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu X" ) ) )
68 cut();
69 else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu C" ) ) )
70 copy();
71 }
72 } );
73 }
74
75 public JUndoableFormattedTextField( String text )
76 {
77 super( text );
78 init();
79 }
80
81 public JUndoableFormattedTextField( int columns )
82 {
83 super( columns );
84 init();
85 }
86
87 public JUndoableFormattedTextField( AbstractFormatter formatter )
88 {
89 super( formatter );
90 init();
91 }
92
93 public void setText( String text )
94 {
95 ensureUndoManager();
96 super.setText( text == null ? "" : text );
97
98 if( discardEditsOnSet && undoManager != null )
99 undoManager.discardAllEdits();
100 }
101
102 public boolean isDiscardEditsOnSet()
103 {
104 return discardEditsOnSet;
105 }
106
107 public void setDiscardEditsOnSet( boolean discardEditsOnSet )
108 {
109 this.discardEditsOnSet = discardEditsOnSet;
110 }
111
112 public UndoManager getUndoManager()
113 {
114 return undoManager;
115 }
116
117 private void ensureUndoManager()
118 {
119 if( isEditable() && undoManager == null )
120 {
121 undoManager = new UndoManager();
122 undoManager.setLimit( UNDO_LIMIT );
123 }
124 }
125
126 public void focusGained( FocusEvent fe )
127 {
128 ensureUndoManager();
129 }
130
131 public void focusLost( FocusEvent fe )
132 {
133
134 }
135
136 public void undoableEditHappened( UndoableEditEvent e )
137 {
138 if( undoManager != null )
139 undoManager.addEdit( e.getEdit() );
140 }
141
142 public void undo()
143 {
144 if( !isEditable() )
145 {
146 getToolkit().beep();
147 return;
148 }
149
150 try
151 {
152 if( undoManager != null )
153 undoManager.undo();
154 }
155 catch( CannotUndoException cue )
156 {
157 Toolkit.getDefaultToolkit().beep();
158 }
159 }
160
161 public void redo()
162 {
163 if( !isEditable() )
164 {
165 getToolkit().beep();
166 return;
167 }
168
169 try
170 {
171 if( undoManager != null )
172 undoManager.redo();
173 }
174 catch( CannotRedoException cue )
175 {
176 Toolkit.getDefaultToolkit().beep();
177 }
178 }
179
180 public void setSelectedText( String txt )
181 {
182 replaceSelection( txt );
183 }
184
185 public boolean canRedo()
186 {
187 return undoManager != null && undoManager.canRedo();
188 }
189
190 public boolean canUndo()
191 {
192 return undoManager != null && undoManager.canUndo();
193 }
194 }