KickJava   Java API By Example, From Geeks To Geeks.

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


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.launchConfigurations;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
18 import org.eclipse.debug.core.ILaunchDelegate;
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
21 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
22 import org.eclipse.debug.internal.ui.SWTFactory;
23 import org.eclipse.debug.ui.IDebugUIConstants;
24 import org.eclipse.jface.dialogs.IDialogConstants;
25 import org.eclipse.jface.dialogs.IDialogSettings;
26 import org.eclipse.jface.viewers.CheckStateChangedEvent;
27 import org.eclipse.jface.viewers.CheckboxTableViewer;
28 import org.eclipse.jface.viewers.IBaseLabelProvider;
29 import org.eclipse.jface.viewers.ICheckStateListener;
30 import org.eclipse.jface.viewers.ILabelProvider;
31 import org.eclipse.jface.viewers.ILabelProviderListener;
32 import org.eclipse.jface.viewers.ISelectionChangedListener;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.SelectionChangedEvent;
35 import org.eclipse.jface.viewers.StructuredSelection;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.events.SelectionListener;
39 import org.eclipse.swt.graphics.Image;
40 import org.eclipse.swt.graphics.Point;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.widgets.Button;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Group;
45 import org.eclipse.swt.widgets.Link;
46 import org.eclipse.swt.widgets.Shell;
47 import org.eclipse.swt.widgets.Text;
48
49 /**
50  * This dialog is used to select a preferred launcher, and also provides access to the
51  * workspace defaults for preferred launchers
52  *
53  * @since 3.3
54  */

55 public class SelectLaunchersDialog extends AbstractDebugCheckboxSelectionDialog {
56
57     /**
58      * Builds labels for table control
59      */

60     class DelegatesLabelProvider implements ILabelProvider {
61         public Image getImage(Object JavaDoc element) {return null;}
62         public String JavaDoc getText(Object JavaDoc element) {
63             if(element instanceof ILaunchDelegate) {
64                 ILaunchDelegate ldp = (ILaunchDelegate) element;
65                 String JavaDoc name = ldp.getName();
66                 if(name == null) {
67                     name = ldp.getContributorName();
68                 }
69                 return name;
70             }
71             return element.toString();
72         }
73         public void addListener(ILabelProviderListener listener) {}
74         public void dispose() {}
75         public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {return false;}
76         public void removeListener(ILabelProviderListener listener) {}
77     }
78     
79     private Text fDescriptionText = null;
80     private ILaunchDelegate[] fDelegates = null;
81     private Button fUseSystemLauncher = null;
82     private ILaunchConfigurationWorkingCopy fConfiguration = null;
83     private String JavaDoc fLaunchMode = null;
84     
85     /**
86      * Constructor
87      * @param parentShell
88      */

89     public SelectLaunchersDialog(Shell parentShell, ILaunchDelegate[] delegates, ILaunchConfigurationWorkingCopy configuration, String JavaDoc launchmode) {
90         super(parentShell);
91         super.setTitle(LaunchConfigurationsMessages.SelectLaunchersDialog_0);
92         setShellStyle(getShellStyle() | SWT.RESIZE);
93         fDelegates = delegates;
94         fConfiguration = configuration;
95         fLaunchMode = launchmode;
96     }
97
98     /* (non-Javadoc)
99      * @see org.eclipse.jface.dialogs.Dialog#getInitialSize()
100      */

101     protected Point getInitialSize() {
102         IDialogSettings settings = getDialogBoundsSettings();
103         if(settings != null) {
104             try {
105                 int width = settings.getInt("DIALOG_WIDTH"); //$NON-NLS-1$
106
int height = settings.getInt("DIALOG_HEIGHT"); //$NON-NLS-1$
107
if(width > 0 & height > 0) {
108                     return new Point(width, height);
109                 }
110             }
111             catch (NumberFormatException JavaDoc nfe) {
112                 return new Point(450, 450);
113             }
114         }
115         return new Point(450, 450);
116     }
117     
118     /* (non-Javadoc)
119      * @see org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugSelectionDialog#getDialogSettingsId()
120      */

121     protected String JavaDoc getDialogSettingsId() {
122         return IDebugUIConstants.PLUGIN_ID + ".SELECT_LAUNCHERS_DIALOG"; //$NON-NLS-1$
123
}
124
125     /* (non-Javadoc)
126      * @see org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugSelectionDialog#getHelpContextId()
127      */

128     protected String JavaDoc getHelpContextId() {
129         return IDebugHelpContextIds.SELECT_LAUNCHERS_DIALOG;
130     }
131
132     /* (non-Javadoc)
133      * @see org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugSelectionDialog#getLabelProvider()
134      */

135     protected IBaseLabelProvider getLabelProvider() {
136         return new DelegatesLabelProvider();
137     }
138
139     /* (non-Javadoc)
140      * @see org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugSelectionDialog#getViewerInput()
141      */

142     protected Object JavaDoc getViewerInput() {
143         return fDelegates;
144     }
145     
146     /* (non-Javadoc)
147      * @see org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugCheckboxSelectionDialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
148      */

149     protected void createButtonsForButtonBar(Composite parent) {
150         super.createButtonsForButtonBar(parent);
151         // Even if a user does not change anything, they can still press ok
152
getButton(IDialogConstants.OK_ID).setEnabled(true);
153     }
154     
155     /* (non-Javadoc)
156      * @see org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugSelectionDialog#addCustomHeaderControls(org.eclipse.swt.widgets.Composite)
157      */

158     protected void addCustomHeaderControls(Composite parent) {
159         SWTFactory.createWrapLabel(parent, LaunchConfigurationsMessages.SelectLaunchersDialog_2, 1);
160         Link link = new Link(parent, SWT.WRAP);
161         link.setText(LaunchConfigurationsMessages.SelectLaunchersDialog_4);
162         link.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));
163         link.addSelectionListener(new SelectionListener() {
164             public void widgetDefaultSelected(SelectionEvent e) {}
165             public void widgetSelected(SelectionEvent e) {
166                 SWTFactory.showPreferencePage("org.eclipse.debug.ui.LaunchDelegatesPreferencePage"); //$NON-NLS-1$
167
if(!fUseSystemLauncher.getSelection()) {
168                     resetDelegate();
169                 }
170             }
171         });
172         fUseSystemLauncher = SWTFactory.createCheckButton(parent, LaunchConfigurationsMessages.SelectLaunchersDialog_1, null, true, 1);
173         fUseSystemLauncher.addSelectionListener(new SelectionListener() {
174             public void widgetDefaultSelected(SelectionEvent e) {}
175             public void widgetSelected(SelectionEvent e) {
176                 boolean checked = ((Button)e.widget).getSelection();
177                 getCheckBoxTableViewer().getTable().setEnabled(checked);
178                 resetDelegate();
179             }
180         });
181     }
182     
183     /**
184      * Returns the currently checked launch delegate
185      * @return the currently selected launch delegate or <code>null</code> if none are checked
186      */

187     protected ILaunchDelegate getSelectedDelegate() {
188         Object JavaDoc[] checked = getCheckBoxTableViewer().getCheckedElements();
189         if(checked.length > 0) {
190             return (ILaunchDelegate) checked[0];
191         }
192         return null;
193     }
194     
195     /**
196      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
197      */

198     protected void okPressed() {
199         ILaunchDelegate delegate = null;
200         Set JavaDoc modes = getCurrentModeSet();
201         if(fUseSystemLauncher.getSelection()) {
202             delegate = getSelectedDelegate();
203             if(delegate != null) {
204                 fConfiguration.setPreferredLaunchDelegate(modes, delegate.getId());
205             }
206         }
207         else {
208             fConfiguration.setPreferredLaunchDelegate(modes, null);
209         }
210         if(fConfiguration.isDirty()) {
211             try {
212                 fConfiguration.doSave();
213             }
214             catch (CoreException e) {DebugUIPlugin.log(e);}
215         }
216         super.okPressed();
217     }
218
219     /**
220      * Resets the selected and checked delegate in the preferred launcher view part to be the one from the workspace
221      */

222     private void resetDelegate() {
223         if(!fUseSystemLauncher.getSelection()) {
224             try {
225                 ILaunchDelegate preferred = fConfiguration.getType().getPreferredDelegate(getCurrentModeSet());
226                 CheckboxTableViewer viewer = getCheckBoxTableViewer();
227                 if(preferred != null) {
228                     viewer.setSelection(new StructuredSelection(preferred));
229                     viewer.setCheckedElements(new Object JavaDoc[] {preferred});
230                 }
231                 else {
232                     viewer.setSelection(new StructuredSelection());
233                     viewer.setAllChecked(false);
234                 }
235             }
236             catch (CoreException ce) {DebugUIPlugin.log(ce);}
237         }
238     }
239     
240     /* (non-Javadoc)
241      * @see org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugSelectionDialog#addCustomFooterControls(org.eclipse.swt.widgets.Composite)
242      */

243     protected void addCustomFooterControls(Composite parent) {
244         Group group = SWTFactory.createGroup(parent, LaunchConfigurationsMessages.SelectLaunchersDialog_5, 1, 1, GridData.FILL_BOTH);
245         fDescriptionText = SWTFactory.createText(group, SWT.WRAP | SWT.READ_ONLY, 1, GridData.FILL_BOTH);
246         fDescriptionText.setBackground(group.getBackground());
247     }
248
249     /**
250      * @return the complete set of modes that the associated launch configuration is concerned with
251      */

252     protected Set JavaDoc getCurrentModeSet() {
253         Set JavaDoc modes = new HashSet JavaDoc();
254         try {
255             modes = fConfiguration.getModes();
256             modes.add(fLaunchMode);
257         }
258         catch (CoreException ce) {DebugUIPlugin.log(ce);}
259         return modes;
260     }
261     
262     /* (non-Javadoc)
263      * @see org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugSelectionDialog#initializeControls()
264      */

265     protected void initializeControls() {
266         final CheckboxTableViewer viewer = getCheckBoxTableViewer();
267         viewer.addCheckStateListener(new ICheckStateListener() {
268             public void checkStateChanged(CheckStateChangedEvent event) {
269                 viewer.setCheckedElements(new Object JavaDoc[]{event.getElement()});
270             }
271         });
272         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
273             public void selectionChanged(SelectionChangedEvent event) {
274                 IStructuredSelection ss = (IStructuredSelection) event.getSelection();
275                 if(ss != null && !ss.isEmpty()) {
276                     fDescriptionText.setText(((ILaunchDelegate)ss.getFirstElement()).getDescription());
277                 }
278                 else {
279                     fDescriptionText.setText(IInternalDebugUIConstants.EMPTY_STRING);
280                 }
281             }
282         });
283         try {
284             ILaunchDelegate delegate = fConfiguration.getPreferredDelegate(getCurrentModeSet());
285             boolean custom = delegate != null;
286             fUseSystemLauncher.setSelection(custom);
287             if(custom) {
288                 viewer.setSelection(new StructuredSelection(delegate));
289                 viewer.setCheckedElements(new Object JavaDoc[] {delegate});
290             }
291             else {
292                 resetDelegate();
293                 getCheckBoxTableViewer().getTable().setEnabled(false);
294             }
295         }
296         catch (CoreException ce) {DebugUIPlugin.log(ce);}
297     }
298
299     /* (non-Javadoc)
300      * @see org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugSelectionDialog#getViewerLabel()
301      */

302     protected String JavaDoc getViewerLabel() {
303         return null;
304     }
305 }
306
Popular Tags