View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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, FocusListener
39  {
40  	public static final int UNDO_LIMIT = 100;
41  
42  	private UndoManager undoManager;
43  	private boolean discardEditsOnSet = false;
44  
45  	public JUndoableFormattedTextField()
46  	{
47  		super();
48  		init();
49  	}
50  	
51  	private void init()
52  	{
53  		getDocument().addUndoableEditListener(this);
54  		addFocusListener(this);
55  		
56  		setMinimumSize( new Dimension( 50, 50 ));
57  		addKeyListener( new KeyAdapter() {
58  
59  			public void keyPressed(KeyEvent e)
60  			{
61  				if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu Z" )))
62  				   undo();
63  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu Y" )))
64  					redo();
65  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu X" )))
66  					cut();
67  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu C" )))
68  					copy();
69  			}} );
70  	}
71  
72  	public JUndoableFormattedTextField( String text )
73  	{
74  		super( text );
75  		init();
76  	}
77  	
78  	public JUndoableFormattedTextField( int columns )
79  	{
80  		super( columns );
81  		init();
82  	}
83  
84  	public JUndoableFormattedTextField( AbstractFormatter formatter )
85  	{
86  		super( formatter );
87  		init();
88  	}
89  
90  	public void setText(String text)
91  	{
92  		ensureUndoManager();
93  		super.setText(text == null ? "" : text);
94  		
95  		if( discardEditsOnSet && undoManager != null )
96  			undoManager.discardAllEdits();
97  	}
98  	
99  	public boolean isDiscardEditsOnSet()
100 	{
101 		return discardEditsOnSet;
102 	}
103 
104 	public void setDiscardEditsOnSet(boolean discardEditsOnSet)
105 	{
106 		this.discardEditsOnSet = discardEditsOnSet;
107 	}
108 
109 	public UndoManager getUndoManager()
110 	{
111 		return undoManager;
112 	}
113 	
114 	private void ensureUndoManager()
115 	{
116 		if (isEditable() && undoManager == null )
117 		{
118 			undoManager = new UndoManager();
119 			undoManager.setLimit(UNDO_LIMIT);
120 		}
121 	}
122 	
123 	public void focusGained(FocusEvent fe)
124 	{
125 		ensureUndoManager();
126 	}
127 
128 	public void focusLost(FocusEvent fe)
129 	{
130 		//removeUndoMananger();
131 	}
132 
133 	public void undoableEditHappened(UndoableEditEvent e)
134 	{
135 		if (undoManager != null)
136 			undoManager.addEdit(e.getEdit());
137 	}
138 
139 	public void undo()
140 	{
141 		if( !isEditable()  )
142 		{
143 			getToolkit().beep();
144 			return;
145 		}
146 		
147 		try
148 		{
149 			if( undoManager != null )
150 				undoManager.undo();
151 		}
152 		catch (CannotUndoException cue)
153 		{
154 			Toolkit.getDefaultToolkit().beep();
155 		}
156 	}
157 
158 	public void redo()
159 	{
160 		if( !isEditable()  )
161 		{
162 			getToolkit().beep();
163 			return;
164 		}
165 		
166 		try
167 		{
168 			if( undoManager != null )
169 				undoManager.redo();
170 		}
171 		catch (CannotRedoException cue)
172 		{
173 			Toolkit.getDefaultToolkit().beep();
174 		}
175 	}
176 
177 	public void setSelectedText(String txt)
178 	{
179 		replaceSelection( txt );
180 	}
181 
182 	public boolean canRedo()
183 	{
184 		return undoManager != null && undoManager.canRedo();
185 	}
186 
187 	public boolean canUndo()
188 	{
189 		return undoManager != null && undoManager.canUndo();
190 	}
191 }