KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.resources.IResource;
17 import org.eclipse.core.resources.IWorkspaceRoot;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.variables.IStringVariableManager;
24 import org.eclipse.core.variables.VariablesPlugin;
25 import org.eclipse.debug.core.ILaunchConfiguration;
26 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.pde.internal.ui.PDEPlugin;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.pde.internal.ui.util.FileNameFilter;
31 import org.eclipse.pde.ui.launcher.AbstractLauncherTab;
32 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.FileDialog;
41 import org.eclipse.swt.widgets.Group;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
44 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
45 import org.eclipse.ui.model.WorkbenchContentProvider;
46 import org.eclipse.ui.model.WorkbenchLabelProvider;
47
48 public class ConfigurationTemplateBlock extends BaseBlock {
49
50     private Button fGenerateFileButton;
51     private Button fUseTemplateButton;
52
53     public ConfigurationTemplateBlock(AbstractLauncherTab tab) {
54         super(tab);
55     }
56     
57     public void createControl(Composite parent) {
58         Group group = new Group(parent, SWT.NONE);
59         group.setText(PDEUIMessages.ConfigurationTab_configFileGroup);
60         group.setLayout(new GridLayout(2, false));
61         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
62         
63         fGenerateFileButton = new Button(group, SWT.RADIO);
64         fGenerateFileButton.setText(PDEUIMessages.ConfigurationTab_defaultConfigIni);
65         GridData gd = new GridData();
66         gd.horizontalSpan = 2;
67         fGenerateFileButton.setLayoutData(gd);
68         fGenerateFileButton.addSelectionListener(new SelectionAdapter() {
69             public void widgetSelected(SelectionEvent e) {
70                 enableBrowseSection(!fGenerateFileButton.getSelection());
71             }
72         });
73         
74         fUseTemplateButton = new Button(group, SWT.RADIO);
75         fUseTemplateButton.setText(PDEUIMessages.ConfigurationTab_existingConfigIni);
76         gd = new GridData();
77         gd.horizontalSpan = 2;
78         fUseTemplateButton.setLayoutData(gd);
79         
80         createText(group, PDEUIMessages.ConfigurationTab_templateLoc, 20);
81
82         Composite buttons = new Composite(group, SWT.NONE);
83         GridLayout layout = new GridLayout(4, false);
84         layout.marginHeight = layout.marginWidth = 0;
85         buttons.setLayout(layout);
86         gd = new GridData(GridData.FILL_HORIZONTAL);
87         gd.horizontalSpan = 2;
88         buttons.setLayoutData(gd);
89
90         Label label = new Label(buttons, SWT.NONE);
91         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
92         
93         createButtons(buttons, new String JavaDoc[] {
94                 PDEUIMessages.BaseBlock_workspaceS, PDEUIMessages.BaseBlock_filesystemS, PDEUIMessages.BaseBlock_variablesS
95         });
96     }
97     
98     public void initializeFrom(ILaunchConfiguration configuration) throws CoreException {
99         boolean generateDefault = configuration.getAttribute(IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, true);
100         fGenerateFileButton.setSelection(generateDefault);
101         fUseTemplateButton.setSelection(!generateDefault);
102         enableBrowseSection(!generateDefault);
103         fLocationText.setText(configuration.getAttribute(IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION, "")); //$NON-NLS-1$
104
}
105     
106     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
107         configuration.setAttribute(IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, fGenerateFileButton.getSelection());
108         if (!fGenerateFileButton.getSelection())
109             configuration.setAttribute(IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION, fLocationText.getText().trim());
110     }
111     
112     public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
113         configuration.setAttribute(IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, true);
114         configuration.setAttribute(IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION,
115                 "${target_home}" + File.separatorChar + "configuration" + File.separatorChar + "config.ini"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
116
}
117     
118
119     protected String JavaDoc getName() {
120         return PDEUIMessages.ConfigurationTemplateBlock_name;
121     }
122         
123     protected void handleBrowseWorkspace() {
124         ElementTreeSelectionDialog dialog =
125                         new ElementTreeSelectionDialog(
126                                 fTab.getControl().getShell(),
127                                 new WorkbenchLabelProvider(),
128                                 new WorkbenchContentProvider());
129         
130         IFile file = getFile();
131         if (file != null)
132             dialog.setInitialSelection(file);
133         dialog.setInput(PDEPlugin.getWorkspace().getRoot());
134         dialog.addFilter(new FileNameFilter("config.ini")); //$NON-NLS-1$
135
dialog.setAllowMultiple(false);
136         dialog.setTitle(PDEUIMessages.ConfigurationTab_fileSelection);
137         dialog.setMessage(PDEUIMessages.ConfigurationTab_fileDialogMessage);
138         dialog.setValidator(new ISelectionStatusValidator() {
139             public IStatus validate(Object JavaDoc[] selection) {
140                 if (selection.length > 0 && selection[0] instanceof IFile)
141                     return new Status(IStatus.OK, PDEPlugin.getPluginId(),
142                             IStatus.OK, "", null); //$NON-NLS-1$
143

144                 return new Status(IStatus.ERROR, PDEPlugin.getPluginId(),
145                         IStatus.ERROR, "", null); //$NON-NLS-1$
146
}
147         });
148         if (dialog.open() == Window.OK) {
149             file = (IFile) dialog.getFirstResult();
150             fLocationText.setText("${workspace_loc:" + file.getFullPath().makeRelative() + "}"); //$NON-NLS-1$ //$NON-NLS-2$
151
}
152     }
153     
154     protected IFile getFile() {
155         String JavaDoc path = getLocation();
156         if (path.length() > 0) {
157             IResource res = null;
158             IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
159             if (path.startsWith("${workspace_loc:")) { //$NON-NLS-1$
160
IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
161                 try {
162                     path = manager.performStringSubstitution(path, false);
163                     IFile[] containers = root.findFilesForLocation(new Path(path));
164                     if (containers.length > 0)
165                         res = containers[0];
166                 } catch (CoreException e) {
167                 }
168             } else {
169                 res = root.findMember(path);
170             }
171             if (res instanceof IFile) {
172                 return (IFile)res;
173             }
174         }
175         return null;
176     }
177
178
179     protected void handleBrowseFileSystem() {
180         FileDialog dialog = new FileDialog(fTab.getControl().getShell());
181         dialog.setFilterExtensions(new String JavaDoc[] {"*.ini"}); //$NON-NLS-1$
182
dialog.setFilterPath(getLocation());
183         dialog.setText(PDEUIMessages.ConfigurationTab_configLocMessage);
184         String JavaDoc res = dialog.open();
185         if (res != null)
186             fLocationText.setText(res);
187     }
188     
189     protected String JavaDoc getLocation() {
190         String JavaDoc path = fLocationText.getText().trim();
191         IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
192         try {
193            return manager.performStringSubstitution(path, false);
194         } catch (CoreException e) {
195             return path;
196         }
197     }
198
199 }
200
Popular Tags