KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > target > LocationDialog


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.editor.target;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Path;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.debug.ui.StringVariableSelectionDialog;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.dialogs.StatusDialog;
19 import org.eclipse.jface.window.Window;
20 import org.eclipse.pde.core.plugin.TargetPlatform;
21 import org.eclipse.pde.internal.core.itarget.IAdditionalLocation;
22 import org.eclipse.pde.internal.core.itarget.ILocationInfo;
23 import org.eclipse.pde.internal.core.itarget.ITarget;
24 import org.eclipse.pde.internal.ui.PDEPlugin;
25 import org.eclipse.pde.internal.ui.PDEUIMessages;
26 import org.eclipse.pde.internal.ui.util.SWTUtil;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.ModifyEvent;
29 import org.eclipse.swt.events.ModifyListener;
30 import org.eclipse.swt.events.SelectionAdapter;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.DirectoryDialog;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.swt.widgets.Text;
41
42 public class LocationDialog extends StatusDialog {
43     
44     private Text fPath;
45     private ITarget fTarget;
46     private IAdditionalLocation fLocation;
47     private IStatus fOkStatus;
48     private IStatus fErrorStatus;
49
50     public LocationDialog(Shell parent, ITarget target, IAdditionalLocation location) {
51         super(parent);
52         fTarget = target;
53         fLocation = location;
54     }
55     
56     protected Control createDialogArea(Composite parent) {
57         Composite container = new Composite(parent, SWT.NULL);
58         GridLayout layout = new GridLayout();
59         layout.numColumns = 4;
60         layout.marginHeight = layout.marginWidth = 10;
61         container.setLayout(layout);
62         GridData gd = new GridData(GridData.FILL_BOTH);
63         container.setLayoutData(gd);
64         
65         createEntry(container);
66
67         ModifyListener listener = new ModifyListener() {
68             public void modifyText(ModifyEvent e) {
69                 dialogChanged();
70             }
71         };
72         fPath.addModifyListener(listener);
73         setTitle(PDEUIMessages.LocationDialog_title);
74         Dialog.applyDialogFont(container);
75         
76         dialogChanged();
77         
78         return container;
79     }
80     
81     protected void createEntry(Composite container) {
82         Label label = new Label(container, SWT.NULL);
83         label.setText(PDEUIMessages.LocationDialog_path);
84         label.setLayoutData(new GridData());
85         
86         fPath = new Text(container, SWT.SINGLE | SWT.BORDER);
87         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
88         gd.horizontalSpan = 3;
89         fPath.setLayoutData(gd);
90         
91         if (fLocation != null) {
92             fPath.setText(fLocation.getPath());
93         }
94         
95         label = new Label(container, SWT.NONE);
96         gd = new GridData(GridData.FILL_HORIZONTAL);
97         gd.horizontalSpan = 2;
98         label.setLayoutData(gd);
99         
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 JavaDoc message) {
122         return new Status(IStatus.ERROR, PDEPlugin.getPluginId(), IStatus.OK,
123                 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 JavaDoc path) {
154         Path checkPath = new Path(path);
155         Path currentPath = (fLocation != null) ? new Path(fLocation.getPath()) : null;
156         if (checkPath.equals(currentPath))
157             return false;
158         IAdditionalLocation[] locs = fTarget.getAdditionalDirectories();
159         for (int i = 0; i < locs.length; i++) {
160             if (new Path(locs[i].getPath()).equals(checkPath))
161                 return true;
162         }
163         return isTargetLocation(checkPath);
164     }
165     
166     private boolean isTargetLocation(Path path) {
167         ILocationInfo info = fTarget.getLocationInfo();
168         if (info.useDefault()) {
169             Path home = new Path(TargetPlatform.getDefaultLocation());
170             return home.equals(path);
171         }
172         return new Path(info.getPath()).equals(path);
173     }
174     
175     protected void handleBrowseFileSystem() {
176         DirectoryDialog dialog = new DirectoryDialog(getShell());
177         String JavaDoc text = fPath.getText();
178         if (text.length() == 0)
179             text = TargetEditor.LAST_PATH;
180         dialog.setFilterPath(text);
181         dialog.setText(PDEUIMessages.BaseBlock_dirSelection);
182         dialog.setMessage(PDEUIMessages.BaseBlock_dirChoose);
183         String JavaDoc result = dialog.open();
184         if (result != null) {
185             fPath.setText(result);
186             TargetEditor.LAST_PATH = result;
187         }
188     }
189     
190     private void handleInsertVariable() {
191         StringVariableSelectionDialog dialog =
192                     new StringVariableSelectionDialog(getShell());
193         if (dialog.open() == Window.OK) {
194             fPath.insert(dialog.getVariableExpression());
195         }
196     }
197     
198     protected void okPressed() {
199         boolean add = fLocation == null;
200         if (add) {
201             fLocation = fTarget.getModel().getFactory().createAdditionalLocation();
202         }
203         fLocation.setPath(fPath.getText());
204         if (add)
205             fTarget.addAdditionalDirectories(new IAdditionalLocation[]{fLocation});
206         super.okPressed();
207     }
208 }
209
Popular Tags