KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > ScopePart


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.search.internal.ui;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.accessibility.AccessibleAdapter;
23 import org.eclipse.swt.accessibility.AccessibleEvent;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Group;
31 import org.eclipse.swt.widgets.Text;
32
33 import org.eclipse.jface.dialogs.IDialogSettings;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.jface.window.Window;
37
38 import org.eclipse.ui.IWorkbenchPage;
39 import org.eclipse.ui.IWorkingSet;
40 import org.eclipse.ui.IWorkingSetManager;
41 import org.eclipse.ui.PlatformUI;
42 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
43
44 import org.eclipse.search.ui.ISearchPageContainer;
45 import org.eclipse.search.ui.NewSearchUI;
46
47 import org.eclipse.search.internal.ui.util.PixelConverter;
48 import org.eclipse.search.internal.ui.util.SWTUtil;
49
50 public class ScopePart {
51
52     // Settings store
53
private static final String JavaDoc DIALOG_SETTINGS_KEY= "SearchDialog.ScopePart"; //$NON-NLS-1$
54
private static final String JavaDoc STORE_SCOPE= "scope"; //$NON-NLS-1$
55
private static final String JavaDoc STORE_LRU_WORKING_SET_NAME= "lastUsedWorkingSetName"; //$NON-NLS-1$
56
private static final String JavaDoc STORE_LRU_WORKING_SET_NAMES= "lastUsedWorkingSetNames"; //$NON-NLS-1$
57

58     private IDialogSettings fSettingsStore;
59     private Group fPart;
60
61     // Scope radio buttons
62
private Button fUseWorkspace;
63     private Button fUseSelection;
64     private Button fUseProject;
65     private Button fUseWorkingSet;
66
67     private int fScope;
68     private boolean fCanSearchEnclosingProjects;
69     private Text fWorkingSetText;
70     private IWorkingSet[] fWorkingSets;
71
72     // Reference to its search page container (can be null)
73
private SearchDialog fSearchDialog;
74
75     /**
76      * Returns a new scope part with workspace as initial scope.
77      * The part is not yet created.
78      * @param searchDialog The parent container
79      * @param searchEnclosingProjects If true, add the 'search enclosing project' radio button
80      */

81     public ScopePart(SearchDialog searchDialog, boolean searchEnclosingProjects) {
82         fSearchDialog= searchDialog;
83         fCanSearchEnclosingProjects= searchEnclosingProjects;
84         
85         fSettingsStore= SearchPlugin.getDefault().getDialogSettingsSection(DIALOG_SETTINGS_KEY);
86         fScope= getStoredScope(fSettingsStore, searchEnclosingProjects);
87         
88         fWorkingSets= getStoredWorkingSets();
89     }
90
91     private static int getStoredScope(IDialogSettings settingsStore, boolean canSearchEnclosingProjects) {
92         int scope;
93         try {
94             scope= settingsStore.getInt(STORE_SCOPE);
95         } catch (NumberFormatException JavaDoc ex) {
96             scope= ISearchPageContainer.WORKSPACE_SCOPE;
97         }
98         if (scope != ISearchPageContainer.WORKING_SET_SCOPE
99             && scope != ISearchPageContainer.SELECTION_SCOPE
100             && scope != ISearchPageContainer.SELECTED_PROJECTS_SCOPE
101             && scope != ISearchPageContainer.WORKSPACE_SCOPE)
102             scope= ISearchPageContainer.WORKSPACE_SCOPE;
103         
104         if (!canSearchEnclosingProjects && scope == ISearchPageContainer.SELECTED_PROJECTS_SCOPE)
105             scope= ISearchPageContainer.WORKSPACE_SCOPE;
106         
107         return scope;
108     }
109     
110     private IWorkingSet getWorkingSet(IWorkingSetManager workingSetManager, String JavaDoc storedName) {
111         if (storedName.length() == 0) {
112             IWorkbenchPage page= fSearchDialog.getWorkbenchWindow().getActivePage();
113             if (page != null) {
114                 return page.getAggregateWorkingSet();
115             }
116             return null;
117         }
118         return workingSetManager.getWorkingSet(storedName);
119     }
120     
121     
122     private IWorkingSet[] getStoredWorkingSets() {
123         String JavaDoc[] lruWorkingSetNames= fSettingsStore.getArray(STORE_LRU_WORKING_SET_NAMES);
124
125         IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager();
126         if (lruWorkingSetNames != null) {
127             Set JavaDoc existingWorkingSets= new HashSet JavaDoc(lruWorkingSetNames.length);
128             for (int i= 0; i < lruWorkingSetNames.length; i++) {
129                 IWorkingSet workingSet= getWorkingSet(workingSetManager, lruWorkingSetNames[i]);
130                 if (workingSet != null) {
131                     existingWorkingSets.add(workingSet);
132                 }
133             }
134             if (!existingWorkingSets.isEmpty()) {
135                 return (IWorkingSet[]) existingWorkingSets.toArray(new IWorkingSet[existingWorkingSets.size()]);
136             }
137         } else {
138             // Backward compatibility
139
String JavaDoc workingSetName= fSettingsStore.get(STORE_LRU_WORKING_SET_NAME);
140             if (workingSetName != null) {
141                 IWorkingSet workingSet= getWorkingSet(workingSetManager, workingSetName);
142                 if (workingSet != null) {
143                     return new IWorkingSet[] { workingSet };
144                 }
145             }
146         }
147         return null;
148     }
149
150
151     /**
152      * Returns the scope selected in this part
153      *
154      * @return the selected scope
155      */

156     public int getSelectedScope() {
157         return fScope;
158     }
159
160     /**
161      * Sets the selected scope.
162      * This method must only be called on a created part.
163      *
164      * @param scope the scope to be selected in this part
165      */

166     public void setSelectedScope(int scope) {
167         Assert.isLegal(scope >= 0 && scope <= 3);
168         Assert.isNotNull(fUseWorkspace);
169         Assert.isNotNull(fUseSelection);
170         Assert.isNotNull(fUseWorkingSet);
171         Assert.isNotNull(fUseProject);
172
173         if (scope == ISearchPageContainer.SELECTED_PROJECTS_SCOPE) {
174             if (!fCanSearchEnclosingProjects) {
175                 SearchPlugin.log(new Status(IStatus.WARNING, NewSearchUI.PLUGIN_ID, IStatus.WARNING, "Enclosing projects scope set on search page that does not support it", null)); //$NON-NLS-1$
176
scope= ISearchPageContainer.WORKSPACE_SCOPE;
177             } else if (!fUseProject.isEnabled()) {
178                 scope= ISearchPageContainer.WORKSPACE_SCOPE;
179             }
180         } else if (scope == ISearchPageContainer.SELECTION_SCOPE) {
181             if (!fUseSelection.isEnabled()) {
182                 scope= ISearchPageContainer.WORKSPACE_SCOPE;
183             }
184         }
185         fScope= scope;
186         
187         fUseWorkspace.setSelection(scope == ISearchPageContainer.WORKSPACE_SCOPE);
188         fUseSelection.setSelection(scope == ISearchPageContainer.SELECTION_SCOPE);
189         fUseProject.setSelection(scope == ISearchPageContainer.SELECTED_PROJECTS_SCOPE);
190         fUseWorkingSet.setSelection(scope == ISearchPageContainer.WORKING_SET_SCOPE);
191
192         updateSearchPageContainerActionPerformedEnablement();
193         fSettingsStore.put(STORE_SCOPE, scope);
194         
195     }
196
197     private void updateSearchPageContainerActionPerformedEnablement() {
198         fSearchDialog.notifyScopeSelectionChanged();
199     }
200
201     /**
202      * Returns the selected working set of this part.
203      *
204      * @return the selected working set or null
205      * - if the scope is not WORKING_SET_SCOPE
206      * - if there is no working set selected
207      */

208     public IWorkingSet[] getSelectedWorkingSets() {
209         if (getSelectedScope() == ISearchPageContainer.WORKING_SET_SCOPE)
210             return fWorkingSets;
211         return null;
212     }
213
214     /**
215      * Sets the selected working set for this part.
216      * This method must only be called on a created part.
217      *
218      * @param workingSets the working set to be selected
219      */

220     public void setSelectedWorkingSets(IWorkingSet[] workingSets) {
221         Assert.isNotNull(workingSets);
222         setSelectedScope(ISearchPageContainer.WORKING_SET_SCOPE);
223         fWorkingSets= null;
224         Set JavaDoc existingWorkingSets= new HashSet JavaDoc(workingSets.length);
225         for (int i= 0; i < workingSets.length; i++) {
226             String JavaDoc name= workingSets[i].getName();
227             IWorkingSet workingSet= PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(name);
228             if (workingSet != null)
229                 existingWorkingSets.add(workingSet);
230         }
231         if (!existingWorkingSets.isEmpty())
232             fWorkingSets= (IWorkingSet[]) existingWorkingSets.toArray(new IWorkingSet[existingWorkingSets.size()]);
233
234         saveState();
235
236         if (fWorkingSetText != null)
237             fWorkingSetText.setText(toString(fWorkingSets));
238     }
239
240     /**
241      * Saves the last recently used working sets,
242      * if any.
243      */

244     private void saveState() {
245         if (fWorkingSets != null && fWorkingSets.length > 0) {
246             String JavaDoc[] existingWorkingSetNames= new String JavaDoc[fWorkingSets.length];
247             for (int i= 0; i < fWorkingSets.length; i++) {
248                 IWorkingSet curr= fWorkingSets[i];
249                 // use empty name for aggregateWS
250
existingWorkingSetNames[i]= curr.isAggregateWorkingSet() ? "" : curr.getName(); //$NON-NLS-1$
251
}
252             fSettingsStore.put(STORE_LRU_WORKING_SET_NAMES, existingWorkingSetNames);
253         }
254     }
255
256     /**
257      * Creates this scope part.
258      *
259      * @param parent a widget which will be the parent of the new instance (cannot be null)
260      * @return Returns the created part control
261      */

262     public Composite createPart(Composite parent) {
263         fPart= new Group(parent, SWT.NONE);
264         fPart.setText(SearchMessages.ScopePart_group_text);
265
266         GridLayout layout= new GridLayout();
267         layout.numColumns= 4;
268         fPart.setLayout(layout);
269         fPart.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
270
271         fUseWorkspace= new Button(fPart, SWT.RADIO);
272         fUseWorkspace.setData(new Integer JavaDoc(ISearchPageContainer.WORKSPACE_SCOPE));
273         fUseWorkspace.setText(SearchMessages.ScopePart_workspaceScope_text);
274
275         fUseSelection= new Button(fPart, SWT.RADIO);
276         fUseSelection.setData(new Integer JavaDoc(ISearchPageContainer.SELECTION_SCOPE));
277         fUseSelection.setText(SearchMessages.ScopePart_selectedResourcesScope_text);
278         
279         boolean canSearchInSelection= canSearchInSelection();
280         fUseSelection.setEnabled(canSearchInSelection);
281         
282         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
283         gd.horizontalIndent= 8;
284         fUseSelection.setLayoutData(gd);
285
286         fUseProject= new Button(fPart, SWT.RADIO);
287         fUseProject.setData(new Integer JavaDoc(ISearchPageContainer.SELECTED_PROJECTS_SCOPE));
288         fUseProject.setText(SearchMessages.ScopePart_enclosingProjectsScope_text);
289         fUseProject.setEnabled(fSearchDialog.getEnclosingProjectNames().length > 0);
290
291         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
292         gd.horizontalSpan= 2;
293         gd.horizontalIndent= 8;
294         fUseProject.setLayoutData(gd);
295         if (!fCanSearchEnclosingProjects)
296             fUseProject.setVisible(false);
297
298         fUseWorkingSet= new Button(fPart, SWT.RADIO);
299         fUseWorkingSet.setData(new Integer JavaDoc(ISearchPageContainer.WORKING_SET_SCOPE));
300         fUseWorkingSet.setText(SearchMessages.ScopePart_workingSetScope_text);
301         fWorkingSetText= new Text(fPart, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY);
302         fWorkingSetText.getAccessible().addAccessibleListener(new AccessibleAdapter() {
303             public void getName(AccessibleEvent e) {
304                 e.result= SearchMessages.ScopePart_workingSetText_accessible_label;
305             }
306         });
307         
308         Button chooseWorkingSet= new Button(fPart, SWT.PUSH);
309         chooseWorkingSet.setLayoutData(new GridData());
310         chooseWorkingSet.setText(SearchMessages.ScopePart_workingSetChooseButton_text);
311         SWTUtil.setButtonDimensionHint(chooseWorkingSet);
312         chooseWorkingSet.addSelectionListener(new SelectionAdapter() {
313             public void widgetSelected(SelectionEvent e) {
314                 if (handleChooseWorkingSet()) {
315                     setSelectedScope(ISearchPageContainer.WORKING_SET_SCOPE);
316                 }
317             }
318         });
319         gd= new GridData(GridData.FILL_HORIZONTAL);
320         gd.horizontalIndent= 8;
321         gd.horizontalSpan= 2;
322         gd.widthHint= new PixelConverter(fWorkingSetText).convertWidthInCharsToPixels(30);
323         fWorkingSetText.setLayoutData(gd);
324
325         // Add scope change listeners
326
SelectionAdapter scopeChangedLister= new SelectionAdapter() {
327             public void widgetSelected(SelectionEvent e) {
328                 handleScopeChanged(e);
329             }
330         };
331         fUseWorkspace.addSelectionListener(scopeChangedLister);
332         fUseSelection.addSelectionListener(scopeChangedLister);
333         fUseProject.addSelectionListener(scopeChangedLister);
334         fUseWorkingSet.addSelectionListener(scopeChangedLister);
335
336         // Set initial scope
337
setSelectedScope(fScope);
338
339         // Set initial working set
340
if (fWorkingSets != null)
341             fWorkingSetText.setText(toString(fWorkingSets));
342
343         return fPart;
344     }
345
346
347     private boolean canSearchInSelection() {
348         ISelection selection= fSearchDialog.getSelection();
349         return selection instanceof IStructuredSelection && !selection.isEmpty();
350     }
351
352     private void handleScopeChanged(SelectionEvent e) {
353         Object JavaDoc source= e.getSource();
354         if (source instanceof Button) {
355             Button button= (Button) source;
356             if (button.getSelection())
357                 setSelectedScope(((Integer JavaDoc) button.getData()).intValue());
358         }
359     }
360
361     private boolean handleChooseWorkingSet() {
362         IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager();
363         IWorkingSetSelectionDialog dialog= workingSetManager.createWorkingSetSelectionDialog(fUseSelection.getShell(), true);
364
365         if (fWorkingSets != null)
366             dialog.setSelection(fWorkingSets);
367         if (dialog.open() == Window.OK) {
368             Object JavaDoc[] result= dialog.getSelection();
369             if (result.length > 0) {
370                 setSelectedWorkingSets((IWorkingSet[]) result);
371                 return true;
372             }
373             fWorkingSetText.setText(""); //$NON-NLS-1$
374
fWorkingSets= null;
375             if (fScope == ISearchPageContainer.WORKING_SET_SCOPE)
376                 setSelectedScope(ISearchPageContainer.WORKSPACE_SCOPE);
377             return false;
378         }
379         if (fWorkingSets != null) {
380             // test if selected working set has been removed
381
int i= 0;
382             while (i < fWorkingSets.length) {
383                 IWorkingSet workingSet= fWorkingSets[i];
384                 if (!workingSet.isAggregateWorkingSet() && workingSetManager.getWorkingSet(workingSet.getName()) == null)
385                     break;
386                 i++;
387             }
388             if (i < fWorkingSets.length) {
389                 fWorkingSetText.setText(""); //$NON-NLS-1$
390
fWorkingSets= null;
391                 updateSearchPageContainerActionPerformedEnablement();
392             }
393         }
394         return false;
395     }
396
397     void setVisible(boolean state) {
398         fPart.setVisible(state);
399     }
400
401     public static String JavaDoc toString(IWorkingSet[] workingSets) {
402         String JavaDoc result= ""; //$NON-NLS-1$
403
if (workingSets != null && workingSets.length > 0) {
404             Arrays.sort(workingSets, new WorkingSetComparator());
405             boolean firstFound= false;
406             for (int i= 0; i < workingSets.length; i++) {
407                 String JavaDoc workingSetName= workingSets[i].getLabel();
408                 if (firstFound)
409                     result= Messages.format(SearchMessages.ScopePart_workingSetConcatenation, new String JavaDoc[] { result, workingSetName });
410                 else {
411                     result= workingSetName;
412                     firstFound= true;
413                 }
414             }
415         }
416         return result;
417     }
418 }
419
Popular Tags