View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
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.JTextField;
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  import com.eviware.soapui.support.swing.JTextComponentPopupMenu;
32  
33  /***
34   * JTextArea with Undo/Redo keyboard/popup support
35   * 
36   * @author Ole.Matzura
37   */
38  
39  public class JUndoableTextField extends JTextField implements Undoable, UndoableEditListener, FocusListener
40  {
41  	public static final int UNDO_LIMIT = 100;
42  
43  	private UndoManager undoManager;
44  	private boolean discardEditsOnSet = false;
45  
46  	public JUndoableTextField()
47  	{
48  		super();
49  		init();
50  	}
51  
52  	public JUndoableTextField( boolean addPopup )
53  	{
54  		this();
55  		if( addPopup )
56  			JTextComponentPopupMenu.add( this );
57  	}
58  
59  	private void init()
60  	{
61  		getDocument().addUndoableEditListener( this );
62  		addFocusListener( this );
63  
64  		setMinimumSize( new Dimension( 50, 50 ) );
65  		addKeyListener( new KeyAdapter()
66  		{
67  
68  			public void keyPressed( KeyEvent e )
69  			{
70  				if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu Z" ) ) )
71  					undo();
72  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu Y" ) ) )
73  					redo();
74  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu X" ) ) )
75  					cut();
76  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu C" ) ) )
77  					copy();
78  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu V" ) ) )
79  					paste();
80  				else
81  					return;
82  				
83  				e.consume();
84  			}
85  		} );
86  	}
87  
88  	public JUndoableTextField( String text )
89  	{
90  		super( text );
91  		init();
92  	}
93  
94  	public JUndoableTextField( int columns )
95  	{
96  		super( columns );
97  		init();
98  	}
99  
100 	public JUndoableTextField( String text, int columns )
101 	{
102 		super( text, columns );
103 		init();
104 	}
105 
106 	public void setText( String text )
107 	{
108 		ensureUndoManager();
109 		super.setText( text == null ? "" : text );
110 
111 		if( discardEditsOnSet && undoManager != null )
112 			undoManager.discardAllEdits();
113 	}
114 
115 	public boolean isDiscardEditsOnSet()
116 	{
117 		return discardEditsOnSet;
118 	}
119 
120 	public void setDiscardEditsOnSet( boolean discardEditsOnSet )
121 	{
122 		this.discardEditsOnSet = discardEditsOnSet;
123 	}
124 
125 	public UndoManager getUndoManager()
126 	{
127 		return undoManager;
128 	}
129 
130 	private void ensureUndoManager()
131 	{
132 		if( isEditable() && undoManager == null )
133 		{
134 			undoManager = new UndoManager();
135 			undoManager.setLimit( UNDO_LIMIT );
136 		}
137 	}
138 
139 	public void focusGained( FocusEvent fe )
140 	{
141 		ensureUndoManager();
142 	}
143 
144 	public void focusLost( FocusEvent fe )
145 	{
146 		// removeUndoMananger();
147 	}
148 
149 	public void undoableEditHappened( UndoableEditEvent e )
150 	{
151 		if( undoManager != null )
152 			undoManager.addEdit( e.getEdit() );
153 	}
154 
155 	public void undo()
156 	{
157 		if( !isEditable() )
158 		{
159 			getToolkit().beep();
160 			return;
161 		}
162 
163 		try
164 		{
165 			if( undoManager != null )
166 				undoManager.undo();
167 		}
168 		catch( CannotUndoException cue )
169 		{
170 			Toolkit.getDefaultToolkit().beep();
171 		}
172 	}
173 
174 	public void redo()
175 	{
176 		if( !isEditable() )
177 		{
178 			getToolkit().beep();
179 			return;
180 		}
181 
182 		try
183 		{
184 			if( undoManager != null )
185 				undoManager.redo();
186 		}
187 		catch( CannotRedoException cue )
188 		{
189 			Toolkit.getDefaultToolkit().beep();
190 		}
191 	}
192 
193 	public void setSelectedText( String txt )
194 	{
195 		replaceSelection( txt );
196 	}
197 
198 	public boolean canRedo()
199 	{
200 		return undoManager != null && undoManager.canRedo();
201 	}
202 
203 	public boolean canUndo()
204 	{
205 		return undoManager != null && undoManager.canUndo();
206 	}
207 }