001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards.extension;
011:
012: import java.util.Locale;
013: import java.util.StringTokenizer;
014:
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.IExtension;
017: import org.eclipse.core.runtime.IExtensionPoint;
018: import org.eclipse.core.runtime.IExtensionRegistry;
019: import org.eclipse.core.runtime.Platform;
020: import org.eclipse.pde.internal.ui.PDEPlugin;
021: import org.eclipse.pde.internal.ui.PDEUIMessages;
022: import org.eclipse.pde.internal.ui.elements.ElementList;
023: import org.eclipse.pde.internal.ui.wizards.Category;
024: import org.eclipse.pde.internal.ui.wizards.WizardCollectionElement;
025: import org.eclipse.pde.internal.ui.wizards.WizardElement;
026: import org.eclipse.swt.graphics.Image;
027:
028: public class NewExtensionRegistryReader {
029: public static final String TAG_WIZARD = "wizard"; //$NON-NLS-1$
030: public static final String TAG_EDITOR_WIZARD = "editorWizard"; //$NON-NLS-1$
031: public static final String ATT_CATEGORY = "category"; //$NON-NLS-1$
032: public static final String ATT_SHORTCUTTABLE = "availableAsShortcut"; //$NON-NLS-1$
033: public static final String CATEGORY_SEPARATOR = "/"; //$NON-NLS-1$
034: public static final String TAG_CATEGORY = "category"; //$NON-NLS-1$
035: public static final String TAG_DESCRIPTION = "description"; //$NON-NLS-1$
036:
037: private final static String UNCATEGORIZED_WIZARD_CATEGORY = "org.eclipse.pde.ui.Other"; //$NON-NLS-1$
038: private final static String UNCATEGORIZED_WIZARD_CATEGORY_LABEL = "Other"; //$NON-NLS-1$
039:
040: private boolean editorWizardMode;
041:
042: public NewExtensionRegistryReader() {
043: this (false);
044: }
045:
046: public NewExtensionRegistryReader(boolean editorWizardMode) {
047: this .editorWizardMode = editorWizardMode;
048: }
049:
050: protected WizardCollectionElement createCollectionElement(
051: WizardCollectionElement parent, String id, String label) {
052: WizardCollectionElement newElement = new WizardCollectionElement(
053: id, label, parent);
054:
055: if (parent != null)
056: parent.add(newElement);
057:
058: return newElement;
059: }
060:
061: protected WizardElement createWizardElement(
062: IConfigurationElement config) {
063: String name = config.getAttribute(WizardElement.ATT_NAME);
064: String id = config.getAttribute(WizardElement.ATT_ID);
065: String className = config.getAttribute(WizardElement.ATT_CLASS);
066: String template = config
067: .getAttribute(WizardElement.ATT_TEMPLATE);
068: if (name == null || id == null)
069: return null;
070: if (className == null && template == null)
071: return null;
072: WizardElement element = new WizardElement(config);
073: String imageName = config.getAttribute(WizardElement.ATT_ICON);
074: if (imageName != null) {
075: String pluginID = config.getNamespaceIdentifier();
076: Image image = PDEPlugin.getDefault().getLabelProvider()
077: .getImageFromPlugin(pluginID, imageName);
078: element.setImage(image);
079: }
080: return element;
081: }
082:
083: protected WizardElement createEditorWizardElement(
084: IConfigurationElement config) {
085: String name = config.getAttribute(WizardElement.ATT_NAME);
086: String id = config.getAttribute(WizardElement.ATT_ID);
087: String className = config.getAttribute(WizardElement.ATT_CLASS);
088: String point = config.getAttribute(WizardElement.ATT_POINT);
089: if (name == null || id == null || className == null)
090: return null;
091: if (point == null)
092: return null;
093: WizardElement element = new WizardElement(config);
094: String imageName = config.getAttribute(WizardElement.ATT_ICON);
095: if (imageName != null) {
096: String pluginID = config.getNamespaceIdentifier();
097: Image image = PDEPlugin.getDefault().getLabelProvider()
098: .getImageFromPlugin(pluginID, imageName);
099: element.setImage(image);
100: }
101: return element;
102: }
103:
104: protected String getCategoryStringFor(IConfigurationElement config) {
105: String result = config.getAttribute(ATT_CATEGORY);
106: if (result == null)
107: result = UNCATEGORIZED_WIZARD_CATEGORY;
108:
109: return result;
110: }
111:
112: protected WizardCollectionElement getChildWithID(
113: WizardCollectionElement parent, String id) {
114: Object[] children = parent.getChildren();
115:
116: if (children != null) {
117: for (int i = 0; i < children.length; i++) {
118: WizardCollectionElement currentChild = (WizardCollectionElement) children[i];
119: if (currentChild.getId().equals(id))
120: return currentChild;
121: }
122: }
123: return null;
124: }
125:
126: protected void insertUsingCategory(WizardElement element,
127: ElementList result) {
128: WizardCollectionElement currentResult = (WizardCollectionElement) result;
129: StringTokenizer familyTokenizer = new StringTokenizer(
130: getCategoryStringFor(element.getConfigurationElement()),
131: CATEGORY_SEPARATOR);
132:
133: // use the period-separated sections of the current Wizard's category
134: // to traverse through the NamedSolution "tree" that was previously
135: // created
136: WizardCollectionElement currentCollectionElement = currentResult; // ie.-
137: // root
138: boolean moveToOther = false;
139:
140: while (familyTokenizer.hasMoreElements()) {
141: WizardCollectionElement tempCollectionElement = getChildWithID(
142: currentCollectionElement, familyTokenizer
143: .nextToken());
144:
145: if (tempCollectionElement == null) { // can't find the path; bump it
146: // to uncategorized
147: moveToOther = true;
148: break;
149: }
150: currentCollectionElement = tempCollectionElement;
151: }
152:
153: if (moveToOther)
154: moveElementToUncategorizedCategory(currentResult, element);
155: else
156: currentCollectionElement.getWizards().add(element);
157: }
158:
159: protected void moveElementToUncategorizedCategory(
160: WizardCollectionElement root, WizardElement element) {
161: WizardCollectionElement otherCategory = getChildWithID(root,
162: UNCATEGORIZED_WIZARD_CATEGORY);
163:
164: if (otherCategory == null)
165: otherCategory = createCollectionElement(root,
166: UNCATEGORIZED_WIZARD_CATEGORY,
167: UNCATEGORIZED_WIZARD_CATEGORY_LABEL);
168:
169: otherCategory.getWizards().add(element);
170: }
171:
172: private void processCategory(IConfigurationElement config,
173: ElementList list) {
174: WizardCollectionElement result = (WizardCollectionElement) list;
175: Category category = null;
176:
177: category = new Category(config);
178: if (category.getID() == null || category.getLabel() == null) {
179: System.out
180: .println(PDEUIMessages.NewExtensionRegistryReader_missingProperty);
181: return;
182: }
183:
184: String[] categoryPath = category.getParentCategoryPath();
185: WizardCollectionElement parent = result; // ie.- root
186:
187: if (categoryPath != null) {
188: for (int i = 0; i < categoryPath.length; i++) {
189: WizardCollectionElement tempElement = getChildWithID(
190: parent, categoryPath[i]);
191: if (tempElement == null) {
192: parent = null;
193: break;
194: }
195: parent = tempElement;
196: }
197: }
198:
199: if (parent != null)
200: createCollectionElement(parent, category.getID(), category
201: .getLabel());
202: }
203:
204: protected void processElement(IConfigurationElement element,
205: ElementList result, boolean shortcutsOnly) {
206: String tag = element.getName();
207: if (tag.equals(TAG_WIZARD) && !editorWizardMode) {
208: WizardElement wizard = createWizardElement(element);
209: if (shortcutsOnly) {
210: String shortcut = element
211: .getAttribute(ATT_SHORTCUTTABLE);
212: if (shortcut != null
213: && shortcut.toLowerCase(Locale.ENGLISH).equals(
214: "true")) { //$NON-NLS-1$
215: result.add(wizard);
216: }
217: } else
218: insertUsingCategory(wizard, result);
219: } else if (tag.equals(TAG_EDITOR_WIZARD) && editorWizardMode) {
220: WizardElement wizard = createEditorWizardElement(element);
221: if (shortcutsOnly) {
222: result.add(wizard);
223: } else
224: insertUsingCategory(wizard, result);
225: } else if (tag.equals(TAG_CATEGORY)) {
226: if (shortcutsOnly == false) {
227: processCategory(element, result);
228: }
229: }
230: }
231:
232: public ElementList readRegistry(String pluginId,
233: String pluginPointId, boolean shortcutsOnly) {
234: ElementList result = (shortcutsOnly) ? (new ElementList(
235: "shortcuts")) //$NON-NLS-1$
236: : (new WizardCollectionElement("root", "root", null)); //$NON-NLS-1$ //$NON-NLS-2$
237: IExtensionRegistry registry = Platform.getExtensionRegistry();
238: IExtensionPoint point = registry.getExtensionPoint(pluginId,
239: pluginPointId);
240: if (point == null)
241: return null;
242:
243: IExtension[] extensions = point.getExtensions();
244: for (int i = 0; i < extensions.length; i++) {
245: IConfigurationElement[] elements = extensions[i]
246: .getConfigurationElements();
247: for (int j = 0; j < elements.length; j++) {
248: IConfigurationElement config = elements[j];
249: processElement(config, result, shortcutsOnly);
250: }
251: }
252: return result;
253: }
254: }
|