1 11 package org.eclipse.debug.internal.ui.preferences; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.debug.core.DebugPlugin; 23 import org.eclipse.debug.core.ILaunchConfigurationType; 24 import org.eclipse.debug.core.ILaunchDelegate; 25 import org.eclipse.debug.internal.core.LaunchDelegate; 26 import org.eclipse.debug.internal.core.LaunchManager; 27 import org.eclipse.debug.internal.ui.DebugUIPlugin; 28 import org.eclipse.debug.internal.ui.IDebugHelpContextIds; 29 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 30 import org.eclipse.debug.internal.ui.SWTFactory; 31 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationPresentationManager; 32 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchGroupExtension; 33 import org.eclipse.debug.internal.ui.launchConfigurations.MultiLaunchGroupFilter; 34 import org.eclipse.debug.internal.ui.launchConfigurations.PerspectiveManager; 35 import org.eclipse.debug.ui.DebugUITools; 36 import org.eclipse.debug.ui.IDebugUIConstants; 37 import org.eclipse.jface.dialogs.Dialog; 38 import org.eclipse.jface.dialogs.MessageDialogWithToggle; 39 import org.eclipse.jface.preference.PreferencePage; 40 import org.eclipse.jface.preference.RadioGroupFieldEditor; 41 import org.eclipse.jface.viewers.ISelectionChangedListener; 42 import org.eclipse.jface.viewers.IStructuredSelection; 43 import org.eclipse.jface.viewers.ITreeContentProvider; 44 import org.eclipse.jface.viewers.SelectionChangedEvent; 45 import org.eclipse.jface.viewers.StructuredSelection; 46 import org.eclipse.jface.viewers.TreeViewer; 47 import org.eclipse.jface.viewers.Viewer; 48 import org.eclipse.swt.SWT; 49 import org.eclipse.swt.events.SelectionEvent; 50 import org.eclipse.swt.events.SelectionListener; 51 import org.eclipse.swt.graphics.Point; 52 import org.eclipse.swt.layout.GridData; 53 import org.eclipse.swt.widgets.Combo; 54 import org.eclipse.swt.widgets.Composite; 55 import org.eclipse.swt.widgets.Control; 56 import org.eclipse.swt.widgets.Label; 57 import org.eclipse.swt.widgets.Tree; 58 import org.eclipse.swt.widgets.TreeItem; 59 import org.eclipse.ui.IPerspectiveDescriptor; 60 import org.eclipse.ui.IPerspectiveRegistry; 61 import org.eclipse.ui.IWorkbench; 62 import org.eclipse.ui.IWorkbenchPreferencePage; 63 import org.eclipse.ui.PlatformUI; 64 import org.eclipse.ui.activities.ActivityManagerEvent; 65 import org.eclipse.ui.activities.IActivityManagerListener; 66 import org.eclipse.ui.activities.WorkbenchActivityHelper; 67 import org.eclipse.ui.model.WorkbenchViewerComparator; 68 69 74 public class LaunchPerspectivePreferencePage extends PreferencePage implements IWorkbenchPreferencePage, IActivityManagerListener { 75 76 79 final class PerspectiveChange { 80 private ILaunchConfigurationType fType = null; 81 private ILaunchDelegate fDelegate = null; 82 private Set fModes = null; 83 private String fPid = null; 84 85 public PerspectiveChange(ILaunchConfigurationType type, ILaunchDelegate delegate, Set modes, String perspectiveid) { 86 fType = type; 87 fDelegate = delegate; 88 fModes = modes; 89 fPid = perspectiveid; 90 } 91 92 public ILaunchConfigurationType getType() {return fType;} 93 public ILaunchDelegate getDelegate() {return fDelegate;} 94 public String getPerspectiveId() {return fPid;} 95 public Set getModes() {return fModes;} 96 public boolean equals(Object o) { 97 if(o instanceof PerspectiveChange) { 98 PerspectiveChange change = (PerspectiveChange) o; 99 return change.getDelegate() == fDelegate && 100 change.getType().equals(fType) && 101 change.getModes().equals(fModes); 102 } 103 return super.equals(o); 104 } 105 } 106 107 110 final class PerspectivesTreeViewer extends TreeViewer { 111 public PerspectivesTreeViewer(Tree tree) { 112 super(tree); 113 } 114 public Object [] getFilteredChildren(Object o) {return super.getFilteredChildren(o);} 115 } 116 117 120 final class PerspectiveContentProvider implements ITreeContentProvider { 121 public Object [] getChildren(Object parentElement) { 122 if(parentElement instanceof ILaunchConfigurationType) { 123 ILaunchConfigurationType type = (ILaunchConfigurationType) parentElement; 124 return ((LaunchManager)DebugPlugin.getDefault().getLaunchManager()).getLaunchDelegates(type.getIdentifier()); 125 } 126 return new Object [0]; 127 } 128 public Object [] getElements(Object inputElement) { 129 return DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationTypes(); 130 } 131 public boolean hasChildren(Object element) {return element instanceof ILaunchConfigurationType;} 132 public Object getParent(Object element) {return null;} 133 public void dispose() {} 134 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {} 135 } 136 137 140 class PerspectivesPanel { 141 142 private Composite fMainComposite = null; 143 private Label fMessage = null; 144 145 public PerspectivesPanel(Composite parent, String heading) { 146 createPanel(parent, heading); 147 } 148 149 protected void createPanel(Composite parent, String heading) { 150 fMainComposite = SWTFactory.createComposite(parent, 2, 1, GridData.FILL_BOTH); 151 SWTFactory.createWrapLabel(fMainComposite, heading, 2); 152 fMessage = SWTFactory.createWrapLabel(fMainComposite, "", 2, 250); } 154 155 public void setMessage(String msg) { 156 fMessage.setText((msg == null ? "" : msg)); } 158 159 public void refreshPanel(IStructuredSelection selection) { 160 Control[] children = fMainComposite.getChildren(); 162 for(int i = 2; i < children.length; i++) { 163 children[i].dispose(); 164 } 165 if(fgCurrentWorkingContext == null) { 166 fgCurrentWorkingContext = new HashSet (); 167 } 168 fgCurrentWorkingContext.clear(); 169 if(!selection.isEmpty()) { 170 Point pt = getShell().getSize(); 171 createCombos(fMainComposite, selection.toArray()); 172 fMainComposite.layout(); 173 if(!fInitializing) { 174 Point pt2 = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 175 if(pt2.x > pt.x) { 176 getShell().setSize(pt2); 177 } 178 } 179 } 180 else { 181 SWTFactory.createWrapLabel(fMainComposite, DebugPreferencesMessages.LaunchPerspectivePreferencePage_0, 2, 275); 182 } 183 fMainComposite.layout(); 184 } 185 } 186 187 190 private RadioGroupFieldEditor fSwitchLaunch = null; 191 private RadioGroupFieldEditor fSwitchSuspend = null; 192 private Tree fTree = null; 193 private PerspectivesTreeViewer fTreeViewer = null; 194 private PerspectivesPanel fPerspectivesPanel = null; 195 196 199 private static String [] fgPerspectiveLabels = null; 200 private static Map fgPerspectiveIdMap = null; 201 private static HashSet fgChangeSet = null; 202 private static HashSet fgCurrentWorkingContext = null; 203 204 207 private boolean fInitializing = false; 208 209 212 private SelectionListener fSelectionListener = new SelectionListener() { 213 public void widgetDefaultSelected(SelectionEvent e) {} 214 public void widgetSelected(SelectionEvent e) { 215 Object o = e.getSource(); 216 if(o instanceof Combo) { 217 Combo combo = (Combo) o; 218 LaunchDelegate delegate = null; 219 ILaunchConfigurationType type = null; 220 PerspectiveChange change = null; 221 Set modes = null; 222 for(Iterator iter = fgCurrentWorkingContext.iterator(); iter.hasNext();) { 223 o = iter.next(); 224 if(o instanceof ILaunchDelegate) { 225 delegate = (LaunchDelegate) o; 226 type = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(delegate.getLaunchConfigurationTypeId()); 227 } 228 else if(o instanceof ILaunchConfigurationType) { 229 delegate = null; 230 type = (ILaunchConfigurationType) o; 231 } 232 modes = (Set ) combo.getData(); 233 change = findChange(type, delegate, modes); 234 if(change == null) { 235 change = new PerspectiveChange(type, delegate, modes, (String )fgPerspectiveIdMap.get(combo.getText())); 236 fgChangeSet.add(change); 237 } 238 else { 239 change.fPid = (String )fgPerspectiveIdMap.get(combo.getText()); 240 } 241 } 242 } 243 } 244 }; 245 246 249 public LaunchPerspectivePreferencePage() {} 250 251 254 public void dispose() { 255 PlatformUI.getWorkbench().getActivitySupport().getActivityManager().removeActivityManagerListener(this); 256 fgPerspectiveIdMap.clear(); 257 fgPerspectiveIdMap = null; 258 fgPerspectiveLabels = null; 259 fgChangeSet.clear(); 260 fgChangeSet = null; 261 if(fgCurrentWorkingContext != null) { 262 fgCurrentWorkingContext.clear(); 263 fgCurrentWorkingContext = null; 264 } 265 super.dispose(); 266 } 267 268 271 public void createControl(Composite parent) { 272 super.createControl(parent); 273 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IDebugHelpContextIds.PERSPECTIVE_PREFERENCE_PAGE); 274 } 275 276 279 protected Control createContents(Composite parent) { 280 281 SWTFactory.createWrapLabel(parent, DebugPreferencesMessages.PerspectivePreferencePage_0, 2, 300); 282 283 SWTFactory.createVerticalSpacer(parent, 1); 284 285 fSwitchLaunch = new RadioGroupFieldEditor( 286 IInternalDebugUIConstants.PREF_SWITCH_TO_PERSPECTIVE, 287 DebugPreferencesMessages.LaunchingPreferencePage_11, 3, 288 new String [][] {{DebugPreferencesMessages.LaunchingPreferencePage_12, MessageDialogWithToggle.ALWAYS }, 289 { DebugPreferencesMessages.LaunchingPreferencePage_13, MessageDialogWithToggle.NEVER }, 290 { DebugPreferencesMessages.LaunchingPreferencePage_14, MessageDialogWithToggle.PROMPT } }, 291 SWTFactory.createComposite(parent, 1, 2, GridData.FILL_HORIZONTAL), 292 true); 293 fSwitchLaunch.setPreferenceName(IInternalDebugUIConstants.PREF_SWITCH_TO_PERSPECTIVE); 294 fSwitchLaunch.setPreferenceStore(getPreferenceStore()); 295 fSwitchSuspend = new RadioGroupFieldEditor( 296 IInternalDebugUIConstants.PREF_SWITCH_PERSPECTIVE_ON_SUSPEND, 297 DebugPreferencesMessages.DebugPreferencePage_21, 3, 298 new String [][] {{ DebugPreferencesMessages.DebugPreferencePage_22, MessageDialogWithToggle.ALWAYS }, 299 { DebugPreferencesMessages.DebugPreferencePage_23, MessageDialogWithToggle.NEVER }, 300 { DebugPreferencesMessages.DebugPreferencePage_24, MessageDialogWithToggle.PROMPT } }, 301 SWTFactory.createComposite(parent, 1, 2, GridData.FILL_HORIZONTAL), 302 true); 303 fSwitchSuspend.setPreferenceName(IInternalDebugUIConstants.PREF_SWITCH_PERSPECTIVE_ON_SUSPEND); 304 fSwitchSuspend.setPreferenceStore(getPreferenceStore()); 305 306 SWTFactory.createVerticalSpacer(parent, 1); 307 SWTFactory.createWrapLabel(parent, DebugPreferencesMessages.PerspectivePreferencePage_5, 2, 300); 308 Composite comp = SWTFactory.createComposite(parent, 2, 1, GridData.FILL_BOTH); 309 createTreeViewer(comp); 310 fPerspectivesPanel = new PerspectivesPanel(comp, DebugPreferencesMessages.PerspectivePreferencePage_2); 311 initializeControls(); 312 PlatformUI.getWorkbench().getActivitySupport().getActivityManager().addActivityManagerListener(this); 313 Dialog.applyDialogFont(parent); 314 return parent; 315 } 316 317 321 protected void createTreeViewer(Composite parent) { 322 Composite comp = SWTFactory.createComposite(parent, 1, 1, GridData.FILL_BOTH); 323 SWTFactory.createWrapLabel(comp, DebugPreferencesMessages.PerspectivePreferencePage_1, 1); 324 fTree = new Tree(comp, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.MULTI); 325 GridData gd = new GridData(GridData.FILL_VERTICAL); 326 gd.widthHint = 220; 327 gd.heightHint = 250; 328 fTree.setLayoutData(gd); 329 fTreeViewer = new PerspectivesTreeViewer(fTree); 330 fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() { 331 public void selectionChanged(SelectionChangedEvent event) { 332 fPerspectivesPanel.refreshPanel((IStructuredSelection) event.getSelection()); 333 } 334 }); 335 fTreeViewer.setLabelProvider(DebugUITools.newDebugModelPresentation()); 336 fTreeViewer.setComparator(new WorkbenchViewerComparator()); 337 fTreeViewer.setContentProvider(new PerspectiveContentProvider()); 338 MultiLaunchGroupFilter filter = new MultiLaunchGroupFilter(new LaunchGroupExtension[] { 339 DebugUIPlugin.getDefault().getLaunchConfigurationManager().getLaunchGroup("org.eclipse.debug.ui.launchGroup.debug"), DebugUIPlugin.getDefault().getLaunchConfigurationManager().getLaunchGroup("org.eclipse.ui.externaltools.launchGroup") }); 342 fTreeViewer.addFilter(filter); 343 fTreeViewer.setInput(DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationTypes()); 344 } 345 346 351 protected void createCombos(Composite parent, Object [] selection) { 352 Set modes = collectCommonModeSets(selection); 353 if(modes.isEmpty()) { 354 fPerspectivesPanel.setMessage(DebugPreferencesMessages.LaunchPerspectivePreferencePage_1); 355 return; 356 } 357 fPerspectivesPanel.setMessage(""); List fmodes = null; 359 Combo combo = null; 360 Label label = null; 361 Set smodes = null; 362 for(Iterator iter = modes.iterator(); iter.hasNext();) { 363 smodes = (Set ) iter.next(); 364 fmodes = LaunchConfigurationPresentationManager.getDefault().getLaunchModeNames(smodes); 365 if(!fmodes.isEmpty()) { 366 label = SWTFactory.createLabel(parent, fmodes.toString()+":", 1); ((GridData)label.getLayoutData()).grabExcessHorizontalSpace = true; 369 combo = SWTFactory.createCombo(parent, SWT.READ_ONLY, 1, fgPerspectiveLabels); 370 String text = getComboSelection(smodes); 371 if(text != null) { 372 combo.setText(text); 373 } 374 combo.setData(smodes); 375 combo.addSelectionListener(fSelectionListener); 376 GridData gd = (GridData)combo.getLayoutData(); 377 gd.widthHint = 140; 378 gd.horizontalAlignment = SWT.END; 379 gd.grabExcessHorizontalSpace = false; 380 } 381 } 382 } 383 384 389 private String getComboSelection(Set modes) { 390 String text = DebugPreferencesMessages.PerspectivePreferencePage_4; 391 IStructuredSelection ss = (IStructuredSelection) fTreeViewer.getSelection(); 392 if(ss != null && !ss.isEmpty()) { 393 Object o = null; 394 Set tmp = new HashSet (); 395 String id = null; 396 ILaunchConfigurationType type = null; 397 LaunchDelegate delegate = null; 398 PerspectiveChange change = null; 399 for(Iterator iter = ss.iterator(); iter.hasNext();) { 400 o = iter.next(); 401 if(o instanceof LaunchDelegate) { 402 delegate = (LaunchDelegate) o; 403 type = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(delegate.getLaunchConfigurationTypeId()); 404 } 405 else if(o instanceof ILaunchConfigurationType) { 406 type = (ILaunchConfigurationType) o; 407 } 408 change = findChange(type, delegate, modes); 409 if(change != null) { 410 id = change.getPerspectiveId(); 411 } 412 else { 413 id = DebugUIPlugin.getDefault().getPerspectiveManager().getLaunchPerspective(type, modes, delegate); 414 } 415 if(id == null) { 416 id = IDebugUIConstants.PERSPECTIVE_NONE; 417 } 418 tmp.add(id); 419 } 420 if(tmp.size() == 1) { 421 id = (String ) tmp.iterator().next(); 422 if(!IDebugUIConstants.PERSPECTIVE_NONE.equals(id)) { 423 String label = null; 424 for(Iterator iter = fgPerspectiveIdMap.keySet().iterator(); iter.hasNext();) { 425 label = (String ) iter.next(); 426 if(id.equals(fgPerspectiveIdMap.get(label))) { 427 return label; 428 } 429 } 430 } 431 } 432 } 433 434 return text; 435 } 436 437 445 private PerspectiveChange findChange(ILaunchConfigurationType type, ILaunchDelegate delegate, Set modes) { 446 PerspectiveChange change = new PerspectiveChange(type, delegate, modes, null); 447 Object o = null; 448 for(Iterator iter = fgChangeSet.iterator(); iter.hasNext();) { 449 o = iter.next(); 450 if(change.equals(o)) { 451 return (PerspectiveChange) o; 452 } 453 } 454 return null; 455 } 456 457 464 protected Set collectCommonModeSets(Object [] selection) { 465 HashSet common = new HashSet (); 466 HashSet delegates = new HashSet (); 468 Object o = null; 469 Object [] kids = null; 470 for(int i = 0; i < selection.length; i++) { 471 o = selection[i]; 472 if(o instanceof ILaunchDelegate) { 473 delegates.add(o); 474 } 475 else if(o instanceof ILaunchConfigurationType) { 476 fgCurrentWorkingContext.add(o); 477 kids = fTreeViewer.getFilteredChildren(o); 478 delegates.addAll(Arrays.asList(kids)); 479 } 480 } 481 ILaunchDelegate delegate = null; 483 List modes = null; 484 HashSet pruned = new HashSet (); 485 Set fmodes = null; 486 if(!delegates.isEmpty()) { 487 for(Iterator iter = delegates.iterator(); iter.hasNext();) { 488 delegate = (ILaunchDelegate) iter.next(); 489 modes = delegate.getModes(); 490 for(Iterator iter2 = modes.iterator(); iter2.hasNext();) { 491 fmodes = (Set ) iter2.next(); 492 if(isCommonModeset(fmodes, delegates, pruned)) { 493 common.add(fmodes); 494 fgCurrentWorkingContext.add(delegate); 495 } 496 } 497 } 498 } 499 return common; 500 } 501 502 510 private boolean isCommonModeset(Set modeset, Set delegates, Set pruned) { 511 if(!pruned.contains(modeset)) { 512 ILaunchDelegate delegate = null; 513 boolean common = true; 514 for(Iterator iter = delegates.iterator(); iter.hasNext();) { 515 delegate = (ILaunchDelegate) iter.next(); 516 common &= delegate.getModes().contains(modeset); 517 } 518 if(!common) { 519 pruned.add(modeset); 520 } 521 else { 522 return true; 523 } 524 } 525 return false; 526 } 527 528 532 protected void initializeControls() { 533 fInitializing = true; 534 if(fTree.getItemCount() > 0) { 535 TreeItem item = fTree.getItem(0); 536 fTreeViewer.setSelection(new StructuredSelection(item.getData())); 537 fTreeViewer.expandToLevel(item.getData(), 1); 538 } 539 fSwitchLaunch.load(); 541 fSwitchSuspend.load(); 542 fInitializing = false; 543 } 544 545 548 protected void performDefaults() { 549 fgChangeSet.clear(); 550 fSwitchLaunch.loadDefault(); 551 fSwitchSuspend.loadDefault(); 552 553 PerspectiveManager pm = DebugUIPlugin.getDefault().getPerspectiveManager(); 554 TreeItem[] items = fTree.getItems(); 555 ILaunchConfigurationType type = null; 556 Set modes = null; 557 Set modeset = null; 558 Object [] delegates = null; 559 for(int i = 0; i < items.length; i++) { 560 type = (ILaunchConfigurationType) items[i].getData(); 562 modes = type.getSupportedModeCombinations(); 563 delegates = fTreeViewer.getFilteredChildren(type); 564 for(Iterator iter = modes.iterator(); iter.hasNext();) { 565 modeset = (Set ) iter.next(); 566 fgChangeSet.add(new PerspectiveChange(type, null, modeset, pm.getDefaultLaunchPerspective(type, null, modeset))); 567 } 568 for(int j = 0; j < delegates.length; j++) { 569 modes = new HashSet (((ILaunchDelegate)delegates[j]).getModes()); 570 for(Iterator iter = modes.iterator(); iter.hasNext();) { 571 modeset = (Set ) iter.next(); 572 fgChangeSet.add(new PerspectiveChange(type, (ILaunchDelegate) delegates[j], modeset, pm.getDefaultLaunchPerspective(type, (ILaunchDelegate) delegates[j], modeset))); 573 } 574 } 575 } 576 if(fTree.getItemCount() > 0) { 577 TreeItem item = fTree.getItem(0); 578 fTreeViewer.setSelection(new StructuredSelection(item.getData())); 579 fTreeViewer.expandToLevel(item.getData(), 1); 580 } 581 super.performDefaults(); 582 } 583 584 587 public void init(IWorkbench workbench) { 588 setPreferenceStore(DebugUIPlugin.getDefault().getPreferenceStore()); 589 fgChangeSet = new HashSet (); 590 fgPerspectiveIdMap = new HashMap (); 592 ArrayList labels = new ArrayList (); 593 labels.add(DebugPreferencesMessages.PerspectivePreferencePage_4); 594 IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry(); 595 IPerspectiveDescriptor[] descriptors = registry.getPerspectives(); 596 String label = null; 597 for(int i = 0; i < descriptors.length; i++) { 598 if(!WorkbenchActivityHelper.filterItem(descriptors[i])) { 599 label = descriptors[i].getLabel(); 600 labels.add(label); 601 fgPerspectiveIdMap.put(label, descriptors[i].getId()); 602 } 603 } 604 fgPerspectiveLabels = (String []) labels.toArray(new String [labels.size()]); 605 } 606 607 610 public void activityManagerChanged(ActivityManagerEvent activityManagerEvent) { 611 if(!fTree.isDisposed()) { 612 fTreeViewer.refresh(); 613 } 614 } 615 616 619 public boolean performOk() { 620 fSwitchLaunch.store(); 621 fSwitchSuspend.store(); 622 if(!fgChangeSet.isEmpty()) { 623 PerspectiveChange change = null; 624 PerspectiveManager mgr = DebugUIPlugin.getDefault().getPerspectiveManager(); 625 for(Iterator iter = fgChangeSet.iterator(); iter.hasNext();) { 626 change = (PerspectiveChange) iter.next(); 627 mgr.setLaunchPerspective(change.getType(), change.getModes(), change.getDelegate(), change.getPerspectiveId()); 628 } 629 } 630 return super.performOk(); 631 } 632 } 633 | Popular Tags |