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