Skip to main content
summaryrefslogtreecommitdiffstats
blob: d7249810f3b29d4a9d777ae9b227ef2dcd0f709a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//------------------------------------------------------------------------------
// Copyright (c) 2005, 2007 IBM Corporation and others.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
// IBM Corporation - initial implementation
//------------------------------------------------------------------------------
package org.eclipse.epf.richtext.dialogs;

import org.eclipse.epf.richtext.Messages;
import org.eclipse.epf.richtext.actions.FindReplaceAction;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Prompts the user to specify the search and replace strings and options.
 * 
 * @author Kelvin Low
 * @since 1.0
 */
public class FindReplaceDialog extends BaseDialog {

	private FindReplaceAction findReplaceAction;

	private boolean findOnly;

	private Text findText;

	private Text replaceText;

	private Button searchForwardRadioButton;

	private Button searchBackwardRadioButton;

	private Button caseSensitiveCheckbox;

	private Button wholeWordCheckbox;

	private Button findButton;

	private Button replaceButton;

	private Button replaceFindButton;

	private Button replaceAllButton;

	private Label statusLabel;

	/**
	 * Creates a new instance.
	 * 
	 * @param parent
	 *            the parent shell
	 * @param findReplaceAction
	 *            the Find and Replace action
	 * @param findOnly
	 *            if <code>true</code>, disable the replace and replace all
	 *            functionalities
	 */
	public FindReplaceDialog(Shell parent, FindReplaceAction findReplaceAction,
			boolean findOnly) {
		super(parent);
		setShellStyle(SWT.DIALOG_TRIM | SWT.MODELESS | getDefaultOrientation());
		setBlockOnOpen(false);
		this.findReplaceAction = findReplaceAction;
		this.findOnly = findOnly;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		GridLayout layout = (GridLayout) composite.getLayout();
		layout.numColumns = 1;

		Composite textComposite = new Composite(composite, SWT.NONE);
		textComposite.setLayout(new GridLayout(2, false));
		textComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		Label findLabel = new Label(textComposite, SWT.NONE);
		findLabel.setText(Messages.findLabel_text);
		findText = new Text(textComposite, SWT.BORDER);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.widthHint = 200;
		findText.setLayoutData(gridData);
		findText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if (findButton != null) {
					findButton
							.setEnabled(findText.getText().trim().length() > 0);
				}
				if (replaceAllButton != null && !findOnly) {
					replaceAllButton.setEnabled(findText.getText().trim()
							.length() > 0);
				}
			}
		});

		Label replaceLabel = new Label(textComposite, SWT.NONE);
		replaceLabel.setText(Messages.replaceLabel_text);
		replaceText = new Text(textComposite, SWT.BORDER);
		replaceText.setLayoutData(gridData);
		if (findOnly) {
			replaceText.setEnabled(false);
		} else {
			replaceText.addModifyListener(new ModifyListener() {
				public void modifyText(ModifyEvent e) {
					if (replaceButton != null) {
						replaceButton.setEnabled(findReplaceAction
								.getFoundMatch());
					}
					if (replaceFindButton != null) {
						replaceFindButton.setEnabled(findReplaceAction
								.getFoundMatch());
					}
				}
			});
		}

		Composite optionsComposite = new Composite(composite, SWT.NONE);
		optionsComposite.setLayout(new GridLayout(2, true));
		optionsComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		Group directionGroup = new Group(optionsComposite, SWT.NONE);
		directionGroup.setText(Messages.directionGroup_text);
		directionGroup.setLayout(new GridLayout(1, false));
		directionGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		searchForwardRadioButton = new Button(directionGroup, SWT.RADIO);
		searchForwardRadioButton
				.setText(Messages.forwardRadioButton_text);
		searchForwardRadioButton.setSelection(true);
		searchBackwardRadioButton = new Button(directionGroup, SWT.RADIO);
		searchBackwardRadioButton
				.setText(Messages.backwardRadioButton_text);

		Group optionsGroup = new Group(optionsComposite, SWT.NONE);
		optionsGroup.setText(Messages.optionsGroup_text);
		optionsGroup.setLayout(new GridLayout(1, false));
		optionsGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		caseSensitiveCheckbox = new Button(optionsGroup, SWT.CHECK);
		caseSensitiveCheckbox
				.setText(Messages.caseSensitiveCheckbox_text);
		wholeWordCheckbox = new Button(optionsGroup, SWT.CHECK);
		wholeWordCheckbox.setText(Messages.wholeWordCheckbox_text);

		statusLabel = new Label(composite, SWT.NONE);
		statusLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		super.getShell().setText(Messages.findReplaceDialog_title);

		return composite;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.CLIENT_ID + 1,
				Messages.findButton_text, true);
		findButton = super.getButton(IDialogConstants.CLIENT_ID + 1);
		findButton.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent event) {
				findReplaceAction.run(FindReplaceAction.FIND_TEXT,
						getFindText(), getReplaceText(), getMatchDirection(),
						getMatchOptions());
				if (!findOnly) {
					replaceButton.setEnabled(findReplaceAction.getFoundMatch());
					replaceFindButton.setEnabled(findReplaceAction
							.getFoundMatch());
				}
				if (findReplaceAction.getFoundMatch()) {
					statusLabel.setText(""); //$NON-NLS-1$
				} else {
					statusLabel
							.setText(Messages.FindReplace_Status_noMatch_label);
				}
				findButton.setFocus();
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		createButton(parent, IDialogConstants.CLIENT_ID + 2,
				Messages.replaceButton_text, false);
		replaceButton = super.getButton(IDialogConstants.CLIENT_ID + 2);
		if (!findOnly) {
			replaceButton.addSelectionListener(new SelectionListener() {
				public void widgetSelected(SelectionEvent event) {
					findReplaceAction.run(FindReplaceAction.REPLACE_TEXT,
							getFindText(), getReplaceText(),
							getMatchDirection(), getMatchOptions());
					replaceButton.setFocus();
				}

				public void widgetDefaultSelected(SelectionEvent e) {
				}
			});
		}

		createButton(parent, IDialogConstants.CLIENT_ID + 3,
				Messages.replaceFindButton_text, false);
		replaceFindButton = super.getButton(IDialogConstants.CLIENT_ID + 3);
		if (!findOnly) {
			replaceFindButton.addSelectionListener(new SelectionListener() {
				public void widgetSelected(SelectionEvent event) {
					findReplaceAction.run(FindReplaceAction.REPLACE_FIND_TEXT,
							getFindText(), getReplaceText(),
							getMatchDirection(), getMatchOptions());
					replaceButton.setEnabled(findReplaceAction.getFoundMatch());
					replaceFindButton.setEnabled(findReplaceAction
							.getFoundMatch());
					replaceFindButton.setFocus();
				}

				public void widgetDefaultSelected(SelectionEvent e) {
				}
			});
		}

		createButton(parent, IDialogConstants.CLIENT_ID + 4,
				Messages.replaceallButton_text, false);
		replaceAllButton = super.getButton(IDialogConstants.CLIENT_ID + 4);
		if (!findOnly) {
			replaceAllButton.addSelectionListener(new SelectionListener() {
				public void widgetSelected(SelectionEvent event) {
					findReplaceAction.run(FindReplaceAction.REPLACE_ALL_TEXT,
							getFindText(), getReplaceText(),
							getMatchDirection(), getMatchOptions());
					replaceButton.setEnabled(false);
					replaceFindButton.setEnabled(false);
					replaceAllButton.setFocus();
				}

				public void widgetDefaultSelected(SelectionEvent e) {
				}
			});
		}

		// Create the Cancel button.
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
		cancelButton = super.getButton(IDialogConstants.CANCEL_ID);

		findButton.setEnabled(false);
		replaceButton.setEnabled(false);
		replaceFindButton.setEnabled(false);
		replaceAllButton.setEnabled(false);
	}

	/**
	 * Gets the user specified find text.
	 * 
	 * @return the find text
	 */
	public String getFindText() {
		return findText.getText();
	}

	/**
	 * Gets the user specified replace text.
	 * 
	 * @return the replace text
	 */
	public String getReplaceText() {
		return replaceText.getText();
	}

	/**
	 * Gets the text match direction.
	 * 
	 * @return <code>FIND_FORWARD</code> or <code>FIND_BACKWARD</code>
	 */
	public int getMatchDirection() {
		return searchForwardRadioButton.getSelection() ? FindReplaceAction.FORWARD_MATCH
				: FindReplaceAction.BACKWARD_MATCH;
	}

	/**
	 * Gets the text match options.
	 * 
	 * @return the text match options
	 */
	public int getMatchOptions() {
		int options = 0;
		if (wholeWordCheckbox.getSelection() == true) {
			options |= FindReplaceAction.WHOLE_WORD_MATCH;
		}
		if (caseSensitiveCheckbox.getSelection() == true) {
			options |= FindReplaceAction.CASE_SENSITIVE_MATCH;
		}
		return options;
	}

	/**
	 * Checks the find only option.
	 * 
	 * @return <code>true</code> if find only option is enabled
	 */
	public boolean isFindOnly() {
		return findOnly;
	}

	/**
	 * Sets the find only option.
	 * 
	 * @param findOnly,
	 *            if <code>true</code>, enable the find only option
	 */
	public void setFindOnly(boolean findOnly) {
		this.findOnly = findOnly;
	}

}

Back to the top