KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > 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.core.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.core.plugin.IPluginObject;
25 import org.eclipse.pde.internal.core.util.PatternConstructor;
26
27
28 public class PluginSearchOperation {
29     protected PluginSearchInput fInput;
30     private IPluginSearchResultCollector fCollector;
31     private Pattern JavaDoc fPattern;
32     
33     public PluginSearchOperation(
34         PluginSearchInput input,
35         IPluginSearchResultCollector collector) {
36         this.fInput = input;
37         this.fCollector = collector;
38         collector.setOperation(this);
39         this.fPattern = PatternConstructor.createPattern(
40                 input.getSearchString(), input.isCaseSensitive());
41     }
42     
43     public void execute(IProgressMonitor monitor) {
44         IPluginModelBase[] entries = fInput.getSearchScope().getMatchingModels();
45         fCollector.searchStarted();
46         fCollector.setProgressMonitor(monitor);
47         monitor.beginTask("", entries.length); //$NON-NLS-1$
48

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