1 11 12 package org.eclipse.debug.internal.ui.preferences; 13 14 import java.io.IOException ; 15 import java.io.StringReader ; 16 import com.ibm.icu.text.MessageFormat; 17 import java.util.ArrayList ; 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 import javax.xml.parsers.ParserConfigurationException ; 24 25 import org.eclipse.debug.core.DebugPlugin; 26 import org.eclipse.debug.core.ILaunchConfigurationType; 27 import org.eclipse.debug.core.ILaunchManager; 28 import org.eclipse.debug.core.ILaunchMode; 29 import org.eclipse.debug.internal.ui.DebugUIPlugin; 30 import org.eclipse.debug.internal.ui.IDebugHelpContextIds; 31 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 32 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager; 33 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchGroupFilter; 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.IPreferenceStore; 40 import org.eclipse.jface.preference.PreferencePage; 41 import org.eclipse.jface.preference.RadioGroupFieldEditor; 42 import org.eclipse.jface.viewers.ISelectionChangedListener; 43 import org.eclipse.jface.viewers.IStructuredSelection; 44 import org.eclipse.jface.viewers.SelectionChangedEvent; 45 import org.eclipse.jface.viewers.TreeViewer; 46 import org.eclipse.swt.SWT; 47 import org.eclipse.swt.events.SelectionAdapter; 48 import org.eclipse.swt.events.SelectionEvent; 49 import org.eclipse.swt.graphics.Font; 50 import org.eclipse.swt.layout.GridData; 51 import org.eclipse.swt.layout.GridLayout; 52 import org.eclipse.swt.widgets.Combo; 53 import org.eclipse.swt.widgets.Composite; 54 import org.eclipse.swt.widgets.Control; 55 import org.eclipse.swt.widgets.Label; 56 import org.eclipse.swt.widgets.Tree; 57 import org.eclipse.swt.widgets.TreeItem; 58 import org.eclipse.ui.IPerspectiveDescriptor; 59 import org.eclipse.ui.IPerspectiveRegistry; 60 import org.eclipse.ui.IWorkbench; 61 import org.eclipse.ui.IWorkbenchPreferencePage; 62 import org.eclipse.ui.PlatformUI; 63 import org.eclipse.ui.activities.ActivityManagerEvent; 64 import org.eclipse.ui.activities.IActivityManagerListener; 65 import org.eclipse.ui.activities.WorkbenchActivityHelper; 66 import org.eclipse.ui.model.WorkbenchViewerSorter; 67 import org.w3c.dom.Element ; 68 import org.w3c.dom.Node ; 69 import org.w3c.dom.NodeList ; 70 import org.xml.sax.InputSource ; 71 import org.xml.sax.SAXException ; 72 import org.xml.sax.helpers.DefaultHandler ; 73 74 79 public class PerspectivePreferencePage extends PreferencePage implements IWorkbenchPreferencePage, IDebugPreferenceConstants, IActivityManagerListener { 80 81 private static final String DEBUG_LAUNCH_GROUP = "org.eclipse.debug.ui.launchGroup.debug"; 84 private static final String LAST_SELECTED_CONFIGTYPE = "last_selected"; 86 private static final int LABEL_WIDTH_HINT = 450; 87 88 91 private Composite fPerspectiveComp = null; 92 93 98 private HashMap fTypeInformationMapping = null; 99 100 103 private String [] fPerspectiveLabels = null; 104 105 108 private HashMap fPerspectiveIds = null; 109 110 113 private Composite fComboPlaceHolder = null; 114 115 118 private TreeViewer fViewer = null; 119 120 123 private Tree fTree = null; 124 125 128 private ILaunchManager fLManager = DebugPlugin.getDefault().getLaunchManager(); 129 130 133 private LaunchConfigurationManager fLCM = DebugUIPlugin.getDefault().getLaunchConfigurationManager(); 134 135 138 private PerspectiveManager fPmanager = DebugUIPlugin.getDefault().getPerspectiveManager(); 139 140 143 private RadioGroupFieldEditor perspec = null; 144 145 148 private RadioGroupFieldEditor suspend = null; 149 150 153 private ILaunchConfigurationType fCurrentType = null; 154 155 160 private SelectionAdapter fSelectionAdapter = new SelectionAdapter() { 161 public void widgetSelected(SelectionEvent e) { 162 Object source = e.getSource(); 163 if (source instanceof Combo) { 164 Combo combo = (Combo) source; 165 HashMap map = (HashMap ) fTypeInformationMapping.get(fCurrentType); 166 if (map == null) { 167 map = new HashMap (); 168 } map.put(combo.getData(), fPerspectiveIds.get(combo.getText())); 170 fTypeInformationMapping.put(fCurrentType, map); 171 } } }; 174 175 178 public PerspectivePreferencePage() { 179 IPreferenceStore store = DebugUIPlugin.getDefault().getPreferenceStore(); 180 setPreferenceStore(store); 181 setTitle(DebugPreferencesMessages.PerspectivePreferencePage_6); 182 setDescription(DebugPreferencesMessages.PerspectivePreferencePage_0); 183 } 185 190 public boolean performOk() { 191 IPreferenceStore store = getPreferenceStore(); 192 perspec.store(); 193 suspend.store(); 194 for (Iterator iter = fTypeInformationMapping.keySet().iterator(); iter.hasNext();) { 195 ILaunchConfigurationType typekey = (ILaunchConfigurationType)iter.next(); 196 HashMap map = (HashMap ) fTypeInformationMapping.get(typekey); 197 for (Iterator iter2 = map.keySet().iterator(); iter2.hasNext();) { 198 String modekey = (String ) iter2.next(); 199 String perspective = (String ) map.get(modekey); 200 if (perspective != null) { 201 if (perspective.equals(DebugPreferencesMessages.PerspectivePreferencePage_4)) { 202 perspective = IDebugUIConstants.PERSPECTIVE_NONE; 203 } } else { 206 perspective = IDebugUIConstants.PERSPECTIVE_NONE; 207 } fPmanager.setLaunchPerspective(typekey, modekey, perspective); 209 } } if(fCurrentType != null) { 212 store.setValue(LAST_SELECTED_CONFIGTYPE, fCurrentType.getName()); 213 } 214 DebugUIPlugin.getDefault().savePluginPreferences(); 215 return super.performOk(); 216 } 218 223 public void init(IWorkbench workbench) {} 224 225 228 private void restoreState() { 229 String xml = getPreferenceStore().getString(IInternalDebugUIConstants.PREF_LAUNCH_PERSPECTIVES); 230 setDefaultPerspectives(); 232 if (xml != null && xml.length() > 0) { 233 try { 234 Element root = null; 235 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 236 parser.setErrorHandler(new DefaultHandler ()); 237 StringReader reader = new StringReader (xml); 238 InputSource source = new InputSource (reader); 239 root = parser.parse(source).getDocumentElement(); 240 NodeList list = root.getChildNodes(); 241 int length = list.getLength(); 242 for (int i = 0; i < length; ++i) { 243 Node node = list.item(i); 244 short nt = node.getNodeType(); 245 if (nt == Node.ELEMENT_NODE) { 246 Element element = (Element ) node; 247 String nodeName = element.getNodeName(); 248 if (nodeName.equalsIgnoreCase(PerspectiveManager.ELEMENT_PERSPECTIVE)) { 249 String type = element.getAttribute(PerspectiveManager.ATTR_TYPE_ID); 250 ILaunchConfigurationType typeobj = fLManager.getLaunchConfigurationType(type); 251 String mode = element.getAttribute(PerspectiveManager.ATTR_MODE_ID); 252 if(typeobj != null) { 253 String perspective = fPmanager.getLaunchPerspective(typeobj, mode); 254 HashMap map = (HashMap ) fTypeInformationMapping.get(typeobj); 255 if(map == null) { 256 map = new HashMap (); 257 } map.put(mode, (perspective != null ? perspective : IDebugUIConstants.PERSPECTIVE_NONE)); 259 fTypeInformationMapping.put(typeobj, map); 260 } } } } } catch (ParserConfigurationException e) {DebugUIPlugin.log(e);} 266 catch (SAXException e) {DebugUIPlugin.log(e);} 267 catch (IOException e) {DebugUIPlugin.log(e);} 268 } perspec.load(); 270 suspend.load(); 271 TreeItem item = findLastSelected(getPreferenceStore().getString(LAST_SELECTED_CONFIGTYPE)); 272 if (item != null) { 273 fTree.setSelection(new TreeItem[] { item }); 274 } else { 276 if(fTree.getItemCount() > 0) { 277 fTree.setSelection(new TreeItem[] { fTree.getItem(0) }); 278 } 279 } fCurrentType = (ILaunchConfigurationType) ((IStructuredSelection) fViewer.getSelection()).getFirstElement(); 281 buildComboBoxes(fCurrentType); 282 } 284 287 private void setDefaultPerspectives() { 288 ILaunchConfigurationType[] types = fLManager.getLaunchConfigurationTypes(); 289 ArrayList modes = null; 290 HashMap map = null; 291 for (int i = 0; i < types.length; i++) { 292 modes = new ArrayList (types[i].getSupportedModes()); 293 map = new HashMap (); 294 for (int j = 0; j < modes.size(); j++) { 295 String mode = (String ) modes.get(j); 296 String persp = fPmanager.getDefaultLaunchPerspective(types[i], mode); 297 map.put(mode, (persp != null ? persp : IDebugUIConstants.PERSPECTIVE_NONE)); 298 } fTypeInformationMapping.put(types[i], map); 300 } } 303 310 private TreeItem findLastSelected(String last) { 311 TreeItem[] selection = fTree.getItems(); 312 for (int i = 0; i < selection.length; i++) { 313 if (selection[i].getText().equals(last)) { 314 return selection[i]; 315 } } return null; 318 } 320 323 private void buildComboBoxes(ILaunchConfigurationType type) { 324 HashMap launchmodes = (HashMap ) fTypeInformationMapping.get(type); 325 if (fComboPlaceHolder != null) { 326 fComboPlaceHolder.dispose(); 327 } Font font = fPerspectiveComp.getFont(); 329 fComboPlaceHolder = new Composite(fPerspectiveComp, SWT.NONE); 330 fComboPlaceHolder.setLayout(new GridLayout(2, false)); 331 fComboPlaceHolder.setLayoutData(new GridData(GridData.FILL_BOTH)); 332 fComboPlaceHolder.setFont(font); 333 Label label = new Label(fComboPlaceHolder, SWT.NONE); 334 label.setText(DebugPreferencesMessages.PerspectivePreferencePage_2); 335 label.setFont(font); 336 GridData gd = null; 337 gd = new GridData(GridData.FILL_HORIZONTAL); 338 gd.horizontalSpan = 2; 339 label.setLayoutData(gd); 340 if(launchmodes != null) { 341 for(Iterator iter = launchmodes.keySet().iterator(); iter.hasNext();) { 342 String modekey = (String )iter.next(); 343 String persp = (String )launchmodes.get(modekey); 344 label = new Label(fComboPlaceHolder, SWT.NONE); 346 label.setFont(font); 347 gd = new GridData(GridData.BEGINNING); 348 label.setLayoutData(gd); 349 ILaunchMode mode = fLManager.getLaunchMode(modekey); 350 String clabel = mode.getLabel(); 351 if(clabel.equals(DebugPreferencesMessages.PerspectivePreferencePage_7)) { 353 clabel = DebugPreferencesMessages.PerspectivePreferencePage_8; 354 } 355 label.setText(MessageFormat.format(DebugPreferencesMessages.PerspectivePreferencePage_3, new String [] { clabel })); 356 Combo combo = new Combo(fComboPlaceHolder, SWT.READ_ONLY); 358 combo.setFont(font); 359 combo.setItems(fPerspectiveLabels); 360 combo.setData(modekey); 361 gd = new GridData(GridData.BEGINNING); 362 combo.setLayoutData(gd); 363 if(persp.equals(IDebugUIConstants.PERSPECTIVE_NONE)) { 364 persp = DebugPreferencesMessages.PerspectivePreferencePage_4; 365 } else { 367 IPerspectiveDescriptor desc = PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(persp); 368 persp = (desc != null ? desc.getLabel() : DebugPreferencesMessages.PerspectivePreferencePage_4); 369 } combo.setText(persp); 371 combo.addSelectionListener(fSelectionAdapter); 372 } } 374 fPerspectiveComp.layout(); 375 } 377 383 private void handleLaunchConfigurationSelectionChanged(SelectionChangedEvent event) { 384 ILaunchConfigurationType type = (ILaunchConfigurationType) ((IStructuredSelection) event.getSelection()).getFirstElement(); 386 if(type != null) { 387 if(!type.equals(fCurrentType)) { 388 fCurrentType = type; 390 buildComboBoxes(fCurrentType); 391 } 392 } 393 } 395 400 private void getPerspectiveLabels() { 401 IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry(); 402 IPerspectiveDescriptor[] descriptors = filterIds(registry.getPerspectives()); 403 fPerspectiveLabels = new String [descriptors.length + 1]; 404 fPerspectiveLabels[0] = DebugPreferencesMessages.PerspectivePreferencePage_4; 405 fPerspectiveIds = new HashMap (); 406 for (int i = 0; i < descriptors.length; i++) { 407 fPerspectiveLabels[i + 1] = descriptors[i].getLabel(); 408 fPerspectiveIds.put(descriptors[i].getLabel(), descriptors[i].getId()); 409 } } 412 417 private IPerspectiveDescriptor[] filterIds(IPerspectiveDescriptor[] ids) { 418 ArrayList list = new ArrayList (); 419 for(int i = 0; i < ids.length; i++) { 420 if(!WorkbenchActivityHelper.filterItem(ids[i])) { 421 list.add(ids[i]); 422 } 423 } 424 return (IPerspectiveDescriptor[])list.toArray(new IPerspectiveDescriptor[list.size()]); 425 } 426 427 435 protected void createSpacer(Composite composite, int columnSpan) { 436 Label label = new Label(composite, SWT.NONE); 437 GridData gd = new GridData(); 438 gd.horizontalSpan = columnSpan; 439 label.setLayoutData(gd); 440 } 442 447 protected void performDefaults() { 448 perspec.loadDefault(); 449 suspend.loadDefault(); 450 setDefaultPerspectives(); 451 buildComboBoxes(fCurrentType); 452 super.performDefaults(); 453 } 455 458 public void createControl(Composite parent) { 459 super.createControl(parent); 460 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IDebugHelpContextIds.PERSPECTIVE_PREFERENCE_PAGE); 461 } 462 463 468 protected Control createContents(Composite parent) { 469 Composite composite = new Composite(parent, SWT.NONE); 470 composite.setLayout(new GridLayout(1, false)); 471 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 472 473 createSpacer(composite, 1); 475 perspec = new RadioGroupFieldEditor( 476 IInternalDebugUIConstants.PREF_SWITCH_TO_PERSPECTIVE, 477 DebugPreferencesMessages.LaunchingPreferencePage_11, 3, 478 new String [][] {{DebugPreferencesMessages.LaunchingPreferencePage_12, MessageDialogWithToggle.ALWAYS }, 479 { DebugPreferencesMessages.LaunchingPreferencePage_13, MessageDialogWithToggle.NEVER }, 480 { DebugPreferencesMessages.LaunchingPreferencePage_14, MessageDialogWithToggle.PROMPT } }, 481 composite, 482 true); 483 perspec.setPreferenceName(IInternalDebugUIConstants.PREF_SWITCH_TO_PERSPECTIVE); 484 perspec.setPreferenceStore(getPreferenceStore()); 485 suspend = new RadioGroupFieldEditor( 486 IInternalDebugUIConstants.PREF_SWITCH_PERSPECTIVE_ON_SUSPEND, 487 DebugPreferencesMessages.DebugPreferencePage_21, 3, 488 new String [][] {{ DebugPreferencesMessages.DebugPreferencePage_22, MessageDialogWithToggle.ALWAYS }, 489 { DebugPreferencesMessages.DebugPreferencePage_23, MessageDialogWithToggle.NEVER }, 490 { DebugPreferencesMessages.DebugPreferencePage_24, MessageDialogWithToggle.PROMPT } }, 491 composite, 492 true); 493 suspend.setPreferenceName(IInternalDebugUIConstants.PREF_SWITCH_PERSPECTIVE_ON_SUSPEND); 494 suspend.setPreferenceStore(getPreferenceStore()); 495 496 createSpacer(composite, 1); 497 498 Label lbl = new Label(composite, SWT.LEFT + SWT.WRAP); 499 lbl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 500 ((GridData)lbl.getLayoutData()).widthHint = LABEL_WIDTH_HINT; 501 lbl.setText(MessageFormat.format(DebugPreferencesMessages.PerspectivePreferencePage_5, null)); 502 503 Composite comp = new Composite(composite, SWT.FILL); 504 comp.setLayout(new GridLayout(2, false)); 505 comp.setLayoutData(new GridData(GridData.FILL_BOTH)); 506 507 Composite treeComp = new Composite(comp, SWT.NONE); 508 treeComp.setLayout(new GridLayout(1, true)); 509 treeComp.setLayoutData(new GridData(GridData.FILL_BOTH)); 510 Label label = new Label(treeComp, SWT.NONE); 511 label.setText(DebugPreferencesMessages.PerspectivePreferencePage_1); 512 513 fTree = new Tree(treeComp, SWT.H_SCROLL + SWT.V_SCROLL + SWT.SINGLE + SWT.BORDER); 514 fTree.setLayoutData(new GridData(GridData.FILL_BOTH)); 515 fViewer = new TreeViewer(fTree); 516 fViewer.setLabelProvider(DebugUITools.newDebugModelPresentation()); 517 fViewer.setSorter(new WorkbenchViewerSorter()); 518 fViewer.setContentProvider(new LaunchConfigurationTreeContentProviderNoChildren(null, parent.getShell())); 519 fViewer.addFilter(new LaunchGroupFilter(fLCM.getLaunchGroup(DEBUG_LAUNCH_GROUP))); 520 fViewer.setInput(fLManager.getLaunchConfigurationTypes()); 521 fViewer.expandAll(); 522 fViewer.addPostSelectionChangedListener(new ISelectionChangedListener() { 523 public void selectionChanged(SelectionChangedEvent event) { 524 handleLaunchConfigurationSelectionChanged(event); 525 } }); 527 fPerspectiveComp = new Composite(comp, SWT.NONE); 528 fPerspectiveComp.setLayout(new GridLayout(1, true)); 529 fPerspectiveComp.setLayoutData(new GridData(GridData.FILL_BOTH)); 530 fTypeInformationMapping = new HashMap (); 531 getPerspectiveLabels(); 533 restoreState(); 535 536 PlatformUI.getWorkbench().getActivitySupport().getActivityManager().addActivityManagerListener(this); 537 538 Dialog.applyDialogFont(composite); 539 return composite; 540 } 542 545 public void activityManagerChanged(ActivityManagerEvent activityManagerEvent) { 546 if(!fTree.isDisposed()) { 547 fViewer.refresh(); 548 getPerspectiveLabels(); 549 restoreState(); 550 } 551 } 552 553 556 public void dispose() { 557 PlatformUI.getWorkbench().getActivitySupport().getActivityManager().removeActivityManagerListener(this); 558 super.dispose(); 559 } 560 561 } | Popular Tags |