KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > preferences > LaunchPerspectivePreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.preferences;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
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 /**
70  * The preference page for selecting and changing launch perspectives
71  *
72  * @since 3.3
73  */

74 public class LaunchPerspectivePreferencePage extends PreferencePage implements IWorkbenchPreferencePage, IActivityManagerListener {
75
76     /**
77      * Represents a perspective delta for a given type, delegate and mode set combination.
78      */

79     final class PerspectiveChange {
80         private ILaunchConfigurationType fType = null;
81         private ILaunchDelegate fDelegate = null;
82         private Set JavaDoc fModes = null;
83         private String JavaDoc fPid = null;
84         
85         public PerspectiveChange(ILaunchConfigurationType type, ILaunchDelegate delegate, Set JavaDoc modes, String JavaDoc 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 JavaDoc getPerspectiveId() {return fPid;}
95         public Set JavaDoc getModes() {return fModes;}
96         public boolean equals(Object JavaDoc 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     /**
108      * Implementation to expose use of getFilteredChildren method
109      */

110     final class PerspectivesTreeViewer extends TreeViewer {
111         public PerspectivesTreeViewer(Tree tree) {
112             super(tree);
113         }
114         public Object JavaDoc[] getFilteredChildren(Object JavaDoc o) {return super.getFilteredChildren(o);}
115     }
116     
117     /**
118      * Provides content for the configuration tree viewer
119      */

120     final class PerspectiveContentProvider implements ITreeContentProvider {
121         public Object JavaDoc[] getChildren(Object JavaDoc 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 JavaDoc[0];
127         }
128         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
129             return DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationTypes();
130         }
131         public boolean hasChildren(Object JavaDoc element) {return element instanceof ILaunchConfigurationType;}
132         public Object JavaDoc getParent(Object JavaDoc element) {return null;}
133         public void dispose() {}
134         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {}
135     }
136     
137     /**
138      * Panel container that is reused to present series of combo boxes to users for perspective selections
139      */

140     class PerspectivesPanel {
141         
142         private Composite fMainComposite = null;
143         private Label fMessage = null;
144         
145         public PerspectivesPanel(Composite parent, String JavaDoc heading) {
146             createPanel(parent, heading);
147         }
148         
149         protected void createPanel(Composite parent, String JavaDoc heading) {
150             fMainComposite = SWTFactory.createComposite(parent, 2, 1, GridData.FILL_BOTH);
151             SWTFactory.createWrapLabel(fMainComposite, heading, 2);
152             fMessage = SWTFactory.createWrapLabel(fMainComposite, "", 2, 250); //$NON-NLS-1$
153
}
154         
155         public void setMessage(String JavaDoc msg) {
156             fMessage.setText((msg == null ? "" : msg)); //$NON-NLS-1$
157
}
158         
159         public void refreshPanel(IStructuredSelection selection) {
160             //get rid of any existing children, but leave the first two (the label for the control, and the message area)
161
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 JavaDoc();
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     /**
188      * Widgets
189      */

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     /**
197      * Caches
198      */

199     private static String JavaDoc[] fgPerspectiveLabels = null;
200     private static Map JavaDoc fgPerspectiveIdMap = null;
201     private static HashSet JavaDoc fgChangeSet = null;
202     private static HashSet JavaDoc fgCurrentWorkingContext = null;
203     
204     /**
205      * fields
206      */

207     private boolean fInitializing = false;
208     
209     /**
210      * A default selection listener to be reused by all combo boxes presenting perspective data
211      */

212     private SelectionListener fSelectionListener = new SelectionListener() {
213         public void widgetDefaultSelected(SelectionEvent e) {}
214         public void widgetSelected(SelectionEvent e) {
215             Object JavaDoc 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 JavaDoc modes = null;
222                 for(Iterator JavaDoc 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 JavaDoc) combo.getData();
233                     change = findChange(type, delegate, modes);
234                     if(change == null) {
235                         change = new PerspectiveChange(type, delegate, modes, (String JavaDoc)fgPerspectiveIdMap.get(combo.getText()));
236                         fgChangeSet.add(change);
237                     }
238                     else {
239                         change.fPid = (String JavaDoc)fgPerspectiveIdMap.get(combo.getText());
240                     }
241                 }
242             }
243         }
244     };
245     
246     /**
247      * Constructor
248      */

249     public LaunchPerspectivePreferencePage() {}
250     
251     /**
252      * @see org.eclipse.jface.dialogs.DialogPage#dispose()
253      */

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     /**
269      * @see org.eclipse.jface.preference.PreferencePage#createControl(org.eclipse.swt.widgets.Composite)
270      */

271     public void createControl(Composite parent) {
272         super.createControl(parent);
273         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IDebugHelpContextIds.PERSPECTIVE_PREFERENCE_PAGE);
274     }
275     
276     /**
277      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
278      */

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 JavaDoc[][] {{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 JavaDoc[][] {{ 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     /**
318      * Creates the <code>Tree</code> and <code>TreeViewer</code> widgets
319      * @param parent the parent to add these components to
320      */

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"), //$NON-NLS-1$
340
DebugUIPlugin.getDefault().getLaunchConfigurationManager().getLaunchGroup("org.eclipse.ui.externaltools.launchGroup") //$NON-NLS-1$
341
});
342         fTreeViewer.addFilter(filter);
343         fTreeViewer.setInput(DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationTypes());
344     }
345     
346     /**
347      * Creates a set of combo boxes on a per-selection basis that display a listing of available perspectives to switch to
348      * @param parent the parent to add the created combo boxes to
349      * @param selection the selection in the tree viewer
350      */

351     protected void createCombos(Composite parent, Object JavaDoc[] selection) {
352         Set JavaDoc modes = collectCommonModeSets(selection);
353         if(modes.isEmpty()) {
354             fPerspectivesPanel.setMessage(DebugPreferencesMessages.LaunchPerspectivePreferencePage_1);
355             return;
356         }
357         fPerspectivesPanel.setMessage(""); //$NON-NLS-1$
358
List JavaDoc fmodes = null;
359         Combo combo = null;
360         Label label = null;
361         Set JavaDoc smodes = null;
362         for(Iterator JavaDoc iter = modes.iterator(); iter.hasNext();) {
363             smodes = (Set JavaDoc) iter.next();
364             fmodes = LaunchConfigurationPresentationManager.getDefault().getLaunchModeNames(smodes);
365             if(!fmodes.isEmpty()) {
366                 //add the modeset and create a combo
367
label = SWTFactory.createLabel(parent, fmodes.toString()+":", 1); //$NON-NLS-1$
368
((GridData)label.getLayoutData()).grabExcessHorizontalSpace = true;
369                 combo = SWTFactory.createCombo(parent, SWT.READ_ONLY, 1, fgPerspectiveLabels);
370                 String JavaDoc 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     /**
385      * Returns the text item to select for the current combo context given the current working set context
386      * @param modes the set of modes
387      * @return the text to select in the current combo / current working set context, or "None"
388      */

389     private String JavaDoc getComboSelection(Set JavaDoc modes) {
390         String JavaDoc text = DebugPreferencesMessages.PerspectivePreferencePage_4;
391         IStructuredSelection ss = (IStructuredSelection) fTreeViewer.getSelection();
392         if(ss != null && !ss.isEmpty()) {
393             Object JavaDoc o = null;
394             Set JavaDoc tmp = new HashSet JavaDoc();
395             String JavaDoc id = null;
396             ILaunchConfigurationType type = null;
397             LaunchDelegate delegate = null;
398             PerspectiveChange change = null;
399             for(Iterator JavaDoc 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 JavaDoc) tmp.iterator().next();
422                 if(!IDebugUIConstants.PERSPECTIVE_NONE.equals(id)) {
423                     String JavaDoc label = null;
424                     for(Iterator JavaDoc iter = fgPerspectiveIdMap.keySet().iterator(); iter.hasNext();) {
425                         label = (String JavaDoc) iter.next();
426                         if(id.equals(fgPerspectiveIdMap.get(label))) {
427                             return label;
428                         }
429                     }
430                 }
431             }
432         }
433             
434         return text;
435     }
436     
437     /**
438      * Traverses the current change set to find a matching change. Matching in this context considers only the
439      * type, delegate and mode set, we do not compare perspective ids, as they can change many times.
440      * @param type the type
441      * @param delegate the delegate, possibly <code>null</code>
442      * @param modes the current mode set
443      * @return the existing <code>PerspectiveChange</code> if there is one, <code>null</code> otherwise
444      */

445     private PerspectiveChange findChange(ILaunchConfigurationType type, ILaunchDelegate delegate, Set JavaDoc modes) {
446         PerspectiveChange change = new PerspectiveChange(type, delegate, modes, null);
447         Object JavaDoc o = null;
448         for(Iterator JavaDoc 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     /**
458      * Collects a list of mode sets that are common to the current selection context. It is possible
459      * that there are no mode sets in comomon.
460      * @param selection the current selection context
461      * @param list the list to fill
462      * @return a list of mode sets or an empty list, never <code>null</code>
463      */

464     protected Set JavaDoc collectCommonModeSets(Object JavaDoc[] selection) {
465         HashSet JavaDoc common = new HashSet JavaDoc();
466     //prep selection context, remove types from the equation
467
HashSet JavaDoc delegates = new HashSet JavaDoc();
468         Object JavaDoc o = null;
469         Object JavaDoc[] 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     //compare the listing of delegates to find common mode sets
482
ILaunchDelegate delegate = null;
483         List JavaDoc modes = null;
484         HashSet JavaDoc pruned = new HashSet JavaDoc();
485         Set JavaDoc fmodes = null;
486         if(!delegates.isEmpty()) {
487             for(Iterator JavaDoc iter = delegates.iterator(); iter.hasNext();) {
488                 delegate = (ILaunchDelegate) iter.next();
489                 modes = delegate.getModes();
490                 for(Iterator JavaDoc iter2 = modes.iterator(); iter2.hasNext();) {
491                     fmodes = (Set JavaDoc) 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     /**
503      * Returns if the specified mode set is common to the listing of delegates, at the same time adding any not common
504      * mode sets to a listing used to prune the search as we go along
505      * @param modeset the set to test for commonality
506      * @param delegates the listing to test against
507      * @param pruned the monotonic listing of pruned mode sets
508      * @return true if the specified mode set is common to all members of the specified listing of launch delegates, false otherwise
509      */

510     private boolean isCommonModeset(Set JavaDoc modeset, Set JavaDoc delegates, Set JavaDoc pruned) {
511         if(!pruned.contains(modeset)) {
512             ILaunchDelegate delegate = null;
513             boolean common = true;
514             for(Iterator JavaDoc 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     /**
529      * Restores the widget state from the preference store, called after all of the widgets have been created and triggers
530      * a selection changed event from the tree viewer
531      */

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     //load the group selections
540
fSwitchLaunch.load();
541         fSwitchSuspend.load();
542         fInitializing = false;
543     }
544     
545     /**
546      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
547      */

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 JavaDoc modes = null;
557         Set JavaDoc modeset = null;
558         Object JavaDoc[] delegates = null;
559         for(int i = 0; i < items.length; i++) {
560             //reset type
561
type = (ILaunchConfigurationType) items[i].getData();
562             modes = type.getSupportedModeCombinations();
563             delegates = fTreeViewer.getFilteredChildren(type);
564             for(Iterator JavaDoc iter = modes.iterator(); iter.hasNext();) {
565                 modeset = (Set JavaDoc) 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 JavaDoc(((ILaunchDelegate)delegates[j]).getModes());
570                 for(Iterator JavaDoc iter = modes.iterator(); iter.hasNext();) {
571                     modeset = (Set JavaDoc) 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     /**
585      * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
586      */

587     public void init(IWorkbench workbench) {
588         setPreferenceStore(DebugUIPlugin.getDefault().getPreferenceStore());
589         fgChangeSet = new HashSet JavaDoc();
590     //init the labels mapping and the list of labels
591
fgPerspectiveIdMap = new HashMap JavaDoc();
592         ArrayList JavaDoc labels = new ArrayList JavaDoc();
593         labels.add(DebugPreferencesMessages.PerspectivePreferencePage_4);
594         IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry();
595         IPerspectiveDescriptor[] descriptors = registry.getPerspectives();
596         String JavaDoc 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 JavaDoc[]) labels.toArray(new String JavaDoc[labels.size()]);
605     }
606
607     /**
608      * @see org.eclipse.ui.activities.IActivityManagerListener#activityManagerChanged(org.eclipse.ui.activities.ActivityManagerEvent)
609      */

610     public void activityManagerChanged(ActivityManagerEvent activityManagerEvent) {
611         if(!fTree.isDisposed()) {
612             fTreeViewer.refresh();
613         }
614     }
615     
616     /**
617      * @see org.eclipse.jface.preference.PreferencePage#performOk()
618      */

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 JavaDoc 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