KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > views > actions > SearchForBuildFilesDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.views.actions;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.regex.Matcher JavaDoc;
18 import java.util.regex.Pattern JavaDoc;
19 import org.eclipse.ant.internal.ui.AntUIPlugin;
20 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds;
21 import org.eclipse.ant.internal.ui.IAntUIPreferenceConstants;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.IResourceProxy;
25 import org.eclipse.core.resources.IResourceProxyVisitor;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IAdaptable;
29 import org.eclipse.jface.dialogs.IDialogSettings;
30 import org.eclipse.jface.dialogs.IInputValidator;
31 import org.eclipse.jface.dialogs.InputDialog;
32 import org.eclipse.jface.window.Window;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.graphics.Font;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.Display;
43 import org.eclipse.swt.widgets.Group;
44 import org.eclipse.swt.widgets.Shell;
45 import org.eclipse.swt.widgets.Text;
46 import org.eclipse.ui.IWorkingSet;
47 import org.eclipse.ui.PlatformUI;
48 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
49
50 /**
51  * This dialog allows the user to search for Ant build files whose names match a
52  * given pattern. The search may be performed on the entire workspace or it can
53  * be limited to a particular working set.
54  */

55 public class SearchForBuildFilesDialog extends InputDialog {
56
57     /**
58      * List of <code>IFile</code> objects that were found
59      */

60     private List JavaDoc results = new ArrayList JavaDoc();
61     /**
62      * List of <code>IResource</code> objects in which to search.
63      *
64      * If the searchScopes are <code>null</code>, the user has asked to search
65      * the workspace. If the searchScopes are empty, the user has asked to
66      * search a working set that has no resources.
67      */

68     private List JavaDoc searchScopes = null;
69     /**
70      * The working set scope radio button.
71      */

72     private Button workingSetScopeButton;
73     /**
74      * The workspace scope radio button.
75      */

76     private Button workspaceScopeButton;
77     /**
78      * The text field that displays the current working set name
79      */

80     private Text workingSetText;
81     /**
82      * The button that allows the user to decide if error results should be
83      * parsed
84      */

85     private Button includeErrorResultButton;
86     /**
87      * The dialog settings used to persist this dialog's settings.
88      */

89     private static IDialogSettings settings= AntUIPlugin.getDefault().getDialogSettings();
90     
91     /**
92      * Initialize any dialog settings that haven't been set.
93      */

94     static {
95         if (settings.get(IAntUIPreferenceConstants.ANTVIEW_LAST_SEARCH_STRING) == null) {
96             settings.put(IAntUIPreferenceConstants.ANTVIEW_LAST_SEARCH_STRING, "build.xml"); //$NON-NLS-1$
97
}
98         if (settings.get(IAntUIPreferenceConstants.ANTVIEW_LAST_WORKINGSET_SEARCH_SCOPE) == null) {
99             settings.put(IAntUIPreferenceConstants.ANTVIEW_LAST_WORKINGSET_SEARCH_SCOPE, ""); //$NON-NLS-1$
100
}
101     }
102
103     /**
104      * Creates a new dialog to search for build files.
105      */

106     public SearchForBuildFilesDialog() {
107         super(Display.getCurrent().getActiveShell(), AntViewActionMessages.SearchForBuildFilesDialog_Search_for_Build_Files_1, AntViewActionMessages.SearchForBuildFilesDialog__Input,
108                 settings.get(IAntUIPreferenceConstants.ANTVIEW_LAST_SEARCH_STRING), new IInputValidator() {
109             public String JavaDoc isValid(String JavaDoc newText) {
110                 String JavaDoc trimmedText = newText.trim();
111                 if (trimmedText.length() == 0) {
112                     return AntViewActionMessages.SearchForBuildFilesDialog_Build_name_cannot_be_empty_3;
113                 }
114                 return null;
115             }
116         });
117     }
118
119     /**
120      * Change the label on the "Ok" button and initialize the enabled state
121      */

122     protected void createButtonsForButtonBar(Composite parent) {
123         super.createButtonsForButtonBar(parent);
124         getOkButton().setText(AntViewActionMessages.SearchForBuildFilesDialog__Search_4);
125
126         String JavaDoc workingSetName= settings.get(IAntUIPreferenceConstants.ANTVIEW_LAST_WORKINGSET_SEARCH_SCOPE);
127         if (workingSetName.length() > 0) {
128             setWorkingSet(PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(workingSetName));
129         }
130         if (!settings.getBoolean(IAntUIPreferenceConstants.ANTVIEW_USE_WORKINGSET_SEARCH_SCOPE)) {
131             selectRadioButton(workspaceScopeButton);
132             handleRadioButtonPressed();
133         }
134     }
135
136     /**
137      * Add the scope selection widgets to the dialog area
138      */

139     protected Control createDialogArea(Composite parent) {
140         Font font = parent.getFont();
141         
142         Composite composite = (Composite) super.createDialogArea(parent);
143         createIncludeErrorResultButton(composite, font);
144         createScopeGroup(composite, font);
145         return composite;
146     }
147     
148     private void createScopeGroup(Composite composite, Font font) {
149         Group scope= new Group(composite, SWT.NONE);
150         scope.setText(AntViewActionMessages.SearchForBuildFilesDialog_Scope_5);
151         GridData data= new GridData(GridData.FILL_BOTH);
152         scope.setLayoutData(data);
153         GridLayout layout= new GridLayout(3, false);
154         scope.setLayout(layout);
155         scope.setFont(font);
156         
157         // Create a composite for the radio buttons
158
Composite radioComposite= new Composite(scope, SWT.NONE);
159         GridLayout radioLayout= new GridLayout();
160         radioLayout.marginHeight= 0;
161         radioComposite.setLayout(radioLayout);
162
163         SelectionAdapter selectionListener= new SelectionAdapter() {
164             public void widgetSelected(SelectionEvent e) {
165                 handleRadioButtonPressed();
166             }
167         };
168
169         workspaceScopeButton= new Button(radioComposite, SWT.RADIO);
170         workspaceScopeButton.setFont(font);
171         workspaceScopeButton.setText(AntViewActionMessages.SearchForBuildFilesDialog__Workspace_6);
172         workspaceScopeButton.addSelectionListener(selectionListener);
173
174         workingSetScopeButton=new Button(radioComposite, SWT.RADIO);
175         workingSetScopeButton.setFont(font);
176         workingSetScopeButton.setText(AntViewActionMessages.SearchForBuildFilesDialog_Wor_king_Set__7);
177         workingSetScopeButton.addSelectionListener(selectionListener);
178         
179         selectRadioButton(workspaceScopeButton);
180
181         workingSetText= new Text(scope, SWT.BORDER);
182         workingSetText.setEditable(false);
183         data= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_END);
184         workingSetText.setLayoutData(data);
185         workingSetText.setFont(font);
186
187         Button chooseButton = new Button(scope, SWT.PUSH);
188         data= new GridData(GridData.VERTICAL_ALIGN_END);
189         chooseButton.setLayoutData(data);
190         chooseButton.setFont(font);
191         chooseButton.setText(AntViewActionMessages.SearchForBuildFilesDialog__Choose____8);
192         chooseButton.addSelectionListener(new SelectionAdapter() {
193             public void widgetSelected(SelectionEvent evt) {
194                 handleChooseButtonPressed();
195             }
196         });
197     }
198     
199     /**
200      * Programatically selects the given radio button, deselecting the other
201      * radio button.
202      *
203      * @param button the radio button to select. This parameter must be one of
204      * either the <code>workingSetScopeButton</code> or the
205      * <code>workspaceScopeButton</code> or this method will have no effect.
206      */

207     private void selectRadioButton(Button button) {
208         if (button == workingSetScopeButton) {
209             workingSetScopeButton.setSelection(true);
210             workspaceScopeButton.setSelection(false);
211         } else if (button == workspaceScopeButton) {
212             workspaceScopeButton.setSelection(true);
213             workingSetScopeButton.setSelection(false);
214         }
215     }
216     
217     /**
218      * One of the search scope radio buttons has been pressed. Update the dialog
219      * accordingly.
220      */

221     private void handleRadioButtonPressed() {
222         if (workingSetScopeButton.getSelection()) {
223             IWorkingSet set= PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(getWorkingSetName());
224             if (set != null) {
225                 setWorkingSet(set);
226                 return;
227             }
228         }
229         setWorkingSet(null);
230     }
231     
232     /**
233      * Returns the working set name currently displayed.
234      */

235     private String JavaDoc getWorkingSetName() {
236         return workingSetText.getText().trim();
237     }
238     
239     /**
240      * Creates the button that allows the user to specify whether or not build
241      * files should that cannot be parsed should be included in the results.
242      */

243     private void createIncludeErrorResultButton(Composite composite, Font font) {
244         includeErrorResultButton= new Button(composite, SWT.CHECK);
245         includeErrorResultButton.setFont(font);
246         includeErrorResultButton.setText(AntViewActionMessages.SearchForBuildFilesDialog_Include_errors);
247         includeErrorResultButton.setSelection(settings.getBoolean(IAntUIPreferenceConstants.ANTVIEW_INCLUDE_ERROR_SEARCH_RESULTS));
248     }
249     
250     /**
251      * Updates the dialog based on the state of the working set settings
252      * <ul>
253      * <li>Sets the enablement of the "Search" button based on the validity of
254      * the settings</li>
255      * <li>Sets any or clears the error message</li>
256      * </ul>
257      */

258     private void updateForWorkingSetSettings() {
259         if (workingSetScopeButton.getSelection()) {
260             String JavaDoc error= null;
261             if (searchScopes == null) {
262                 error= AntViewActionMessages.SearchForBuildFilesDialog_Must_select_a_working_set_10;
263             } else if (searchScopes.isEmpty()) {
264                 error= AntViewActionMessages.SearchForBuildFilesDialog_No_searchable;
265             }
266             if (error != null) {
267                 setErrorMessage(error);
268                 getOkButton().setEnabled(false);
269                 return;
270             }
271         }
272         getOkButton().setEnabled(true);
273         setErrorMessage(null);
274     }
275
276     /**
277      * Handles the working set choose button pressed. Returns the name of the
278      * chosen working set or <code>null</code> if none.
279      */

280     private void handleChooseButtonPressed() {
281         IWorkingSetSelectionDialog dialog= PlatformUI.getWorkbench().getWorkingSetManager().createWorkingSetSelectionDialog(getShell(), false);
282         if (dialog.open() == Window.CANCEL) {
283             return;
284         }
285         IWorkingSet[] sets= dialog.getSelection();
286         if (sets == null) {
287             return;
288         }
289         if (sets.length == 0) {
290             setWorkingSet(null); //ok pressed with no working set selected
291
} else {
292             setWorkingSet(sets[0]); // We disallowed multi-select
293
}
294     }
295     
296     /**
297      * Sets the current working set search scope. This populates the search
298      * scope with resources found in the given working set and updates the
299      * enabled state of the dialog based on the sets contents.
300      *
301      * @param set the working set scope for the search
302      */

303     private void setWorkingSet(IWorkingSet set) {
304         if (set == null) {
305             searchScopes= null;
306             workingSetText.setText(""); //$NON-NLS-1$
307
validateInput();
308             return;
309         }
310         IAdaptable[] elements= set.getElements();
311         searchScopes= new ArrayList JavaDoc();
312         for (int i = 0; i < elements.length; i++) {
313             // Try to get an IResource object from each element
314
IResource resource= null;
315             IAdaptable adaptable = elements[i];
316             if (adaptable instanceof IResource) {
317                 resource= (IResource) adaptable;
318             } else {
319                 resource= (IResource) adaptable.getAdapter(IResource.class);
320             }
321             if (resource != null) {
322                 searchScopes.add(resource);
323             }
324         }
325         workingSetText.setText(set.getName());
326         selectRadioButton(workingSetScopeButton);
327         
328         validateInput();
329     }
330
331     /**
332      * Returns the trimmed user input
333      */

334     private String JavaDoc getInput() {
335         return getText().getText().trim();
336     }
337
338     /**
339      * Returns the search results
340      */

341     public IFile[] getResults() {
342         return (IFile[]) results.toArray(new IFile[results.size()]);
343     }
344     
345     /**
346      * Returns whether the user wishes to include results which cannot be
347      * parsed.
348      */

349     protected boolean getIncludeErrorResults() {
350         return settings.getBoolean(IAntUIPreferenceConstants.ANTVIEW_INCLUDE_ERROR_SEARCH_RESULTS);
351     }
352
353     /**
354      * When the user presses the search button (tied to the OK id), search the
355      * workspace for files matching the regular expression in the input field.
356      */

357     protected void okPressed() {
358         String JavaDoc input = getInput();
359         settings.put(IAntUIPreferenceConstants.ANTVIEW_LAST_SEARCH_STRING, input);
360         settings.put(IAntUIPreferenceConstants.ANTVIEW_INCLUDE_ERROR_SEARCH_RESULTS, includeErrorResultButton.getSelection());
361         settings.put(IAntUIPreferenceConstants.ANTVIEW_LAST_WORKINGSET_SEARCH_SCOPE, getWorkingSetName());
362         settings.put(IAntUIPreferenceConstants.ANTVIEW_USE_WORKINGSET_SEARCH_SCOPE, workingSetScopeButton.getSelection());
363         results = new ArrayList JavaDoc(); // Clear previous results
364
ResourceProxyVisitor visitor= new ResourceProxyVisitor();
365         if (searchScopes == null || searchScopes.isEmpty()) {
366             try {
367                 ResourcesPlugin.getWorkspace().getRoot().accept(visitor, IResource.NONE);
368             } catch (CoreException ce) {
369                 //Closed project...don't want build files from there
370
}
371         } else {
372             Iterator JavaDoc iter= searchScopes.iterator();
373             while(iter.hasNext()) {
374                 try {
375                     ((IResource) iter.next()).accept(visitor, IResource.NONE);
376                 } catch (CoreException ce) {
377                     //Closed project...don't want build files from there
378
}
379             }
380         }
381         super.okPressed();
382     }
383     
384     /**
385      * Searches for files whose name matches the given regular expression.
386      */

387     class ResourceProxyVisitor implements IResourceProxyVisitor {
388         Pattern JavaDoc pattern;
389         
390         ResourceProxyVisitor() {
391 // Users use "*" and "?" where regex uses ".*" and ".?"
392
// The character "." must be escaped in regex
393
String JavaDoc input = getInput();
394 // replace "." with "\\."
395
input = input.replaceAll("\\.", "\\\\."); //$NON-NLS-1$ //$NON-NLS-2$
396
// replace "*" with ".*"
397
input = input.replaceAll("\\*", "\\.\\*"); //$NON-NLS-1$ //$NON-NLS-2$
398
// replace "?" with ".?"
399
input = input.replaceAll("\\?", "\\.\\?"); //$NON-NLS-1$ //$NON-NLS-2$
400
pattern = Pattern.compile(input);
401         }
402
403         /**
404          * @see org.eclipse.core.resources.IResourceProxyVisitor#visit(org.eclipse.core.resources.IResourceProxy)
405          */

406         public boolean visit(IResourceProxy proxy) {
407             if (proxy.getType() == IResource.FILE) {
408                 Matcher JavaDoc matcher = pattern.matcher(proxy.getName());
409                 if (matcher.find()) {
410                     results.add(proxy.requestResource());
411                 }
412                 return false;
413             }
414             return true;
415         }
416     }
417     /* (non-Javadoc)
418      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
419      */

420     protected void configureShell(Shell shell) {
421         super.configureShell(shell);
422         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IAntUIHelpContextIds.SEARCH_FOR_BUILDFILES_DIALOG);
423     }
424
425     /* (non-Javadoc)
426      * @see org.eclipse.jface.dialogs.InputDialog#validateInput()
427      */

428     protected void validateInput() {
429         String JavaDoc errorMessage = null;
430         if (getValidator() != null) {
431             errorMessage = getValidator().isValid(getText().getText());
432         }
433
434         setErrorMessage(errorMessage);
435         if (errorMessage == null) {
436             updateForWorkingSetSettings();
437         }
438     }
439 }
440
Popular Tags