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  			}
79  		} );
80  	}
81  
82  	public JUndoableTextField( String text )
83  	{
84  		super( text );
85  		init();
86  	}
87  
88  	public JUndoableTextField( int columns )
89  	{
90  		super( columns );
91  		init();
92  	}
93  
94  	public JUndoableTextField( String text, int columns )
95  	{
96  		super( text, columns );
97  		init();
98  	}
99  
100 	public void setText( String text )
101 	{
102 		ensureUndoManager();
103 		super.setText( text == null ? "" : text );
104 
105 		if( discardEditsOnSet && undoManager != null )
106 			undoManager.discardAllEdits();
107 	}
108 
109 	public boolean isDiscardEditsOnSet()
110 	{
111 		return discardEditsOnSet;
112 	}
113 
114 	public void setDiscardEditsOnSet( boolean discardEditsOnSet )
115 	{
116 		this.discardEditsOnSet = discardEditsOnSet;
117 	}
118 
119 	public UndoManager getUndoManager()
120 	{
121 		return undoManager;
122 	}
123 
124 	private void ensureUndoManager()
125 	{
126 		if( isEditable() && undoManager == null )
127 		{
128 			undoManager = new UndoManager();
129 			undoManager.setLimit( UNDO_LIMIT );
130 		}
131 	}
132 
133 	public void focusGained( FocusEvent fe )
134 	{
135 		ensureUndoManager();
136 	}
137 
138 	public void focusLost( FocusEvent fe )
139 	{
140 		// removeUndoMananger();
141 	}
142 
143 	public void undoableEditHappened( UndoableEditEvent e )
144 	{
145 		if( undoManager != null )
146 			undoManager.addEdit( e.getEdit() );
147 	}
148 
149 	public void undo()
150 	{
151 		if( !isEditable() )
152 		{
153 			getToolkit().beep();
154 			return;
155 		}
156 
157 		try
158 		{
159 			if( undoManager != null )
160 				undoManager.undo();
161 		}
162 		catch( CannotUndoException cue )
163 		{
164 			Toolkit.getDefaultToolkit().beep();
165 		}
166 	}
167 
168 	public void redo()
169 	{
170 		if( !isEditable() )
171 		{
172 			getToolkit().beep();
173 			return;
174 		}
175 
176 		try
177 		{
178 			if( undoManager != null )
179 				undoManager.redo();
180 		}
181 		catch( CannotRedoException cue )
182 		{
183 			Toolkit.getDefaultToolkit().beep();
184 		}
185 	}
186 
187 	public void setSelectedText( String txt )
188 	{
189 		replaceSelection( txt );
190 	}
191 
192 	public boolean canRedo()
193 	{
194 		return undoManager != null && undoManager.canRedo();
195 	}
196 
197 	public boolean canUndo()
198 	{
199 		return undoManager != null && undoManager.canUndo();
200 	}
201 }