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.actions;
14  
15  import java.awt.BorderLayout;
16  import java.awt.GridLayout;
17  import java.awt.Toolkit;
18  import java.awt.event.ActionEvent;
19  
20  import javax.swing.AbstractAction;
21  import javax.swing.Action;
22  import javax.swing.BorderFactory;
23  import javax.swing.ButtonGroup;
24  import javax.swing.JButton;
25  import javax.swing.JCheckBox;
26  import javax.swing.JComboBox;
27  import javax.swing.JDialog;
28  import javax.swing.JLabel;
29  import javax.swing.JPanel;
30  import javax.swing.JRadioButton;
31  
32  import com.eviware.soapui.support.UISupport;
33  import com.jgoodies.forms.builder.ButtonBarBuilder;
34  
35  /***
36   * Find-and-Replace dialog for a JXmlTextArea
37   * 
38   * @author Ole.Matzura
39   */
40  
41  public class FindAndReplaceDialog extends AbstractAction
42  {
43  	private final FindAndReplaceable target;
44  	private JDialog dialog;
45  	private JCheckBox caseCheck;
46  	private JRadioButton allButton;
47  	private JRadioButton selectedLinesButton;
48  	private JRadioButton forwardButton;
49  	private JRadioButton backwardButton;
50  	private JCheckBox wholeWordCheck;
51  	private JButton findButton;
52  	private JButton replaceButton;
53  	private JButton replaceAllButton;
54  	private JComboBox findCombo;
55  	private JComboBox replaceCombo;
56  	private JCheckBox wrapCheck;
57  
58  	public FindAndReplaceDialog( FindAndReplaceable target )
59  	{
60  		super( "Find / Replace" );
61  		putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "F3" ) );
62  		this.target = target;
63  	}
64  
65  	public void actionPerformed( ActionEvent e )
66  	{
67  		show();
68  	}
69  
70  	public void show()
71  	{
72  		if( dialog == null )
73  			buildDialog();
74  
75  		replaceCombo.setEnabled( target.isEditable() );
76  		replaceAllButton.setEnabled( target.isEditable() );
77  		replaceButton.setEnabled( target.isEditable() );
78  
79  		UISupport.showDialog( dialog );
80  		findCombo.getEditor().selectAll();
81  		findCombo.requestFocus();
82  	}
83  
84  	private void buildDialog()
85  	{
86  		dialog = new JDialog( UISupport.getMainFrame(), "Find / Replace", false );
87  
88  		JPanel panel = new JPanel( new BorderLayout() );
89  		findCombo = new JComboBox();
90  		findCombo.setEditable( true );
91  		replaceCombo = new JComboBox();
92  		replaceCombo.setEditable( true );
93  
94  		// create inputs
95  		GridLayout gridLayout = new GridLayout( 2, 2 );
96  		gridLayout.setVgap( 5 );
97  		JPanel inputPanel = new JPanel( gridLayout );
98  		inputPanel.add( new JLabel( "Find:" ) );
99  		inputPanel.add( findCombo );
100 		inputPanel.add( new JLabel( "Replace with:" ) );
101 		inputPanel.add( replaceCombo );
102 		inputPanel.setBorder( BorderFactory.createEmptyBorder( 8, 8, 8, 8 ) );
103 
104 		// create direction panel
105 		ButtonGroup directionGroup = new ButtonGroup();
106 		forwardButton = new JRadioButton( "Forward", true );
107 		forwardButton.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
108 		directionGroup.add( forwardButton );
109 		backwardButton = new JRadioButton( "Backward" );
110 		backwardButton.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
111 		directionGroup.add( backwardButton );
112 
113 		JPanel directionPanel = new JPanel( new GridLayout( 2, 1 ) );
114 		directionPanel.add( forwardButton );
115 		directionPanel.add( backwardButton );
116 		directionPanel.setBorder( BorderFactory.createTitledBorder( "Direction" ) );
117 
118 		// create scope panel
119 		ButtonGroup scopeGroup = new ButtonGroup();
120 		allButton = new JRadioButton( "All", true );
121 		allButton.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
122 		selectedLinesButton = new JRadioButton( "Selected Lines" );
123 		selectedLinesButton.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
124 		scopeGroup.add( allButton );
125 		scopeGroup.add( selectedLinesButton );
126 
127 		JPanel scopePanel = new JPanel( new GridLayout( 2, 1 ) );
128 		scopePanel.add( allButton );
129 		scopePanel.add( selectedLinesButton );
130 		scopePanel.setBorder( BorderFactory.createTitledBorder( "Scope" ) );
131 
132 		// create options
133 		caseCheck = new JCheckBox( "Case Sensitive" );
134 		caseCheck.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
135 		wholeWordCheck = new JCheckBox( "Whole Word" );
136 		wholeWordCheck.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
137 		wrapCheck = new JCheckBox( "Wrap Search" );
138 		wrapCheck.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
139 		JPanel optionsPanel = new JPanel( new GridLayout( 3, 1 ) );
140 		optionsPanel.add( caseCheck );
141 		optionsPanel.add( wholeWordCheck );
142 		optionsPanel.add( wrapCheck );
143 		optionsPanel.setBorder( BorderFactory.createTitledBorder( "Options" ) );
144 
145 		// create panel with options
146 		JPanel options = new JPanel( new GridLayout( 1, 2 ) );
147 
148 		JPanel radios = new JPanel( new GridLayout( 2, 1 ) );
149 		radios.add( directionPanel );
150 		radios.add( scopePanel );
151 
152 		options.add( optionsPanel );
153 		options.add( radios );
154 		options.setBorder( BorderFactory.createEmptyBorder( 0, 8, 0, 8 ) );
155 
156 		// create buttons
157 		ButtonBarBuilder builder = new ButtonBarBuilder();
158 		findButton = new JButton( new FindAction() );
159 		builder.addFixed( findButton );
160 		builder.addRelatedGap();
161 		replaceButton = new JButton( new ReplaceAction() );
162 		builder.addFixed( replaceButton );
163 		builder.addRelatedGap();
164 		replaceAllButton = new JButton( new ReplaceAllAction() );
165 		builder.addFixed( replaceAllButton );
166 		builder.addUnrelatedGap();
167 		builder.addFixed( new JButton( new CloseAction() ) );
168 		builder.setBorder( BorderFactory.createEmptyBorder( 8, 8, 8, 8 ) );
169 
170 		// tie it up!
171 		panel.add( inputPanel, BorderLayout.NORTH );
172 		panel.add( options, BorderLayout.CENTER );
173 		panel.add( builder.getPanel(), BorderLayout.SOUTH );
174 
175 		dialog.getContentPane().add( panel );
176 		dialog.pack();
177 		UISupport.initDialogActions( dialog, null, findButton );
178 	}
179 
180 	private int findNext( int pos, String txt, String value )
181 	{
182 		int ix = forwardButton.isSelected() ? txt.indexOf( value, pos ) : txt.lastIndexOf( value, pos );
183 
184 		if( wholeWordCheck.isSelected() )
185 		{
186 			while( ix != -1
187 					&& ( ix > 0 && Character.isLetterOrDigit( txt.charAt( ix - 1 ) ) )
188 					|| ( ix < txt.length() - value.length() - 1 && Character.isLetterOrDigit( txt.charAt( ix
189 							+ value.length() ) ) ) )
190 			{
191 				ix = findNext( ix, txt, value );
192 			}
193 		}
194 
195 		if( ix == -1 && wrapCheck.isSelected() )
196 		{
197 			if( forwardButton.isSelected() && pos > 0 )
198 				return findNext( 0, txt, value );
199 			else if( backwardButton.isSelected() && pos < txt.length() - 1 )
200 				return findNext( txt.length() - 1, txt, value );
201 		}
202 
203 		if( selectedLinesButton.isSelected() && ( ix < target.getSelectionStart() || ix > target.getSelectionEnd() ) )
204 			ix = -1;
205 
206 		return ix;
207 	}
208 
209 	private class FindAction extends AbstractAction
210 	{
211 		String lastSearchedItem = "";
212 		int lastPositionF = -1;
213 
214 		public FindAction()
215 		{
216 			super( "Find/Find Next" );
217 		}
218 
219 		public void actionPerformed( ActionEvent e )
220 		{
221 			int pos = target.getCaretPosition();
222 			int selstart = target.getSelectionStart();
223 			if( selstart < pos && selstart != -1 )
224 				pos = selstart;
225 
226 			String txt = target.getText();
227 
228 			if( findCombo.getSelectedItem() == null )
229 			{
230 				return;
231 			}
232 			String value = findCombo.getSelectedItem().toString();
233 			if( value.length() == 0 || pos == txt.length() )
234 				return;
235 
236 			if( !caseCheck.isSelected() )
237 			{
238 				value = value.toUpperCase();
239 				txt = txt.toUpperCase();
240 			}
241 
242 			if( pos == lastPositionF && value.equals( lastSearchedItem ) )
243 				if( forwardButton.isSelected() )
244 					pos += value.length() + 1;
245 				else
246 					pos -= value.length() - 1;
247 
248 			int ix = findNext( pos, txt, value );
249 
250 			lastSearchedItem = value;
251 			lastPositionF = ix;
252 
253 			if( ix != -1 )
254 			{
255 				target.select( ix, ix + value.length() );
256 
257 				for( int c = 0; c < findCombo.getItemCount(); c++ )
258 				{
259 					if( findCombo.getItemAt( c ).equals( value ) )
260 					{
261 						findCombo.removeItem( c );
262 						break;
263 					}
264 				}
265 
266 				findCombo.insertItemAt( value, 0 );
267 			}
268 			else
269 				Toolkit.getDefaultToolkit().beep();
270 		}
271 	}
272 
273 	private class ReplaceAction extends AbstractAction
274 	{
275 		String lastSearchedItem = "";
276 		int lastPositionF = -1;
277 
278 		public ReplaceAction()
279 		{
280 			super( "Replace/Replace Next" );
281 		}
282 
283 		public void actionPerformed( ActionEvent e )
284 		{
285 			int pos = target.getCaretPosition();
286 			String txt = target.getText();
287 
288 			if( findCombo.getSelectedItem() == null )
289 			{
290 				return;
291 			}
292 			String value = findCombo.getSelectedItem().toString();
293 			if( value.length() == 0 || txt.length() == 0 )
294 				return;
295 
296 			String newValue = replaceCombo.getSelectedItem() == null ? "" : replaceCombo.getSelectedItem().toString();
297 
298 			if( !caseCheck.isSelected() )
299 			{
300 				if( newValue.equalsIgnoreCase( value ) )
301 					return;
302 				value = value.toUpperCase();
303 				txt = txt.toUpperCase();
304 			}
305 			else if( newValue.equals( value ) )
306 				return;
307 
308 			if( pos == lastPositionF && value.equals( lastSearchedItem ) )
309 				if( forwardButton.isSelected() )
310 					pos += value.length() + 1;
311 				else
312 					pos -= value.length() - 1;
313 
314 			int ix = findNext( pos, txt, value );
315 
316 			lastSearchedItem = value;
317 			lastPositionF = ix;
318 
319 			int firstIx = ix;
320 			int valueInNewValueIx = !caseCheck.isSelected() ? newValue.toUpperCase().indexOf( value ) : newValue
321 					.indexOf( value );
322 
323 			if( ix != -1 )
324 			{
325 				System.out.println( "found match at " + ix + ", " + firstIx + ", " + valueInNewValueIx );
326 				target.select( ix, ix + value.length() );
327 
328 				target.setSelectedText( newValue );
329 				target.select( ix, ix + newValue.length() );
330 
331 				// adjust firstix
332 				if( ix < firstIx )
333 					firstIx += newValue.length() - value.length();
334 
335 				txt = target.getText();
336 				if( !caseCheck.isSelected() )
337 				{
338 					txt = txt.toUpperCase();
339 				}
340 
341 				if( forwardButton.isSelected() )
342 				{
343 					ix = findNext( ix + newValue.length(), txt, value );
344 				}
345 				else
346 				{
347 					ix = findNext( ix - 1, txt, value );
348 				}
349 			}
350 		}
351 	}
352 
353 	private class ReplaceAllAction extends AbstractAction
354 	{
355 		public ReplaceAllAction()
356 		{
357 			super( "Replace All" );
358 		}
359 
360 		public void actionPerformed( ActionEvent e )
361 		{
362 			int pos = target.getCaretPosition();
363 			String txt = target.getText();
364 
365 			if( findCombo.getSelectedItem() == null )
366 			{
367 				return;
368 			}
369 			String value = findCombo.getSelectedItem().toString();
370 			if( value.length() == 0 || txt.length() == 0 )
371 				return;
372 
373 			String newValue = replaceCombo.getSelectedItem() == null ? "" : replaceCombo.getSelectedItem().toString();
374 
375 			if( !caseCheck.isSelected() )
376 			{
377 				if( newValue.equalsIgnoreCase( value ) )
378 					return;
379 				value = value.toUpperCase();
380 				txt = txt.toUpperCase();
381 			}
382 			else if( newValue.equals( value ) )
383 				return;
384 
385 			int ix = findNext( pos, txt, value );
386 			int firstIx = ix;
387 			int valueInNewValueIx = !caseCheck.isSelected() ? newValue.toUpperCase().indexOf( value ) : newValue
388 					.indexOf( value );
389 
390 			while( ix != -1 )
391 			{
392 				System.out.println( "found match at " + ix + ", " + firstIx + ", " + valueInNewValueIx );
393 				target.select( ix, ix + value.length() );
394 
395 				target.setSelectedText( newValue );
396 				target.select( ix, ix + newValue.length() );
397 
398 				// adjust firstix
399 				if( ix < firstIx )
400 					firstIx += newValue.length() - value.length();
401 
402 				txt = target.getText();
403 				if( !caseCheck.isSelected() )
404 				{
405 					txt = txt.toUpperCase();
406 				}
407 
408 				if( forwardButton.isSelected() )
409 				{
410 					ix = findNext( ix + newValue.length(), txt, value );
411 				}
412 				else
413 				{
414 					ix = findNext( ix - 1, txt, value );
415 				}
416 				if( wrapCheck.isSelected() && valueInNewValueIx != -1 && ix == firstIx + valueInNewValueIx )
417 				{
418 					break;
419 				}
420 			}
421 		}
422 	}
423 
424 	private class CloseAction extends AbstractAction
425 	{
426 		public CloseAction()
427 		{
428 			super( "Close" );
429 		}
430 
431 		public void actionPerformed( ActionEvent e )
432 		{
433 			dialog.setVisible( false );
434 		}
435 	}
436 
437 }