KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > PropertyPageContributorManager


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.ui.internal.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Comparator JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.IExtension;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
25 import org.eclipse.ui.internal.IWorkbenchConstants;
26 import org.eclipse.ui.internal.ObjectContributorManager;
27 import org.eclipse.ui.internal.WorkbenchPlugin;
28 import org.eclipse.ui.internal.registry.PropertyPagesRegistryReader;
29
30 import com.ibm.icu.text.Collator;
31
32 /**
33  * Extends generic object contributor manager by loading property page
34  * contributors from the registry.
35  */

36
37 public class PropertyPageContributorManager extends ObjectContributorManager {
38     private static PropertyPageContributorManager sharedInstance = null;
39
40     private class CategorizedPageNode {
41         RegistryPageContributor contributor;
42
43         CategorizedPageNode parent;
44
45         String JavaDoc qualifiedName;
46
47         CategorizedPageNode(RegistryPageContributor page) {
48             contributor = page;
49         }
50
51         void setParent(CategorizedPageNode node) {
52             parent = node;
53         }
54
55         String JavaDoc getQualifiedName() {
56
57             if (qualifiedName == null) {
58                 if (parent == null) {
59                     qualifiedName = contributor.getPageName();
60                 } else {
61                     StringBuffer JavaDoc nameBuffer = new StringBuffer JavaDoc();
62                     nameBuffer.append(parent.getQualifiedName());
63                     nameBuffer
64                             .append(WorkbenchPlugin.PREFERENCE_PAGE_CATEGORY_SEPARATOR);
65                     nameBuffer.append(contributor.getPageName());
66                     qualifiedName = nameBuffer.toString();
67                 }
68             }
69             return qualifiedName;
70
71         }
72     }
73
74     private static final Comparator JavaDoc comparer = new Comparator JavaDoc() {
75         private Collator collator = Collator.getInstance();
76
77         public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
78             // Make sure the workbench info page is always at the top.
79
CategorizedPageNode c1 = (CategorizedPageNode) arg0;
80             CategorizedPageNode c2 = (CategorizedPageNode) arg1;
81             if (IWorkbenchConstants.WORKBENCH_PROPERTIES_PAGE_INFO.equals(c1
82                     .contributor.getPageId())) {
83                 // c1 is the info page
84
if (IWorkbenchConstants.WORKBENCH_PROPERTIES_PAGE_INFO
85                         .equals(c2.contributor.getPageId())) {
86                     // both are the info page so c2 is not greater
87
return 0;
88                 }
89                 // c2 is any other page so it must be greater
90
return -1;
91             }
92             if (IWorkbenchConstants.WORKBENCH_PROPERTIES_PAGE_INFO.equals(c2
93                     .contributor.getPageId())) {
94                 // c1 is any other page so it is greater
95
return 1;
96             }
97             // The other pages are sorted in alphabetical order
98
String JavaDoc s1 = c1.getQualifiedName();
99             String JavaDoc s2 = c2.getQualifiedName();
100             return collator.compare(s1, s2);
101         }
102     };
103
104     /**
105      * The constructor.
106      */

107     public PropertyPageContributorManager() {
108         super();
109         // load contributions on startup so that getContributors() returns the
110
// proper content
111
loadContributors();
112     }
113
114     /**
115      * Given the object class, this method will find all the registered matching
116      * contributors and sequentially invoke them to contribute to the property
117      * page manager. Matching algorithm will also check subclasses and
118      * implemented interfaces.
119      * @param manager
120      * @param object
121      * @return true if contribution took place, false otherwise.
122      */

123     public boolean contribute(PropertyPageManager manager, Object JavaDoc object) {
124
125         List JavaDoc result = getContributors(object);
126
127         if (result == null || result.size() == 0) {
128             return false;
129         }
130
131         // Sort the results
132
List JavaDoc sortedResult = buildNodeList(result);
133         Collections.sort(sortedResult, comparer);
134         
135         Iterator JavaDoc resultIterator = sortedResult.iterator();
136
137         // Allow each contributor to add its page to the manager.
138
boolean actualContributions = false;
139         while(resultIterator.hasNext()) {
140             for (int i = 0; i < sortedResult.size(); i++) {
141                 CategorizedPageNode next = (CategorizedPageNode) resultIterator.next();
142                 IPropertyPageContributor ppcont = next.contributor;
143                 if (!ppcont.isApplicableTo(object)) {
144                     continue;
145                 }
146                 if (ppcont.contributePropertyPages(manager, object)) {
147                     actualContributions = true;
148                 }
149             }
150         }
151         return actualContributions;
152     }
153
154     /**
155      * Build the list of nodes to be sorted.
156      * @param nodes
157      * @return List of CategorizedPageNode
158      */

159     private List JavaDoc buildNodeList(List JavaDoc nodes) {
160         Hashtable JavaDoc mapping = new Hashtable JavaDoc();
161         
162         Iterator JavaDoc nodesIterator = nodes.iterator();
163         while(nodesIterator.hasNext()){
164             RegistryPageContributor page = (RegistryPageContributor) nodesIterator.next();
165             mapping.put(page.getPageId(),new CategorizedPageNode(page));
166         }
167         
168         Iterator JavaDoc values = mapping.values().iterator();
169         List JavaDoc returnValue = new ArrayList JavaDoc();
170         while(values.hasNext()){
171             CategorizedPageNode next = (CategorizedPageNode) values.next();
172             returnValue.add(next);
173             if(next.contributor.getCategory() == null) {
174                 continue;
175             }
176             Object JavaDoc parent = mapping.get(next.contributor.getCategory());
177             if(parent != null) {
178                 next.setParent((CategorizedPageNode) parent);
179             }
180         }
181         return returnValue;
182     }
183
184     /**
185      * Ideally, shared instance should not be used and manager should be located
186      * in the workbench class.
187      * @return PropertyPageContributorManager
188      */

189     public static PropertyPageContributorManager getManager() {
190         if (sharedInstance == null) {
191             sharedInstance = new PropertyPageContributorManager();
192         }
193         return sharedInstance;
194     }
195
196     /**
197      * Loads property page contributors from the registry.
198      */

199     private void loadContributors() {
200         PropertyPagesRegistryReader reader = new PropertyPagesRegistryReader(
201                 this);
202         reader.registerPropertyPages(Platform.getExtensionRegistry());
203     }
204     
205     /* (non-Javadoc)
206      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
207      */

208     public void addExtension(IExtensionTracker tracker, IExtension extension) {
209         IConfigurationElement[] addedElements = extension.getConfigurationElements();
210         for (int i = 0; i < addedElements.length; i++) {
211             PropertyPagesRegistryReader reader = new PropertyPagesRegistryReader(this);
212             reader.readElement(addedElements[i]);
213         }
214     }
215
216     /**
217      * Return the contributors for element filters on the
218      * enablement. This is currently
219      * only used for test suites.
220      * @param element
221      * @return Collection of PropertyPageContribution
222      */

223     public Collection JavaDoc getApplicableContributors(Object JavaDoc element) {
224         Collection JavaDoc contributors = getContributors(element);
225         Collection JavaDoc result = new ArrayList JavaDoc();
226         for (Iterator JavaDoc iter = contributors.iterator(); iter.hasNext();) {
227             RegistryPageContributor contributor = (RegistryPageContributor) iter.next();
228             if(contributor.isApplicableTo(element))
229                 result.add(contributor);
230             
231         }
232         return result;
233     }
234 }
235
Popular Tags