1 11 package org.eclipse.pde.internal.ui.wizards.extension; 12 13 import java.util.Locale ; 14 import java.util.StringTokenizer ; 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 TAG_WIZARD = "wizard"; public static final String TAG_EDITOR_WIZARD = "editorWizard"; public static final String ATT_CATEGORY = "category"; public static final String ATT_SHORTCUTTABLE = "availableAsShortcut"; public static final String CATEGORY_SEPARATOR = "/"; public static final String TAG_CATEGORY = "category"; public static final String TAG_DESCRIPTION = "description"; 38 private final static String UNCATEGORIZED_WIZARD_CATEGORY = "org.eclipse.pde.ui.Other"; private final static String UNCATEGORIZED_WIZARD_CATEGORY_LABEL = "Other"; 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 id, String 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 name = config.getAttribute(WizardElement.ATT_NAME); 62 String id = config.getAttribute(WizardElement.ATT_ID); 63 String className = config.getAttribute(WizardElement.ATT_CLASS); 64 String 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 imageName = config.getAttribute(WizardElement.ATT_ICON); 71 if (imageName != null) { 72 String 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 name = config.getAttribute(WizardElement.ATT_NAME); 81 String id = config.getAttribute(WizardElement.ATT_ID); 82 String className = config.getAttribute(WizardElement.ATT_CLASS); 83 String 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 imageName = config.getAttribute(WizardElement.ATT_ICON); 90 if (imageName != null) { 91 String 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 getCategoryStringFor(IConfigurationElement config) { 99 String 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 id) { 107 Object [] 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 familyTokenizer = new StringTokenizer ( 121 getCategoryStringFor(element.getConfigurationElement()), 122 CATEGORY_SEPARATOR); 123 124 WizardCollectionElement currentCollectionElement = currentResult; boolean moveToOther = false; 130 131 while (familyTokenizer.hasMoreElements()) { 132 WizardCollectionElement tempCollectionElement = getChildWithID( 133 currentCollectionElement, familyTokenizer.nextToken()); 134 135 if (tempCollectionElement == null) { 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 [] categoryPath = category.getParentCategoryPath(); 171 WizardCollectionElement parent = result; 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 tag = element.getName(); 192 if (tag.equals(TAG_WIZARD) && !editorWizardMode) { 193 WizardElement wizard = createWizardElement(element); 194 if (shortcutsOnly) { 195 String shortcut = element.getAttribute(ATT_SHORTCUTTABLE); 196 if (shortcut != null && shortcut.toLowerCase(Locale.ENGLISH).equals("true")) { 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 pluginId, String pluginPointId, 216 boolean shortcutsOnly) { 217 ElementList result = (shortcutsOnly) 218 ? (new ElementList("shortcuts")) : (new WizardCollectionElement("root", "root", null)); 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 |