KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > VariableCreationDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.ui.wizards.buildpaths;
12
13 import java.io.File JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.DirectoryDialog;
24 import org.eclipse.swt.widgets.FileDialog;
25 import org.eclipse.swt.widgets.Shell;
26
27 import org.eclipse.jface.dialogs.IDialogSettings;
28 import org.eclipse.jface.dialogs.StatusDialog;
29
30 import org.eclipse.ui.PlatformUI;
31
32 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
33 import org.eclipse.jdt.internal.ui.IUIConstants;
34 import org.eclipse.jdt.internal.ui.JavaPlugin;
35 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
36 import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
37 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
38 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
39 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
40 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
41 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
42 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
43 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
44 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
45
46 public class VariableCreationDialog extends StatusDialog {
47         
48     private IDialogSettings fDialogSettings;
49     
50     private StringDialogField fNameField;
51     private StatusInfo fNameStatus;
52     
53     private StringButtonDialogField fPathField;
54     private StatusInfo fPathStatus;
55     private SelectionButtonDialogField fDirButton;
56         
57     private CPVariableElement fElement;
58     
59     private List JavaDoc fExistingNames;
60         
61     public VariableCreationDialog(Shell parent, CPVariableElement element, List JavaDoc existingNames) {
62         super(parent);
63         if (element == null) {
64             setTitle(NewWizardMessages.VariableCreationDialog_titlenew);
65         } else {
66             setTitle(NewWizardMessages.VariableCreationDialog_titleedit);
67         }
68         
69         fDialogSettings= JavaPlugin.getDefault().getDialogSettings();
70         
71         fElement= element;
72         
73         fNameStatus= new StatusInfo();
74         fPathStatus= new StatusInfo();
75         
76         NewVariableAdapter adapter= new NewVariableAdapter();
77         fNameField= new StringDialogField();
78         fNameField.setDialogFieldListener(adapter);
79         fNameField.setLabelText(NewWizardMessages.VariableCreationDialog_name_label);
80
81         fPathField= new StringButtonDialogField(adapter);
82         fPathField.setDialogFieldListener(adapter);
83         fPathField.setLabelText(NewWizardMessages.VariableCreationDialog_path_label);
84         fPathField.setButtonLabel(NewWizardMessages.VariableCreationDialog_path_file_button);
85         
86         fDirButton= new SelectionButtonDialogField(SWT.PUSH);
87         fDirButton.setDialogFieldListener(adapter);
88         fDirButton.setLabelText(NewWizardMessages.VariableCreationDialog_path_dir_button);
89         
90         fExistingNames= existingNames;
91         
92         if (element != null) {
93             fNameField.setText(element.getName());
94             fPathField.setText(element.getPath().toString());
95             fExistingNames.remove(element.getName());
96         } else {
97             fNameField.setText(""); //$NON-NLS-1$
98
fPathField.setText(""); //$NON-NLS-1$
99
}
100     }
101     
102     /*
103      * @see Windows#configureShell
104      */

105     protected void configureShell(Shell newShell) {
106         super.configureShell(newShell);
107         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.VARIABLE_CREATION_DIALOG);
108     }
109     
110
111     public CPVariableElement getClasspathElement() {
112         return new CPVariableElement(fNameField.getText(), new Path(fPathField.getText()));
113     }
114
115     /*
116      * @see Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
117      */

118     protected Control createDialogArea(Composite parent) {
119         Composite composite= (Composite) super.createDialogArea(parent);
120                 
121         Composite inner= new Composite(composite, SWT.NONE);
122         inner.setFont(composite.getFont());
123         
124         GridLayout layout= new GridLayout();
125         layout.marginWidth= 0;
126         layout.marginHeight= 0;
127         layout.numColumns= 3;
128         inner.setLayout(layout);
129         
130         int fieldWidthHint= convertWidthInCharsToPixels(50);
131         
132         fNameField.doFillIntoGrid(inner, 2);
133         LayoutUtil.setWidthHint(fNameField.getTextControl(null), fieldWidthHint);
134         LayoutUtil.setHorizontalGrabbing(fNameField.getTextControl(null));
135         
136         DialogField.createEmptySpace(inner, 1);
137         
138         fPathField.doFillIntoGrid(inner, 3);
139         LayoutUtil.setWidthHint(fPathField.getTextControl(null), fieldWidthHint);
140         
141         DialogField.createEmptySpace(inner, 2);
142         fDirButton.doFillIntoGrid(inner, 1);
143         
144         DialogField focusField= (fElement == null) ? fNameField : fPathField;
145         focusField.postSetFocusOnDialogField(parent.getDisplay());
146         applyDialogFont(composite);
147         return composite;
148     }
149
150         
151     // -------- NewVariableAdapter --------
152

153     private class NewVariableAdapter implements IDialogFieldListener, IStringButtonAdapter {
154         
155         // -------- IDialogFieldListener
156
public void dialogFieldChanged(DialogField field) {
157             doFieldUpdated(field);
158         }
159         
160         // -------- IStringButtonAdapter
161
public void changeControlPressed(DialogField field) {
162             doChangeControlPressed(field);
163         }
164     }
165     
166     private void doChangeControlPressed(DialogField field) {
167         if (field == fPathField) {
168             IPath path= chooseExtJarFile();
169             if (path != null) {
170                 fPathField.setText(path.toString());
171             }
172         }
173     }
174     
175     private void doFieldUpdated(DialogField field) {
176         if (field == fNameField) {
177             fNameStatus= nameUpdated();
178         } else if (field == fPathField) {
179             fPathStatus= pathUpdated();
180         } else if (field == fDirButton) {
181             IPath path= chooseExtDirectory();
182             if (path != null) {
183                 fPathField.setText(path.toString());
184             }
185         }
186         updateStatus(StatusUtil.getMoreSevere(fPathStatus, fNameStatus));
187     }
188     
189     private StatusInfo nameUpdated() {
190         StatusInfo status= new StatusInfo();
191         String JavaDoc name= fNameField.getText();
192         if (name.length() == 0) {
193             status.setError(NewWizardMessages.VariableCreationDialog_error_entername);
194             return status;
195         }
196         if (name.trim().length() != name.length()) {
197             status.setError(NewWizardMessages.VariableCreationDialog_error_whitespace);
198         } else if (!Path.ROOT.isValidSegment(name)) {
199             status.setError(NewWizardMessages.VariableCreationDialog_error_invalidname);
200         } else if (nameConflict(name)) {
201             status.setError(NewWizardMessages.VariableCreationDialog_error_nameexists);
202         }
203         return status;
204     }
205     
206     private boolean nameConflict(String JavaDoc name) {
207         if (fElement != null && fElement.getName().equals(name)) {
208             return false;
209         }
210         for (int i= 0; i < fExistingNames.size(); i++) {
211             CPVariableElement elem= (CPVariableElement)fExistingNames.get(i);
212             if (name.equals(elem.getName())){
213                 return true;
214             }
215         }
216         return false;
217     }
218     
219     
220     private StatusInfo pathUpdated() {
221         StatusInfo status= new StatusInfo();
222         
223         String JavaDoc path= fPathField.getText();
224         if (path.length() > 0) { // empty path is ok
225
if (!Path.ROOT.isValidPath(path)) {
226                 status.setError(NewWizardMessages.VariableCreationDialog_error_invalidpath);
227             } else if (!new File JavaDoc(path).exists()) {
228                 status.setWarning(NewWizardMessages.VariableCreationDialog_warning_pathnotexists);
229             }
230         }
231         return status;
232     }
233     
234     
235     private String JavaDoc getInitPath() {
236         String JavaDoc initPath= fPathField.getText();
237         if (initPath.length() == 0) {
238             initPath= fDialogSettings.get(IUIConstants.DIALOGSTORE_LASTEXTJAR);
239             if (initPath == null) {
240                 initPath= ""; //$NON-NLS-1$
241
}
242         } else {
243             IPath entryPath= new Path(initPath);
244             if (ArchiveFileFilter.isArchivePath(entryPath)) {
245                 entryPath.removeLastSegments(1);
246             }
247             initPath= entryPath.toOSString();
248         }
249         return initPath;
250     }
251     
252     
253     /*
254      * Open a dialog to choose a jar from the file system
255      */

256     private IPath chooseExtJarFile() {
257         String JavaDoc initPath= getInitPath();
258         
259         FileDialog dialog= new FileDialog(getShell());
260         dialog.setText(NewWizardMessages.VariableCreationDialog_extjardialog_text);
261         dialog.setFilterExtensions(new String JavaDoc[] {"*.jar;*.zip"}); //$NON-NLS-1$
262
dialog.setFilterPath(initPath);
263         String JavaDoc res= dialog.open();
264         if (res != null) {
265             fDialogSettings.put(IUIConstants.DIALOGSTORE_LASTEXTJAR, dialog.getFilterPath());
266             return Path.fromOSString(res).makeAbsolute();
267         }
268         return null;
269     }
270     
271     private IPath chooseExtDirectory() {
272         String JavaDoc initPath= getInitPath();
273         
274         DirectoryDialog dialog= new DirectoryDialog(getShell());
275         dialog.setText(NewWizardMessages.VariableCreationDialog_extdirdialog_text);
276         dialog.setMessage(NewWizardMessages.VariableCreationDialog_extdirdialog_message);
277         dialog.setFilterPath(initPath);
278         String JavaDoc res= dialog.open();
279         if (res != null) {
280             fDialogSettings.put(IUIConstants.DIALOGSTORE_LASTEXTJAR, dialog.getFilterPath());
281             return Path.fromOSString(res);
282         }
283         return null;
284     }
285     
286         
287     
288 }
289
Popular Tags