1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashMap ; 16 import java.util.Hashtable ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.Path; 23 import org.eclipse.jface.dialogs.Dialog; 24 import org.eclipse.jface.dialogs.IDialogSettings; 25 import org.eclipse.jface.resource.ImageDescriptor; 26 import org.eclipse.jface.viewers.DoubleClickEvent; 27 import org.eclipse.jface.viewers.IDoubleClickListener; 28 import org.eclipse.jface.viewers.ISelectionChangedListener; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.jface.viewers.SelectionChangedEvent; 31 import org.eclipse.jface.viewers.StructuredSelection; 32 import org.eclipse.jface.viewers.TreeViewer; 33 import org.eclipse.jface.viewers.ViewerFilter; 34 import org.eclipse.jface.wizard.IWizardContainer; 35 import org.eclipse.jface.wizard.IWizardContainer2; 36 import org.eclipse.swt.SWT; 37 import org.eclipse.swt.custom.CLabel; 38 import org.eclipse.swt.events.DisposeEvent; 39 import org.eclipse.swt.events.DisposeListener; 40 import org.eclipse.swt.events.SelectionAdapter; 41 import org.eclipse.swt.events.SelectionEvent; 42 import org.eclipse.swt.graphics.Font; 43 import org.eclipse.swt.graphics.Image; 44 import org.eclipse.swt.layout.GridData; 45 import org.eclipse.swt.layout.GridLayout; 46 import org.eclipse.swt.widgets.Button; 47 import org.eclipse.swt.widgets.Composite; 48 import org.eclipse.swt.widgets.Control; 49 import org.eclipse.swt.widgets.Label; 50 import org.eclipse.ui.IWorkbenchWizard; 51 import org.eclipse.ui.activities.WorkbenchActivityHelper; 52 import org.eclipse.ui.dialogs.FilteredTree; 53 import org.eclipse.ui.internal.WorkbenchMessages; 54 import org.eclipse.ui.model.AdaptableList; 55 import org.eclipse.ui.model.WorkbenchLabelProvider; 56 import org.eclipse.ui.wizards.IWizardCategory; 57 import org.eclipse.ui.wizards.IWizardDescriptor; 58 59 63 class NewWizardNewPage implements ISelectionChangedListener { 64 65 private static final String DIALOG_SETTING_SECTION_NAME = "NewWizardSelectionPage."; 68 private final static int SIZING_LISTS_HEIGHT = 200; 69 70 private final static int SIZING_VIEWER_WIDTH = 300; 71 72 private final static String STORE_EXPANDED_CATEGORIES_ID = DIALOG_SETTING_SECTION_NAME 73 + "STORE_EXPANDED_CATEGORIES_ID"; 75 private final static String STORE_SELECTED_ID = DIALOG_SETTING_SECTION_NAME 76 + "STORE_SELECTED_ID"; 78 private NewWizardSelectionPage page; 79 80 private FilteredTree filteredTree; 81 82 private WizardPatternFilter filteredTreeFilter; 83 84 private Hashtable selectedWizards = new Hashtable (); 86 87 private IDialogSettings settings; 88 89 private Button showAllCheck; 90 91 private IWizardCategory wizardCategories; 92 93 private IWizardDescriptor [] primaryWizards; 94 95 private CLabel descImageCanvas; 96 97 private Map imageTable = new HashMap (); 98 99 private IWizardDescriptor selectedElement; 100 101 private WizardActivityFilter filter = new WizardActivityFilter(); 102 103 private boolean needShowAll; 104 105 private boolean projectsOnly; 106 107 private ViewerFilter projectFilter = new WizardTagFilter(new String [] {WorkbenchWizardElement.TAG_PROJECT}); 108 109 116 public NewWizardNewPage(NewWizardSelectionPage mainPage, 117 IWizardCategory wizardCategories, 118 IWizardDescriptor[] primaryWizards, boolean projectsOnly) { 119 this.page = mainPage; 120 this.wizardCategories = wizardCategories; 121 this.primaryWizards = primaryWizards; 122 this.projectsOnly = projectsOnly; 123 124 trimPrimaryWizards(); 125 126 if (this.primaryWizards.length > 0) { 127 if (allPrimary(wizardCategories)) { 128 this.wizardCategories = null; needShowAll = false; 130 } else { 131 needShowAll = !allActivityEnabled(wizardCategories); 132 } 133 } else { 134 needShowAll = !allActivityEnabled(wizardCategories); 135 } 136 } 137 138 142 private boolean allActivityEnabled(IWizardCategory category) { 143 IWizardDescriptor [] wizards = category.getWizards(); 144 for (int i = 0; i < wizards.length; i++) { 145 IWizardDescriptor wizard = wizards[i]; 146 if (WorkbenchActivityHelper.filterItem(wizard)) { 147 return false; 148 } 149 } 150 151 IWizardCategory [] children = category.getCategories(); 152 for (int i = 0; i < children.length; i++) { 153 if (!allActivityEnabled(children[i])) { 154 return false; 155 } 156 } 157 158 return true; 159 } 160 161 164 private void trimPrimaryWizards() { 165 ArrayList newPrimaryWizards = new ArrayList (primaryWizards.length); 166 167 if (wizardCategories == null) { 168 return; } 170 171 for (int i = 0; i < primaryWizards.length; i++) { 172 if (wizardCategories.findWizard(primaryWizards[i].getId()) != null) { 173 newPrimaryWizards.add(primaryWizards[i]); 174 } 175 } 176 177 primaryWizards = (WorkbenchWizardElement[]) newPrimaryWizards 178 .toArray(new WorkbenchWizardElement[newPrimaryWizards.size()]); 179 } 180 181 185 private boolean allPrimary(IWizardCategory category) { 186 IWizardDescriptor [] wizards = category.getWizards(); 187 for (int i = 0; i < wizards.length; i++) { 188 IWizardDescriptor wizard = wizards[i]; 189 if (!isPrimary(wizard)) { 190 return false; 191 } 192 } 193 194 IWizardCategory [] children = category.getCategories(); 195 for (int i = 0; i < children.length; i++) { 196 if (!allPrimary(children[i])) { 197 return false; 198 } 199 } 200 201 return true; 202 } 203 204 208 private boolean isPrimary(IWizardDescriptor wizard) { 209 for (int j = 0; j < primaryWizards.length; j++) { 210 if (primaryWizards[j].equals(wizard)) { 211 return true; 212 } 213 } 214 215 return false; 216 } 217 218 221 public void activate() { 222 page.setDescription(WorkbenchMessages.NewWizardNewPage_description); 223 } 224 225 231 protected Control createControl(Composite parent) { 232 233 Font wizardFont = parent.getFont(); 234 Composite outerContainer = new Composite(parent, SWT.NONE); 236 GridLayout layout = new GridLayout(); 237 outerContainer.setLayout(layout); 238 239 Label wizardLabel = new Label(outerContainer, SWT.NONE); 240 GridData data = new GridData(SWT.BEGINNING, SWT.FILL, false, true); 241 outerContainer.setLayoutData(data); 242 wizardLabel.setFont(wizardFont); 243 wizardLabel.setText(WorkbenchMessages.NewWizardNewPage_wizardsLabel); 244 245 Composite innerContainer = new Composite(outerContainer, SWT.NONE); 246 layout = new GridLayout(2, false); 247 layout.marginHeight = 0; 248 layout.marginWidth = 0; 249 innerContainer.setLayout(layout); 250 innerContainer.setFont(wizardFont); 251 data = new GridData(SWT.FILL, SWT.FILL, true, true); 252 innerContainer.setLayoutData(data); 253 254 filteredTree = createFilteredTree(innerContainer); 255 createOptionsButtons(innerContainer); 256 257 createImage(innerContainer); 258 259 updateDescription(null); 260 261 restoreWidgetValues(); 264 265 return outerContainer; 266 } 267 268 274 protected FilteredTree createFilteredTree(Composite parent){ 275 Composite composite = new Composite(parent, SWT.NONE); 276 GridLayout layout = new GridLayout(); 277 layout.marginHeight = 0; 278 layout.marginWidth = 0; 279 composite.setLayout(layout); 280 281 GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); 282 data.widthHint = SIZING_VIEWER_WIDTH; 283 data.horizontalSpan = 2; 284 data.grabExcessHorizontalSpace = true; 285 data.grabExcessVerticalSpace = true; 286 287 boolean needsHint = DialogUtil.inRegularFontMode(parent); 288 289 if (needsHint) { 291 data.heightHint = SIZING_LISTS_HEIGHT; 292 } 293 composite.setLayoutData(data); 294 295 filteredTreeFilter = new WizardPatternFilter(); 296 FilteredTree filterTree = new FilteredTree(composite, 297 SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER, filteredTreeFilter); 298 299 final TreeViewer treeViewer = filterTree.getViewer(); 300 treeViewer.setContentProvider(new WizardContentProvider()); 301 treeViewer.setLabelProvider(new WorkbenchLabelProvider()); 302 treeViewer.setComparator(NewWizardCollectionComparator.INSTANCE); 303 treeViewer.addSelectionChangedListener(this); 304 305 ArrayList inputArray = new ArrayList (); 306 307 for (int i = 0; i < primaryWizards.length; i++) { 308 inputArray.add(primaryWizards[i]); 309 } 310 311 boolean expandTop = false; 312 313 if (wizardCategories != null) { 314 if (wizardCategories.getParent() == null) { 315 IWizardCategory [] children = wizardCategories.getCategories(); 316 for (int i = 0; i < children.length; i++) { 317 inputArray.add(children[i]); 318 } 319 } else { 320 expandTop = true; 321 inputArray.add(wizardCategories); 322 } 323 } 324 325 if (expandTop) { 327 treeViewer.setAutoExpandLevel(2); 328 } 329 330 AdaptableList input = new AdaptableList(inputArray); 331 332 treeViewer.setInput(input); 333 334 filterTree.setBackground(parent.getDisplay().getSystemColor( 335 SWT.COLOR_WIDGET_BACKGROUND)); 336 337 treeViewer.getTree().setFont(parent.getFont()); 338 339 treeViewer.addDoubleClickListener(new IDoubleClickListener() { 340 345 public void doubleClick(DoubleClickEvent event) { 346 IStructuredSelection s = (IStructuredSelection) event 347 .getSelection(); 348 selectionChanged(new SelectionChangedEvent(event.getViewer(), s)); 349 350 Object element = s.getFirstElement(); 351 if (treeViewer.isExpandable(element)) { 352 treeViewer.setExpandedState(element, !treeViewer 353 .getExpandedState(element)); 354 } else if (element instanceof WorkbenchWizardElement) { 355 page.advanceToNextPageOrFinish(); 356 } 357 } 358 }); 359 360 treeViewer.addFilter(filter); 361 362 if (projectsOnly) { 363 treeViewer.addFilter(projectFilter); 364 } 365 366 Dialog.applyDialogFont(filterTree); 367 return filterTree; 368 } 369 370 375 private void createOptionsButtons(Composite parent){ 376 if (needShowAll) { 377 showAllCheck = new Button(parent, SWT.CHECK); 378 GridData data = new GridData(); 379 showAllCheck.setLayoutData(data); 380 showAllCheck.setFont(parent.getFont()); 381 showAllCheck.setText(WorkbenchMessages.NewWizardNewPage_showAll); 382 showAllCheck.setSelection(false); 383 384 showAllCheck.addSelectionListener(new SelectionAdapter() { 386 387 private Object [] delta = new Object [0]; 390 391 public void widgetSelected(SelectionEvent e) { 392 boolean showAll = showAllCheck.getSelection(); 393 394 if (showAll) { 395 filteredTree.getViewer().getControl().setRedraw(false); 396 } else { 397 delta = filteredTree.getViewer().getExpandedElements(); 402 } 403 404 try { 405 if (showAll) { 406 filteredTree.getViewer().resetFilters(); 407 filteredTree.getViewer().addFilter(filteredTreeFilter); 408 if (projectsOnly) { 409 filteredTree.getViewer().addFilter(projectFilter); 410 } 411 412 Object [] currentExpanded = filteredTree.getViewer() 416 .getExpandedElements(); 417 Object [] expanded = new Object [delta.length 418 + currentExpanded.length]; 419 System.arraycopy(currentExpanded, 0, expanded, 0, 420 currentExpanded.length); 421 System.arraycopy(delta, 0, expanded, 422 currentExpanded.length, delta.length); 423 filteredTree.getViewer().setExpandedElements(expanded); 424 } else { 425 filteredTree.getViewer().addFilter(filter); 426 if (projectsOnly) { 427 filteredTree.getViewer().addFilter(projectFilter); 428 } 429 } 430 filteredTree.getViewer().refresh(false); 431 432 if (!showAll) { 433 Object [] newExpanded = filteredTree.getViewer().getExpandedElements(); 438 List deltaList = new ArrayList (Arrays.asList(delta)); 439 deltaList.removeAll(Arrays.asList(newExpanded)); 440 } 441 } finally { 442 if (showAll) { 443 filteredTree.getViewer().getControl().setRedraw(true); 444 } 445 } 446 } 447 }); 448 } 449 } 450 451 457 private void createImage(Composite parent) { 458 descImageCanvas = new CLabel(parent, SWT.NONE); 459 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING 460 | GridData.VERTICAL_ALIGN_BEGINNING); 461 data.widthHint = 0; 462 data.heightHint = 0; 463 descImageCanvas.setLayoutData(data); 464 465 descImageCanvas.addDisposeListener(new DisposeListener() { 467 468 471 public void widgetDisposed(DisposeEvent e) { 472 for (Iterator i = imageTable.values().iterator(); i.hasNext();) { 473 ((Image) i.next()).dispose(); 474 } 475 imageTable.clear(); 476 } 477 }); 478 } 479 480 485 protected void expandPreviouslyExpandedCategories() { 486 String [] expandedCategoryPaths = settings 487 .getArray(STORE_EXPANDED_CATEGORIES_ID); 488 if (expandedCategoryPaths == null || expandedCategoryPaths.length == 0) { 489 return; 490 } 491 492 List categoriesToExpand = new ArrayList (expandedCategoryPaths.length); 493 494 if (wizardCategories != null) { 495 for (int i = 0; i < expandedCategoryPaths.length; i++) { 496 IWizardCategory category = wizardCategories 497 .findCategory(new Path(expandedCategoryPaths[i])); 498 if (category != null) { 499 categoriesToExpand.add(category); 500 } 501 } 502 } 503 504 if (!categoriesToExpand.isEmpty()) { 505 filteredTree.getViewer().setExpandedElements(categoriesToExpand.toArray()); 506 } 507 508 } 509 510 515 protected Object getSingleSelection(IStructuredSelection selection) { 516 return selection.size() == 1 ? selection.getFirstElement() : null; 517 } 518 519 524 protected void restoreWidgetValues() { 525 expandPreviouslyExpandedCategories(); 526 selectPreviouslySelected(); 527 } 528 529 534 public void saveWidgetValues() { 535 storeExpandedCategories(); 536 storeSelectedCategoryAndWizard(); 537 } 538 539 545 public void selectionChanged(SelectionChangedEvent selectionEvent) { 546 page.setErrorMessage(null); 547 page.setMessage(null); 548 549 Object selectedObject = getSingleSelection((IStructuredSelection) selectionEvent 550 .getSelection()); 551 552 if (selectedObject instanceof IWizardDescriptor) { 553 if (selectedObject == selectedElement) { 554 return; 555 } 556 updateWizardSelection((IWizardDescriptor) selectedObject); 557 } else { 558 selectedElement = null; 559 page.setHasPages(false); 560 page.setCanFinishEarly(false); 561 page.selectWizardNode(null); 562 updateDescription(null); 563 } 564 } 565 566 571 protected void selectPreviouslySelected() { 572 String selectedId = settings.get(STORE_SELECTED_ID); 573 if (selectedId == null) { 574 return; 575 } 576 577 if (wizardCategories == null) { 578 return; 579 } 580 581 Object selected = wizardCategories.findCategory(new Path( 582 selectedId)); 583 584 if (selected == null) { 585 selected = wizardCategories.findWizard(selectedId); 586 587 if (selected == null) { 588 return; 590 } 591 } 592 593 final StructuredSelection selection = new StructuredSelection(selected); 595 filteredTree.getViewer().getControl().getDisplay().asyncExec(new Runnable () { 596 public void run() { 597 filteredTree.getViewer().setSelection(selection, true); 598 } 599 }); 600 } 601 602 607 public void setDialogSettings(IDialogSettings settings) { 608 this.settings = settings; 609 } 610 611 616 protected void storeExpandedCategories() { 617 Object [] expandedElements = filteredTree.getViewer().getExpandedElements(); 618 List expandedElementPaths = new ArrayList (expandedElements.length); 619 for (int i = 0; i < expandedElements.length; ++i) { 620 if (expandedElements[i] instanceof IWizardCategory) { 621 expandedElementPaths 622 .add(((IWizardCategory) expandedElements[i]) 623 .getPath().toString()); 624 } 625 } 626 settings.put(STORE_EXPANDED_CATEGORIES_ID, 627 (String []) expandedElementPaths 628 .toArray(new String [expandedElementPaths.size()])); 629 } 630 631 635 protected void storeSelectedCategoryAndWizard() { 636 Object selected = getSingleSelection((IStructuredSelection) filteredTree 637 .getViewer().getSelection()); 638 639 if (selected != null) { 640 if (selected instanceof IWizardCategory) { 641 settings.put(STORE_SELECTED_ID, 642 ((IWizardCategory) selected).getPath() 643 .toString()); 644 } else { 645 settings.put(STORE_SELECTED_ID, 647 ((IWizardDescriptor) selected).getId()); 648 } 649 } 650 } 651 652 658 private void updateDescription(IWizardDescriptor selectedObject) { 659 String string = ""; if (selectedObject != null) { 661 string = selectedObject.getDescription(); 662 } 663 664 page.setDescription(string); 665 666 if (hasImage(selectedObject)) { 667 ImageDescriptor descriptor = null; 668 if (selectedObject != null) { 669 descriptor = selectedObject.getDescriptionImage(); 670 } 671 672 if (descriptor != null) { 673 GridData data = (GridData)descImageCanvas.getLayoutData(); 674 data.widthHint = SWT.DEFAULT; 675 data.heightHint = SWT.DEFAULT; 676 Image image = (Image) imageTable.get(descriptor); 677 if (image == null) { 678 image = descriptor.createImage(false); 679 imageTable.put(descriptor, image); 680 } 681 descImageCanvas.setImage(image); 682 } 683 } else { 684 GridData data = (GridData)descImageCanvas.getLayoutData(); 685 data.widthHint = 0; 686 data.heightHint = 0; 687 descImageCanvas.setImage(null); 688 } 689 690 descImageCanvas.getParent().layout(true); 691 692 IWizardContainer container = page.getWizard().getContainer(); 693 if (container instanceof IWizardContainer2) { 694 ((IWizardContainer2) container).updateSize(); 695 } 696 } 697 698 704 private boolean hasImage(IWizardDescriptor selectedObject) { 705 if (selectedObject == null) { 706 return false; 707 } 708 709 if (selectedObject.getDescriptionImage() != null) { 710 return true; 711 } 712 713 return false; 714 } 715 716 719 private void updateWizardSelection(IWizardDescriptor selectedObject) { 720 selectedElement = selectedObject; 721 WorkbenchWizardNode selectedNode; 722 if (selectedWizards.containsKey(selectedObject)) { 723 selectedNode = (WorkbenchWizardNode) selectedWizards 724 .get(selectedObject); 725 } else { 726 selectedNode = new WorkbenchWizardNode(page, selectedObject) { 727 public IWorkbenchWizard createWizard() throws CoreException { 728 return wizardElement.createWizard(); 729 } 730 }; 731 selectedWizards.put(selectedObject, selectedNode); 732 } 733 734 page.setCanFinishEarly(selectedObject.canFinishEarly()); 735 page.setHasPages(selectedObject.hasPages()); 736 page.selectWizardNode(selectedNode); 737 738 updateDescription(selectedObject); 739 } 740 } 741 | Popular Tags |