1 11 package org.eclipse.ui.internal.registry; 12 13 import java.text.Collator ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Collections ; 17 import java.util.Comparator ; 18 import java.util.Iterator ; 19 import java.util.StringTokenizer ; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IConfigurationElement; 23 import org.eclipse.core.runtime.IExtension; 24 import org.eclipse.jface.resource.ImageDescriptor; 25 import org.eclipse.ui.internal.IWorkbenchConstants; 26 import org.eclipse.ui.internal.WorkbenchMessages; 27 import org.eclipse.ui.internal.WorkbenchPlugin; 28 import org.eclipse.ui.internal.dialogs.WizardCollectionElement; 29 import org.eclipse.ui.internal.dialogs.WorkbenchWizardElement; 30 import org.eclipse.ui.plugin.AbstractUIPlugin; 31 32 37 public class NewWizardsRegistryReader extends WizardsRegistryReader { 38 39 private boolean projectsOnly; 40 private WizardCollectionElement wizardElements = null; 41 private ArrayList deferWizards = null; 42 private ArrayList deferCategories = null; 43 private ArrayList deferPrimary; 44 45 public final static String BASE_CATEGORY = "Base"; public final static String EXAMPLES_WIZARD_CATEGORY = "Examples"; public final static String FULL_EXAMPLES_WIZARD_CATEGORY = "org.eclipse.ui.Examples"; private final static String TAG_CATEGORY = "category"; private final static String TAG_PRIMARYWIZARD = "primaryWizard"; private final static String ATT_HELP_HREF = "helpHref"; private final static String ATT_DESCRIPTION_IMAGE = "descriptionImage"; private final static String UNCATEGORIZED_WIZARD_CATEGORY = "org.eclipse.ui.Other"; private final static String UNCATEGORIZED_WIZARD_CATEGORY_LABEL = WorkbenchMessages.getString("NewWizardsRegistryReader.otherCategory"); private final static String CATEGORY_SEPARATOR = "/"; private final static String ATT_CATEGORY = "category"; private final static String ATT_PROJECT = "project"; private final static String STR_TRUE = "true"; 61 private WorkbenchWizardElement [] primaryWizards = new WorkbenchWizardElement[0]; 62 63 private class CategoryNode { 64 private Category category; 65 private String path; 66 public CategoryNode(Category cat) { 67 category = cat; 68 path = ""; String [] categoryPath = category.getParentPath(); 70 if (categoryPath != null) { 71 for (int nX = 0; nX < categoryPath.length; nX ++) { 72 path += categoryPath[nX] + '/'; } 74 } 75 path += cat.getId(); 76 } 77 public String getPath() { 78 return path; 79 } 80 public Category getCategory() { 81 return category; 82 } 83 } 84 85 private static final Comparator comparer = new Comparator () { 86 private Collator collator = Collator.getInstance(); 87 88 public int compare(Object arg0, Object arg1) { 89 String s1 = ((CategoryNode)arg0).getPath(); 90 String s2 = ((CategoryNode)arg1).getPath(); 91 return collator.compare(s1, s2); 92 } 93 }; 94 95 98 public NewWizardsRegistryReader() { 99 this(false); 100 } 101 106 public NewWizardsRegistryReader(boolean projectsOnly) { 107 super(IWorkbenchConstants.PL_NEW); 108 this.projectsOnly = projectsOnly; 109 } 110 117 protected void addNewElementToResult(WorkbenchWizardElement element, IConfigurationElement config) { 118 deferWizard(element); 119 } 120 130 protected WizardCollectionElement createCollectionElement(WizardCollectionElement parent, String id, String pluginId, String label) { 131 WizardCollectionElement newElement = new WizardCollectionElement(id, pluginId, label, parent); 132 133 parent.add(newElement); 134 return newElement; 135 } 136 140 protected void createEmptyWizardCollection() { 141 wizardElements = new WizardCollectionElement("root", null, "root", null);} 143 150 protected WorkbenchWizardElement createWizardElement(IConfigurationElement element) { 151 if (projectsOnly) { 152 String flag = element.getAttribute(ATT_PROJECT); 153 if (flag == null || !flag.equalsIgnoreCase(STR_TRUE)) 154 return null; 155 } 156 return super.createWizardElement(element); 157 } 158 161 private void deferCategory(IConfigurationElement config) { 162 Category category = null; 164 try { 165 category = new Category(config); 166 } catch (CoreException e) { 167 WorkbenchPlugin.log("Cannot create category: ", e.getStatus()); return; 169 } 170 171 if (deferCategories == null) 173 deferCategories = new ArrayList (20); 174 deferCategories.add(category); 175 } 176 179 private void deferWizard(WorkbenchWizardElement element) { 180 if (deferWizards == null) 181 deferWizards = new ArrayList (50); 182 deferWizards.add(element); 183 } 184 188 private void finishCategories() { 189 if (deferCategories == null) 191 return; 192 193 CategoryNode [] flatArray = new CategoryNode[deferCategories.size()]; 195 for (int i=0; i < deferCategories.size(); i++) { 196 flatArray[i] = new CategoryNode((Category)deferCategories.get(i)); 197 } 198 Collections.sort(Arrays.asList(flatArray), comparer); 199 200 for (int nX = 0; nX < flatArray.length; nX ++) { 202 Category cat = flatArray[nX].getCategory(); 203 finishCategory(cat); 204 } 205 206 deferCategories = null; 208 } 209 212 private void finishCategory(Category category) { 213 String [] categoryPath = category.getParentPath(); 214 WizardCollectionElement parent = wizardElements; 216 if (categoryPath != null) { 218 for (int i = 0; i < categoryPath.length; i++) { 219 WizardCollectionElement tempElement = getChildWithID(parent,categoryPath[i]); 220 if (tempElement == null) { 221 return; 225 } else 226 parent = tempElement; 227 } 228 } 229 230 Object test = getChildWithID(parent, category.getId()); 232 if (test != null) 233 return; 234 235 if (parent != null) 236 createCollectionElement(parent, category.getId(), category.getPluginId(), category.getLabel()); 237 } 238 239 242 private void finishPrimary() { 243 if (deferPrimary != null) { 244 ArrayList primary = new ArrayList (); 245 for (Iterator i = deferPrimary.iterator(); i.hasNext();) { 246 String id = (String ) i.next(); 247 WorkbenchWizardElement element = getWizardElements().findWizard(id, true); 248 if (element != null) { 249 primary.add(element); 250 } 251 } 252 253 primaryWizards = (WorkbenchWizardElement []) primary.toArray(new WorkbenchWizardElement [primary.size()]); 254 255 deferPrimary = null; 256 } 257 } 258 259 267 private void finishWizard(WorkbenchWizardElement element, IConfigurationElement config) { 268 StringTokenizer familyTokenizer = new StringTokenizer (getCategoryStringFor(config),CATEGORY_SEPARATOR); 269 270 WizardCollectionElement currentCollectionElement = wizardElements; boolean moveToOther = false; 274 275 while (familyTokenizer.hasMoreElements()) { 276 WizardCollectionElement tempCollectionElement = 277 getChildWithID(currentCollectionElement,familyTokenizer.nextToken()); 278 279 if (tempCollectionElement == null) { moveToOther = true; 281 break; 282 } 283 else 284 currentCollectionElement = tempCollectionElement; 285 } 286 287 if (moveToOther) 288 moveElementToUncategorizedCategory(wizardElements, element); 289 else 290 currentCollectionElement.add(element); 291 } 292 295 private void finishWizards() { 296 if (deferWizards != null) { 297 Iterator iter = deferWizards.iterator(); 298 while (iter.hasNext()) { 299 WorkbenchWizardElement wizard = (WorkbenchWizardElement)iter.next(); 300 IConfigurationElement config = wizard.getConfigurationElement(); 301 finishWizard(wizard, config); 302 } 303 deferWizards = null; 304 } 305 } 306 310 protected String getCategoryStringFor(IConfigurationElement config) { 311 String result = config.getAttribute(ATT_CATEGORY); 312 if (result == null) 313 result = UNCATEGORIZED_WIZARD_CATEGORY; 314 315 return result; 316 } 317 325 protected WizardCollectionElement getChildWithID(WizardCollectionElement parent, String id) { 326 Object [] children = parent.getChildren(null); 327 for (int i = 0; i < children.length; ++i) { 328 WizardCollectionElement currentChild = (WizardCollectionElement)children[i]; 329 if (currentChild.getId().equals(id)) 330 return currentChild; 331 } 332 return null; 333 } 334 337 protected void moveElementToUncategorizedCategory(WizardCollectionElement root, WorkbenchWizardElement element) { 338 WizardCollectionElement otherCategory = getChildWithID(root, UNCATEGORIZED_WIZARD_CATEGORY); 339 340 if (otherCategory == null) 341 otherCategory = createCollectionElement(root,UNCATEGORIZED_WIZARD_CATEGORY, null, UNCATEGORIZED_WIZARD_CATEGORY_LABEL); 342 343 otherCategory.add(element); 344 } 345 348 private void pruneEmptyCategories(WizardCollectionElement parent) { 349 Object [] children = parent.getChildren(null); 350 for (int nX = 0; nX < children.length; nX ++) { 351 WizardCollectionElement child = (WizardCollectionElement)children[nX]; 352 pruneEmptyCategories(child); 353 boolean shouldPrune = projectsOnly || child.getId().equals(FULL_EXAMPLES_WIZARD_CATEGORY); 354 if (child.isEmpty() && shouldPrune) 355 parent.remove(child); 356 } 357 } 358 361 public boolean readElement(IConfigurationElement element) { 362 if (element.getName().equals(TAG_CATEGORY)) { 363 deferCategory(element); 364 return true; 365 } 366 else if (element.getName().equals(TAG_PRIMARYWIZARD)) { 367 if (deferPrimary == null) 368 deferPrimary = new ArrayList (50); 369 deferPrimary.add(element.getAttribute(ATT_ID)); 370 371 return true; 372 } 373 else { 374 375 return super.readElement(element); 376 } 377 } 378 388 protected void readWizards() { 389 super.readWizards(); 390 finishCategories(); 391 finishWizards(); 392 finishPrimary(); 393 if (wizardElements != null) { 394 pruneEmptyCategories(wizardElements); 395 } 396 } 397 398 404 public WorkbenchWizardElement [] getPrimaryWizards() { 405 if (!areWizardsRead()) { 406 readWizards(); 407 } 408 return primaryWizards; 409 } 410 411 414 protected boolean areWizardsRead() { 415 return wizardElements != null; 416 } 417 423 public WizardCollectionElement getWizardElements() { 424 if (!areWizardsRead()) { 425 readWizards(); 426 } 427 return wizardElements; 428 } 429 protected Object [] getWizardCollectionElements() { 430 if (!areWizardsRead()) { 431 readWizards(); 432 } 433 return wizardElements.getChildren(); 434 } 435 438 protected boolean initializeWizard(WorkbenchWizardElement element, 439 IConfigurationElement config) { 440 boolean result = super.initializeWizard(element, config); 441 if (!result) 442 return result; 443 element.setHelpHref(config.getAttribute(ATT_HELP_HREF)); 444 String descImage = config.getAttribute(ATT_DESCRIPTION_IMAGE); 445 if (descImage != null) { 446 IExtension extension = config.getDeclaringExtension(); 447 String extendingPluginId = extension.getNamespace(); 448 ImageDescriptor image = 449 AbstractUIPlugin.imageDescriptorFromPlugin(extendingPluginId, descImage); 450 element.setDescriptionImage(image); 451 } 452 return result; 453 } 454 } 455 | Popular Tags |