001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 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 org.eclipse.core.resources.IContainer;
013: import org.eclipse.core.resources.IResource;
014: import org.eclipse.core.resources.IWorkspaceRoot;
015: import org.eclipse.core.resources.ResourcesPlugin;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IPath;
018: import org.eclipse.core.runtime.Path;
019: import org.eclipse.core.variables.IStringVariableManager;
020: import org.eclipse.core.variables.VariablesPlugin;
021: import org.eclipse.debug.ui.StringVariableSelectionDialog;
022: import org.eclipse.jface.window.Window;
023: import org.eclipse.osgi.util.NLS;
024: import org.eclipse.pde.internal.ui.PDEUIMessages;
025: import org.eclipse.pde.internal.ui.util.SWTUtil;
026: import org.eclipse.pde.ui.launcher.AbstractLauncherTab;
027: import org.eclipse.swt.SWT;
028: import org.eclipse.swt.events.ModifyEvent;
029: import org.eclipse.swt.events.ModifyListener;
030: import org.eclipse.swt.events.SelectionAdapter;
031: import org.eclipse.swt.events.SelectionEvent;
032: import org.eclipse.swt.layout.GridData;
033: import org.eclipse.swt.widgets.Button;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.swt.widgets.DirectoryDialog;
036: import org.eclipse.swt.widgets.Label;
037: import org.eclipse.swt.widgets.Text;
038: import org.eclipse.ui.dialogs.ContainerSelectionDialog;
039:
040: public abstract class BaseBlock {
041:
042: protected AbstractLauncherTab fTab;
043:
044: private Button fVariablesButton;
045: private Button fFileSystemButton;
046: private Button fWorkspaceButton;
047:
048: protected Text fLocationText;
049:
050: protected Listener fListener = new Listener();
051:
052: protected Label fLocationLabel;
053:
054: class Listener extends SelectionAdapter implements ModifyListener {
055: public void widgetSelected(SelectionEvent e) {
056: Object source = e.getSource();
057: if (source == fFileSystemButton) {
058: handleBrowseFileSystem();
059: } else if (source == fWorkspaceButton) {
060: handleBrowseWorkspace();
061: } else if (source == fVariablesButton) {
062: handleInsertVariable();
063: } else {
064: fTab.updateLaunchConfigurationDialog();
065: }
066: }
067:
068: public void modifyText(ModifyEvent e) {
069: fTab.updateLaunchConfigurationDialog();
070: }
071: }
072:
073: public BaseBlock(AbstractLauncherTab tab) {
074: fTab = tab;
075: }
076:
077: protected void createText(Composite parent, String text, int indent) {
078: fLocationLabel = new Label(parent, SWT.NONE);
079: fLocationLabel.setText(text);
080: if (indent > 0) {
081: GridData gd = new GridData();
082: gd.horizontalIndent = indent;
083: fLocationLabel.setLayoutData(gd);
084: }
085:
086: fLocationText = new Text(parent, SWT.SINGLE | SWT.BORDER);
087: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
088: gd.widthHint = 400;
089: fLocationText.setLayoutData(gd);
090: fLocationText.addModifyListener(fListener);
091: }
092:
093: protected void createButtons(Composite parent, String[] buttonLabels) {
094: fWorkspaceButton = createButton(parent, buttonLabels[0]);
095: fFileSystemButton = createButton(parent, buttonLabels[1]);
096: fVariablesButton = createButton(parent, buttonLabels[2]);
097: }
098:
099: protected Button createButton(Composite parent, String text) {
100: Button button = new Button(parent, SWT.PUSH);
101: button.setText(text);
102: button.setLayoutData(new GridData());
103: button.addSelectionListener(fListener);
104: SWTUtil.setButtonDimensionHint(button);
105: return button;
106: }
107:
108: protected void handleBrowseFileSystem() {
109: DirectoryDialog dialog = new DirectoryDialog(fTab.getControl()
110: .getShell());
111: dialog.setFilterPath(getLocation());
112: dialog.setText(PDEUIMessages.BaseBlock_dirSelection);
113: dialog.setMessage(PDEUIMessages.BaseBlock_dirChoose);
114: String result = dialog.open();
115: if (result != null)
116: fLocationText.setText(result);
117: }
118:
119: protected void handleBrowseWorkspace() {
120: ContainerSelectionDialog dialog = new ContainerSelectionDialog(
121: fTab.getControl().getShell(), getContainer(), true,
122: PDEUIMessages.BaseBlock_relative);
123: if (dialog.open() == Window.OK) {
124: Object[] result = dialog.getResult();
125: if (result.length == 0)
126: return;
127: IPath path = (IPath) result[0];
128: fLocationText
129: .setText("${workspace_loc:" + path.makeRelative().toString() + "}"); //$NON-NLS-1$ //$NON-NLS-2$
130: }
131: }
132:
133: protected IContainer getContainer() {
134: String path = getLocation();
135: if (path.length() > 0) {
136: IResource res = null;
137: IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
138: .getRoot();
139: if (path.startsWith("${workspace_loc:")) { //$NON-NLS-1$
140: IStringVariableManager manager = VariablesPlugin
141: .getDefault().getStringVariableManager();
142: try {
143: path = manager.performStringSubstitution(path,
144: false);
145: IContainer[] containers = root
146: .findContainersForLocation(new Path(path));
147: if (containers.length > 0)
148: res = containers[0];
149: } catch (CoreException e) {
150: }
151: } else {
152: res = root.findMember(path);
153: }
154: if (res instanceof IContainer) {
155: return (IContainer) res;
156: }
157: }
158: return ResourcesPlugin.getWorkspace().getRoot();
159: }
160:
161: private void handleInsertVariable() {
162: StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(
163: fTab.getControl().getShell());
164: if (dialog.open() == Window.OK)
165: fLocationText.insert(dialog.getVariableExpression());
166: }
167:
168: protected String getLocation() {
169: return fLocationText.getText().trim();
170: }
171:
172: public String validate() {
173: return (fLocationText.isEnabled() && getLocation().length() == 0) ? NLS
174: .bind(PDEUIMessages.BaseBlock_errorMessage, getName())
175: : null;
176: }
177:
178: protected abstract String getName();
179:
180: protected void enableBrowseSection(boolean enabled) {
181: fLocationLabel.setEnabled(enabled);
182: fLocationText.setEnabled(enabled);
183: fFileSystemButton.setEnabled(enabled);
184: fWorkspaceButton.setEnabled(enabled);
185: fVariablesButton.setEnabled(enabled);
186: fTab.updateLaunchConfigurationDialog();
187: }
188:
189: }
|