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.regex.Pattern;
014:
015: import org.eclipse.core.runtime.IProgressMonitor;
016: import org.eclipse.pde.core.plugin.IFragment;
017: import org.eclipse.pde.core.plugin.IPlugin;
018: import org.eclipse.pde.core.plugin.IPluginBase;
019: import org.eclipse.pde.core.plugin.IPluginExtension;
020: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
021: import org.eclipse.pde.core.plugin.IPluginImport;
022: import org.eclipse.pde.core.plugin.IPluginModelBase;
023: import org.eclipse.pde.internal.core.search.PluginSearchInput;
024: import org.eclipse.pde.internal.core.util.PatternConstructor;
025:
026: public class PluginSearchOperation {
027: protected PluginSearchInput fInput;
028: private ISearchResultCollector fCollector;
029: private Pattern fPattern;
030:
031: public PluginSearchOperation(PluginSearchInput input,
032: ISearchResultCollector collector) {
033: this .fInput = input;
034: this .fCollector = collector;
035: this .fPattern = PatternConstructor.createPattern(input
036: .getSearchString(), input.isCaseSensitive());
037: }
038:
039: public void execute(IProgressMonitor monitor) {
040: IPluginModelBase[] entries = fInput.getSearchScope()
041: .getMatchingModels();
042: monitor.beginTask("", entries.length); //$NON-NLS-1$
043:
044: try {
045: for (int i = 0; i < entries.length; i++) {
046: IPluginModelBase candidate = entries[i];
047: visit(candidate);
048: monitor.worked(1);
049: }
050: } finally {
051: monitor.done();
052: }
053: }
054:
055: private void visit(IPluginModelBase model) {
056: ArrayList matches = findMatch(model);
057: for (int i = 0; i < matches.size(); i++) {
058: fCollector.accept(matches.get(i));
059: }
060: }
061:
062: private ArrayList findMatch(IPluginModelBase model) {
063: ArrayList result = new ArrayList();
064: int searchLimit = fInput.getSearchLimit();
065: switch (fInput.getSearchElement()) {
066: case PluginSearchInput.ELEMENT_PLUGIN:
067: if (searchLimit != PluginSearchInput.LIMIT_REFERENCES)
068: findPluginDeclaration(model, result);
069: if (searchLimit != PluginSearchInput.LIMIT_DECLARATIONS)
070: findPluginReferences(model, result);
071: break;
072: case PluginSearchInput.ELEMENT_FRAGMENT:
073: findFragmentDeclaration(model, result);
074: break;
075: case PluginSearchInput.ELEMENT_EXTENSION_POINT:
076: if (searchLimit != PluginSearchInput.LIMIT_REFERENCES)
077: findExtensionPointDeclarations(model, result);
078: if (searchLimit != PluginSearchInput.LIMIT_DECLARATIONS)
079: findExtensionPointReferences(model, result);
080: break;
081: }
082: return result;
083: }
084:
085: private void findFragmentDeclaration(IPluginModelBase model,
086: ArrayList result) {
087: IPluginBase pluginBase = model.getPluginBase();
088: if (pluginBase instanceof IFragment
089: && fPattern.matcher(pluginBase.getId()).matches()) {
090: result.add(pluginBase);
091: }
092: }
093:
094: private void findPluginDeclaration(IPluginModelBase model,
095: ArrayList result) {
096: IPluginBase pluginBase = model.getPluginBase();
097: if (pluginBase instanceof IPlugin
098: && fPattern.matcher(pluginBase.getId()).matches())
099: result.add(pluginBase);
100: }
101:
102: private void findPluginReferences(IPluginModelBase model,
103: ArrayList result) {
104: IPluginBase pluginBase = model.getPluginBase();
105: if (pluginBase instanceof IFragment) {
106: if (fPattern
107: .matcher(((IFragment) pluginBase).getPluginId())
108: .matches())
109: result.add(pluginBase);
110: }
111: IPluginImport[] imports = pluginBase.getImports();
112: for (int i = 0; i < imports.length; i++) {
113: if (fPattern.matcher(imports[i].getId()).matches())
114: result.add(imports[i]);
115: }
116: }
117:
118: private void findExtensionPointDeclarations(IPluginModelBase model,
119: ArrayList result) {
120: IPluginExtensionPoint[] extensionPoints = model.getPluginBase()
121: .getExtensionPoints();
122: for (int i = 0; i < extensionPoints.length; i++) {
123: if (fPattern.matcher(extensionPoints[i].getFullId())
124: .matches())
125: result.add(extensionPoints[i]);
126: }
127: }
128:
129: private void findExtensionPointReferences(IPluginModelBase model,
130: ArrayList result) {
131: IPluginExtension[] extensions = model.getPluginBase()
132: .getExtensions();
133: for (int i = 0; i < extensions.length; i++) {
134: if (fPattern.matcher(extensions[i].getPoint()).matches())
135: result.add(extensions[i]);
136: }
137: }
138:
139: }
|