KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > launchConfigurations > PerspectivesTab


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.launchConfigurations;
12
13 import java.text.MessageFormat JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.ILaunchConfiguration;
20 import org.eclipse.debug.core.ILaunchConfigurationListener;
21 import org.eclipse.debug.core.ILaunchConfigurationType;
22 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
23 import org.eclipse.debug.core.ILaunchMode;
24 import org.eclipse.debug.internal.ui.DebugPluginImages;
25 import org.eclipse.debug.internal.ui.DebugUIPlugin;
26 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
27 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
28 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
29 import org.eclipse.debug.ui.DebugUITools;
30 import org.eclipse.debug.ui.IDebugUIConstants;
31 import org.eclipse.jface.dialogs.Dialog;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.events.ControlAdapter;
34 import org.eclipse.swt.events.ControlEvent;
35 import org.eclipse.swt.events.SelectionAdapter;
36 import org.eclipse.swt.events.SelectionEvent;
37 import org.eclipse.swt.graphics.Image;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.layout.GridLayout;
40 import org.eclipse.swt.widgets.Button;
41 import org.eclipse.swt.widgets.Combo;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Label;
44 import org.eclipse.ui.IPerspectiveDescriptor;
45 import org.eclipse.ui.IPerspectiveRegistry;
46 import org.eclipse.ui.PlatformUI;
47
48 /**
49  * PerspectivesTab
50  */

51 public class PerspectivesTab extends AbstractLaunchConfigurationTab implements ILaunchConfigurationListener {
52     
53     /**
54      * The launch config type this tab pertains to
55      */

56     private ILaunchConfigurationType fType = null;
57     
58     /**
59      * Array containing modes this config type supports
60      */

61     private String JavaDoc[] fModeIds = null;
62             
63     /**
64      * Array of all perspective labels for combo box (including 'None')
65      */

66     private String JavaDoc[] fPerspectiveLabels = null;
67     
68     /**
69      * Map of perspective labels to ids
70      */

71     private Map JavaDoc fPerspectiveIds = null;
72
73     /**
74      * Combo boxes corresponding to modes
75      */

76     private Combo[] fCombos = null;
77     
78     private Button fRestoreDefaults;
79     
80     /**
81      * A selection adapter which responds to widget selections in this tab
82      */

83     private SelectionAdapter fSelectionAdapter= new SelectionAdapter() {
84         public void widgetSelected(SelectionEvent e) {
85             Object JavaDoc source= e.getSource();
86             if (source == fRestoreDefaults) {
87                 handleRestoreDefaultsSelected();
88             }
89             updateLaunchConfigurationDialog();
90         }
91         private void handleRestoreDefaultsSelected() {
92             for (int i = 0; i < fCombos.length; i++) {
93                 String JavaDoc mode = (String JavaDoc)fCombos[i].getData();
94                 String JavaDoc def = DebugUIPlugin.getDefault().getPerspectiveManager().getDefaultLaunchPerspective(getLaunchConfigurationType(), mode);
95                 if (def == null) {
96                     fCombos[i].setText(LaunchConfigurationsMessages.PerspectivesTab_1); //$NON-NLS-1$
97
} else {
98                     IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry();
99                     IPerspectiveDescriptor descriptor = registry.findPerspectiveWithId(def);
100                     fCombos[i].setText(descriptor.getLabel());
101                 }
102             }
103         }
104     };
105     
106     /**
107      * Flag indicating the UI is updating from the config, and should not
108      * update the config in response to the change.
109      */

110     private boolean fInitializing = false;
111     
112     /* (non-Javadoc)
113      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#dispose()
114      */

115     public void dispose() {
116         super.dispose();
117         DebugPlugin.getDefault().getLaunchManager().removeLaunchConfigurationListener(this);
118     }
119
120     /* (non-Javadoc)
121      * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationAdded(org.eclipse.debug.core.ILaunchConfiguration)
122      */

123     public void launchConfigurationAdded(ILaunchConfiguration configuration) {
124     }
125
126     /* (non-Javadoc)
127      * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationChanged(org.eclipse.debug.core.ILaunchConfiguration)
128      */

129     public void launchConfigurationChanged(ILaunchConfiguration configuration) {
130         if (!configuration.isWorkingCopy()) {
131             if (configuration.getName().startsWith(getLaunchConfigurationType().getIdentifier())) {
132                 for (int i = 0; i < fModeIds.length; i++) {
133                     String JavaDoc mode = fModeIds[i];
134                     try {
135                         String JavaDoc persp = configuration.getAttribute(mode, (String JavaDoc)null);
136                         if (persp == null) {
137                             // default
138
persp = IDebugUIConstants.PERSPECTIVE_DEFAULT;
139                         }
140                         DebugUITools.setLaunchPerspective(getLaunchConfigurationType(), mode, persp);
141                     } catch (CoreException e) {
142                         DebugUIPlugin.log(e);
143                     }
144                 }
145             }
146         }
147     }
148
149     /* (non-Javadoc)
150      * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationRemoved(org.eclipse.debug.core.ILaunchConfiguration)
151      */

152     public void launchConfigurationRemoved(ILaunchConfiguration configuration) {
153     }
154
155     /**
156      * Constructs a new tab
157      *
158      * @param type
159      */

160     public PerspectivesTab(ILaunchConfigurationType type) {
161         super();
162         fType = type;
163         DebugPlugin.getDefault().getLaunchManager().addLaunchConfigurationListener(this);
164     }
165
166     /* (non-Javadoc)
167      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
168      */

169     public Image getImage() {
170         return DebugPluginImages.getImage(IInternalDebugUIConstants.IMG_OBJS_PERSPECTIVE_TAB);
171     }
172
173     /* (non-Javadoc)
174      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
175      */

176     public void createControl(Composite parent) {
177         final Composite composite = new Composite(parent, SWT.NONE);
178         setControl(composite);
179         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_PERSPECTIVE_TAB);
180         final GridLayout layout = new GridLayout(2, false);
181         composite.setLayout(layout);
182         GridData gd = new GridData(GridData.FILL_BOTH);
183         composite.setLayoutData(gd);
184         composite.setFont(parent.getFont());
185         
186         Label label = new Label(composite, SWT.LEFT + SWT.WRAP);
187         label.setFont(parent.getFont());
188         label.setText(MessageFormat.format(LaunchConfigurationsMessages.PerspectivesTab_0, new String JavaDoc[]{getLaunchConfigurationType().getName()})); //$NON-NLS-1$
189
final GridData finalGd = new GridData();
190         finalGd.horizontalSpan = 2;
191         label.setLayoutData(finalGd);
192         composite.addControlListener(new ControlAdapter(){
193             public void controlResized(ControlEvent e){
194                 finalGd.widthHint = composite.getClientArea().width - 2*layout.marginWidth;
195                 composite.layout(true);
196             }
197         });
198
199         
200         // init modes
201
ILaunchMode[] modes = DebugPlugin.getDefault().getLaunchManager().getLaunchModes();
202         ArrayList JavaDoc supported = new ArrayList JavaDoc();
203         for (int i = 0; i < modes.length; i++) {
204             ILaunchMode mode = modes[i];
205             if (getLaunchConfigurationType().supportsMode(mode.getIdentifier())) {
206                 supported.add(mode.getIdentifier());
207             }
208         }
209         fModeIds = (String JavaDoc[])supported.toArray(new String JavaDoc[supported.size()]);
210         
211         // init perspective labels
212
IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry();
213         IPerspectiveDescriptor[] descriptors = registry.getPerspectives();
214         fPerspectiveLabels = new String JavaDoc[descriptors.length + 1];
215         fPerspectiveLabels[0] = LaunchConfigurationsMessages.PerspectivesTab_1; //$NON-NLS-1$
216
fPerspectiveIds = new HashMap JavaDoc(descriptors.length);
217         for (int i = 0; i < descriptors.length; i++) {
218             IPerspectiveDescriptor descriptor = descriptors[i];
219             fPerspectiveLabels[i + 1] = descriptor.getLabel();
220             fPerspectiveIds.put(descriptor.getLabel(), descriptor.getId());
221         }
222         
223         // spacer
224
createVerticalSpacer(composite, 2);
225         
226         fCombos = new Combo[fModeIds.length];
227         for (int i = 0; i < fModeIds.length; i++) {
228             label = new Label(composite, SWT.NONE);
229             label.setFont(composite.getFont());
230             gd = new GridData(GridData.BEGINNING);
231             gd.horizontalSpan= 1;
232             label.setLayoutData(gd);
233             String JavaDoc text = DebugPlugin.getDefault().getLaunchManager().getLaunchMode(fModeIds[i]).getLabel();
234             label.setText(MessageFormat.format(LaunchConfigurationsMessages.PerspectivesTab_2, new String JavaDoc[]{text})); //$NON-NLS-1$
235

236             Combo combo = new Combo(composite, SWT.READ_ONLY);
237             combo.setFont(composite.getFont());
238             combo.setItems(fPerspectiveLabels);
239             combo.setData(fModeIds[i]);
240             gd = new GridData(GridData.BEGINNING);
241             combo.setLayoutData(gd);
242             fCombos[i] = combo;
243             combo.addSelectionListener(fSelectionAdapter);
244         }
245         
246         createVerticalSpacer(composite, 2);
247         
248         fRestoreDefaults = createPushButton(composite, LaunchConfigurationsMessages.PerspectivesTab_3, null); //$NON-NLS-1$
249
gd= new GridData(GridData.FILL_BOTH);
250         gd.horizontalSpan= 2;
251         gd.horizontalAlignment= SWT.END;
252         gd.verticalAlignment= SWT.END;
253         fRestoreDefaults.setLayoutData(gd);
254         fRestoreDefaults.addSelectionListener(fSelectionAdapter);
255         
256         Dialog.applyDialogFont(composite);
257     }
258
259     /* (non-Javadoc)
260      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
261      */

262     public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
263         for (int i = 0; i < fModeIds.length; i++) {
264             String JavaDoc mode = fModeIds[i];
265             // null indicates default
266
configuration.setAttribute(mode, (String JavaDoc)null);
267         }
268     }
269
270     /* (non-Javadoc)
271      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
272      */

273     public void initializeFrom(ILaunchConfiguration configuration) {
274         // each perspective is stored with its mode identifier
275
fInitializing = true;
276         IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry();
277         for (int i = 0; i < fModeIds.length; i++) {
278             String JavaDoc mode = fModeIds[i];
279             String JavaDoc persp;
280             try {
281                 persp = configuration.getAttribute(mode, (String JavaDoc)null);
282                 if (persp == null) {
283                     // null indicates default
284
persp = DebugUITools.getLaunchPerspective(getLaunchConfigurationType(), mode);
285                 }
286                 if (IDebugUIConstants.PERSPECTIVE_NONE.equals(persp)) {
287                     persp = null;
288                 }
289                 IPerspectiveDescriptor descriptor = null;
290                 if (persp != null) {
291                     descriptor = registry.findPerspectiveWithId(persp);
292                 }
293                 if (descriptor == null) {
294                     // select none
295
fCombos[i].setText(LaunchConfigurationsMessages.PerspectivesTab_1); //$NON-NLS-1$
296
} else {
297                     fCombos[i].setText(descriptor.getLabel());
298                 }
299             } catch (CoreException e) {
300                 DebugUIPlugin.log(e);
301             }
302         }
303         fInitializing = false;
304
305     }
306
307     /* (non-Javadoc)
308      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
309      */

310     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
311         for (int i = 0; i < fCombos.length; i++) {
312             updateConfigFromCombo(fCombos[i], configuration);
313         }
314     }
315     
316     /**
317      * Updates the configuration based on the user's selection in the
318      * perspective combo by setting the given configurations perspective
319      * attribute.
320      *
321      * @param combo the combo widget
322      * @param workingCopy the launch configuration to update
323      */

324     protected void updateConfigFromCombo(Combo combo, ILaunchConfigurationWorkingCopy workingCopy) {
325         if (!fInitializing) {
326             String JavaDoc mode = (String JavaDoc)combo.getData();
327             String JavaDoc persp = combo.getText();
328             if (persp.equals(LaunchConfigurationsMessages.PerspectivesTab_1)) { //$NON-NLS-1$
329
persp = IDebugUIConstants.PERSPECTIVE_NONE;
330             } else {
331                 persp = (String JavaDoc)fPerspectiveIds.get(persp);
332             }
333             // if the same as default, use null which indicates default
334
String JavaDoc def = DebugUIPlugin.getDefault().getPerspectiveManager().getDefaultLaunchPerspective(getLaunchConfigurationType(), mode);
335             if (def == null) {
336                 def = IDebugUIConstants.PERSPECTIVE_NONE;
337             }
338             if (persp.equals(def)) {
339                 persp = null;
340             }
341             workingCopy.setAttribute(mode, persp);
342         }
343     }
344
345     /* (non-Javadoc)
346      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
347      */

348     public String JavaDoc getName() {
349         return LaunchConfigurationsMessages.PerspectivesTab_7; //$NON-NLS-1$
350
}
351     
352     /**
353      * Returns the launch configuration type this tab was opened on.
354      *
355      * @return launch config type
356      */

357     protected ILaunchConfigurationType getLaunchConfigurationType() {
358         return fType;
359     }
360
361     /* (non-Javadoc)
362      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
363      */

364     public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
365         // do nothing on activation
366
}
367
368     /* (non-Javadoc)
369      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#deactivated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
370      */

371     public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) {
372         // do nothing on deactivation
373
}
374
375     /**
376      * Returns the description associated with the current launch configuration
377      * type in the current mode or <code>null</code> if none.
378      *
379      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getMessage()
380      */

381     public String JavaDoc getMessage() {
382         String JavaDoc description = super.getMessage();
383         if(fType != null) {
384             String JavaDoc mode = getLaunchConfigurationDialog().getMode();
385             LaunchConfigurationPresentationManager manager = LaunchConfigurationPresentationManager.getDefault();
386             LaunchConfigurationTabGroupExtension extension = manager.getExtension(fType.getAttribute("id"), mode); //$NON-NLS-1$
387
description = extension.getDescription(mode);
388         }
389         return description;
390     }
391
392 }
393
Popular Tags