KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > ProjectContentsLocationArea


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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
12 package org.eclipse.ui.internal.ide.dialogs;
13
14 import java.net.URI JavaDoc;
15 import java.net.URISyntaxException JavaDoc;
16 import org.eclipse.core.filesystem.IFileInfo;
17 import org.eclipse.core.filesystem.URIUtil;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.osgi.util.TextProcessor;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.ModifyEvent;
27 import org.eclipse.swt.events.ModifyListener;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.DirectoryDialog;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Text;
37 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
38 import org.eclipse.ui.internal.ide.filesystem.FileSystemConfiguration;
39 import org.eclipse.ui.internal.ide.filesystem.FileSystemSupportRegistry;
40
41 /**
42  * ProjectContentsLocationArea is a convenience class for area that handle entry
43  * of locations using URIs.
44  *
45  * @since 3.2
46  *
47  */

48 public class ProjectContentsLocationArea {
49     /**
50      * IErrorMessageReporter is an interface for type that allow message
51      * reporting.
52      *
53      */

54     public interface IErrorMessageReporter {
55         /**
56          * Report the error message
57          *
58          * @param errorMessage
59          * String or <code>null</code>. If the errorMessage is
60          * null then clear any error state.
61          */

62         public void reportError(String JavaDoc errorMessage);
63     }
64
65     private static String JavaDoc BROWSE_LABEL = IDEWorkbenchMessages.ProjectLocationSelectionDialog_browseLabel;
66
67     private static final int SIZING_TEXT_FIELD_WIDTH = 250;
68
69     private static final String JavaDoc FILE_SCHEME = "file"; //$NON-NLS-1$
70

71     private Label locationLabel;
72
73     private Text locationPathField;
74
75     private Button browseButton;
76
77     private IErrorMessageReporter errorReporter;
78
79     private String JavaDoc projectName = IDEResourceInfoUtils.EMPTY_STRING;
80
81     private String JavaDoc userPath = IDEResourceInfoUtils.EMPTY_STRING;
82
83     private Button useDefaultsButton;
84
85     private IProject existingProject;
86
87     private FileSystemSelectionArea fileSystemSelectionArea;
88
89     /**
90      * Create a new instance of the receiver.
91      *
92      * @param reporter
93      * @param composite
94      * @param startProject
95      */

96     public ProjectContentsLocationArea(IErrorMessageReporter reporter,
97             Composite composite, IProject startProject) {
98
99         errorReporter = reporter;
100         projectName = startProject.getName();
101         existingProject = startProject;
102
103         boolean defaultEnabled = true;
104         try {
105             defaultEnabled = startProject.getDescription().getLocationURI() == null;
106         } catch (CoreException e1) {
107             // If we get a CoreException assume the default.
108
}
109         createContents(composite, defaultEnabled);
110     }
111
112     /**
113      * Create a new instance of a ProjectContentsLocationArea.
114      *
115      * @param reporter
116      * @param composite
117      */

118     public ProjectContentsLocationArea(IErrorMessageReporter reporter,
119             Composite composite) {
120         errorReporter = reporter;
121
122         // If it is a new project always start enabled
123
createContents(composite, true);
124     }
125
126     /**
127      * Create the contents of the receiver.
128      *
129      * @param composite
130      * @param defaultEnabled
131      */

132     private void createContents(Composite composite, boolean defaultEnabled) {
133
134         int columns = 4;
135
136         // project specification group
137
Composite projectGroup = new Composite(composite, SWT.NONE);
138         GridLayout layout = new GridLayout();
139         layout.numColumns = columns;
140         projectGroup.setLayout(layout);
141         projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
142
143         useDefaultsButton = new Button(projectGroup, SWT.CHECK | SWT.RIGHT);
144         useDefaultsButton
145                 .setText(IDEWorkbenchMessages.ProjectLocationSelectionDialog_useDefaultLabel);
146         useDefaultsButton.setSelection(defaultEnabled);
147         GridData buttonData = new GridData();
148         buttonData.horizontalSpan = columns;
149         useDefaultsButton.setLayoutData(buttonData);
150
151         createUserEntryArea(projectGroup, defaultEnabled);
152
153         useDefaultsButton.addSelectionListener(new SelectionAdapter() {
154             public void widgetSelected(SelectionEvent e) {
155                 boolean useDefaults = useDefaultsButton.getSelection();
156
157                 if (useDefaults) {
158                     userPath = locationPathField.getText();
159                     locationPathField.setText(TextProcessor
160                             .process(getDefaultPathDisplayString()));
161                 } else {
162                     locationPathField.setText(TextProcessor.process(userPath));
163                 }
164                 setUserAreaEnabled(!useDefaults);
165             }
166         });
167         setUserAreaEnabled(!defaultEnabled);
168     }
169
170     /**
171      * Return whether or not we are currently showing the default location for
172      * the project.
173      *
174      * @return boolean
175      */

176     public boolean isDefault() {
177         return useDefaultsButton.getSelection();
178     }
179
180     /**
181      * Create the area for user entry.
182      *
183      * @param composite
184      * @param defaultEnabled
185      */

186     private void createUserEntryArea(Composite composite, boolean defaultEnabled) {
187         // location label
188
locationLabel = new Label(composite, SWT.NONE);
189         locationLabel
190                 .setText(IDEWorkbenchMessages.ProjectLocationSelectionDialog_locationLabel);
191
192         // project location entry field
193
locationPathField = new Text(composite, SWT.BORDER);
194         GridData data = new GridData(GridData.FILL_HORIZONTAL);
195         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
196         data.horizontalSpan = 2;
197         locationPathField.setLayoutData(data);
198
199         // browse button
200
browseButton = new Button(composite, SWT.PUSH);
201         browseButton.setText(BROWSE_LABEL);
202         browseButton.addSelectionListener(new SelectionAdapter() {
203             public void widgetSelected(SelectionEvent event) {
204                 handleLocationBrowseButtonPressed();
205             }
206         });
207
208         createFileSystemSelection(composite);
209
210         if (defaultEnabled) {
211             locationPathField.setText(TextProcessor
212                     .process(getDefaultPathDisplayString()));
213         } else {
214             if (existingProject == null) {
215                 locationPathField.setText(IDEResourceInfoUtils.EMPTY_STRING);
216             } else {
217                 locationPathField.setText(TextProcessor.process(existingProject
218                         .getLocation().toString()));
219             }
220         }
221
222         locationPathField.addModifyListener(new ModifyListener() {
223             /*
224              * (non-Javadoc)
225              *
226              * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
227              */

228             public void modifyText(ModifyEvent e) {
229                 errorReporter.reportError(checkValidLocation());
230             }
231         });
232     }
233
234     /**
235      * Create the file system selection area.
236      *
237      * @param composite
238      */

239     private void createFileSystemSelection(Composite composite) {
240
241         // Always use the default if that is all there is.
242
if (FileSystemSupportRegistry.getInstance().hasOneFileSystem()) {
243             return;
244         }
245
246         new Label(composite, SWT.NONE);
247
248         fileSystemSelectionArea = new FileSystemSelectionArea();
249         fileSystemSelectionArea.createContents(composite);
250     }
251
252     /**
253      * Return the path we are going to display. If it is a file URI then remove
254      * the file prefix.
255      *
256      * @return String
257      */

258     private String JavaDoc getDefaultPathDisplayString() {
259
260         URI JavaDoc defaultURI = null;
261         if (existingProject != null) {
262             defaultURI = existingProject.getLocationURI();
263         }
264
265         // Handle files specially. Assume a file if there is no project to query
266
if (defaultURI == null || defaultURI.getScheme().equals(FILE_SCHEME)) {
267             return Platform.getLocation().append(projectName).toString();
268         }
269         return defaultURI.toString();
270
271     }
272
273     /**
274      * Set the enablement state of the receiver.
275      *
276      * @param enabled
277      */

278     private void setUserAreaEnabled(boolean enabled) {
279
280         locationLabel.setEnabled(enabled);
281         locationPathField.setEnabled(enabled);
282         browseButton.setEnabled(enabled);
283         if (fileSystemSelectionArea != null) {
284             fileSystemSelectionArea.setEnabled(enabled);
285         }
286     }
287
288     /**
289      * Return the browse button. Usually referenced in order to set the layout
290      * data for a dialog.
291      *
292      * @return Button
293      */

294     public Button getBrowseButton() {
295         return browseButton;
296     }
297
298     /**
299      * Open an appropriate directory browser
300      */

301     private void handleLocationBrowseButtonPressed() {
302
303         String JavaDoc selectedDirectory = null;
304         String JavaDoc dirName = getPathFromLocationField();
305
306         if (!dirName.equals(IDEResourceInfoUtils.EMPTY_STRING)) {
307             IFileInfo info;
308             info = IDEResourceInfoUtils.getFileInfo(dirName);
309
310             if (info == null || !(info.exists()))
311                 dirName = IDEResourceInfoUtils.EMPTY_STRING;
312         }
313
314         FileSystemConfiguration config = getSelectedConfiguration();
315         if (config== null || config.equals(
316                 FileSystemSupportRegistry.getInstance()
317                         .getDefaultConfiguration())) {
318             DirectoryDialog dialog = new DirectoryDialog(locationPathField
319                     .getShell());
320             dialog
321                     .setMessage(IDEWorkbenchMessages.ProjectLocationSelectionDialog_directoryLabel);
322
323             dialog.setFilterPath(dirName);
324
325             selectedDirectory = dialog.open();
326
327         } else {
328             URI JavaDoc uri = getSelectedConfiguration().getContributor()
329                     .browseFileSystem(dirName, browseButton.getShell());
330             if (uri != null)
331                 selectedDirectory = uri.toString();
332         }
333
334         if (selectedDirectory != null)
335             updateLocationField(selectedDirectory);
336     }
337
338     /**
339      * Update the location field based on the selected path.
340      *
341      * @param selectedPath
342      */

343     private void updateLocationField(String JavaDoc selectedPath) {
344         locationPathField.setText(TextProcessor.process(selectedPath));
345     }
346
347     /**
348      * Return the path on the location field.
349      *
350      * @return String
351      */

352     private String JavaDoc getPathFromLocationField() {
353         URI JavaDoc fieldURI;
354         try {
355             fieldURI = new URI JavaDoc(locationPathField.getText());
356         } catch (URISyntaxException JavaDoc e) {
357             return locationPathField.getText();
358         }
359         return fieldURI.getPath();
360     }
361
362     /**
363      * Check if the entry in the widget location is valid. If it is valid return
364      * null. Otherwise return a string that indicates the problem.
365      *
366      * @return String
367      */

368     public String JavaDoc checkValidLocation() {
369
370         if (isDefault()) {
371             return null;
372         }
373
374         String JavaDoc locationFieldContents = locationPathField.getText();
375         if (locationFieldContents.length() == 0) {
376             return (IDEWorkbenchMessages.WizardNewProjectCreationPage_projectLocationEmpty);
377         }
378
379         URI JavaDoc newPath = getProjectLocationURI();
380         if (newPath == null) {
381             return IDEWorkbenchMessages.ProjectLocationSelectionDialog_locationError;
382         }
383
384         //create a dummy project for the purpose of validation if necessary
385
IProject project = existingProject;
386         if (project == null) {
387             String JavaDoc name = new Path(locationFieldContents).lastSegment();
388             if (name != null && Path.EMPTY.isValidSegment(name)){
389                 project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
390             }
391             else {
392                 return IDEWorkbenchMessages.ProjectLocationSelectionDialog_locationError;
393             }
394         }
395         IStatus locationStatus = project.getWorkspace()
396                 .validateProjectLocationURI(project, newPath);
397
398         if (!locationStatus.isOK()) {
399             return locationStatus.getMessage();
400         }
401         if (existingProject != null) {
402             URI JavaDoc projectPath = existingProject.getLocationURI();
403             if (projectPath != null && URIUtil.equals(projectPath, newPath)) {
404                 return IDEWorkbenchMessages.ProjectLocationSelectionDialog_locationError;
405             }
406         }
407
408         return null;
409     }
410
411     /**
412      * Get the URI for the location field if possible.
413      *
414      * @return URI or <code>null</code> if it is not valid.
415      */

416     public URI JavaDoc getProjectLocationURI() {
417
418         FileSystemConfiguration configuration = getSelectedConfiguration();
419         if (configuration == null) {
420             return null;
421         }
422
423         return configuration.getContributor().getURI(
424                 locationPathField.getText());
425
426     }
427
428     /**
429      * Return the selected contributor
430      *
431      * @return FileSystemConfiguration or <code>null</code> if it cannot be
432      * determined.
433      */

434     private FileSystemConfiguration getSelectedConfiguration() {
435         if (fileSystemSelectionArea == null) {
436             return FileSystemSupportRegistry.getInstance()
437                     .getDefaultConfiguration();
438         }
439
440         return fileSystemSelectionArea.getSelectedConfiguration();
441     }
442
443     /**
444      * Set the text to the default or clear it if not using the defaults.
445      *
446      * @param useDefaults
447      * @param newName
448      * the name of the project to use. If <code>null</code> use the
449      * existing project name.
450      */

451     public void updateProjectName(String JavaDoc newName) {
452         projectName = newName;
453         if (isDefault()) {
454             locationPathField.setText(TextProcessor
455                     .process(getDefaultPathDisplayString()));
456         }
457
458     }
459
460     /**
461      * Return the location for the project. If we are using defaults then return
462      * the workspace root so that core creates it with default values.
463      *
464      * @return String
465      */

466     public String JavaDoc getProjectLocation() {
467         if (isDefault()) {
468             return Platform.getLocation().toString();
469         }
470         return locationPathField.getText();
471     }
472 }
473
Popular Tags