KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > views > properties > tabbed > view > TabbedPropertyRegistry


1 /*******************************************************************************
2  * Copyright (c) 2001, 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.views.properties.tabbed.view;
12
13 import com.ibm.icu.text.MessageFormat;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Comparator JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.IExtensionPoint;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.jface.viewers.ILabelProvider;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.ui.IWorkbenchPart;
29 import org.eclipse.ui.internal.views.properties.tabbed.TabbedPropertyViewPlugin;
30 import org.eclipse.ui.internal.views.properties.tabbed.TabbedPropertyViewStatusCodes;
31 import org.eclipse.ui.internal.views.properties.tabbed.l10n.TabbedPropertyMessages;
32 import org.eclipse.ui.views.properties.tabbed.IActionProvider;
33 import org.eclipse.ui.views.properties.tabbed.ISectionDescriptor;
34 import org.eclipse.ui.views.properties.tabbed.ISectionDescriptorProvider;
35 import org.eclipse.ui.views.properties.tabbed.ITypeMapper;
36
37 /**
38  * Provides information about the tabbed property extension points. Each tabbed
39  * property registry is associated with a unique contributor ID.
40  *
41  * @author Anthony Hunter
42  */

43 public class TabbedPropertyRegistry {
44
45     private final static String JavaDoc NO_TAB_ERROR = TabbedPropertyMessages.TabbedPropertyRegistry_Non_existing_tab;
46
47     private final static String JavaDoc CONTRIBUTOR_ERROR = TabbedPropertyMessages.TabbedPropertyRegistry_contributor_error;
48
49     // extension point constants
50
private static final String JavaDoc EXTPT_CONTRIBUTOR = "propertyContributor"; //$NON-NLS-1$
51

52     private static final String JavaDoc EXTPT_TABS = "propertyTabs"; //$NON-NLS-1$
53

54     private static final String JavaDoc EXTPT_SECTIONS = "propertySections"; //$NON-NLS-1$
55

56     private static final String JavaDoc ELEMENT_TAB = "propertyTab"; //$NON-NLS-1$
57

58     private static final String JavaDoc ELEMENT_SECTION = "propertySection"; //$NON-NLS-1$
59

60     private static final String JavaDoc ELEMENT_PROPERTY_CATEGORY = "propertyCategory"; //$NON-NLS-1$
61

62     private static final String JavaDoc ATT_CATEGORY = "category"; //$NON-NLS-1$
63

64     private static final String JavaDoc ATT_CONTRIBUTOR_ID = "contributorId"; //$NON-NLS-1$
65

66     private static final String JavaDoc ATT_TYPE_MAPPER = "typeMapper"; //$NON-NLS-1$
67

68     private static final String JavaDoc ATT_LABEL_PROVIDER = "labelProvider"; //$NON-NLS-1$
69

70     private static final String JavaDoc ATT_ACTION_PROVIDER = "actionProvider"; //$NON-NLS-1$
71

72     private static final String JavaDoc TOP = "top"; //$NON-NLS-1$
73

74     protected String JavaDoc contributorId;
75
76     protected IConfigurationElement contributorConfigurationElement;
77
78     protected List JavaDoc propertyCategories;
79
80     protected ILabelProvider labelProvider;
81
82     protected IActionProvider actionProvider;
83
84     protected ITypeMapper typeMapper;
85
86     protected ISectionDescriptorProvider sectionDescriptorProvider;
87
88     protected TabDescriptor[] tabDescriptors;
89
90     protected static final TabDescriptor[] EMPTY_DESCRIPTOR_ARRAY = new TabDescriptor[0];
91
92     /**
93      * There is one details registry for each contributor type.
94      */

95     protected TabbedPropertyRegistry(String JavaDoc id) {
96         this.contributorId = id;
97         this.propertyCategories = new ArrayList JavaDoc();
98         IConfigurationElement[] extensions = getConfigurationElements(EXTPT_CONTRIBUTOR);
99         for (int i = 0; i < extensions.length; i++) {
100             IConfigurationElement configurationElement = extensions[i];
101             String JavaDoc contributor = configurationElement
102                 .getAttribute(ATT_CONTRIBUTOR_ID);
103             if (contributor == null || !id.equals(contributor)) {
104                 continue;
105             }
106             this.contributorConfigurationElement = configurationElement;
107             try {
108                 if (configurationElement.getAttribute(ATT_LABEL_PROVIDER) != null) {
109                     labelProvider = (ILabelProvider) configurationElement
110                         .createExecutableExtension(ATT_LABEL_PROVIDER);
111                 }
112                 if (configurationElement.getAttribute(ATT_ACTION_PROVIDER) != null) {
113                     actionProvider = (IActionProvider) configurationElement
114                         .createExecutableExtension(ATT_ACTION_PROVIDER);
115                 }
116                 if (configurationElement.getAttribute(ATT_TYPE_MAPPER) != null) {
117                     typeMapper = (ITypeMapper) configurationElement
118                         .createExecutableExtension(ATT_TYPE_MAPPER);
119                 }
120             } catch (CoreException exception) {
121                 handleConfigurationError(id, exception);
122             }
123             addPropertyCategories(configurationElement);
124         }
125         if (propertyCategories == null || contributorId == null
126             || contributorConfigurationElement == null) {
127             handleConfigurationError(id, null);
128             this.contributorId = null;
129         }
130     }
131
132     /**
133      * Gets the categories that are valid for this contributor.
134      *
135      * @param configurationElement
136      * the configuration element for this contributor.
137      * @return the the categories that are valid for this contributor.
138      */

139     private void addPropertyCategories(
140             IConfigurationElement configurationElement) {
141         IConfigurationElement[] elements = configurationElement
142             .getChildren(ELEMENT_PROPERTY_CATEGORY);
143         for (int i = 0; i < elements.length; i++) {
144             IConfigurationElement element = elements[i];
145             propertyCategories.add(element.getAttribute(ATT_CATEGORY));
146         }
147     }
148
149     /**
150      * Handle the error when an issue is found loading from the configuration
151      * element.
152      *
153      * @param configurationElement
154      * the configuration element
155      * @param exception
156      * an optional CoreException
157      */

158     private void handleConfigurationError(String JavaDoc id, CoreException exception) {
159         String JavaDoc message = MessageFormat.format(CONTRIBUTOR_ERROR,
160             new Object JavaDoc[] {id});
161         IStatus status = new Status(IStatus.ERROR, TabbedPropertyViewPlugin
162             .getPlugin().getBundle().getSymbolicName(),
163             TabbedPropertyViewStatusCodes.CONTRIBUTOR_ERROR, message, exception);
164         TabbedPropertyViewPlugin.getPlugin().getLog().log(status);
165     }
166
167     /**
168      * Reads property section extensions. Returns all section descriptors for
169      * the current contributor id or an empty array if none is found.
170      */

171     protected ISectionDescriptor[] readSectionDescriptors() {
172         List JavaDoc result = new ArrayList JavaDoc();
173         IConfigurationElement[] extensions = getConfigurationElements(EXTPT_SECTIONS);
174         for (int i = 0; i < extensions.length; i++) {
175             IConfigurationElement extension = extensions[i];
176             IConfigurationElement[] sections = extension
177                 .getChildren(ELEMENT_SECTION);
178             for (int j = 0; j < sections.length; j++) {
179                 IConfigurationElement section = sections[j];
180                 ISectionDescriptor descriptor = new SectionDescriptor(section,
181                     typeMapper);
182                 result.add(descriptor);
183             }
184         }
185         return (ISectionDescriptor[]) result
186             .toArray(new ISectionDescriptor[result.size()]);
187     }
188
189     /**
190      * Returns the configuration elements targeted for the given extension point
191      * and the current contributor id. The elements are also sorted by plugin
192      * prerequisite order.
193      */

194     protected IConfigurationElement[] getConfigurationElements(
195             String JavaDoc extensionPointId) {
196         if (contributorId == null) {
197             return new IConfigurationElement[0];
198         }
199         IExtensionPoint point = Platform.getExtensionRegistry()
200             .getExtensionPoint(
201                 TabbedPropertyViewPlugin.getPlugin().getBundle()
202                     .getSymbolicName(), extensionPointId);
203         IConfigurationElement[] extensions = point.getConfigurationElements();
204         List JavaDoc unordered = new ArrayList JavaDoc(extensions.length);
205         for (int i = 0; i < extensions.length; i++) {
206             IConfigurationElement extension = extensions[i];
207             if (!extension.getName().equals(extensionPointId)) {
208                 continue;
209             }
210             String JavaDoc contributor = extension.getAttribute(ATT_CONTRIBUTOR_ID);
211             if (!contributorId.equals(contributor)) {
212                 continue;
213             }
214             unordered.add(extension);
215         }
216         return (IConfigurationElement[]) unordered
217             .toArray(new IConfigurationElement[unordered.size()]);
218     }
219
220     /**
221      * Returns the index of the given element in the array.
222      */

223     private int getIndex(Object JavaDoc[] array, Object JavaDoc target) {
224         for (int i = 0; i < array.length; i++) {
225             if (array[i].equals(target)) {
226                 return i;
227             }
228         }
229         return -1; // should never happen
230
}
231
232     /**
233      * Returns property tab descriptors for the given contributor id and object
234      * input. The descriptors are sorted using the afterPage attribute.
235      */

236     public TabDescriptor[] getTabDescriptors(IWorkbenchPart part,
237             ISelection selection) {
238         if (selection == null || selection.isEmpty()) {
239             return EMPTY_DESCRIPTOR_ARRAY;
240         }
241
242         TabDescriptor[] allDescriptors = getAllTabDescriptors();
243         TabDescriptor[] result = filterTabDescriptors(allDescriptors, part,
244             selection);
245         return result;
246     }
247
248     /**
249      * Filters out the tab descriptors that do not have any sections for the
250      * given input.
251      */

252     protected TabDescriptor[] filterTabDescriptors(TabDescriptor[] descriptors,
253             IWorkbenchPart part, ISelection selection) {
254         List JavaDoc result = new ArrayList JavaDoc();
255         for (int i = 0; i < descriptors.length; i++) {
256             TabDescriptor descriptor = adaptDescriptorFor(descriptors[i], part,
257                 selection);
258             if (!descriptor.getSectionDescriptors().isEmpty()) {
259                 result.add(descriptor);
260             }
261         }
262         if (result.size() == 0) {
263             return EMPTY_DESCRIPTOR_ARRAY;
264         }
265         return (TabDescriptor[]) result
266             .toArray(new TabDescriptor[result.size()]);
267     }
268
269     /**
270      * Given a property tab descriptor remove all its section descriptors that
271      * do not apply to the given input object.
272      */

273     protected TabDescriptor adaptDescriptorFor(TabDescriptor target,
274             IWorkbenchPart part, ISelection selection) {
275         List JavaDoc filteredSectionDescriptors = new ArrayList JavaDoc();
276         List JavaDoc descriptors = target.getSectionDescriptors();
277         for (Iterator JavaDoc iter = descriptors.iterator(); iter.hasNext();) {
278             ISectionDescriptor descriptor = (ISectionDescriptor) iter.next();
279             if (descriptor.appliesTo(part, selection)) {
280                 filteredSectionDescriptors.add(descriptor);
281             }
282         }
283         TabDescriptor result = (TabDescriptor) target.clone();
284         result.setSectionDescriptors(filteredSectionDescriptors);
285         return result;
286     }
287
288     /**
289      * Reads property tab extensions. Returns all tab descriptors for the
290      * current contributor id or an empty array if none is found.
291      */

292     protected TabDescriptor[] getAllTabDescriptors() {
293         if (tabDescriptors == null) {
294             List JavaDoc temp = readTabDescriptors();
295             populateWithSectionDescriptors(temp);
296             temp = sortTabDescriptorsByCategory(temp);
297             temp = sortTabDescriptorsByAfterTab(temp);
298             tabDescriptors = (TabDescriptor[]) temp
299                 .toArray(new TabDescriptor[temp.size()]);
300         }
301         return tabDescriptors;
302     }
303
304     /**
305      * Reads property tab extensions. Returns all tab descriptors for the
306      * current contributor id or an empty list if none is found.
307      */

308     protected List JavaDoc readTabDescriptors() {
309         List JavaDoc result = new ArrayList JavaDoc();
310         IConfigurationElement[] extensions = getConfigurationElements(EXTPT_TABS);
311         for (int i = 0; i < extensions.length; i++) {
312             IConfigurationElement extension = extensions[i];
313             IConfigurationElement[] tabs = extension.getChildren(ELEMENT_TAB);
314             for (int j = 0; j < tabs.length; j++) {
315                 IConfigurationElement tab = tabs[j];
316                 TabDescriptor descriptor = new TabDescriptor(tab);
317                 result.add(descriptor);
318             }
319         }
320         return result;
321     }
322
323     /**
324      * Populates the given tab descriptors with section descriptors.
325      */

326     protected void populateWithSectionDescriptors(List JavaDoc aTabDescriptors) {
327         ISectionDescriptor[] sections = null;
328         if (sectionDescriptorProvider != null) {
329             sections = sectionDescriptorProvider.getSectionDescriptors();
330         } else {
331             sections = readSectionDescriptors();
332         }
333         for (int i = 0; i < sections.length; i++) {
334             ISectionDescriptor section = sections[i];
335             appendToTabDescriptor(section, aTabDescriptors);
336         }
337     }
338
339     /**
340      * Appends the given section to a tab from the list.
341      */

342     protected void appendToTabDescriptor(ISectionDescriptor section,
343             List JavaDoc aTabDescriptors) {
344         for (Iterator JavaDoc i = aTabDescriptors.iterator(); i.hasNext();) {
345             TabDescriptor tab = (TabDescriptor) i.next();
346             if (tab.append(section)) {
347                 return;
348             }
349         }
350         // could not append the section to any of the existing tabs - log error
351
String JavaDoc message = MessageFormat.format(NO_TAB_ERROR, new Object JavaDoc[] {
352             section.getId(), section.getTargetTab()});
353         IStatus status = new Status(IStatus.ERROR, TabbedPropertyViewPlugin
354             .getPlugin().getBundle().getSymbolicName(),
355             TabbedPropertyViewStatusCodes.NO_TAB_ERROR, message, null);
356         TabbedPropertyViewPlugin.getPlugin().getLog().log(status);
357     }
358
359     /**
360      * Sorts the tab descriptors in the given list according to category.
361      */

362     protected List JavaDoc sortTabDescriptorsByCategory(List JavaDoc descriptors) {
363         Collections.sort(descriptors, new Comparator JavaDoc() {
364
365             public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
366                 TabDescriptor one = (TabDescriptor) arg0;
367                 TabDescriptor two = (TabDescriptor) arg1;
368                 String JavaDoc categoryOne = one.getCategory();
369                 String JavaDoc categoryTwo = two.getCategory();
370                 int categoryOnePosition = getIndex(propertyCategories.toArray(),
371                     categoryOne);
372                 int categoryTwoPosition = getIndex(propertyCategories.toArray(),
373                     categoryTwo);
374                 return categoryOnePosition - categoryTwoPosition;
375             }
376         });
377         return descriptors;
378     }
379
380     /**
381      * Sorts the tab descriptors in the given list according to afterTab.
382      */

383     protected List JavaDoc sortTabDescriptorsByAfterTab(List JavaDoc tabs) {
384         if (tabs.size() == 0 || propertyCategories == null) {
385             return tabs;
386         }
387         List JavaDoc sorted = new ArrayList JavaDoc();
388         int categoryIndex = 0;
389         for (int i = 0; i < propertyCategories.size(); i++) {
390             List JavaDoc categoryList = new ArrayList JavaDoc();
391             String JavaDoc category = (String JavaDoc)propertyCategories.get(i);
392             int topOfCategory = categoryIndex;
393             int endOfCategory = categoryIndex;
394             while (endOfCategory < tabs.size()
395                 && ((TabDescriptor) tabs.get(endOfCategory)).getCategory()
396                     .equals(category)) {
397                 endOfCategory++;
398             }
399             for (int j = topOfCategory; j < endOfCategory; j++) {
400                 TabDescriptor tab = (TabDescriptor) tabs.get(j);
401                 if (tab.getAfterTab().equals(TOP)) {
402                     categoryList.add(0, tabs.get(j));
403                 } else {
404                     categoryList.add(tabs.get(j));
405                 }
406             }
407             Collections.sort(categoryList, new Comparator JavaDoc() {
408
409                 public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
410                     TabDescriptor one = (TabDescriptor) arg0;
411                     TabDescriptor two = (TabDescriptor) arg1;
412                     if (two.getAfterTab().equals(one.getId())) {
413                         return -1;
414                     } else if (one.getAfterTab().equals(two.getId())) {
415                         return 1;
416                     } else {
417                         return 0;
418                     }
419                 }
420             });
421             for (int j = 0; j < categoryList.size(); j++) {
422                 sorted.add(categoryList.get(j));
423             }
424             categoryIndex = endOfCategory;
425         }
426         return sorted;
427     }
428
429     /**
430      * Gets the type mapper for the contributor.
431      *
432      * @return the type mapper for the contributor.
433      */

434     public ITypeMapper getTypeMapper() {
435         return typeMapper;
436     }
437
438     /**
439      * Gets the label provider for the contributor.
440      *
441      * @return the label provider for the contributor.
442      */

443     public ILabelProvider getLabelProvider() {
444         return labelProvider;
445     }
446
447     /**
448      * Gets the action provider for the contributor.
449      *
450      * @return the action provider for the contributor.
451      */

452     public IActionProvider getActionProvider() {
453         return actionProvider;
454     }
455
456     /**
457      * Sets the section descriptor provider for the contributor.
458      *
459      * @param sectionDescriptorProvider
460      * the section descriptor provider for the contributor.
461      */

462     public void setSectionDescriptorProvider(
463             ISectionDescriptorProvider sectionDescriptorProvider) {
464         this.sectionDescriptorProvider = sectionDescriptorProvider;
465     }
466 }
467
Popular Tags