KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > ConfigurationTab


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.pde.internal.ui.launcher;
12
13 import java.io.File JavaDoc;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.debug.core.ILaunchConfiguration;
21 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.viewers.Viewer;
24 import org.eclipse.jface.viewers.ViewerFilter;
25 import org.eclipse.pde.internal.core.PDECore;
26 import org.eclipse.pde.internal.ui.IHelpContextIds;
27 import org.eclipse.pde.internal.ui.PDEPlugin;
28 import org.eclipse.pde.internal.ui.PDEPluginImages;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.pde.internal.ui.util.SWTUtil;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.ModifyEvent;
33 import org.eclipse.swt.events.ModifyListener;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.graphics.Image;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.DirectoryDialog;
42 import org.eclipse.swt.widgets.Group;
43 import org.eclipse.swt.widgets.Label;
44 import org.eclipse.swt.widgets.Text;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
47 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
48 import org.eclipse.ui.model.WorkbenchContentProvider;
49 import org.eclipse.ui.model.WorkbenchLabelProvider;
50
51 public class ConfigurationTab extends AbstractLauncherTab implements ILauncherSettings {
52     private Button fClearConfig;
53     private Image fImage;
54     
55     private Button fUseDefaultLocationButton;
56     private Label fConfigAreaLabel;
57     private Text fConfigAreaText;
58     private Button fConfigAreaBrowse;
59     
60     private Button fGenerateFileButton;
61     private Button fUseTemplateButton;
62     private Label fTemplateLocationLabel;
63     private Text fTemplateLocationText;
64     private Button fTemplateLocationBrowse;
65     
66     private String JavaDoc fLastEnteredConfigArea = ""; //$NON-NLS-1$
67
private String JavaDoc fConfigName;
68     private boolean fBlockChanges;
69     private boolean fJUnitConfig;
70     
71     
72     public ConfigurationTab() {
73         this(false);
74     }
75     
76     public ConfigurationTab(boolean isJUnitConfig) {
77         fImage = PDEPluginImages.DESC_PLUGIN_CONFIG_OBJ.createImage();
78         fJUnitConfig = isJUnitConfig;
79     }
80     /* (non-Javadoc)
81      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
82      */

83     public void createControl(Composite parent) {
84         Composite container = new Composite(parent, SWT.NONE);
85         container.setLayout(new GridLayout());
86         container.setLayoutData(new GridData(GridData.FILL_BOTH));
87         
88         createConfigAreaGroup(container);
89         createStartingSpace(container, 1);
90         createConfigFileGroup(container);
91         
92         Dialog.applyDialogFont(container);
93         setControl(container);
94         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.LAUNCHER_CONFIGURATION);
95     }
96     
97     private void createConfigAreaGroup(Composite parent) {
98         Group group = new Group(parent, SWT.NONE);
99         group.setText(PDEUIMessages.ConfigurationTab_configAreaGroup); //$NON-NLS-1$
100
group.setLayout(new GridLayout(3, false));
101         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
102         
103         fUseDefaultLocationButton = new Button(group, SWT.CHECK);
104         GridData gd = new GridData();
105         gd.horizontalSpan = 3;
106         fUseDefaultLocationButton.setLayoutData(gd);
107         fUseDefaultLocationButton.setText(PDEUIMessages.ConfigurationTab_useDefaultLoc); //$NON-NLS-1$
108
fUseDefaultLocationButton.addSelectionListener(new SelectionAdapter() {
109             public void widgetSelected(SelectionEvent e) {
110                 boolean selected = fUseDefaultLocationButton.getSelection();
111                 fConfigAreaLabel.setEnabled(!selected);
112                 fConfigAreaText.setEnabled(!selected);
113                 fConfigAreaBrowse.setEnabled(!selected);
114                 if (!selected)
115                     fConfigAreaText.setText(fLastEnteredConfigArea);
116                 else
117                     fConfigAreaText.setText(PDECore.getDefault().getStateLocation().append(fConfigName).toOSString());
118                 updateStatus();
119             }
120         });
121         
122         fConfigAreaLabel = new Label(group, SWT.NONE);
123         fConfigAreaLabel.setText(PDEUIMessages.ConfigurationTab_configLog); //$NON-NLS-1$
124
gd = new GridData();
125         gd.horizontalIndent = 20;
126         fConfigAreaLabel.setLayoutData(gd);
127         
128         fConfigAreaText = new Text(group, SWT.SINGLE|SWT.BORDER);
129         gd = new GridData(GridData.FILL_HORIZONTAL);
130         gd.widthHint = 300;
131         fConfigAreaText.setLayoutData(gd);
132         fConfigAreaText.addModifyListener(new ModifyListener() {
133             public void modifyText(ModifyEvent e) {
134                 if (!fBlockChanges)
135                     updateStatus();
136             }
137         });
138         
139         fConfigAreaBrowse = new Button(group, SWT.PUSH);
140         fConfigAreaBrowse.setText(PDEUIMessages.ConfigurationTab_configBrowse); //$NON-NLS-1$
141
fConfigAreaBrowse.setLayoutData(new GridData());
142         SWTUtil.setButtonDimensionHint(fConfigAreaBrowse);
143         fConfigAreaBrowse.addSelectionListener(new SelectionAdapter() {
144             public void widgetSelected(SelectionEvent e) {
145                 handleBrowseDirectory();
146             }
147         });
148
149         fClearConfig = new Button(group, SWT.CHECK);
150         fClearConfig.setText(PDEUIMessages.ConfigurationTab_clearArea); //$NON-NLS-1$
151
gd = new GridData();
152         gd.horizontalSpan = 3;
153         fClearConfig.setLayoutData(gd);
154         fClearConfig.addSelectionListener(new SelectionAdapter() {
155             public void widgetSelected(SelectionEvent e) {
156                 updateLaunchConfigurationDialog();
157             }
158         });
159     }
160     
161     protected void handleBrowseDirectory() {
162         DirectoryDialog dialog = new DirectoryDialog(getControl().getShell());
163         dialog.setFilterPath(fConfigAreaText.getText().trim());
164         dialog.setText(PDEUIMessages.ConfigurationTab_configLocTitle); //$NON-NLS-1$
165
dialog.setMessage(PDEUIMessages.ConfigurationTab_configLocMessage); //$NON-NLS-1$
166
String JavaDoc res = dialog.open();
167         if (res != null)
168             fConfigAreaText.setText(res);
169     }
170
171     protected void updateStatus() {
172         if (!fUseDefaultLocationButton.getSelection() && fConfigAreaText.getText().trim().length() == 0) {
173             updateStatus(createStatus(IStatus.ERROR, PDEUIMessages.ConfigurationTab_noConfigLoc)); //$NON-NLS-1$
174
return;
175         }
176         
177         if (fUseTemplateButton.getSelection()) {
178             String JavaDoc location = fTemplateLocationText.getText().trim();
179             if (location.length() == 0) {
180                 updateStatus(createStatus(IStatus.ERROR, PDEUIMessages.ConfigurationTab_noTemplateLoc)); //$NON-NLS-1$
181
return;
182             }
183             File JavaDoc file = new File JavaDoc(location);
184             if (!file.exists() || !file.isFile()) {
185                 updateStatus(createStatus(IStatus.ERROR, PDEUIMessages.ConfigurationTab_templateNotExists)); //$NON-NLS-1$
186
return;
187             }
188         }
189         updateStatus(createStatus(IStatus.OK, "")); //$NON-NLS-1$
190
}
191
192     private void createConfigFileGroup(Composite parent) {
193         Group group = new Group(parent, SWT.NONE);
194         group.setText(PDEUIMessages.ConfigurationTab_configFileGroup); //$NON-NLS-1$
195
group.setLayout(new GridLayout(3, false));
196         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
197         
198         fGenerateFileButton = new Button(group, SWT.RADIO);
199         fGenerateFileButton.setText(PDEUIMessages.ConfigurationTab_defaultConfigIni); //$NON-NLS-1$
200
GridData gd = new GridData();
201         gd.horizontalSpan = 3;
202         fGenerateFileButton.setLayoutData(gd);
203         fGenerateFileButton.addSelectionListener(new SelectionAdapter() {
204             public void widgetSelected(SelectionEvent e) {
205                 boolean selected = fGenerateFileButton.getSelection();
206                 fTemplateLocationLabel.setEnabled(!selected);
207                 fTemplateLocationText.setEnabled(!selected);
208                 fTemplateLocationBrowse.setEnabled(!selected);
209                 updateStatus();
210             }
211         });
212         
213         fUseTemplateButton = new Button(group, SWT.RADIO);
214         fUseTemplateButton.setText(PDEUIMessages.ConfigurationTab_existingConfigIni); //$NON-NLS-1$
215
gd = new GridData();
216         gd.horizontalSpan = 3;
217         fUseTemplateButton.setLayoutData(gd);
218         
219         fTemplateLocationLabel = new Label(group, SWT.NONE);
220         fTemplateLocationLabel.setText(PDEUIMessages.ConfigurationTab_templateLoc); //$NON-NLS-1$
221
gd = new GridData();
222         gd.horizontalIndent = 20;
223         fTemplateLocationLabel.setLayoutData(gd);
224         
225         fTemplateLocationText = new Text(group, SWT.SINGLE|SWT.BORDER);
226         gd = new GridData(GridData.FILL_HORIZONTAL);
227         gd.widthHint = 300;
228         fTemplateLocationText.setLayoutData(gd);
229         fTemplateLocationText.addModifyListener(new ModifyListener() {
230             public void modifyText(ModifyEvent e) {
231                 if (!fBlockChanges)
232                     updateStatus();
233             }
234         });
235         
236         fTemplateLocationBrowse = new Button(group, SWT.PUSH);
237         fTemplateLocationBrowse.setText(PDEUIMessages.ConfigurationTab_templateBrowse); //$NON-NLS-1$
238
fTemplateLocationBrowse.setLayoutData(new GridData());
239         SWTUtil.setButtonDimensionHint(fTemplateLocationBrowse);
240         fTemplateLocationBrowse.addSelectionListener(new SelectionAdapter() {
241             public void widgetSelected(SelectionEvent e) {
242                 handleBrowseWorkspaceFile();
243             }
244         });
245     }
246     
247     protected void handleBrowseWorkspaceFile() {
248         ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
249                 getControl().getShell(), new WorkbenchLabelProvider(),
250                 new WorkbenchContentProvider());
251         
252         IFile file = PDEPlugin.getWorkspace().getRoot().getFileForLocation(new Path(fTemplateLocationText.getText()));
253         if (file != null)
254             dialog.setInitialSelection(file);
255         dialog.setInput(PDEPlugin.getWorkspace().getRoot());
256         dialog.addFilter(new ViewerFilter() {
257             public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
258                 if (element instanceof IFile)
259                     return ((IFile)element).getName().equals("config.ini"); //$NON-NLS-1$
260
return true;
261             }
262         });
263         dialog.setAllowMultiple(false);
264         dialog.setTitle(PDEUIMessages.ConfigurationTab_fileSelection); //$NON-NLS-1$
265
dialog.setMessage(PDEUIMessages.ConfigurationTab_fileDialogMessage); //$NON-NLS-1$
266
dialog.setValidator(new ISelectionStatusValidator() {
267             public IStatus validate(Object JavaDoc[] selection) {
268                 if (selection != null && selection.length > 0
269                         && selection[0] instanceof IFile)
270                     return new Status(IStatus.OK, PDEPlugin.getPluginId(),
271                             IStatus.OK, "", null); //$NON-NLS-1$
272

273                 return new Status(IStatus.ERROR, PDEPlugin.getPluginId(),
274                         IStatus.ERROR, "", null); //$NON-NLS-1$
275
}
276         });
277         if (dialog.open() == ElementTreeSelectionDialog.OK) {
278             file = (IFile) dialog.getFirstResult();
279             fTemplateLocationText.setText(file.getLocation().toOSString());
280         }
281     }
282
283     /* (non-Javadoc)
284      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
285      */

286     public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
287         configuration.setAttribute(CONFIG_USE_DEFAULT_AREA, true);
288         configuration.setAttribute(CONFIG_LOCATION, ""); //$NON-NLS-1$
289
configuration.setAttribute(CONFIG_CLEAR_AREA, fJUnitConfig);
290         configuration.setAttribute(CONFIG_GENERATE_DEFAULT, true);
291         configuration.setAttribute(CONFIG_TEMPLATE_LOCATION, ""); //$NON-NLS-1$
292
}
293     /* (non-Javadoc)
294      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
295      */

296     public void initializeFrom(ILaunchConfiguration configuration) {
297         try {
298             fBlockChanges = true;
299             boolean useDefaultArea = configuration.getAttribute(CONFIG_USE_DEFAULT_AREA, true);
300             fUseDefaultLocationButton.setSelection(useDefaultArea);
301             fConfigAreaLabel.setEnabled(!useDefaultArea);
302             fConfigAreaText.setEnabled(!useDefaultArea);
303             fConfigAreaBrowse.setEnabled(!useDefaultArea);
304             fClearConfig.setSelection(configuration.getAttribute(CONFIG_CLEAR_AREA, false));
305             fConfigName = configuration.getName();
306             fLastEnteredConfigArea = configuration.getAttribute(CONFIG_LOCATION, ""); //$NON-NLS-1$
307
if (useDefaultArea)
308                 fConfigAreaText.setText(PDECore.getDefault().getStateLocation().append(configuration.getName()).toOSString());
309             else
310                 fConfigAreaText.setText(fLastEnteredConfigArea);
311             
312             boolean generateDefault = configuration.getAttribute(CONFIG_GENERATE_DEFAULT, true);
313             fGenerateFileButton.setSelection(generateDefault);
314             fUseTemplateButton.setSelection(!generateDefault);
315             fTemplateLocationLabel.setEnabled(!generateDefault);
316             fTemplateLocationText.setEnabled(!generateDefault);
317             fTemplateLocationBrowse.setEnabled(!generateDefault);
318             fTemplateLocationText.setText(configuration.getAttribute(CONFIG_TEMPLATE_LOCATION, "")); //$NON-NLS-1$
319
} catch (CoreException e) {
320         } finally {
321             fBlockChanges = false;
322         }
323         updateStatus();
324     }
325     
326     /* (non-Javadoc)
327      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
328      */

329     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
330         configuration.setAttribute(CONFIG_USE_DEFAULT_AREA, fUseDefaultLocationButton.getSelection());
331         if (!fUseDefaultLocationButton.getSelection()) {
332             fLastEnteredConfigArea = fConfigAreaText.getText().trim();
333             configuration.setAttribute(CONFIG_LOCATION, fLastEnteredConfigArea);
334         }
335         configuration.setAttribute(CONFIG_CLEAR_AREA, fClearConfig.getSelection());
336         configuration.setAttribute(CONFIG_GENERATE_DEFAULT, fGenerateFileButton.getSelection());
337         if (!fGenerateFileButton.getSelection())
338             configuration.setAttribute(CONFIG_TEMPLATE_LOCATION, fTemplateLocationText.getText().trim());
339     }
340     /* (non-Javadoc)
341      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
342      */

343     public String JavaDoc getName() {
344         return PDEUIMessages.ConfigurationTab_name; //$NON-NLS-1$
345
}
346     
347     /* (non-Javadoc)
348      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#getImage()
349      */

350     public Image getImage() {
351         return fImage;
352     }
353     
354     /* (non-Javadoc)
355      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#dispose()
356      */

357     public void dispose() {
358         if (fImage != null)
359             fImage.dispose();
360     }
361 }
362
Popular Tags