View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.BorderFactory;
23  import javax.swing.JTextArea;
24  import javax.swing.KeyStroke;
25  import javax.swing.event.UndoableEditEvent;
26  import javax.swing.event.UndoableEditListener;
27  import javax.swing.undo.CannotRedoException;
28  import javax.swing.undo.CannotUndoException;
29  import javax.swing.undo.UndoManager;
30  
31  import com.eviware.soapui.support.UISupport;
32  import com.eviware.soapui.support.actions.FindAndReplaceDialog;
33  import com.eviware.soapui.support.actions.FindAndReplaceable;
34  import com.eviware.soapui.support.components.JEditorStatusBar.JEditorStatusBarTarget;
35  import com.eviware.soapui.support.swing.JTextComponentPopupMenu;
36  
37  /***
38   * JEditTextArea extension targeted specifically at XML-editing.
39   *
40   * //@todo move font handling to subclass
41   * 
42   * @author Ole.Matzura
43   */
44  
45  public class JUndoableTextArea extends JTextArea implements	Undoable, UndoableEditListener, FocusListener, FindAndReplaceable, JEditorStatusBarTarget
46  {
47  	public static final int UNDO_LIMIT = 1500;
48  
49  	private UndoManager undoManager;
50  	private boolean discardEditsOnSet = true;
51  	private FindAndReplaceDialog findAndReplaceAction;
52  
53  	public JUndoableTextArea()
54  	{
55  		setBorder(BorderFactory.createEtchedBorder());
56  		getDocument().addUndoableEditListener(this);
57  		addFocusListener(this);
58  		
59  		setMinimumSize( new Dimension( 50, 50 ));
60  		addKeyListener( new KeyAdapter() {
61  
62  			public void keyPressed(KeyEvent e)
63  			{
64  				if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu Z" )))
65  				   undo();
66  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu Y" )))
67  					redo();
68  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu X" )))
69  					cut();
70  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "menu C" )))
71  					copy();
72  				else if( KeyStroke.getKeyStrokeForEvent( e ).equals( UISupport.getKeyStroke( "F3" )))
73  					findAndReplace();
74  			}} );
75  		
76  		JTextComponentPopupMenu.add(  this  );
77  	}
78  
79  	protected void findAndReplace()
80  	{
81  		if( findAndReplaceAction == null )
82  			findAndReplaceAction = new FindAndReplaceDialog( this );
83  		
84  		findAndReplaceAction.show();
85  	}
86  
87  	public void setText(String text)
88  	{
89  		super.setText(text);
90  		
91  		if( discardEditsOnSet && undoManager != null )
92  			undoManager.discardAllEdits();
93  	}
94  	
95  	public boolean isDiscardEditsOnSet()
96  	{
97  		return discardEditsOnSet;
98  	}
99  
100 	public void setDiscardEditsOnSet(boolean discardEditsOnSet)
101 	{
102 		this.discardEditsOnSet = discardEditsOnSet;
103 	}
104 
105 	public UndoManager getUndoManager()
106 	{
107 		return undoManager;
108 	}
109 	private void createUndoMananger()
110 	{
111 		undoManager = new UndoManager();
112 		undoManager.setLimit(UNDO_LIMIT);
113 	}
114 	
115 	public void focusGained(FocusEvent fe)
116 	{
117 		if (isEditable() && undoManager == null )
118 			createUndoMananger();
119 	}
120 
121 	public void focusLost(FocusEvent fe)
122 	{
123 		//removeUndoMananger();
124 	}
125 
126 	public void undoableEditHappened(UndoableEditEvent e)
127 	{
128 		if (undoManager != null)
129 			undoManager.addEdit(e.getEdit());
130 	}
131 
132 	public void undo()
133 	{
134 		if( !isEditable()  )
135 		{
136 			getToolkit().beep();
137 			return;
138 		}
139 		
140 		try
141 		{
142 			if( undoManager != null )
143 				undoManager.undo();
144 		}
145 		catch (CannotUndoException cue)
146 		{
147 			Toolkit.getDefaultToolkit().beep();
148 		}
149 	}
150 
151 	public void redo()
152 	{
153 		if( !isEditable()  )
154 		{
155 			getToolkit().beep();
156 			return;
157 		}
158 		
159 		try
160 		{
161 			if( undoManager != null )
162 				undoManager.redo();
163 		}
164 		catch (CannotRedoException cue)
165 		{
166 			Toolkit.getDefaultToolkit().beep();
167 		}
168 	}
169 
170 	public void setSelectedText(String txt)
171 	{
172 		replaceSelection( txt );
173 	}
174 
175 	public boolean canRedo()
176 	{
177 		return undoManager != null && undoManager.canRedo();
178 	}
179 
180 	public boolean canUndo()
181 	{
182 		return undoManager != null && undoManager.canUndo();
183 	}
184 }