View Javadoc

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