KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > ConfigurationElementSorter


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
12 package org.eclipse.ui.texteditor;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Comparator JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.osgi.framework.Bundle;
25 import org.osgi.framework.BundleException;
26 import org.osgi.framework.Constants;
27
28 import org.eclipse.core.runtime.Assert;
29 import org.eclipse.core.runtime.IConfigurationElement;
30 import org.eclipse.core.runtime.IExtension;
31 import org.eclipse.core.runtime.IStatus;
32 import org.eclipse.core.runtime.Platform;
33 import org.eclipse.core.runtime.Status;
34
35 import org.eclipse.osgi.util.ManifestElement;
36
37
38 import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
39
40 /**
41  * Allows to sort an array based on their elements' configuration elements
42  * according to the prerequisite relation of their defining plug-ins.
43  * <p>
44  * This class may be subclassed.
45  * </p>
46  *
47  * @since 3.0
48  */

49 public abstract class ConfigurationElementSorter {
50
51     /**
52      * Sorts the given array based on its elements' configuration elements
53      * according to the prerequisite relation of their defining plug-ins.
54      *
55      * @param elements the array to be sorted
56      */

57     public final void sort(Object JavaDoc[] elements) {
58         Arrays.sort(elements, new ConfigurationElementComparator(elements));
59     }
60
61     /**
62      * Returns the configuration element for the given object.
63      *
64      * @param object the object
65      * @return the object's configuration element, must not be <code>null</code>
66      */

67     public abstract IConfigurationElement getConfigurationElement(Object JavaDoc object);
68
69     /**
70      * Compare configuration elements according to the prerequisite relation
71      * of their defining plug-ins.
72      */

73     private class ConfigurationElementComparator implements Comparator JavaDoc {
74
75         private Map JavaDoc fDescriptorMapping;
76         private Map JavaDoc fPrereqsMapping;
77
78         public ConfigurationElementComparator(Object JavaDoc[] elements) {
79             Assert.isNotNull(elements);
80             initialize(elements);
81         }
82
83         /*
84          * @see Comparator#compare(java.lang.Object, java.lang.Object)
85          * @since 2.0
86          */

87         public int compare(Object JavaDoc object0, Object JavaDoc object1) {
88
89             if (dependsOn(object0, object1))
90                 return -1;
91
92             if (dependsOn(object1, object0))
93                 return +1;
94
95             return 0;
96         }
97
98         /**
99          * Returns whether one configuration element depends on the other element.
100          * This is done by checking the dependency chain of the defining plug-ins.
101          *
102          * @param element0 the first element
103          * @param element1 the second element
104          * @return <code>true</code> if <code>element0</code> depends on <code>element1</code>.
105          * @since 2.0
106          */

107         private boolean dependsOn(Object JavaDoc element0, Object JavaDoc element1) {
108             if (element0 == null || element1 == null)
109                 return false;
110
111             String JavaDoc pluginDesc0= (String JavaDoc)fDescriptorMapping.get(element0);
112             String JavaDoc pluginDesc1= (String JavaDoc)fDescriptorMapping.get(element1);
113
114             // performance tuning - code below would give same result
115
if (pluginDesc0.equals(pluginDesc1))
116                 return false;
117
118             Set JavaDoc prereqUIds0= (Set JavaDoc)fPrereqsMapping.get(pluginDesc0);
119
120             return prereqUIds0.contains(pluginDesc1);
121         }
122
123         /**
124          * Initialize this comparator.
125          *
126          * @param elements an array of Java editor hover descriptors
127          */

128         private void initialize(Object JavaDoc[] elements) {
129             int length= elements.length;
130             fDescriptorMapping= new HashMap JavaDoc(length);
131             fPrereqsMapping= new HashMap JavaDoc(length);
132             Set JavaDoc fBundleSet= new HashSet JavaDoc(length);
133
134             for (int i= 0; i < length; i++) {
135                 IConfigurationElement configElement= getConfigurationElement(elements[i]);
136                 Bundle bundle= Platform.getBundle(configElement.getContributor().getName());
137                 fDescriptorMapping.put(elements[i], bundle.getSymbolicName());
138                 fBundleSet.add(bundle);
139             }
140
141             Iterator JavaDoc iter= fBundleSet.iterator();
142             while (iter.hasNext()) {
143                 Bundle bundle= (Bundle)iter.next();
144                 List JavaDoc toTest= new ArrayList JavaDoc(fBundleSet);
145                 toTest.remove(bundle);
146                 Set JavaDoc prereqUIds= new HashSet JavaDoc(Math.max(0, toTest.size() - 1));
147                 fPrereqsMapping.put(bundle.getSymbolicName(), prereqUIds);
148
149                 String JavaDoc requires = (String JavaDoc)bundle.getHeaders().get(Constants.REQUIRE_BUNDLE);
150                 ManifestElement[] manifestElements;
151                 try {
152                     manifestElements = ManifestElement.parseHeader(Constants.REQUIRE_BUNDLE, requires);
153                 } catch (BundleException e) {
154                     String JavaDoc uid= getExtensionPointUniqueIdentifier(bundle);
155                     String JavaDoc message= "ConfigurationElementSorter for '" + uid + "': getting required plug-ins for '" + bundle.getSymbolicName() + "' failed"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
156
Status status= new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, message, e);
157                     TextEditorPlugin.getDefault().getLog().log(status);
158                     continue;
159                 }
160
161                 if (manifestElements == null)
162                     continue;
163
164                 int i= 0;
165                 while (i < manifestElements.length && !toTest.isEmpty()) {
166                     String JavaDoc prereqUId= manifestElements[i].getValue();
167                     for (int j= 0; j < toTest.size();) {
168                         Bundle toTest_j= (Bundle)toTest.get(j);
169                         if (toTest_j.getSymbolicName().equals(prereqUId)) {
170                             toTest.remove(toTest_j);
171                             prereqUIds.add(toTest_j.getSymbolicName());
172                         } else
173                             j++;
174                     }
175                     i++;
176                 }
177             }
178         }
179
180         /**
181          * Returns the unique extension point identifier for the
182          * configuration element which belongs to the given bundle.
183          *
184          * @param bundle the bundle
185          * @return the unique extension point identifier or "unknown" if not found
186          * @since 3.0.1
187          */

188         private String JavaDoc getExtensionPointUniqueIdentifier(Bundle bundle) {
189             if (bundle != null) {
190                 String JavaDoc bundleName= bundle.getSymbolicName();
191                 if (bundleName != null) {
192                     Set JavaDoc entries= fDescriptorMapping.entrySet();
193                     Iterator JavaDoc iter= entries.iterator();
194                     while (iter.hasNext()) {
195                         Map.Entry JavaDoc entry= (Map.Entry JavaDoc)iter.next();
196                         if (bundleName.equals(entry.getValue())) {
197                             IExtension extension = getConfigurationElement(entry.getKey()).getDeclaringExtension();
198                             return extension.getExtensionPointUniqueIdentifier();
199                         }
200                     }
201                 }
202             }
203             return "unknown"; //$NON-NLS-1$
204
}
205
206     }
207 }
208
Popular Tags