KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.Arrays JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IAdaptable;
24 import org.eclipse.debug.core.DebugPlugin;
25 import org.eclipse.debug.core.ILaunchConfiguration;
26 import org.eclipse.debug.core.ILaunchConfigurationType;
27 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
28 import org.eclipse.debug.internal.core.LaunchManager;
29 import org.eclipse.debug.internal.ui.DebugUIPlugin;
30 import org.eclipse.debug.internal.ui.DefaultLabelProvider;
31 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
32 import org.eclipse.debug.internal.ui.SWTFactory;
33 import org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugListSelectionDialog;
34 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationComparator;
35 import org.eclipse.debug.ui.IDebugUIConstants;
36 import org.eclipse.jface.dialogs.IDialogConstants;
37 import org.eclipse.jface.viewers.ArrayContentProvider;
38 import org.eclipse.jface.viewers.ISelection;
39 import org.eclipse.jface.viewers.ISelectionChangedListener;
40 import org.eclipse.jface.viewers.IStructuredSelection;
41 import org.eclipse.jface.viewers.SelectionChangedEvent;
42 import org.eclipse.jface.viewers.StructuredSelection;
43 import org.eclipse.jface.viewers.TableViewer;
44 import org.eclipse.jface.window.Window;
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.events.SelectionEvent;
47 import org.eclipse.swt.events.SelectionListener;
48 import org.eclipse.swt.layout.GridData;
49 import org.eclipse.swt.layout.GridLayout;
50 import org.eclipse.swt.widgets.Button;
51 import org.eclipse.swt.widgets.Composite;
52 import org.eclipse.swt.widgets.Control;
53 import org.eclipse.swt.widgets.Table;
54 import org.eclipse.ui.PlatformUI;
55 import org.eclipse.ui.dialogs.PropertyPage;
56 import org.eclipse.ui.dialogs.SelectionDialog;
57
58 import com.ibm.icu.text.MessageFormat;
59
60 /**
61  * Displays default launch configuration settings for a selected resource - associated launch configurations.
62  *
63  * @see {@link PropertyPage}
64  * @see {@link ILaunchConfiguration}
65  * @see {@link org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationsDialog}
66  * @see {@link IDebugHelpContextIds#DEFAULT_LAUNCHCONFIGURATION_PROPERTY_PAGE}
67  *
68  * CONTEXTLAUNCHING
69  *
70  * @since 3.3.0
71  */

72 public class RunDebugPropertiesPage extends PropertyPage {
73     /**
74      * Set of configurations to be deleted
75      */

76     private Set JavaDoc fDeletedConfigurations = new HashSet JavaDoc();
77     
78     /**
79      * Set of original default candidates for the resource
80      */

81     private Set JavaDoc fOriginalCandidates;
82     
83     /**
84      * Holds configurations that need to be saved when the page closes
85      */

86     private Set JavaDoc fChangedConfigurations = new HashSet JavaDoc();
87     
88     /**
89      * List of the applicable launch config types for the backing resource
90      */

91     private List JavaDoc fTypeCandidates = null;
92     
93     //widgets
94
private TableViewer fViewer;
95     private Button fNewButton = null;
96     private Button fEditButton = null;
97     private Button fDuplicateButton = null;
98     private Button fDeleteButton = null;
99     
100     /* (non-Javadoc)
101      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
102      */

103     protected Control createContents(Composite parent) {
104         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IDebugHelpContextIds.RUN_DEBUG_RESOURCE_PROPERTY_PAGE);
105         collectConfigCandidates(getResource());
106         Composite topComposite = SWTFactory.createComposite(parent, 2, 1, GridData.FILL_BOTH);
107         
108         SWTFactory.createWrapLabel(topComposite, DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_0, 2, 300);
109         SWTFactory.createVerticalSpacer(topComposite, 2);
110         SWTFactory.createWrapLabel(topComposite, MessageFormat.format(DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_1, new String JavaDoc[]{getResource().getName()}), 2, 300);
111         fViewer = createViewer(topComposite);
112         
113         Composite buttonComp = SWTFactory.createComposite(topComposite, 1, 1, GridData.FILL_VERTICAL);
114         GridLayout layout = (GridLayout) buttonComp.getLayout();
115         layout.marginHeight = 0;
116         fNewButton = SWTFactory.createPushButton(buttonComp, DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_2, null);
117         fNewButton.setToolTipText(DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_3);
118         fNewButton.addSelectionListener(new SelectionListener() {
119             public void widgetDefaultSelected(SelectionEvent e) {}
120             public void widgetSelected(SelectionEvent e) {
121                 handleNew();
122             }
123         });
124         
125         fDuplicateButton = SWTFactory.createPushButton(buttonComp, DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_4, null);
126         fDuplicateButton.setToolTipText(DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_5);
127         fDuplicateButton.setEnabled(false);
128         fDuplicateButton.addSelectionListener(new SelectionListener() {
129             public void widgetDefaultSelected(SelectionEvent e) {}
130             public void widgetSelected(SelectionEvent e) {
131                 handleCopy();
132             }
133         });
134         fEditButton = SWTFactory.createPushButton(buttonComp, DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_6, null);
135         fEditButton.setToolTipText(DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_7);
136         fEditButton.setEnabled(false);
137         fEditButton.addSelectionListener(new SelectionListener() {
138             public void widgetDefaultSelected(SelectionEvent e) {}
139             public void widgetSelected(SelectionEvent e) {
140                 handleEdit();
141             }
142         });
143         fDeleteButton = SWTFactory.createPushButton(buttonComp, DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_8, null);
144         fDeleteButton.setToolTipText(DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_9);
145         fDeleteButton.setEnabled(false);
146         fDeleteButton.addSelectionListener(new SelectionListener() {
147             public void widgetDefaultSelected(SelectionEvent e) {}
148             public void widgetSelected(SelectionEvent e) {
149                 handleDelete();
150             }
151         });
152         
153         fViewer.setSelection(new StructuredSelection());
154         applyDialogFont(topComposite);
155         return topComposite;
156     }
157
158     /**
159      * Creates and returns the viewer that will display the possible default configurations.
160      *
161      * @param parent parent composite to create the viewer in
162      * @return viewer viewer that will display possible default configurations
163      */

164     protected TableViewer createViewer(Composite parent){
165         TableViewer viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
166         viewer.setLabelProvider(new DefaultLabelProvider());
167         viewer.setContentProvider(new ArrayContentProvider());
168         viewer.setComparator(new LaunchConfigurationComparator());
169         Table builderTable = viewer.getTable();
170         GridData tableGridData = new GridData(GridData.FILL_BOTH);
171         tableGridData.heightHint = 300;
172         builderTable.setLayoutData(tableGridData);
173         IResource resource = getResource();
174         viewer.setInput(collectConfigCandidates(resource));
175         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
176             public void selectionChanged(SelectionChangedEvent event) {
177                 ISelection sel = event.getSelection();
178                 if(sel instanceof IStructuredSelection) {
179                     IStructuredSelection ss = (IStructuredSelection) sel;
180                     boolean empty = ss.isEmpty();
181                     int size = ss.size();
182                     fEditButton.setEnabled(!empty && size == 1);
183                     fDuplicateButton.setEnabled(!empty && size == 1);
184                     fDeleteButton.setEnabled(!empty);
185                 }
186             }
187         });
188         return viewer;
189     }
190
191     /**
192      * @see org.eclipse.jface.dialogs.DialogPage#dispose()
193      */

194     public void dispose() {
195         if(fOriginalCandidates != null) {
196             fOriginalCandidates.clear();
197             fOriginalCandidates = null;
198         }
199         if(fChangedConfigurations != null) {
200             fChangedConfigurations.clear();
201             fChangedConfigurations = null;
202         }
203         super.dispose();
204     }
205
206     /**
207      * Returns the viewer displaying possible default configurations.
208      *
209      * @return viewer
210      */

211     protected TableViewer getViewer() {
212         return fViewer;
213     }
214         
215     /**
216      * Returns the launch manager
217      * @return the launch manager
218      */

219     protected LaunchManager getLaunchManager() {
220         return (LaunchManager) DebugPlugin.getDefault().getLaunchManager();
221     }
222     
223     /**
224      * Collects the applicable launch configuration types for the backing resource.
225      * Default implementation uses the launch shortcut evaluation expressions and leverages the
226      * mapping of launch shortcut to config type id to derive the applicable types.
227      * @return the listing of applicable launch configuration types for the backing resource
228      */

229     protected List JavaDoc collectTypeCandidates() {
230         if(fTypeCandidates == null) {
231             fTypeCandidates = DebugUIPlugin.getDefault().getLaunchConfigurationManager().getApplicableConfigurationTypes(getResource());
232             Collections.sort(fTypeCandidates, new Comparator JavaDoc() {
233                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
234                     ILaunchConfigurationType t1 = (ILaunchConfigurationType) o1;
235                     ILaunchConfigurationType t2 = (ILaunchConfigurationType) o2;
236                     return t1.getName().compareTo(t2.getName());
237                 }
238             });
239         }
240         return fTypeCandidates;
241     }
242     
243     /**
244      * Returns a set of potential default configurations candidates for the given
245      * resource. The configurations are working copies.
246      *
247      * @param resource resource
248      * @return list of default candidates
249      */

250     protected Set JavaDoc collectConfigCandidates(IResource resource) {
251         if(fOriginalCandidates == null) {
252             fOriginalCandidates = new HashSet JavaDoc();
253             try {
254                 List JavaDoc configs = DebugUIPlugin.getDefault().getLaunchConfigurationManager().getApplicableLaunchConfigurations(resource);
255                 for(Iterator JavaDoc iter = configs.iterator(); iter.hasNext();) {
256                     fOriginalCandidates.add(((ILaunchConfiguration)iter.next()).getWorkingCopy());
257                 }
258             }
259             catch(CoreException ce) {DebugUIPlugin.log(ce);}
260         }
261         return fOriginalCandidates;
262     }
263     
264     
265     
266     /**
267      * Returns the resource this property page is open on.
268      *
269      * @return resource
270      */

271     protected IResource getResource() {
272         Object JavaDoc element = getElement();
273         IResource resource = null;
274         if (element instanceof IResource) {
275             resource = (IResource) element;
276         } else if (element instanceof IAdaptable) {
277             resource = (IResource) ((IAdaptable)element).getAdapter(IResource.class);
278         }
279         return resource;
280     }
281
282     /* (non-Javadoc)
283      * @see org.eclipse.jface.preference.PreferencePage#performOk()
284      */

285     public boolean performOk() {
286     //delete
287
Iterator JavaDoc iter = fDeletedConfigurations.iterator();
288         while (iter.hasNext()) {
289             ILaunchConfigurationWorkingCopy currentConfig = (ILaunchConfigurationWorkingCopy) iter.next();
290             try{
291                 if (currentConfig.getOriginal() != null){
292                     currentConfig.getOriginal().delete();
293                 }
294             } catch (CoreException e) {
295                 DebugPlugin.logMessage("Problem deleting configuration " + currentConfig.getName(), e); //$NON-NLS-1$
296
}
297         }
298     //add
299
iter = fChangedConfigurations.iterator();
300         while (iter.hasNext()) {
301             ILaunchConfigurationWorkingCopy currentConfig = (ILaunchConfigurationWorkingCopy) iter.next();
302             try{
303                 currentConfig.doSave();
304             } catch (CoreException e) {
305                 DebugPlugin.logMessage("Problem saving changes to configuration " + currentConfig.getName(), e); //$NON-NLS-1$
306
}
307         }
308         
309         return super.performOk();
310     }
311
312     /* (non-Javadoc)
313      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
314      */

315     protected void performDefaults() {
316         setErrorMessage(null);
317         setValid(true);
318         fOriginalCandidates.clear();
319         fOriginalCandidates = null;
320         getViewer().setInput(collectConfigCandidates(getResource()));
321         fChangedConfigurations.clear();
322         fDeletedConfigurations.clear();
323         fViewer.refresh(true, true);
324         super.performDefaults();
325     }
326     
327     /**
328      * Returns the names of the launch configurations passed in as original input to the tree viewer
329      * @return the names of the original launch configurations
330      */

331     private Set JavaDoc getConfigurationNames() {
332         Set JavaDoc names = new HashSet JavaDoc();
333         Iterator JavaDoc iter = fOriginalCandidates.iterator();
334         while (iter.hasNext()) {
335             names.add(((ILaunchConfiguration)iter.next()).getName());
336         }
337         iter = fChangedConfigurations.iterator();
338         while (iter.hasNext()) {
339             names.add(((ILaunchConfiguration)iter.next()).getName());
340         }
341         return names;
342     }
343     
344     /**
345      * Returns selected configurations.
346      *
347      * @return selected configurations
348      */

349     private ILaunchConfigurationWorkingCopy[] getSelectedConfigurations() {
350         IStructuredSelection ss = (IStructuredSelection) fViewer.getSelection();
351         return (ILaunchConfigurationWorkingCopy[]) ss.toList().toArray(new ILaunchConfigurationWorkingCopy[ss.size()]);
352     }
353
354     /**
355      * Copy the selection
356      */

357     private void handleCopy() {
358         ILaunchConfigurationWorkingCopy configuration = getSelectedConfigurations()[0];
359         try {
360             ILaunchConfigurationWorkingCopy copy = configuration.copy(
361                     ((LaunchManager)DebugPlugin.getDefault().getLaunchManager()).generateUniqueLaunchConfigurationNameFrom(configuration.getName(), getConfigurationNames()));
362             copy.setAttributes(configuration.getAttributes());
363             fChangedConfigurations.add(copy);
364             fViewer.add(copy);
365             fViewer.setSelection(new StructuredSelection(copy));
366         } catch (CoreException e) {
367             setErrorMessage(e.getMessage());
368         }
369     }
370
371     /**
372      * Delete the selection
373      */

374     private void handleDelete() {
375         Table table = fViewer.getTable();
376         int[] indices = table.getSelectionIndices();
377         Arrays.sort(indices);
378         ILaunchConfiguration[] configurations = getSelectedConfigurations();
379         for (int i = 0; i < configurations.length; i++) {
380             fDeletedConfigurations.add(configurations[i]);
381             fChangedConfigurations.remove(configurations[i]);
382             fViewer.remove(configurations[i]);
383         }
384         if (indices[0] < table.getItemCount()) {
385             fViewer.setSelection(new StructuredSelection(table.getItem(indices[0]).getData()));
386         } else if (table.getItemCount() > 0) {
387             fViewer.setSelection(new StructuredSelection(table.getItem(table.getItemCount() - 1).getData()));
388         }
389     }
390
391     /**
392      * Edit the selection
393      */

394     private void handleEdit() {
395         ILaunchConfigurationWorkingCopy config = getSelectedConfigurations()[0];
396         if(edit(config, false) == IDialogConstants.OK_ID) {
397             fChangedConfigurations.add(config);
398             fViewer.refresh(config, true, true);
399         }
400     }
401
402     /**
403      * Edits the given configuration as a nested working copy.
404      * Returns the code from the dialog used to edit the configuration.
405      *
406      * @param configuration
407      * @param setDefaults whether to set default values in the config
408      * @return dialog return code - OK or CANCEL
409      */

410     private int edit(ILaunchConfigurationWorkingCopy configuration, boolean setDefaults) {
411         return DebugUIPlugin.openLaunchConfigurationPropertiesDialog(getShell(), configuration, IDebugUIConstants.ID_RUN_LAUNCH_GROUP, getConfigurationNames(), null, setDefaults);
412     }
413
414     /**
415      * Create a new configuration
416      */

417     private void handleNew() {
418         
419         final List JavaDoc typeCandidates = collectTypeCandidates();
420         
421         SelectionDialog dialog = new AbstractDebugListSelectionDialog(getShell()){
422
423             /* (non-Javadoc)
424              * @see org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugSelectionDialog#getDialogSettingsId()
425              */

426             protected String JavaDoc getDialogSettingsId() {
427                 return DebugUIPlugin.getUniqueIdentifier() + ".SELECT_CONFIGURATION_TYPE_DIALOG"; //$NON-NLS-1$
428
}
429
430             /* (non-Javadoc)
431              * @see org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugSelectionDialog#getViewerInput()
432              */

433             protected Object JavaDoc getViewerInput() {
434                 return typeCandidates;
435             }
436             
437             /* (non-Javadoc)
438              * @see org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugSelectionDialog#getHelpContextId()
439              */

440             protected String JavaDoc getHelpContextId() {
441                 return IDebugHelpContextIds.SELECT_CONFIGURATION_TYPE_DIALOG;
442             }
443
444             /* (non-Javadoc)
445              * @see org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugSelectionDialog#getViewerLabel()
446              */

447             protected String JavaDoc getViewerLabel() {
448                 return DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_12;
449             }
450                 
451         };
452         dialog.setTitle(DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_11);
453
454         if (dialog.open() == Window.OK) {
455             Object JavaDoc[] result = dialog.getResult();
456             if (result.length == 1) {
457                 ILaunchConfigurationType type = (ILaunchConfigurationType) result[0];
458                 try {
459                     ILaunchConfigurationWorkingCopy wc = type.newInstance(null,
460                             ((LaunchManager)DebugPlugin.getDefault().getLaunchManager()).
461                             generateUniqueLaunchConfigurationNameFrom("New_configuration", getConfigurationNames())); //$NON-NLS-1$
462
if (edit(wc, true) == Window.OK) {
463                         fChangedConfigurations.add(wc);
464                         fViewer.add(wc);
465                         fViewer.setSelection(new StructuredSelection(wc));
466                     }
467                 } catch (CoreException e) {
468                     setErrorMessage(e.getMessage());
469                 }
470             }
471         }
472     }
473 }
474
Popular Tags