001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.search;
011:
012: import java.util.ArrayList;
013: import java.util.HashSet;
014: import java.util.Iterator;
015:
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.runtime.IAdaptable;
018: import org.eclipse.jface.dialogs.Dialog;
019: import org.eclipse.jface.dialogs.DialogPage;
020: import org.eclipse.jface.text.TextSelection;
021: import org.eclipse.jface.viewers.ISelection;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.pde.internal.core.search.ExtensionPluginSearchScope;
024: import org.eclipse.pde.internal.core.search.PluginSearchInput;
025: import org.eclipse.pde.internal.core.search.PluginSearchScope;
026: import org.eclipse.pde.internal.ui.IHelpContextIds;
027: import org.eclipse.pde.internal.ui.PDEUIMessages;
028: import org.eclipse.search.ui.ISearchPage;
029: import org.eclipse.search.ui.ISearchPageContainer;
030: import org.eclipse.search.ui.NewSearchUI;
031: import org.eclipse.swt.SWT;
032: import org.eclipse.swt.events.ModifyEvent;
033: import org.eclipse.swt.events.ModifyListener;
034: import org.eclipse.swt.events.SelectionAdapter;
035: import org.eclipse.swt.events.SelectionEvent;
036: import org.eclipse.swt.layout.GridData;
037: import org.eclipse.swt.layout.GridLayout;
038: import org.eclipse.swt.widgets.Button;
039: import org.eclipse.swt.widgets.Combo;
040: import org.eclipse.swt.widgets.Composite;
041: import org.eclipse.swt.widgets.Group;
042: import org.eclipse.swt.widgets.Label;
043: import org.eclipse.ui.IWorkingSet;
044: import org.eclipse.ui.PlatformUI;
045:
046: public class PluginSearchPage extends DialogPage implements ISearchPage {
047:
048: class QueryData {
049: public String text;
050: public boolean isCaseSensitive;
051: public int searchElement;
052: public int limit;
053: public int externalScope;
054: public int workspaceScope;
055: public IWorkingSet[] workingSets;
056:
057: public boolean equals(Object obj) {
058: if (obj instanceof QueryData) {
059: if (((QueryData) obj).text.equals(text))
060: return true;
061: }
062: return false;
063: }
064:
065: }
066:
067: private static ArrayList previousQueries = new ArrayList();
068:
069: private Button caseSensitive;
070: private ISearchPageContainer container;
071: private Button[] externalScopeButtons = new Button[3];
072: private boolean firstTime = true;
073: private Button[] limitToButtons = new Button[3];
074: private Combo patternCombo;
075: private Button[] searchForButtons = new Button[3];
076:
077: public void createControl(Composite parent) {
078: Composite result = new Composite(parent, SWT.NONE);
079: GridLayout layout = new GridLayout(1, true);
080: layout.marginHeight = 0;
081: layout.marginWidth = 0;
082: result.setLayout(layout);
083: result.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
084: | GridData.GRAB_HORIZONTAL));
085:
086: createPatternSection(result);
087: createSettingsSection(result);
088:
089: hookListeners();
090:
091: setControl(result);
092: Dialog.applyDialogFont(result);
093: PlatformUI.getWorkbench().getHelpSystem().setHelp(result,
094: IHelpContextIds.SEARCH_PAGE);
095: }
096:
097: private void createGroup(Composite parent, Button[] buttons,
098: String groupLabel, String[] buttonLabels, int defaultEnabled) {
099: Group group = new Group(parent, SWT.NONE);
100: group.setLayout(new GridLayout(1, true));
101: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
102:
103: group.setText(groupLabel);
104: for (int i = 0; i < buttonLabels.length; i++) {
105: buttons[i] = new Button(group, SWT.RADIO);
106: buttons[i].setText(buttonLabels[i]);
107: buttons[i].setSelection(i == defaultEnabled);
108: }
109: }
110:
111: private void createPatternSection(Composite parent) {
112: Composite result = new Composite(parent, SWT.NONE);
113: result.setLayout(new GridLayout(2, false));
114: result.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
115:
116: Label label = new Label(result, SWT.NONE);
117: GridData data = new GridData();
118: data.horizontalSpan = 2;
119: label.setLayoutData(data);
120: label.setText(PDEUIMessages.SearchPage_searchString);
121:
122: patternCombo = new Combo(result, SWT.SINGLE | SWT.BORDER);
123: patternCombo.setLayoutData(new GridData(
124: GridData.FILL_HORIZONTAL));
125:
126: caseSensitive = new Button(result, SWT.CHECK);
127: caseSensitive.setText(PDEUIMessages.SearchPage_caseSensitive);
128: }
129:
130: private void createSettingsSection(Composite parent) {
131: Composite result = new Composite(parent, SWT.NONE);
132: result.setLayout(new GridLayout(3, true));
133: result.setLayoutData(new GridData(GridData.FILL_BOTH));
134:
135: createGroup(result, searchForButtons,
136: PDEUIMessages.SearchPage_searchFor, new String[] {
137: PDEUIMessages.SearchPage_plugin,
138: PDEUIMessages.SearchPage_fragment,
139: PDEUIMessages.SearchPage_extPt }, 2);
140: createGroup(result, limitToButtons,
141: PDEUIMessages.SearchPage_limitTo, new String[] {
142: PDEUIMessages.SearchPage_declarations,
143: PDEUIMessages.SearchPage_references,
144: PDEUIMessages.SearchPage_allOccurrences }, 1);
145: createGroup(result, externalScopeButtons,
146: PDEUIMessages.SearchPage_externalScope, new String[] {
147: PDEUIMessages.SearchPage_all,
148: PDEUIMessages.SearchPage_enabledOnly,
149: PDEUIMessages.SearchPage_none }, 1);
150: }
151:
152: private int getExternalScope() {
153: if (externalScopeButtons[0].getSelection())
154: return PluginSearchScope.EXTERNAL_SCOPE_ALL;
155: if (externalScopeButtons[1].getSelection())
156: return PluginSearchScope.EXTERNAL_SCOPE_ENABLED;
157: return PluginSearchScope.EXTERNAL_SCOPE_NONE;
158: }
159:
160: private PluginSearchInput getInput() {
161: PluginSearchInput input = new PluginSearchInput();
162: int searchFor = getSearchFor();
163: input.setSearchElement(searchFor);
164: input.setSearchLimit(getLimitTo());
165:
166: PluginSearchScope scope = null;
167: String searchString = patternCombo.getText().trim();
168: if (searchFor == PluginSearchInput.ELEMENT_EXTENSION_POINT) {
169: if (searchString.indexOf('.') == -1) {
170: searchString = "*." + searchString; //$NON-NLS-1$
171: }
172: // if not using wildcards, use optimized search scope
173: if (searchString.indexOf('*') == -1) {
174: scope = new ExtensionPluginSearchScope(
175: getWorkspaceScope(), getExternalScope(),
176: getSelectedResources(), input);
177: }
178: }
179: if (scope == null)
180: scope = new PluginSearchScope(getWorkspaceScope(),
181: getExternalScope(), getSelectedResources());
182: input.setSearchScope(scope);
183: input.setSearchString(searchString);
184: input.setCaseSensitive(caseSensitive.getSelection());
185: return input;
186: }
187:
188: private int getLimitTo() {
189: if (limitToButtons[0].getSelection())
190: return PluginSearchInput.LIMIT_DECLARATIONS;
191: if (limitToButtons[1].getSelection())
192: return PluginSearchInput.LIMIT_REFERENCES;
193: return PluginSearchInput.LIMIT_ALL;
194: }
195:
196: private int getSearchFor() {
197: if (searchForButtons[0].getSelection())
198: return PluginSearchInput.ELEMENT_PLUGIN;
199: if (searchForButtons[1].getSelection())
200: return PluginSearchInput.ELEMENT_FRAGMENT;
201: return PluginSearchInput.ELEMENT_EXTENSION_POINT;
202: }
203:
204: private HashSet getSelectedResources() {
205: HashSet result = new HashSet();
206: int scope = container.getSelectedScope();
207: if (scope == ISearchPageContainer.WORKSPACE_SCOPE)
208: return null;
209: if (scope == ISearchPageContainer.SELECTION_SCOPE
210: || scope == ISearchPageContainer.SELECTED_PROJECTS_SCOPE) {
211: if (container.getSelection() instanceof IStructuredSelection) {
212: IStructuredSelection selection = (IStructuredSelection) container
213: .getSelection();
214: Iterator iter = selection.iterator();
215: while (iter.hasNext()) {
216: Object item = iter.next();
217: if (item instanceof IResource)
218: result.add(((IResource) item).getProject());
219: }
220: }
221: } else if (scope == ISearchPageContainer.WORKING_SET_SCOPE) {
222: IWorkingSet[] workingSets = container
223: .getSelectedWorkingSets();
224: if (workingSets != null) {
225: for (int i = 0; i < workingSets.length; i++) {
226: IAdaptable[] elements = workingSets[i]
227: .getElements();
228: for (int j = 0; j < elements.length; j++) {
229: IResource resource = (IResource) elements[j]
230: .getAdapter(IResource.class);
231: if (resource != null)
232: result.add(resource.getProject());
233: }
234: }
235: }
236: }
237: return result;
238: }
239:
240: private int getWorkspaceScope() {
241: switch (container.getSelectedScope()) {
242: case ISearchPageContainer.SELECTION_SCOPE:
243: return PluginSearchScope.SCOPE_SELECTION;
244: case ISearchPageContainer.WORKING_SET_SCOPE:
245: return PluginSearchScope.SCOPE_WORKING_SETS;
246: default:
247: return PluginSearchScope.SCOPE_WORKSPACE;
248: }
249: }
250:
251: private void hookListeners() {
252: searchForButtons[1]
253: .addSelectionListener(new SelectionAdapter() {
254: public void widgetSelected(SelectionEvent e) {
255: boolean selected = searchForButtons[1]
256: .getSelection();
257: if (selected) {
258: limitToButtons[0].setSelection(true);
259: limitToButtons[1].setSelection(false);
260: limitToButtons[2].setSelection(false);
261: }
262: limitToButtons[1].setEnabled(!selected);
263: limitToButtons[2].setEnabled(!selected);
264: }
265: });
266:
267: patternCombo.addSelectionListener(new SelectionAdapter() {
268: public void widgetSelected(SelectionEvent e) {
269: int index = previousQueries.size()
270: - patternCombo.getSelectionIndex() - 1;
271: if (previousQueries.size() > index) {
272: QueryData data = (QueryData) previousQueries
273: .get(index);
274: resetPage(data);
275: }
276: container.setPerformActionEnabled(patternCombo
277: .getText().length() > 0);
278: }
279: });
280:
281: patternCombo.addModifyListener(new ModifyListener() {
282: public void modifyText(ModifyEvent e) {
283: container.setPerformActionEnabled(patternCombo
284: .getText().trim().length() > 0);
285: }
286: });
287: }
288:
289: public boolean performAction() {
290: saveQueryData();
291: NewSearchUI.activateSearchResultView();
292: NewSearchUI.runQueryInBackground(new PluginSearchQuery(
293: getInput()));
294: return true;
295: }
296:
297: private void resetPage(QueryData data) {
298: caseSensitive.setSelection(data.isCaseSensitive);
299:
300: searchForButtons[0]
301: .setSelection(data.searchElement == PluginSearchInput.ELEMENT_PLUGIN);
302: searchForButtons[1]
303: .setSelection(data.searchElement == PluginSearchInput.ELEMENT_FRAGMENT);
304: searchForButtons[2]
305: .setSelection(data.searchElement == PluginSearchInput.ELEMENT_EXTENSION_POINT);
306:
307: limitToButtons[0]
308: .setSelection(data.limit == PluginSearchInput.LIMIT_DECLARATIONS);
309: limitToButtons[1]
310: .setSelection(data.limit == PluginSearchInput.LIMIT_REFERENCES);
311: limitToButtons[1].setEnabled(!searchForButtons[1]
312: .getSelection());
313: limitToButtons[2]
314: .setSelection(data.limit == PluginSearchInput.LIMIT_ALL);
315: limitToButtons[2].setEnabled(!searchForButtons[1]
316: .getSelection());
317:
318: externalScopeButtons[0]
319: .setSelection(data.externalScope == PluginSearchScope.EXTERNAL_SCOPE_ALL);
320: externalScopeButtons[1]
321: .setSelection(data.externalScope == PluginSearchScope.EXTERNAL_SCOPE_ENABLED);
322: externalScopeButtons[2]
323: .setSelection(data.externalScope == PluginSearchScope.EXTERNAL_SCOPE_NONE);
324:
325: container.setSelectedScope(data.workspaceScope);
326: if (data.workingSets != null)
327: container.setSelectedWorkingSets(data.workingSets);
328: }
329:
330: private void saveQueryData() {
331: QueryData data = new QueryData();
332: data.text = patternCombo.getText();
333: data.isCaseSensitive = caseSensitive.getSelection();
334: data.searchElement = getSearchFor();
335: data.limit = getLimitTo();
336: data.externalScope = getExternalScope();
337: data.workspaceScope = container.getSelectedScope();
338: data.workingSets = container.getSelectedWorkingSets();
339:
340: if (previousQueries.contains(data))
341: previousQueries.remove(data);
342:
343: previousQueries.add(data);
344: if (previousQueries.size() > 10)
345: previousQueries.remove(0);
346: }
347:
348: public void setContainer(ISearchPageContainer container) {
349: this .container = container;
350: }
351:
352: public void setVisible(boolean visible) {
353: if (visible && patternCombo != null) {
354: if (firstTime) {
355: firstTime = false;
356: String[] patterns = new String[previousQueries.size()];
357: for (int i = previousQueries.size() - 1, j = 0; i >= 0; i--, j++) {
358: patterns[j] = ((QueryData) previousQueries.get(i)).text;
359: }
360: patternCombo.setItems(patterns);
361: initSelections();
362: container.setPerformActionEnabled(patternCombo
363: .getText().length() > 0);
364: }
365: patternCombo.setFocus();
366: }
367: super .setVisible(visible);
368: }
369:
370: private void initSelections() {
371: ISelection selection = container.getSelection();
372: if (selection instanceof TextSelection) {
373: patternCombo.setText(((TextSelection) selection).getText());
374: }
375: }
376:
377: }
|