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.editor.target;
011:
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.core.runtime.Path;
014: import org.eclipse.core.runtime.Status;
015: import org.eclipse.debug.ui.StringVariableSelectionDialog;
016: import org.eclipse.jface.dialogs.Dialog;
017: import org.eclipse.jface.dialogs.StatusDialog;
018: import org.eclipse.jface.window.Window;
019: import org.eclipse.pde.core.plugin.TargetPlatform;
020: import org.eclipse.pde.internal.core.itarget.IAdditionalLocation;
021: import org.eclipse.pde.internal.core.itarget.ILocationInfo;
022: import org.eclipse.pde.internal.core.itarget.ITarget;
023: import org.eclipse.pde.internal.ui.PDEPlugin;
024: import org.eclipse.pde.internal.ui.PDEUIMessages;
025: import org.eclipse.pde.internal.ui.util.SWTUtil;
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.events.ModifyEvent;
028: import org.eclipse.swt.events.ModifyListener;
029: import org.eclipse.swt.events.SelectionAdapter;
030: import org.eclipse.swt.events.SelectionEvent;
031: import org.eclipse.swt.layout.GridData;
032: import org.eclipse.swt.layout.GridLayout;
033: import org.eclipse.swt.widgets.Button;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.swt.widgets.Control;
036: import org.eclipse.swt.widgets.DirectoryDialog;
037: import org.eclipse.swt.widgets.Label;
038: import org.eclipse.swt.widgets.Shell;
039: import org.eclipse.swt.widgets.Text;
040:
041: public class LocationDialog extends StatusDialog {
042:
043: private Text fPath;
044: private ITarget fTarget;
045: private IAdditionalLocation fLocation;
046: private IStatus fOkStatus;
047: private IStatus fErrorStatus;
048:
049: public LocationDialog(Shell parent, ITarget target,
050: IAdditionalLocation location) {
051: super (parent);
052: fTarget = target;
053: fLocation = location;
054: }
055:
056: protected Control createDialogArea(Composite parent) {
057: Composite container = new Composite(parent, SWT.NULL);
058: GridLayout layout = new GridLayout();
059: layout.numColumns = 4;
060: layout.marginHeight = layout.marginWidth = 10;
061: container.setLayout(layout);
062: GridData gd = new GridData(GridData.FILL_BOTH);
063: container.setLayoutData(gd);
064:
065: createEntry(container);
066:
067: ModifyListener listener = new ModifyListener() {
068: public void modifyText(ModifyEvent e) {
069: dialogChanged();
070: }
071: };
072: fPath.addModifyListener(listener);
073: setTitle(PDEUIMessages.LocationDialog_title);
074: Dialog.applyDialogFont(container);
075:
076: dialogChanged();
077:
078: return container;
079: }
080:
081: protected void createEntry(Composite container) {
082: Label label = new Label(container, SWT.NULL);
083: label.setText(PDEUIMessages.LocationDialog_path);
084: label.setLayoutData(new GridData());
085:
086: fPath = new Text(container, SWT.SINGLE | SWT.BORDER);
087: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
088: gd.horizontalSpan = 3;
089: fPath.setLayoutData(gd);
090:
091: if (fLocation != null) {
092: fPath.setText(fLocation.getPath());
093: }
094:
095: label = new Label(container, SWT.NONE);
096: gd = new GridData(GridData.FILL_HORIZONTAL);
097: gd.horizontalSpan = 2;
098: label.setLayoutData(gd);
099:
100: Button fs = new Button(container, SWT.PUSH);
101: fs.setText(PDEUIMessages.LocationDialog_fileSystem);
102: fs.setLayoutData(new GridData());
103: fs.addSelectionListener(new SelectionAdapter() {
104: public void widgetSelected(SelectionEvent e) {
105: handleBrowseFileSystem();
106: }
107: });
108: SWTUtil.setButtonDimensionHint(fs);
109:
110: Button var = new Button(container, SWT.PUSH);
111: var.setText(PDEUIMessages.LocationDialog_variables);
112: var.setLayoutData(new GridData());
113: var.addSelectionListener(new SelectionAdapter() {
114: public void widgetSelected(SelectionEvent e) {
115: handleInsertVariable();
116: }
117: });
118: SWTUtil.setButtonDimensionHint(var);
119: }
120:
121: private IStatus createErrorStatus(String message) {
122: return new Status(IStatus.ERROR, PDEPlugin.getPluginId(),
123: IStatus.OK, message, null);
124: }
125:
126: private void dialogChanged() {
127: IStatus status = null;
128: if (fPath.getText().length() == 0)
129: status = getEmptyErrorStatus();
130: else {
131: if (hasPath(fPath.getText()))
132: status = createErrorStatus(PDEUIMessages.LocationDialog_locationExists);
133: }
134: if (status == null)
135: status = getOKStatus();
136: updateStatus(status);
137: }
138:
139: private IStatus getOKStatus() {
140: if (fOkStatus == null)
141: fOkStatus = new Status(IStatus.OK, PDEPlugin.getPluginId(),
142: IStatus.OK, "", //$NON-NLS-1$
143: null);
144: return fOkStatus;
145: }
146:
147: private IStatus getEmptyErrorStatus() {
148: if (fErrorStatus == null)
149: fErrorStatus = createErrorStatus(PDEUIMessages.LocationDialog_emptyPath);
150: return fErrorStatus;
151: }
152:
153: protected boolean hasPath(String path) {
154: Path checkPath = new Path(path);
155: Path currentPath = (fLocation != null) ? new Path(fLocation
156: .getPath()) : null;
157: if (checkPath.equals(currentPath))
158: return false;
159: IAdditionalLocation[] locs = fTarget.getAdditionalDirectories();
160: for (int i = 0; i < locs.length; i++) {
161: if (new Path(locs[i].getPath()).equals(checkPath))
162: return true;
163: }
164: return isTargetLocation(checkPath);
165: }
166:
167: private boolean isTargetLocation(Path path) {
168: ILocationInfo info = fTarget.getLocationInfo();
169: if (info.useDefault()) {
170: Path home = new Path(TargetPlatform.getDefaultLocation());
171: return home.equals(path);
172: }
173: return new Path(info.getPath()).equals(path);
174: }
175:
176: protected void handleBrowseFileSystem() {
177: DirectoryDialog dialog = new DirectoryDialog(getShell());
178: String text = fPath.getText();
179: if (text.length() == 0)
180: text = TargetEditor.LAST_PATH;
181: dialog.setFilterPath(text);
182: dialog.setText(PDEUIMessages.BaseBlock_dirSelection);
183: dialog.setMessage(PDEUIMessages.BaseBlock_dirChoose);
184: String result = dialog.open();
185: if (result != null) {
186: fPath.setText(result);
187: TargetEditor.LAST_PATH = result;
188: }
189: }
190:
191: private void handleInsertVariable() {
192: StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(
193: getShell());
194: if (dialog.open() == Window.OK) {
195: fPath.insert(dialog.getVariableExpression());
196: }
197: }
198:
199: protected void okPressed() {
200: boolean add = fLocation == null;
201: if (add) {
202: fLocation = fTarget.getModel().getFactory()
203: .createAdditionalLocation();
204: }
205: fLocation.setPath(fPath.getText());
206: if (add)
207: fTarget
208: .addAdditionalDirectories(new IAdditionalLocation[] { fLocation });
209: super.okPressed();
210: }
211: }
|