KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > search > PluginSearchPage


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.pde.internal.ui.search;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.jface.dialogs.Dialog;
20 import org.eclipse.jface.dialogs.DialogPage;
21 import org.eclipse.jface.text.TextSelection;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.pde.internal.core.search.PluginSearchInput;
25 import org.eclipse.pde.internal.core.search.PluginSearchScope;
26 import org.eclipse.pde.internal.ui.IHelpContextIds;
27 import org.eclipse.pde.internal.ui.PDEUIMessages;
28 import org.eclipse.search.ui.ISearchPage;
29 import org.eclipse.search.ui.ISearchPageContainer;
30 import org.eclipse.search.ui.NewSearchUI;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.ModifyEvent;
33 import org.eclipse.swt.events.ModifyListener;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Combo;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Group;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.ui.IWorkingSet;
44 import org.eclipse.ui.PlatformUI;
45
46
47
48 public class PluginSearchPage extends DialogPage implements ISearchPage {
49     
50     class QueryData {
51         public String JavaDoc text;
52         public boolean isCaseSensitive;
53         public int searchElement;
54         public int limit;
55         public int externalScope;
56         public int workspaceScope;
57         public IWorkingSet[] workingSets;
58         
59         public boolean equals(Object JavaDoc obj) {
60             if (obj instanceof QueryData) {
61                 if (((QueryData)obj).text.equals(text))
62                     return true;
63             }
64             return false;
65         }
66         
67     }
68     
69     private static ArrayList JavaDoc previousQueries = new ArrayList JavaDoc();
70     
71     private Button caseSensitive;
72     private ISearchPageContainer container;
73     private Button[] externalScopeButtons = new Button[3];
74     private boolean firstTime = true;
75     private Button[] limitToButtons = new Button[3];
76     private Combo patternCombo;
77     private Button[] searchForButtons = new Button[3];
78
79     public void createControl(Composite parent) {
80         Composite result = new Composite(parent,SWT.NONE);
81         GridLayout layout = new GridLayout(1, true);
82         layout.marginHeight = 0;
83         layout.marginWidth = 0;
84         result.setLayout(layout);
85         result.setLayoutData(new GridData(GridData.FILL_HORIZONTAL|GridData.GRAB_HORIZONTAL));
86         
87         createPatternSection(result);
88         createSettingsSection(result);
89         
90         hookListeners();
91         
92         setControl(result);
93         Dialog.applyDialogFont(result);
94         PlatformUI.getWorkbench().getHelpSystem().setHelp(result, IHelpContextIds.SEARCH_PAGE);
95     }
96         
97     private void createGroup(
98         Composite parent,
99         Button[] buttons,
100         String JavaDoc groupLabel,
101         String JavaDoc[] buttonLabels,
102         int defaultEnabled) {
103         Group group = new Group(parent, SWT.NONE);
104         group.setLayout(new GridLayout(1, true));
105         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
106
107         group.setText(groupLabel);
108         for (int i = 0; i < buttonLabels.length; i++) {
109             buttons[i] = new Button(group, SWT.RADIO);
110             buttons[i].setText(buttonLabels[i]);
111             buttons[i].setSelection(i == defaultEnabled);
112         }
113     }
114         
115     private void createPatternSection(Composite parent) {
116         Composite result = new Composite(parent, SWT.NONE);
117         result.setLayout(new GridLayout(2, false));
118         result.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
119
120         Label label = new Label(result, SWT.NONE);
121         GridData data = new GridData();
122         data.horizontalSpan = 2;
123         label.setLayoutData(data);
124         label.setText(PDEUIMessages.SearchPage_searchString);
125
126         patternCombo = new Combo(result, SWT.SINGLE | SWT.BORDER);
127         patternCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
128
129         caseSensitive = new Button(result, SWT.CHECK);
130         caseSensitive.setText(PDEUIMessages.SearchPage_caseSensitive);
131     }
132     
133     private void createSettingsSection(Composite parent) {
134         Composite result = new Composite(parent, SWT.NONE);
135         result.setLayout(new GridLayout(3, true));
136         result.setLayoutData(new GridData(GridData.FILL_BOTH));
137
138         createGroup(
139             result,
140             searchForButtons,
141             PDEUIMessages.SearchPage_searchFor,
142             new String JavaDoc[] {
143                 PDEUIMessages.SearchPage_plugin,
144                 PDEUIMessages.SearchPage_fragment,
145                 PDEUIMessages.SearchPage_extPt},
146             2);
147         createGroup(
148             result,
149             limitToButtons,
150             PDEUIMessages.SearchPage_limitTo,
151             new String JavaDoc[] {
152                 PDEUIMessages.SearchPage_declarations,
153                 PDEUIMessages.SearchPage_references,
154                 PDEUIMessages.SearchPage_allOccurrences},
155             1);
156         createGroup(
157             result,
158             externalScopeButtons,
159             PDEUIMessages.SearchPage_externalScope,
160             new String JavaDoc[] {
161                 PDEUIMessages.SearchPage_all,
162                 PDEUIMessages.SearchPage_enabledOnly,
163                 PDEUIMessages.SearchPage_none},
164             1);
165     }
166     
167     private int getExternalScope() {
168         if (externalScopeButtons[0].getSelection())
169             return PluginSearchScope.EXTERNAL_SCOPE_ALL;
170         if (externalScopeButtons[1].getSelection())
171             return PluginSearchScope.EXTERNAL_SCOPE_ENABLED;
172         return PluginSearchScope.EXTERNAL_SCOPE_NONE;
173     }
174     
175     private PluginSearchInput getInput() {
176         PluginSearchScope scope =
177             new PluginSearchScope(
178                 getWorkspaceScope(),
179                 getExternalScope(),
180                 getSelectedResources());
181
182         PluginSearchInput input = new PluginSearchInput();
183         int searchFor = getSearchFor();
184         input.setSearchElement(searchFor);
185         input.setSearchLimit(getLimitTo());
186         input.setSearchScope(scope);
187         String JavaDoc searchString = patternCombo.getText().trim();
188         if (searchFor == PluginSearchInput.ELEMENT_EXTENSION_POINT
189             && searchString.indexOf('.') == -1) {
190             searchString = "*." + searchString; //$NON-NLS-1$
191
}
192         input.setSearchString(searchString);
193         input.setCaseSensitive(caseSensitive.getSelection());
194         return input;
195     }
196     
197     private int getLimitTo() {
198         if (limitToButtons[0].getSelection())
199             return PluginSearchInput.LIMIT_DECLARATIONS;
200         if (limitToButtons[1].getSelection())
201             return PluginSearchInput.LIMIT_REFERENCES;
202         return PluginSearchInput.LIMIT_ALL;
203     }
204     
205     private int getSearchFor() {
206         if (searchForButtons[0].getSelection())
207             return PluginSearchInput.ELEMENT_PLUGIN;
208         if (searchForButtons[1].getSelection())
209             return PluginSearchInput.ELEMENT_FRAGMENT;
210         return PluginSearchInput.ELEMENT_EXTENSION_POINT;
211     }
212     
213     private HashSet JavaDoc getSelectedResources() {
214         HashSet JavaDoc result = new HashSet JavaDoc();
215         int scope = container.getSelectedScope();
216         if (scope == ISearchPageContainer.WORKSPACE_SCOPE)
217             return null;
218         if (scope == ISearchPageContainer.SELECTION_SCOPE || scope == ISearchPageContainer.SELECTED_PROJECTS_SCOPE) {
219             if (container.getSelection() instanceof IStructuredSelection) {
220                 IStructuredSelection selection =
221                     (IStructuredSelection) container.getSelection();
222                 Iterator JavaDoc iter = selection.iterator();
223                 while (iter.hasNext()) {
224                     Object JavaDoc item = iter.next();
225                     if (item instanceof IResource)
226                         result.add(((IResource)item).getProject());
227                 }
228             }
229         } else if (scope == ISearchPageContainer.WORKING_SET_SCOPE) {
230             IWorkingSet[] workingSets = container.getSelectedWorkingSets();
231             if (workingSets != null) {
232                 for (int i = 0; i < workingSets.length; i++) {
233                     IAdaptable[] elements = workingSets[i].getElements();
234                     for (int j = 0; j < elements.length; j++) {
235                         IResource resource = (IResource)elements[j].getAdapter(IResource.class);
236                         if (resource != null)
237                             result.add(resource.getProject());
238                     }
239                 }
240             }
241         }
242         return result;
243     }
244     
245     private int getWorkspaceScope() {
246         switch(container.getSelectedScope()) {
247             case ISearchPageContainer.SELECTION_SCOPE:
248                 return PluginSearchScope.SCOPE_SELECTION;
249             case ISearchPageContainer.WORKING_SET_SCOPE:
250                 return PluginSearchScope.SCOPE_WORKING_SETS;
251             default:
252                 return PluginSearchScope.SCOPE_WORKSPACE;
253         }
254     }
255         
256     private void hookListeners() {
257         searchForButtons[1].addSelectionListener(new SelectionAdapter() {
258             public void widgetSelected(SelectionEvent e) {
259                 boolean selected = searchForButtons[1].getSelection();
260                 if (selected) {
261                     limitToButtons[0].setSelection(true);
262                     limitToButtons[1].setSelection(false);
263                     limitToButtons[2].setSelection(false);
264                 }
265                 limitToButtons[1].setEnabled(!selected);
266                 limitToButtons[2].setEnabled(!selected);
267             }
268         });
269         
270         patternCombo.addSelectionListener(new SelectionAdapter() {
271             public void widgetSelected(SelectionEvent e) {
272                 int index = previousQueries.size() - patternCombo.getSelectionIndex() - 1;
273                 if(previousQueries.size() > index) {
274                     QueryData data = (QueryData)previousQueries.get(index);
275                     resetPage(data);
276                 }
277                 container.setPerformActionEnabled(patternCombo.getText().length() > 0);
278             }
279         });
280         
281         patternCombo.addModifyListener(new ModifyListener() {
282             public void modifyText(ModifyEvent e) {
283                 container.setPerformActionEnabled(patternCombo.getText().trim().length() > 0);
284             }
285         });
286     }
287     
288     public boolean performAction() {
289         saveQueryData();
290         NewSearchUI.activateSearchResultView();
291         NewSearchUI.runQueryInBackground(new PluginSearchQuery(getInput()));
292         return true;
293     }
294     
295     private void resetPage (QueryData data) {
296         caseSensitive.setSelection(data.isCaseSensitive);
297         
298         searchForButtons[0].setSelection(data.searchElement == PluginSearchInput.ELEMENT_PLUGIN);
299         searchForButtons[1].setSelection(data.searchElement == PluginSearchInput.ELEMENT_FRAGMENT);
300         searchForButtons[2].setSelection(data.searchElement == PluginSearchInput.ELEMENT_EXTENSION_POINT);
301
302         limitToButtons[0].setSelection(data.limit == PluginSearchInput.LIMIT_DECLARATIONS);
303         limitToButtons[1].setSelection(data.limit == PluginSearchInput.LIMIT_REFERENCES);
304         limitToButtons[1].setEnabled(!searchForButtons[1].getSelection());
305         limitToButtons[2].setSelection(data.limit == PluginSearchInput.LIMIT_ALL);
306         limitToButtons[2].setEnabled(!searchForButtons[1].getSelection());
307         
308         externalScopeButtons[0].setSelection(data.externalScope == PluginSearchScope.EXTERNAL_SCOPE_ALL);
309         externalScopeButtons[1].setSelection(data.externalScope == PluginSearchScope.EXTERNAL_SCOPE_ENABLED);
310         externalScopeButtons[2].setSelection(data.externalScope == PluginSearchScope.EXTERNAL_SCOPE_NONE);
311         
312         container.setSelectedScope(data.workspaceScope);
313         if (data.workingSets != null)
314             container.setSelectedWorkingSets(data.workingSets);
315     }
316         
317     private void saveQueryData() {
318         QueryData data = new QueryData();
319         data.text = patternCombo.getText();
320         data.isCaseSensitive = caseSensitive.getSelection();
321         data.searchElement = getSearchFor();
322         data.limit = getLimitTo();
323         data.externalScope = getExternalScope();
324         data.workspaceScope = container.getSelectedScope();
325         data.workingSets = container.getSelectedWorkingSets();
326         
327         if (previousQueries.contains(data))
328             previousQueries.remove(data);
329             
330         previousQueries.add(data);
331         if (previousQueries.size() > 10)
332             previousQueries.remove(0);
333     }
334     
335     public void setContainer(ISearchPageContainer container) {
336         this.container = container;
337     }
338     
339     public void setVisible(boolean visible) {
340         if (visible && patternCombo != null) {
341             if (firstTime) {
342                 firstTime = false;
343                 String JavaDoc[] patterns = new String JavaDoc[previousQueries.size()];
344                 for (int i = previousQueries.size() - 1, j = 0;
345                     i >= 0;
346                     i--, j++) {
347                     patterns[j] = ((QueryData) previousQueries.get(i)).text;
348                 }
349                 patternCombo.setItems(patterns);
350                 initSelections();
351                 container.setPerformActionEnabled(
352                     patternCombo.getText().length() > 0);
353             }
354             patternCombo.setFocus();
355         }
356         super.setVisible(visible);
357     }
358
359     private void initSelections() {
360         ISelection selection = container.getSelection();
361         if(selection instanceof TextSelection) {
362             patternCombo.setText(((TextSelection) selection).getText());
363         }
364     }
365
366 }
367
Popular Tags