1 11 package org.eclipse.ui.internal.registry; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collections ; 16 import java.util.Comparator ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Set ; 20 import java.util.StringTokenizer ; 21 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IConfigurationElement; 24 import org.eclipse.core.runtime.IExtensionRegistry; 25 import org.eclipse.core.runtime.Platform; 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.internal.util.Util; 31 32 import com.ibm.icu.text.Collator; 33 34 38 public class WizardsRegistryReader extends RegistryReader { 39 40 private String pluginPoint; 41 42 private WizardCollectionElement wizardElements = null; 43 44 private ArrayList deferWizards = null; 45 46 private ArrayList deferCategories = null; 47 48 private Set deferPrimary; 49 50 54 public final static String FULL_EXAMPLES_WIZARD_CATEGORY = "org.eclipse.ui.Examples"; 58 final public static String UNCATEGORIZED_WIZARD_CATEGORY = "org.eclipse.ui.Other"; 62 final public static String GENERAL_WIZARD_CATEGORY = "org.eclipse.ui.Basic"; 64 final private static String UNCATEGORIZED_WIZARD_CATEGORY_LABEL = WorkbenchMessages.NewWizardsRegistryReader_otherCategory; 65 66 private final static String CATEGORY_SEPARATOR = "/"; 68 private WorkbenchWizardElement[] primaryWizards = new WorkbenchWizardElement[0]; 69 70 private class CategoryNode { 71 private Category category; 72 73 private String path; 74 75 CategoryNode(Category cat) { 76 category = cat; 77 path = ""; String [] categoryPath = category.getParentPath(); 79 if (categoryPath != null) { 80 for (int nX = 0; nX < categoryPath.length; nX++) { 81 path += categoryPath[nX] + '/'; 82 } 83 } 84 path += cat.getId(); 85 } 86 87 String getPath() { 88 return path; 89 } 90 91 Category getCategory() { 92 return category; 93 } 94 } 95 96 private static final Comparator comparer = new Comparator () { 97 private Collator collator = Collator.getInstance(); 98 99 public int compare(Object arg0, Object arg1) { 100 String s1 = ((CategoryNode) arg0).getPath(); 101 String s2 = ((CategoryNode) arg1).getPath(); 102 return collator.compare(s1, s2); 103 } 104 }; 105 106 private boolean readAll = true; 107 108 private String plugin; 109 110 116 public WizardsRegistryReader(String pluginId, String pluginPointId) { 117 pluginPoint = pluginPointId; 118 plugin = pluginId; 119 } 120 121 128 protected void addNewElementToResult(WorkbenchWizardElement element, 129 IConfigurationElement config) { 130 deferWizard(element); 132 } 133 134 140 private WizardCollectionElement createCollectionElement(WizardCollectionElement parent, IConfigurationElement element) { 141 WizardCollectionElement newElement = new WizardCollectionElement( 142 element, parent); 143 144 parent.add(newElement); 145 return newElement; 146 } 147 157 protected WizardCollectionElement createCollectionElement( 158 WizardCollectionElement parent, String id, String pluginId, 159 String label) { 160 WizardCollectionElement newElement = new WizardCollectionElement(id, 161 pluginId, label, parent); 162 163 parent.add(newElement); 164 return newElement; 165 } 166 167 171 protected void createEmptyWizardCollection() { 172 wizardElements = new WizardCollectionElement("root", null, "root", null); } 174 175 181 public void setInitialCollection(WizardCollectionElement wizards) { 182 wizardElements = wizards; 183 readAll = false; 184 } 185 186 189 private void deferCategory(IConfigurationElement config) { 190 Category category = null; 192 try { 193 category = new Category(config); 194 } catch (CoreException e) { 195 WorkbenchPlugin.log("Cannot create category: ", e.getStatus()); return; 197 } 198 199 if (deferCategories == null) { 201 deferCategories = new ArrayList (20); 202 } 203 deferCategories.add(category); 204 } 205 206 207 210 private void deferWizard(WorkbenchWizardElement element) { 211 if (deferWizards == null) { 212 deferWizards = new ArrayList (50); 213 } 214 deferWizards.add(element); 215 } 216 217 221 private void finishCategories() { 222 if (deferCategories == null) { 224 return; 225 } 226 227 CategoryNode[] flatArray = new CategoryNode[deferCategories.size()]; 229 for (int i = 0; i < deferCategories.size(); i++) { 230 flatArray[i] = new CategoryNode((Category) deferCategories.get(i)); 231 } 232 Collections.sort(Arrays.asList(flatArray), comparer); 233 234 for (int nX = 0; nX < flatArray.length; nX++) { 236 Category cat = flatArray[nX].getCategory(); 237 finishCategory(cat); 238 } 239 240 deferCategories = null; 242 } 243 244 247 private void finishCategory(Category category) { 248 String [] categoryPath = category.getParentPath(); 249 WizardCollectionElement parent = wizardElements; 251 if (categoryPath != null) { 253 for (int i = 0; i < categoryPath.length; i++) { 254 WizardCollectionElement tempElement = getChildWithID(parent, 255 categoryPath[i]); 256 if (tempElement == null) { 257 return; 261 } 262 parent = tempElement; 263 } 264 } 265 266 Object test = getChildWithID(parent, category.getId()); 268 if (test != null) { 269 return; 270 } 271 272 if (parent != null) { 273 createCollectionElement(parent, (IConfigurationElement) Util.getAdapter(category, 274 IConfigurationElement.class)); 275 } 276 } 277 278 279 282 private void finishPrimary() { 283 if (deferPrimary != null) { 284 ArrayList primary = new ArrayList (); 285 for (Iterator i = deferPrimary.iterator(); i.hasNext();) { 286 String id = (String ) i.next(); 287 WorkbenchWizardElement element = getWizardElements() 288 .findWizard(id, true); 289 if (element != null) { 290 primary.add(element); 291 } 292 } 293 294 primaryWizards = (WorkbenchWizardElement[]) primary 295 .toArray(new WorkbenchWizardElement[primary.size()]); 296 297 deferPrimary = null; 298 } 299 } 300 301 302 309 private void finishWizard(WorkbenchWizardElement element, 310 IConfigurationElement config) { 311 StringTokenizer familyTokenizer = new StringTokenizer ( 312 getCategoryStringFor(config), CATEGORY_SEPARATOR); 313 314 WizardCollectionElement currentCollectionElement = wizardElements; boolean moveToOther = false; 318 319 while (familyTokenizer.hasMoreElements()) { 320 WizardCollectionElement tempCollectionElement = getChildWithID( 321 currentCollectionElement, familyTokenizer.nextToken()); 322 323 if (tempCollectionElement == null) { moveToOther = true; 325 break; 326 } 327 currentCollectionElement = tempCollectionElement; 328 } 329 330 if (moveToOther) { 331 moveElementToUncategorizedCategory(wizardElements, element); 332 } else { 333 currentCollectionElement.add(element); 334 element.setParent(currentCollectionElement); 335 } 336 } 337 338 341 private void finishWizards() { 342 if (deferWizards != null) { 343 Iterator iter = deferWizards.iterator(); 344 while (iter.hasNext()) { 345 WorkbenchWizardElement wizard = (WorkbenchWizardElement) iter 346 .next(); 347 IConfigurationElement config = wizard.getConfigurationElement(); 348 finishWizard(wizard, config); 349 } 350 deferWizards = null; 351 } 352 } 353 354 358 protected String getCategoryStringFor(IConfigurationElement config) { 359 String result = config.getAttribute(IWorkbenchRegistryConstants.TAG_CATEGORY); 360 if (result == null) { 361 result = UNCATEGORIZED_WIZARD_CATEGORY; 362 } 363 364 return result; 365 } 366 367 375 protected WizardCollectionElement getChildWithID( 376 WizardCollectionElement parent, String id) { 377 Object [] children = parent.getChildren(null); 378 for (int i = 0; i < children.length; ++i) { 379 WizardCollectionElement currentChild = (WizardCollectionElement) children[i]; 380 if (currentChild.getId().equals(id)) { 381 return currentChild; 382 } 383 } 384 return null; 385 } 386 387 390 protected void moveElementToUncategorizedCategory( 391 WizardCollectionElement root, WorkbenchWizardElement element) { 392 WizardCollectionElement otherCategory = getChildWithID(root, 393 UNCATEGORIZED_WIZARD_CATEGORY); 394 395 if (otherCategory == null) { 396 otherCategory = createCollectionElement(root, 397 UNCATEGORIZED_WIZARD_CATEGORY, null, 398 UNCATEGORIZED_WIZARD_CATEGORY_LABEL); 399 } 400 401 otherCategory.add(element); 402 element.setParent(otherCategory); 403 } 404 405 408 private void pruneEmptyCategories(WizardCollectionElement parent) { 409 Object [] children = parent.getChildren(null); 410 for (int nX = 0; nX < children.length; nX++) { 411 WizardCollectionElement child = (WizardCollectionElement) children[nX]; 412 pruneEmptyCategories(child); 413 boolean shouldPrune = child.getId().equals(FULL_EXAMPLES_WIZARD_CATEGORY); 414 if (child.isEmpty() && shouldPrune) { 415 parent.remove(child); 416 } 417 } 418 } 419 420 423 public boolean readElement(IConfigurationElement element) { 424 if (element.getName().equals(IWorkbenchRegistryConstants.TAG_CATEGORY)) { 425 deferCategory(element); 426 return true; 427 } else if (element.getName().equals(IWorkbenchRegistryConstants.TAG_PRIMARYWIZARD)) { 428 if (deferPrimary == null) { 429 deferPrimary = new HashSet (); 430 } 431 deferPrimary.add(element.getAttribute(IWorkbenchRegistryConstants.ATT_ID)); 432 433 return true; 434 } else { 435 if (!element.getName().equals(IWorkbenchRegistryConstants.TAG_WIZARD)) { 436 return false; 437 } 438 WorkbenchWizardElement wizard = createWizardElement(element); 439 if (wizard != null) { 440 addNewElementToResult(wizard, element); 441 } 442 return true; 443 } 444 } 445 446 456 protected void readWizards() { 457 if (readAll) { 458 if (!areWizardsRead()) { 459 createEmptyWizardCollection(); 460 IExtensionRegistry registry = Platform.getExtensionRegistry(); 461 readRegistry(registry, plugin, pluginPoint); 462 } 463 } 464 finishCategories(); 465 finishWizards(); 466 finishPrimary(); 467 if (wizardElements != null) { 468 pruneEmptyCategories(wizardElements); 469 } 470 } 471 472 480 public WorkbenchWizardElement [] getPrimaryWizards() { 481 if (!areWizardsRead()) { 482 readWizards(); 483 } 484 return primaryWizards; 485 } 486 487 488 491 protected boolean areWizardsRead() { 492 return wizardElements != null && readAll; 493 } 494 495 503 public WizardCollectionElement getWizardElements() { 504 if (!areWizardsRead()) { 505 readWizards(); 506 } 507 return wizardElements; 508 } 509 510 protected Object [] getWizardCollectionElements() { 511 if (!areWizardsRead()) { 512 readWizards(); 513 } 514 return wizardElements.getChildren(); 515 } 516 517 524 protected WorkbenchWizardElement createWizardElement( 525 IConfigurationElement element) { 526 if (element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME) == null) { 528 logMissingAttribute(element, IWorkbenchRegistryConstants.ATT_NAME); 529 return null; 530 } 531 532 if (getClassValue(element, IWorkbenchRegistryConstants.ATT_CLASS) == null) { 533 logMissingAttribute(element, IWorkbenchRegistryConstants.ATT_CLASS); 534 return null; 535 } 536 return new WorkbenchWizardElement(element); 537 } 538 539 545 public WorkbenchWizardElement findWizard(String id) { 546 Object [] wizards = getWizardCollectionElements(); 547 for (int nX = 0; nX < wizards.length; nX++) { 548 WizardCollectionElement collection = (WizardCollectionElement) wizards[nX]; 549 WorkbenchWizardElement element = collection.findWizard(id, true); 550 if (element != null) { 551 return element; 552 } 553 } 554 return null; 555 } 556 } 557 | Popular Tags |