KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Path;
20
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.custom.CLabel;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Shell;
29
30 import org.eclipse.jface.dialogs.StatusDialog;
31 import org.eclipse.jface.window.Window;
32
33 import org.eclipse.ui.PlatformUI;
34
35 import org.eclipse.jdt.core.JavaCore;
36
37 import org.eclipse.jdt.internal.corext.util.Messages;
38
39 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
40 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
41 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
42 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
43 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
44 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
45 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
46
47 /**
48   */

49 public class EditVariableEntryDialog extends StatusDialog {
50
51     /**
52      * The path to which the archive variable points.
53      * Null if invalid path or not resolvable. Must not exist.
54      */

55     private IPath fFileVariablePath;
56     
57     private IStatus fNameStatus;
58     
59     private Set JavaDoc fExistingEntries;
60     private VariablePathDialogField fFileNameField;
61     private CLabel fFullPathResolvedLabel;
62
63     /**
64      * Constructor for EditVariableEntryDialog.
65      * @param parent
66      */

67     public EditVariableEntryDialog(Shell parent, IPath initialEntry, IPath[] existingEntries) {
68         super(parent);
69         setTitle(NewWizardMessages.EditVariableEntryDialog_title);
70         setShellStyle(getShellStyle() | SWT.RESIZE);
71         
72         fExistingEntries= new HashSet JavaDoc();
73         if (existingEntries != null) {
74             for (int i = 0; i < existingEntries.length; i++) {
75                 IPath curr= existingEntries[i];
76                 if (!curr.equals(initialEntry)) {
77                     fExistingEntries.add(curr);
78                 }
79             }
80         }
81         
82         SourceAttachmentAdapter adapter= new SourceAttachmentAdapter();
83         
84         fFileNameField= new VariablePathDialogField(adapter);
85         fFileNameField.setDialogFieldListener(adapter);
86         fFileNameField.setLabelText(NewWizardMessages.EditVariableEntryDialog_filename_varlabel);
87         fFileNameField.setButtonLabel(NewWizardMessages.EditVariableEntryDialog_filename_external_varbutton);
88         fFileNameField.setVariableButtonLabel(NewWizardMessages.EditVariableEntryDialog_filename_variable_button);
89         String JavaDoc initialString= initialEntry != null ? initialEntry.toString() : ""; //$NON-NLS-1$
90
fFileNameField.setText(initialString);
91     }
92     
93     public IPath getPath() {
94         return Path.fromOSString(fFileNameField.getText());
95     }
96     
97
98     /* (non-Javadoc)
99      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
100      */

101     protected Control createDialogArea(Composite parent) {
102         initializeDialogUnits(parent);
103         Composite composite= (Composite) super.createDialogArea(parent);
104         
105         GridLayout layout= (GridLayout) composite.getLayout();
106         layout.numColumns= 3;
107         
108         int widthHint= convertWidthInCharsToPixels(50);
109         
110         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
111         gd.horizontalSpan= 3;
112                     
113         // archive name field
114
fFileNameField.doFillIntoGrid(composite, 4);
115         LayoutUtil.setHorizontalSpan(fFileNameField.getLabelControl(null), 3);
116         LayoutUtil.setWidthHint(fFileNameField.getTextControl(null), widthHint);
117         LayoutUtil.setHorizontalGrabbing(fFileNameField.getTextControl(null));
118         
119         // label that shows the resolved path for variable jars
120
//DialogField.createEmptySpace(composite, 1);
121
fFullPathResolvedLabel= new CLabel(composite, SWT.LEFT);
122         fFullPathResolvedLabel.setText(getResolvedLabelString());
123         fFullPathResolvedLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
124         DialogField.createEmptySpace(composite, 2);
125         
126     
127         fFileNameField.postSetFocusOnDialogField(parent.getDisplay());
128                 
129         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.SOURCE_ATTACHMENT_BLOCK);
130         applyDialogFont(composite);
131         return composite;
132     }
133
134     private class SourceAttachmentAdapter implements IStringButtonAdapter, IDialogFieldListener {
135         
136         // -------- IStringButtonAdapter --------
137
public void changeControlPressed(DialogField field) {
138             attachmentChangeControlPressed(field);
139         }
140         
141         // ---------- IDialogFieldListener --------
142
public void dialogFieldChanged(DialogField field) {
143             attachmentDialogFieldChanged(field);
144         }
145     }
146     
147     private void attachmentChangeControlPressed(DialogField field) {
148         if (field == fFileNameField) {
149             IPath jarFilePath= chooseExtJarFile();
150             if (jarFilePath != null) {
151                 fFileNameField.setText(jarFilePath.toString());
152             }
153         }
154     }
155     
156     // ---------- IDialogFieldListener --------
157

158     private void attachmentDialogFieldChanged(DialogField field) {
159         if (field == fFileNameField) {
160             fNameStatus= updateFileNameStatus();
161         }
162         doStatusLineUpdate();
163     }
164
165
166     private IPath chooseExtJarFile() {
167         IPath currPath= getPath();
168         IPath resolvedPath= getResolvedPath(currPath);
169         File JavaDoc initialSelection= resolvedPath != null ? resolvedPath.toFile() : null;
170         
171         String JavaDoc currVariable= currPath.segment(0);
172         JARFileSelectionDialog dialog= new JARFileSelectionDialog(getShell(), false, false);
173         dialog.setTitle(NewWizardMessages.EditVariableEntryDialog_extvardialog_title);
174         dialog.setMessage(NewWizardMessages.EditVariableEntryDialog_extvardialog_description);
175         dialog.setInput(fFileVariablePath.toFile());
176         dialog.setInitialSelection(initialSelection);
177         if (dialog.open() == Window.OK) {
178             File JavaDoc result= (File JavaDoc) dialog.getResult()[0];
179             IPath returnPath= Path.fromOSString(result.getPath()).makeAbsolute();
180             return modifyPath(returnPath, currVariable);
181         }
182         return null;
183     }
184     
185     private IPath getResolvedPath(IPath path) {
186         if (path != null) {
187             String JavaDoc varName= path.segment(0);
188             if (varName != null) {
189                 IPath varPath= JavaCore.getClasspathVariable(varName);
190                 if (varPath != null) {
191                     return varPath.append(path.removeFirstSegments(1));
192                 }
193             }
194         }
195         return null;
196     }
197         
198     /**
199      * Takes a path and replaces the beginning with a variable name
200      * (if the beginning matches with the variables value)
201      */

202     private IPath modifyPath(IPath path, String JavaDoc varName) {
203         if (varName == null || path == null) {
204             return null;
205         }
206         if (path.isEmpty()) {
207             return new Path(varName);
208         }
209         
210         IPath varPath= JavaCore.getClasspathVariable(varName);
211         if (varPath != null) {
212             if (varPath.isPrefixOf(path)) {
213                 path= path.removeFirstSegments(varPath.segmentCount());
214             } else {
215                 path= new Path(path.lastSegment());
216             }
217         } else {
218             path= new Path(path.lastSegment());
219         }
220         return new Path(varName).append(path);
221     }
222     
223     private IStatus updateFileNameStatus() {
224         StatusInfo status= new StatusInfo();
225         fFileVariablePath= null;
226         
227         String JavaDoc fileName= fFileNameField.getText();
228         if (fileName.length() == 0) {
229             status.setError(NewWizardMessages.EditVariableEntryDialog_filename_empty);
230             return status;
231         } else {
232             if (!Path.EMPTY.isValidPath(fileName)) {
233                 status.setError(NewWizardMessages.EditVariableEntryDialog_filename_error_notvalid);
234                 return status;
235             }
236             IPath filePath= Path.fromOSString(fileName);
237             IPath resolvedPath;
238
239
240             if (filePath.getDevice() != null) {
241                 status.setError(NewWizardMessages.EditVariableEntryDialog_filename_error_deviceinpath);
242                 return status;
243             }
244             String JavaDoc varName= filePath.segment(0);
245             if (varName == null) {
246                 status.setError(NewWizardMessages.EditVariableEntryDialog_filename_error_notvalid);
247                 return status;
248             }
249             fFileVariablePath= JavaCore.getClasspathVariable(varName);
250             if (fFileVariablePath == null) {
251                 status.setError(NewWizardMessages.EditVariableEntryDialog_filename_error_varnotexists);
252                 return status;
253             }
254             
255             String JavaDoc deprecationMessage= BuildPathSupport.getDeprecationMessage(varName);
256             
257             resolvedPath= fFileVariablePath.append(filePath.removeFirstSegments(1));
258             if (resolvedPath.isEmpty()) {
259                 String JavaDoc message= NewWizardMessages.EditVariableEntryDialog_filename_warning_varempty;
260                 if (deprecationMessage != null) {
261                     message= deprecationMessage + "\n" + message; //$NON-NLS-1$
262
}
263                 status.setWarning(message);
264                 return status;
265             }
266             File JavaDoc file= resolvedPath.toFile();
267             if (!file.isFile()) {
268                 String JavaDoc message= Messages.format(NewWizardMessages.EditVariableEntryDialog_filename_error_filenotexists, resolvedPath.toOSString());
269                 if (deprecationMessage != null) {
270                     message= deprecationMessage + "\n" + message; //$NON-NLS-1$
271
status.setWarning(message);
272                 } else {
273                     status.setInfo(message);
274                 }
275                 return status;
276             }
277             if (deprecationMessage != null) {
278                 status.setWarning(deprecationMessage);
279                 return status;
280             }
281         }
282         return status;
283     }
284     
285     private String JavaDoc getResolvedLabelString() {
286         IPath resolvedPath= getResolvedPath(getPath());
287         if (resolvedPath != null) {
288             return resolvedPath.toOSString();
289         }
290         return ""; //$NON-NLS-1$
291
}
292     
293     private boolean canBrowseFileName() {
294         // to browse with a variable JAR, the variable name must point to a directory
295
if (fFileVariablePath != null) {
296             return fFileVariablePath.toFile().isDirectory();
297         }
298         return false;
299     }
300     
301     private void doStatusLineUpdate() {
302         fFileNameField.enableButton(canBrowseFileName());
303         
304         // set the resolved path for variable jars
305
if (fFullPathResolvedLabel != null) {
306             fFullPathResolvedLabel.setText(getResolvedLabelString());
307         }
308         
309         IStatus status= fNameStatus;
310         if (!status.matches(IStatus.ERROR)) {
311             IPath path= getPath();
312             if (fExistingEntries.contains(path)) {
313                 String JavaDoc message= NewWizardMessages.EditVariableEntryDialog_filename_error_alreadyexists;
314                 status= new StatusInfo(IStatus.ERROR, message);
315             }
316         }
317         updateStatus(status);
318     }
319     
320     /*
321      * overridden to ensure full message is visible
322      * @see org.eclipse.jface.dialogs.StatusDialog#updateStatus(org.eclipse.core.runtime.IStatus)
323      */

324     protected void updateStatus(IStatus status) {
325         super.updateStatus(status);
326         Shell shell= getShell();
327         if (shell != null && ! shell.isDisposed()) {
328             Point size= shell.getSize();
329             Point minSize= shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
330             if (minSize.x > size.x || minSize.y > size.y) {
331                 shell.setSize(minSize);
332             }
333         }
334     }
335 }
336
Popular Tags