KickJava   Java API By Example, From Geeks To Geeks.

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


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.regex.Pattern JavaDoc;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.pde.core.plugin.IFragment;
18 import org.eclipse.pde.core.plugin.IPlugin;
19 import org.eclipse.pde.core.plugin.IPluginBase;
20 import org.eclipse.pde.core.plugin.IPluginExtension;
21 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
22 import org.eclipse.pde.core.plugin.IPluginImport;
23 import org.eclipse.pde.core.plugin.IPluginModelBase;
24 import org.eclipse.pde.internal.core.search.PluginSearchInput;
25 import org.eclipse.pde.internal.core.util.PatternConstructor;
26
27
28 public class PluginSearchOperation {
29     protected PluginSearchInput fInput;
30     private ISearchResultCollector fCollector;
31     private Pattern JavaDoc fPattern;
32     
33     public PluginSearchOperation(
34         PluginSearchInput input,
35         ISearchResultCollector collector) {
36         this.fInput = input;
37         this.fCollector = collector;
38         this.fPattern = PatternConstructor.createPattern(
39                 input.getSearchString(), input.isCaseSensitive());
40     }
41     
42     public void execute(IProgressMonitor monitor) {
43         IPluginModelBase[] entries = fInput.getSearchScope().getMatchingModels();
44         monitor.beginTask("", entries.length); //$NON-NLS-1$
45

46         try {
47             for (int i = 0; i < entries.length; i++) {
48                 IPluginModelBase candidate = entries[i];
49                 visit(candidate);
50                 monitor.worked(1);
51             }
52         } finally {
53             monitor.done();
54         }
55     }
56     
57     private void visit(IPluginModelBase model) {
58         ArrayList JavaDoc matches = findMatch(model);
59         for (int i = 0; i < matches.size(); i++) {
60             fCollector.accept(matches.get(i));
61         }
62     }
63     
64     private ArrayList JavaDoc findMatch(IPluginModelBase model) {
65         ArrayList JavaDoc result = new ArrayList JavaDoc();
66         int searchLimit = fInput.getSearchLimit();
67         switch (fInput.getSearchElement()) {
68             case PluginSearchInput.ELEMENT_PLUGIN :
69                 if (searchLimit != PluginSearchInput.LIMIT_REFERENCES)
70                     findPluginDeclaration(model, result);
71                 if (searchLimit != PluginSearchInput.LIMIT_DECLARATIONS)
72                     findPluginReferences(model, result);
73                 break;
74             case PluginSearchInput.ELEMENT_FRAGMENT :
75                 findFragmentDeclaration(model, result);
76                 break;
77             case PluginSearchInput.ELEMENT_EXTENSION_POINT :
78                 if (searchLimit != PluginSearchInput.LIMIT_REFERENCES)
79                     findExtensionPointDeclarations(model, result);
80                 if (searchLimit != PluginSearchInput.LIMIT_DECLARATIONS)
81                     findExtensionPointReferences(model, result);
82                 break;
83         }
84         return result;
85     }
86     
87     private void findFragmentDeclaration(
88         IPluginModelBase model,
89         ArrayList JavaDoc result) {
90         IPluginBase pluginBase = model.getPluginBase();
91         if (pluginBase instanceof IFragment
92                 && fPattern.matcher(pluginBase.getId()).matches()) {
93             result.add(pluginBase); }
94     }
95                 
96     private void findPluginDeclaration(IPluginModelBase model, ArrayList JavaDoc result) {
97         IPluginBase pluginBase = model.getPluginBase();
98         if (pluginBase instanceof IPlugin
99                 && fPattern.matcher(pluginBase.getId()).matches())
100             result.add(pluginBase);
101     }
102     
103     private void findPluginReferences(
104         IPluginModelBase model,
105         ArrayList JavaDoc result) {
106         IPluginBase pluginBase = model.getPluginBase();
107         if (pluginBase instanceof IFragment) {
108             if (fPattern.matcher(((IFragment) pluginBase).getPluginId())
109                     .matches())
110                 result.add(pluginBase);
111         }
112         IPluginImport[] imports = pluginBase.getImports();
113         for (int i = 0; i < imports.length; i++) {
114             if (fPattern.matcher(imports[i].getId()).matches())
115                 result.add(imports[i]);
116         }
117     }
118
119     private void findExtensionPointDeclarations(
120         IPluginModelBase model,
121         ArrayList JavaDoc result) {
122         IPluginExtensionPoint[] extensionPoints =
123             model.getPluginBase().getExtensionPoints();
124         for (int i = 0; i < extensionPoints.length; i++) {
125             if (fPattern.matcher(extensionPoints[i].getFullId()).matches())
126                 result.add(extensionPoints[i]);
127         }
128     }
129     
130     private void findExtensionPointReferences(IPluginModelBase model, ArrayList JavaDoc result) {
131         IPluginExtension[] extensions = model.getPluginBase().getExtensions();
132         for (int i = 0; i < extensions.length; i++) {
133             if (fPattern.matcher(extensions[i].getPoint()).matches())
134                 result.add(extensions[i]);
135         }
136     }
137         
138 }
139
Popular Tags