001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.launcher;
011:
012: import java.io.File;
013:
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.resources.IResource;
016: import org.eclipse.core.resources.IWorkspaceRoot;
017: import org.eclipse.core.resources.ResourcesPlugin;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.Path;
021: import org.eclipse.core.runtime.Status;
022: import org.eclipse.core.variables.IStringVariableManager;
023: import org.eclipse.core.variables.VariablesPlugin;
024: import org.eclipse.debug.core.ILaunchConfiguration;
025: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
026: import org.eclipse.jface.window.Window;
027: import org.eclipse.pde.internal.ui.PDEPlugin;
028: import org.eclipse.pde.internal.ui.PDEUIMessages;
029: import org.eclipse.pde.internal.ui.util.FileNameFilter;
030: import org.eclipse.pde.ui.launcher.AbstractLauncherTab;
031: import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
032: import org.eclipse.swt.SWT;
033: import org.eclipse.swt.events.SelectionAdapter;
034: import org.eclipse.swt.events.SelectionEvent;
035: import org.eclipse.swt.layout.GridData;
036: import org.eclipse.swt.layout.GridLayout;
037: import org.eclipse.swt.widgets.Button;
038: import org.eclipse.swt.widgets.Composite;
039: import org.eclipse.swt.widgets.FileDialog;
040: import org.eclipse.swt.widgets.Group;
041: import org.eclipse.swt.widgets.Label;
042: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
043: import org.eclipse.ui.dialogs.ISelectionStatusValidator;
044: import org.eclipse.ui.model.WorkbenchContentProvider;
045: import org.eclipse.ui.model.WorkbenchLabelProvider;
046:
047: public class ConfigurationTemplateBlock extends BaseBlock {
048:
049: private Button fGenerateFileButton;
050: private Button fUseTemplateButton;
051:
052: public ConfigurationTemplateBlock(AbstractLauncherTab tab) {
053: super (tab);
054: }
055:
056: public void createControl(Composite parent) {
057: Group group = new Group(parent, SWT.NONE);
058: group.setText(PDEUIMessages.ConfigurationTab_configFileGroup);
059: group.setLayout(new GridLayout(2, false));
060: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
061:
062: fGenerateFileButton = new Button(group, SWT.RADIO);
063: fGenerateFileButton
064: .setText(PDEUIMessages.ConfigurationTab_defaultConfigIni);
065: GridData gd = new GridData();
066: gd.horizontalSpan = 2;
067: fGenerateFileButton.setLayoutData(gd);
068: fGenerateFileButton
069: .addSelectionListener(new SelectionAdapter() {
070: public void widgetSelected(SelectionEvent e) {
071: enableBrowseSection(!fGenerateFileButton
072: .getSelection());
073: }
074: });
075:
076: fUseTemplateButton = new Button(group, SWT.RADIO);
077: fUseTemplateButton
078: .setText(PDEUIMessages.ConfigurationTab_existingConfigIni);
079: gd = new GridData();
080: gd.horizontalSpan = 2;
081: fUseTemplateButton.setLayoutData(gd);
082:
083: createText(group, PDEUIMessages.ConfigurationTab_templateLoc,
084: 20);
085:
086: Composite buttons = new Composite(group, SWT.NONE);
087: GridLayout layout = new GridLayout(4, false);
088: layout.marginHeight = layout.marginWidth = 0;
089: buttons.setLayout(layout);
090: gd = new GridData(GridData.FILL_HORIZONTAL);
091: gd.horizontalSpan = 2;
092: buttons.setLayoutData(gd);
093:
094: Label label = new Label(buttons, SWT.NONE);
095: label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
096:
097: createButtons(buttons, new String[] {
098: PDEUIMessages.BaseBlock_workspaceS,
099: PDEUIMessages.BaseBlock_filesystemS,
100: PDEUIMessages.BaseBlock_variablesS });
101: }
102:
103: public void initializeFrom(ILaunchConfiguration configuration)
104: throws CoreException {
105: boolean generateDefault = configuration.getAttribute(
106: IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, true);
107: fGenerateFileButton.setSelection(generateDefault);
108: fUseTemplateButton.setSelection(!generateDefault);
109: enableBrowseSection(!generateDefault);
110: fLocationText.setText(configuration.getAttribute(
111: IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION, "")); //$NON-NLS-1$
112: }
113:
114: public void performApply(
115: ILaunchConfigurationWorkingCopy configuration) {
116: configuration.setAttribute(
117: IPDELauncherConstants.CONFIG_GENERATE_DEFAULT,
118: fGenerateFileButton.getSelection());
119: if (!fGenerateFileButton.getSelection())
120: configuration.setAttribute(
121: IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION,
122: fLocationText.getText().trim());
123: }
124:
125: public void setDefaults(
126: ILaunchConfigurationWorkingCopy configuration) {
127: configuration.setAttribute(
128: IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, true);
129: configuration
130: .setAttribute(
131: IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION,
132: "${target_home}" + File.separatorChar + "configuration" + File.separatorChar + "config.ini"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
133: }
134:
135: protected String getName() {
136: return PDEUIMessages.ConfigurationTemplateBlock_name;
137: }
138:
139: protected void handleBrowseWorkspace() {
140: ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
141: fTab.getControl().getShell(),
142: new WorkbenchLabelProvider(),
143: new WorkbenchContentProvider());
144:
145: IFile file = getFile();
146: if (file != null)
147: dialog.setInitialSelection(file);
148: dialog.setInput(PDEPlugin.getWorkspace().getRoot());
149: dialog.addFilter(new FileNameFilter("config.ini")); //$NON-NLS-1$
150: dialog.setAllowMultiple(false);
151: dialog.setTitle(PDEUIMessages.ConfigurationTab_fileSelection);
152: dialog
153: .setMessage(PDEUIMessages.ConfigurationTab_fileDialogMessage);
154: dialog.setValidator(new ISelectionStatusValidator() {
155: public IStatus validate(Object[] selection) {
156: if (selection.length > 0
157: && selection[0] instanceof IFile)
158: return new Status(IStatus.OK, PDEPlugin
159: .getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
160:
161: return new Status(IStatus.ERROR, PDEPlugin
162: .getPluginId(), IStatus.ERROR, "", null); //$NON-NLS-1$
163: }
164: });
165: if (dialog.open() == Window.OK) {
166: file = (IFile) dialog.getFirstResult();
167: fLocationText
168: .setText("${workspace_loc:" + file.getFullPath().makeRelative() + "}"); //$NON-NLS-1$ //$NON-NLS-2$
169: }
170: }
171:
172: protected IFile getFile() {
173: String path = getLocation();
174: if (path.length() > 0) {
175: IResource res = null;
176: IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
177: .getRoot();
178: if (path.startsWith("${workspace_loc:")) { //$NON-NLS-1$
179: IStringVariableManager manager = VariablesPlugin
180: .getDefault().getStringVariableManager();
181: try {
182: path = manager.performStringSubstitution(path,
183: false);
184: IFile[] containers = root
185: .findFilesForLocation(new Path(path));
186: if (containers.length > 0)
187: res = containers[0];
188: } catch (CoreException e) {
189: }
190: } else {
191: res = root.findMember(path);
192: }
193: if (res instanceof IFile) {
194: return (IFile) res;
195: }
196: }
197: return null;
198: }
199:
200: protected void handleBrowseFileSystem() {
201: FileDialog dialog = new FileDialog(fTab.getControl().getShell());
202: dialog.setFilterExtensions(new String[] { "*.ini" }); //$NON-NLS-1$
203: dialog.setFilterPath(getLocation());
204: dialog.setText(PDEUIMessages.ConfigurationTab_configLocMessage);
205: String res = dialog.open();
206: if (res != null)
207: fLocationText.setText(res);
208: }
209:
210: protected String getLocation() {
211: String path = fLocationText.getText().trim();
212: IStringVariableManager manager = VariablesPlugin.getDefault()
213: .getStringVariableManager();
214: try {
215: return manager.performStringSubstitution(path, false);
216: } catch (CoreException e) {
217: return path;
218: }
219: }
220:
221: }
|