KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > extension > NewExtensionRegistryReader


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.wizards.extension;
12
13 import java.util.Locale JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExtension;
18 import org.eclipse.core.runtime.IExtensionPoint;
19 import org.eclipse.core.runtime.IExtensionRegistry;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.pde.internal.ui.PDEPlugin;
22 import org.eclipse.pde.internal.ui.PDEUIMessages;
23 import org.eclipse.pde.internal.ui.elements.ElementList;
24 import org.eclipse.pde.internal.ui.wizards.Category;
25 import org.eclipse.pde.internal.ui.wizards.WizardCollectionElement;
26 import org.eclipse.pde.internal.ui.wizards.WizardElement;
27 import org.eclipse.swt.graphics.Image;
28
29 public class NewExtensionRegistryReader {
30     public static final String JavaDoc TAG_WIZARD = "wizard"; //$NON-NLS-1$
31
public static final String JavaDoc TAG_EDITOR_WIZARD = "editorWizard"; //$NON-NLS-1$
32
public static final String JavaDoc ATT_CATEGORY = "category"; //$NON-NLS-1$
33
public static final String JavaDoc ATT_SHORTCUTTABLE = "availableAsShortcut"; //$NON-NLS-1$
34
public static final String JavaDoc CATEGORY_SEPARATOR = "/"; //$NON-NLS-1$
35
public static final String JavaDoc TAG_CATEGORY = "category"; //$NON-NLS-1$
36
public static final String JavaDoc TAG_DESCRIPTION = "description"; //$NON-NLS-1$
37

38     private final static String JavaDoc UNCATEGORIZED_WIZARD_CATEGORY = "org.eclipse.pde.ui.Other"; //$NON-NLS-1$
39
private final static String JavaDoc UNCATEGORIZED_WIZARD_CATEGORY_LABEL = "Other"; //$NON-NLS-1$
40

41     private boolean editorWizardMode;
42     
43     public NewExtensionRegistryReader() {
44         this(false);
45     }
46     public NewExtensionRegistryReader(boolean editorWizardMode) {
47         this.editorWizardMode = editorWizardMode;
48     }
49     protected WizardCollectionElement createCollectionElement(
50             WizardCollectionElement parent, String JavaDoc id, String JavaDoc label) {
51         WizardCollectionElement newElement = new WizardCollectionElement(id,
52                 label, parent);
53
54         if (parent != null)
55             parent.add(newElement);
56
57         return newElement;
58     }
59     
60     protected WizardElement createWizardElement(IConfigurationElement config) {
61         String JavaDoc name = config.getAttribute(WizardElement.ATT_NAME);
62         String JavaDoc id = config.getAttribute(WizardElement.ATT_ID);
63         String JavaDoc className = config.getAttribute(WizardElement.ATT_CLASS);
64         String JavaDoc template = config.getAttribute(WizardElement.ATT_TEMPLATE);
65         if (name == null || id == null)
66             return null;
67         if (className == null && template == null)
68             return null;
69         WizardElement element = new WizardElement(config);
70         String JavaDoc imageName = config.getAttribute(WizardElement.ATT_ICON);
71         if (imageName != null) {
72             String JavaDoc pluginID = config.getNamespaceIdentifier();
73             Image image = PDEPlugin.getDefault().getLabelProvider()
74                     .getImageFromPlugin(pluginID, imageName);
75             element.setImage(image);
76         }
77         return element;
78     }
79     protected WizardElement createEditorWizardElement(IConfigurationElement config) {
80         String JavaDoc name = config.getAttribute(WizardElement.ATT_NAME);
81         String JavaDoc id = config.getAttribute(WizardElement.ATT_ID);
82         String JavaDoc className = config.getAttribute(WizardElement.ATT_CLASS);
83         String JavaDoc point = config.getAttribute(WizardElement.ATT_POINT);
84         if (name == null || id == null || className==null)
85             return null;
86         if (point == null)
87             return null;
88         WizardElement element = new WizardElement(config);
89         String JavaDoc imageName = config.getAttribute(WizardElement.ATT_ICON);
90         if (imageName != null) {
91             String JavaDoc pluginID = config.getNamespaceIdentifier();
92             Image image = PDEPlugin.getDefault().getLabelProvider()
93                     .getImageFromPlugin(pluginID, imageName);
94             element.setImage(image);
95         }
96         return element;
97     }
98     protected String JavaDoc getCategoryStringFor(IConfigurationElement config) {
99         String JavaDoc result = config.getAttribute(ATT_CATEGORY);
100         if (result == null)
101             result = UNCATEGORIZED_WIZARD_CATEGORY;
102
103         return result;
104     }
105     protected WizardCollectionElement getChildWithID(
106             WizardCollectionElement parent, String JavaDoc id) {
107         Object JavaDoc[] children = parent.getChildren();
108
109         if (children != null) {
110             for (int i = 0; i < children.length; i++) {
111                 WizardCollectionElement currentChild = (WizardCollectionElement) children[i];
112                 if (currentChild.getId().equals(id))
113                     return currentChild;
114             }
115         }
116         return null;
117     }
118     protected void insertUsingCategory(WizardElement element, ElementList result) {
119         WizardCollectionElement currentResult = (WizardCollectionElement) result;
120         StringTokenizer JavaDoc familyTokenizer = new StringTokenizer JavaDoc(
121                 getCategoryStringFor(element.getConfigurationElement()),
122                 CATEGORY_SEPARATOR);
123
124         // use the period-separated sections of the current Wizard's category
125
// to traverse through the NamedSolution "tree" that was previously
126
// created
127
WizardCollectionElement currentCollectionElement = currentResult; // ie.-
128
// root
129
boolean moveToOther = false;
130
131         while (familyTokenizer.hasMoreElements()) {
132             WizardCollectionElement tempCollectionElement = getChildWithID(
133                     currentCollectionElement, familyTokenizer.nextToken());
134
135             if (tempCollectionElement == null) { // can't find the path; bump it
136
// to uncategorized
137
moveToOther = true;
138                 break;
139             }
140             currentCollectionElement = tempCollectionElement;
141         }
142
143         if (moveToOther)
144             moveElementToUncategorizedCategory(currentResult, element);
145         else
146             currentCollectionElement.getWizards().add(element);
147     }
148     protected void moveElementToUncategorizedCategory(
149             WizardCollectionElement root, WizardElement element) {
150         WizardCollectionElement otherCategory = getChildWithID(root,
151                 UNCATEGORIZED_WIZARD_CATEGORY);
152
153         if (otherCategory == null)
154             otherCategory = createCollectionElement(root,
155                     UNCATEGORIZED_WIZARD_CATEGORY,
156                     UNCATEGORIZED_WIZARD_CATEGORY_LABEL);
157
158         otherCategory.getWizards().add(element);
159     }
160     private void processCategory(IConfigurationElement config, ElementList list) {
161         WizardCollectionElement result = (WizardCollectionElement) list;
162         Category category = null;
163
164         category = new Category(config);
165         if (category.getID() == null || category.getLabel() == null) {
166             System.out.println(PDEUIMessages.NewExtensionRegistryReader_missingProperty);
167             return;
168         }
169
170         String JavaDoc[] categoryPath = category.getParentCategoryPath();
171         WizardCollectionElement parent = result; // ie.- root
172

173         if (categoryPath != null) {
174             for (int i = 0; i < categoryPath.length; i++) {
175                 WizardCollectionElement tempElement = getChildWithID(parent,
176                         categoryPath[i]);
177                 if (tempElement == null) {
178                     parent = null;
179                     break;
180                 }
181                 parent = tempElement;
182             }
183         }
184
185         if (parent != null)
186             createCollectionElement(parent, category.getID(), category
187                     .getLabel());
188     }
189     protected void processElement(IConfigurationElement element,
190             ElementList result, boolean shortcutsOnly) {
191         String JavaDoc tag = element.getName();
192         if (tag.equals(TAG_WIZARD) && !editorWizardMode) {
193             WizardElement wizard = createWizardElement(element);
194             if (shortcutsOnly) {
195                 String JavaDoc shortcut = element.getAttribute(ATT_SHORTCUTTABLE);
196                 if (shortcut != null && shortcut.toLowerCase(Locale.ENGLISH).equals("true")) { //$NON-NLS-1$
197
result.add(wizard);
198                 }
199             } else
200                 insertUsingCategory(wizard, result);
201         } else if (tag.equals(TAG_EDITOR_WIZARD) && editorWizardMode) {
202             WizardElement wizard = createEditorWizardElement(element);
203             if (shortcutsOnly) {
204                 result.add(wizard);
205             }
206             else
207                 insertUsingCategory(wizard, result);
208         }
209         else if (tag.equals(TAG_CATEGORY)) {
210             if (shortcutsOnly == false) {
211                 processCategory(element, result);
212             }
213         }
214     }
215     public ElementList readRegistry(String JavaDoc pluginId, String JavaDoc pluginPointId,
216             boolean shortcutsOnly) {
217         ElementList result = (shortcutsOnly)
218                 ? (new ElementList("shortcuts")) //$NON-NLS-1$
219
: (new WizardCollectionElement("root", "root", null)); //$NON-NLS-1$ //$NON-NLS-2$
220
IExtensionRegistry registry = Platform.getExtensionRegistry();
221         IExtensionPoint point = registry.getExtensionPoint(pluginId,
222                 pluginPointId);
223         if (point == null)
224             return null;
225
226         IExtension[] extensions = point.getExtensions();
227         for (int i = 0; i < extensions.length; i++) {
228             IConfigurationElement[] elements = extensions[i]
229                     .getConfigurationElements();
230             for (int j = 0; j < elements.length; j++) {
231                 IConfigurationElement config = elements[j];
232                 processElement(config, result, shortcutsOnly);
233             }
234         }
235         return result;
236     }
237 }
238
Popular Tags