KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.debug.core.DebugPlugin;
21 import org.eclipse.debug.core.ILaunchConfigurationType;
22 import org.eclipse.debug.core.ILaunchDelegate;
23 import org.eclipse.debug.internal.core.LaunchManager;
24 import org.eclipse.debug.internal.ui.DebugUIPlugin;
25 import org.eclipse.debug.internal.ui.DefaultLabelProvider;
26 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
27 import org.eclipse.debug.internal.ui.SWTFactory;
28 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationPresentationManager;
29 import org.eclipse.jface.preference.PreferencePage;
30 import org.eclipse.jface.viewers.ArrayContentProvider;
31 import org.eclipse.jface.viewers.CheckStateChangedEvent;
32 import org.eclipse.jface.viewers.CheckboxTableViewer;
33 import org.eclipse.jface.viewers.ICheckStateListener;
34 import org.eclipse.jface.viewers.ISelectionChangedListener;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.jface.viewers.ITreeContentProvider;
37 import org.eclipse.jface.viewers.SelectionChangedEvent;
38 import org.eclipse.jface.viewers.TreeViewer;
39 import org.eclipse.jface.viewers.Viewer;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Control;
44 import org.eclipse.swt.widgets.Group;
45 import org.eclipse.swt.widgets.Table;
46 import org.eclipse.swt.widgets.Text;
47 import org.eclipse.swt.widgets.Tree;
48 import org.eclipse.ui.IWorkbench;
49 import org.eclipse.ui.IWorkbenchPreferencePage;
50 import org.eclipse.ui.PlatformUI;
51 import org.eclipse.ui.model.WorkbenchViewerComparator;
52
53 /**
54  * This class provides a preference page for selecting and changing preferred launch delegates for those of them
55  * that have conflicting delegates.
56  *
57  * Delegates are considered to be conflicting if they are for the same launc configuraiton type, and apply to the same
58  * mode sets.
59  *
60  * @since 3.3
61  *
62  * TODO create a help topic for this page....it needs a good description
63  *
64  */

65 public class LaunchersPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
66
67     /**
68      * Class to collect and persist attributes to sufficiently describe a dupicate launch delegate
69      */

70     class DuplicateDelegate {
71         private ILaunchConfigurationType fType = null;
72         private ILaunchDelegate[] fDelegates = null;
73         private Set JavaDoc fModes = null;
74         
75         public DuplicateDelegate(ILaunchConfigurationType type, ILaunchDelegate[] delegates, Set JavaDoc modes) {
76             fModes = modes;
77             fType = type;
78             fDelegates = delegates;
79         }
80         
81         public ILaunchConfigurationType getType() {
82             return fType;
83         }
84         public ILaunchDelegate[] getDelegates() {
85             return fDelegates;
86         }
87         public Set JavaDoc getModeSet() {
88             return fModes;
89         }
90     }
91     
92     /**
93      * label provider to extend the default one, provides labels to both the tree and table of this page
94      */

95     class LabelProvider extends DefaultLabelProvider {
96         public String JavaDoc getText(Object JavaDoc element) {
97             if(element instanceof ILaunchConfigurationType) {
98                 return super.getText(element);
99             }
100             else if(element instanceof DuplicateDelegate) {
101                 DuplicateDelegate dd = (DuplicateDelegate) element;
102                 return LaunchConfigurationPresentationManager.getDefault().getLaunchModeNames(dd.getModeSet()).toString();
103             }
104             else if(element instanceof ILaunchDelegate){
105                 return ((ILaunchDelegate) element).getName();
106             }
107             return element.toString();
108         }
109     }
110     
111     /**
112      * This class is used to provide content to the tree
113      */

114     class TreeProvider implements ITreeContentProvider {
115         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
116             if(parentElement instanceof ILaunchConfigurationType) {
117                 ILaunchConfigurationType type = (ILaunchConfigurationType) parentElement;
118                 Set JavaDoc dupes = (Set JavaDoc) fDuplicates.get(type);
119                 if(dupes != null) {
120                     return dupes.toArray();
121                 }
122                 return null;
123             }
124             return null;
125         }
126         public boolean hasChildren(Object JavaDoc element) {
127             return element instanceof ILaunchConfigurationType;
128         }
129         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
130             if(inputElement instanceof Map JavaDoc) {
131                 return ((Map JavaDoc)inputElement).keySet().toArray();
132             }
133             return null;
134         }
135         public Object JavaDoc getParent(Object JavaDoc element) {return null;}
136         public void dispose() {}
137         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {}
138     }
139     
140     private TreeViewer fTreeViewer = null;
141     private CheckboxTableViewer fTableViewer = null;
142     private Map JavaDoc fDuplicates = null;
143     private Map JavaDoc fDupeSelections = null;
144     private boolean fDirty = false;
145     private Text fDescription = null;
146     
147     /**
148      * Constructor
149      */

150     public LaunchersPreferencePage() {
151         setTitle(DebugPreferencesMessages.LaunchDelegatesPreferencePage_0);
152     }
153
154     /**
155      * @see org.eclipse.jface.preference.PreferencePage#createControl(org.eclipse.swt.widgets.Composite)
156      */

157     public void createControl(Composite parent) {
158         super.createControl(parent);
159         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IDebugHelpContextIds.LAUNCH_DELEGATES_PREFERENCE_PAGE);
160     }
161
162     /**
163      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
164      */

165     protected Control createContents(Composite parent) {
166         Composite comp = SWTFactory.createComposite(parent, 2, 1, GridData.FILL_BOTH);
167         SWTFactory.createWrapLabel(comp, DebugPreferencesMessages.LaunchDelegatesPreferencePage_1, 2, 300);
168         
169         boolean enabled = fDuplicates.size() > 0;
170         if(!enabled) {
171             SWTFactory.createVerticalSpacer(comp, 1);
172             SWTFactory.createWrapLabel(comp, DebugPreferencesMessages.LaunchersPreferencePage_0, 2, 300);
173         }
174         
175         SWTFactory.createVerticalSpacer(comp, 1);
176     //tree
177
Composite comp1 = SWTFactory.createComposite(comp, 1, 1, GridData.FILL_VERTICAL);
178         SWTFactory.createLabel(comp1, DebugPreferencesMessages.LaunchDelegatesPreferencePage_2, 1);
179         Tree tree = new Tree(comp1, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE);
180         tree.setEnabled(enabled);
181         GridData gd = new GridData(GridData.FILL_BOTH);
182         gd.grabExcessHorizontalSpace = false;
183         tree.setLayoutData(gd);
184         fTreeViewer = new TreeViewer(tree);
185         fTreeViewer.setComparator(new WorkbenchViewerComparator());
186         fTreeViewer.setContentProvider(new TreeProvider());
187         fTreeViewer.setLabelProvider(new LabelProvider());
188         fTreeViewer.setInput(fDuplicates);
189         fTreeViewer.expandToLevel(2);
190         fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
191             public void selectionChanged(SelectionChangedEvent event) {
192                 Object JavaDoc obj = ((IStructuredSelection) event.getSelection()).getFirstElement();
193                 if(obj instanceof DuplicateDelegate) {
194                     fTableViewer.setAllChecked(false);
195                     DuplicateDelegate dd = (DuplicateDelegate) obj;
196                     fTableViewer.setInput(dd.getDelegates());
197                     obj = fDupeSelections.get(dd);
198                     if(obj != null) {
199                         fTableViewer.setChecked(obj, true);
200                     }
201                 }
202                 else {
203                     fTableViewer.setInput(null);
204                 }
205             }
206         });
207         
208     //table
209
Composite comp2 = SWTFactory.createComposite(comp, comp.getFont(), 1, 1, GridData.FILL_BOTH);
210         SWTFactory.createLabel(comp2, DebugPreferencesMessages.LaunchDelegatesPreferencePage_3, 1);
211         Table table = new Table(comp2, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CHECK | SWT.SINGLE);
212         table.setEnabled(enabled);
213         table.setLayoutData(new GridData(GridData.FILL_BOTH));
214         fTableViewer = new CheckboxTableViewer(table);
215         fTableViewer.setComparator(new WorkbenchViewerComparator());
216         fTableViewer.setLabelProvider(new LabelProvider());
217         fTableViewer.setContentProvider(new ArrayContentProvider());
218         fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
219             public void selectionChanged(SelectionChangedEvent event) {
220                 IStructuredSelection ss = (IStructuredSelection) event.getSelection();
221                 if(ss != null && !ss.isEmpty()) {
222                     fDescription.setText(((ILaunchDelegate)ss.getFirstElement()).getDescription());
223                 }
224                 else {
225                     fDescription.setText(""); //$NON-NLS-1$
226
}
227             }
228         });
229         fTableViewer.addCheckStateListener(new ICheckStateListener() {
230             public void checkStateChanged(CheckStateChangedEvent event) {
231                 fDirty = true;
232                 Object JavaDoc element = event.getElement();
233                 boolean checked = event.getChecked();
234                 fTableViewer.setAllChecked(false);
235                 //always set checked, this way users cannot 'undo' a change to selecting a preferred delegate
236
//The story for this is that on startup if there are dupes, the user is prompted to pick a delegate, after that they cannot
237
//return to a state of not being able to launch something, but can pick a different delegate
238
fTableViewer.setChecked(element, true);
239                 //persist the selection
240
Object JavaDoc obj = ((IStructuredSelection) fTreeViewer.getSelection()).getFirstElement();
241                 if(obj instanceof DuplicateDelegate) {
242                     fDupeSelections.remove(obj);
243                     if(checked) {
244                         fDupeSelections.put(obj, element);
245                     }
246                 }
247             }
248         });
249         Group group = SWTFactory.createGroup(comp, DebugPreferencesMessages.LaunchDelegatesPreferencePage_4, 1, 2, GridData.FILL_BOTH);
250         fDescription = SWTFactory.createText(group, SWT.WRAP | SWT.READ_ONLY, 1, GridData.FILL_BOTH);
251         fDescription.setEnabled(enabled);
252         fDescription.setBackground(group.getBackground());
253         return comp;
254     }
255
256     /**
257      * @see org.eclipse.jface.preference.PreferencePage#performOk()
258      */

259     public boolean performOk() {
260         if(fDirty && fDupeSelections != null && fDupeSelections.size() > 0) {
261             fDirty = false;
262             DuplicateDelegate dd = null;
263             ILaunchDelegate delegate = null;
264             for(Iterator JavaDoc iter = fDupeSelections.keySet().iterator(); iter.hasNext();) {
265                 dd = (DuplicateDelegate) iter.next();
266                 delegate = (ILaunchDelegate) fDupeSelections.get(dd);
267                 try {
268                     dd.getType().setPreferredDelegate(dd.getModeSet(), delegate);
269                 }
270                 catch (CoreException e) {DebugUIPlugin.log(e);}
271             }
272         }
273         return super.performOk();
274     }
275
276     /**
277      * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
278      */

279     public void init(IWorkbench workbench) {
280         //init a listing of duplicate delegates arranged by type
281
try {
282             setPreferenceStore(DebugUIPlugin.getDefault().getPreferenceStore());
283             LaunchManager lm = (LaunchManager) DebugPlugin.getDefault().getLaunchManager();
284             ILaunchConfigurationType[] types = lm.getLaunchConfigurationTypes();
285             fDuplicates = new HashMap JavaDoc();
286             fDupeSelections = new HashMap JavaDoc();
287             Set JavaDoc modes = null;
288             ILaunchDelegate[] delegates = null;
289             Set JavaDoc modeset = null;
290             Set JavaDoc tmp = null;
291             ILaunchDelegate prefdelegate = null;
292             DuplicateDelegate dd = null;
293             for(int i = 0; i < types.length; i++) {
294                 modes = types[i].getSupportedModeCombinations();
295                 for(Iterator JavaDoc iter = modes.iterator(); iter.hasNext();) {
296                     modeset = (Set JavaDoc) iter.next();
297                     delegates = types[i].getDelegates(modeset);
298                     if(delegates.length > 1) {
299                         tmp = (Set JavaDoc) fDuplicates.get(types[i]);
300                         if(tmp == null) {
301                             tmp = new HashSet JavaDoc();
302                         }
303                         dd = new DuplicateDelegate(types[i], delegates, modeset);
304                         tmp.add(dd);
305                         fDuplicates.put(types[i], tmp);
306                         prefdelegate = types[i].getPreferredDelegate(modeset);
307                         if(prefdelegate != null) {
308                             fDupeSelections.put(dd, prefdelegate);
309                         }
310                     }
311                 }
312             }
313         }
314         catch(CoreException e) {DebugUIPlugin.log(e);}
315     }
316
317 }
318
Popular Tags