KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > TagetLocationSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui;
12
13 import java.io.File JavaDoc;
14
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.IWorkspace;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
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.jface.dialogs.Dialog;
25 import org.eclipse.jface.resource.JFaceColors;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.events.SelectionListener;
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.Control;
35 import org.eclipse.swt.widgets.DirectoryDialog;
36 import org.eclipse.swt.widgets.Event;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Listener;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.ui.dialogs.SelectionDialog;
42
43 /**
44  * Select the target location that will be the parent of the selected projects.
45  * The behavior of the dialog differs between 1 project and multiple projects.
46  * For one project, the location specified is the location of the project and
47  * the project name can be modified. For multiple projects, it is the parent
48  * location which is specified.
49  */

50 public class TagetLocationSelectionDialog extends SelectionDialog {
51
52     // widgets
53
private Text projectNameField;
54     private Text locationPathField;
55     private Label locationLabel;
56     private Label statusMessageLabel;
57     private Button browseButton;
58     
59     // state
60
private boolean useDefaults = true;
61     private IProject[] targetProjects;
62     private String JavaDoc newProjectName;
63     private String JavaDoc targetLocation;
64     
65     // constants
66
private static final int SIZING_TEXT_FIELD_WIDTH = 250;
67     
68     /**
69      * Constructor.
70      * @param parentShell
71      */

72     public TagetLocationSelectionDialog(Shell parentShell, String JavaDoc title, IProject targetProject) {
73         this(parentShell, title, new IProject[] { targetProject });
74     }
75     
76     /**
77      * Constructor.
78      * @param parentShell
79      */

80     public TagetLocationSelectionDialog(Shell parentShell, String JavaDoc title, IProject[] targetProjects) {
81         super(parentShell);
82         setTitle(title);
83         this.targetProjects = targetProjects;
84         if (targetProjects.length == 1) newProjectName = targetProjects[0].getName();
85     }
86
87     /* (non-Javadoc)
88      * Method declared on Dialog.
89      */

90     protected Control createDialogArea(Composite parent) {
91         // page group
92
Composite composite = (Composite) super.createDialogArea(parent);
93         composite.setLayout(new GridLayout());
94         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
95
96         if (isSingleCheckout())
97             createProjectNameGroup(composite);
98         createProjectLocationGroup(composite);
99
100         //Add in a label for status messages if required
101
statusMessageLabel = new Label(composite, SWT.NONE);
102         statusMessageLabel.setLayoutData(new GridData(GridData.FILL_BOTH));
103
104         Dialog.applyDialogFont(parent);
105         return composite;
106     }
107     
108     /**
109      * Creates the project name specification controls.
110      *
111      * @param parent the parent composite
112      */

113     private void createProjectNameGroup(Composite parent) {
114         // project specification group
115
Composite projectGroup = new Composite(parent,SWT.NONE);
116         GridLayout layout = new GridLayout();
117         layout.numColumns = 2;
118         projectGroup.setLayout(layout);
119         projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
120
121         // new project label
122
Label projectLabel = new Label(projectGroup,SWT.NONE);
123         projectLabel.setText(Policy.bind("TargetLocationSelectionDialog.projectNameLabel")); //$NON-NLS-1$
124

125         // new project name entry field
126
projectNameField = new Text(projectGroup, SWT.BORDER);
127         GridData data = new GridData(GridData.FILL_HORIZONTAL);
128         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
129         projectNameField.setLayoutData(data);
130     
131         // Set the initial value first before listener
132
// to avoid handling an event during the creation.
133
projectNameField.setText(getNewProjectName());
134         projectNameField.selectAll();
135     
136         createNameListener();
137     
138     }
139     
140     /**
141      * Create the listener that is used to validate the entries for the receiver
142      */

143     private void createNameListener() {
144
145         Listener listener = new Listener() {
146             public void handleEvent(Event event) {
147                 newProjectName = projectNameField.getText();
148                 setLocationForSelection(false);
149                 applyValidationResult(checkValid());
150             }
151         };
152
153         this.projectNameField.addListener(SWT.Modify, listener);
154     }
155     
156     /**
157      * Set the location to the default location if we are set to useDefaults.
158      */

159     private void setLocationForSelection(boolean changed) {
160         if (useDefaults) {
161             IPath defaultPath = null;
162             if (isSingleCheckout()) {
163                 try {
164                     defaultPath = getSingleProject().getDescription().getLocation();
165                 } catch (CoreException e) {
166                     // ignore
167
}
168                 if (defaultPath == null) {
169                     defaultPath = Platform.getLocation().append(getSingleProject().getName());
170                 }
171             } else {
172                 defaultPath = Platform.getLocation();
173             }
174             locationPathField.setText(defaultPath.toOSString());
175             targetLocation = null;
176         } else if (changed) {
177             IPath location = null;
178             try {
179                 location = this.targetProjects[0].getDescription().getLocation();
180             } catch (CoreException e) {
181                 // ignore the exception
182
}
183             if (location == null) {
184                 targetLocation = null;
185                 locationPathField.setText(""); //$NON-NLS-1$
186
} else {
187                 if (isSingleCheckout()) {
188                     targetLocation = location.toOSString();
189                 } else {
190                     targetLocation = location.removeLastSegments(1).toOSString();
191                 }
192                 locationPathField.setText(targetLocation);
193             }
194         }
195     }
196     
197     /**
198      * Creates the project location specification controls.
199      *
200      * @param parent the parent composite
201      */

202     private final void createProjectLocationGroup(Composite parent) {
203     
204         // project specification group
205
Composite projectGroup = new Composite(parent, SWT.NONE);
206         GridLayout layout = new GridLayout();
207         layout.numColumns = 3;
208         projectGroup.setLayout(layout);
209         projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
210
211         final Button useDefaultsButton =
212             new Button(projectGroup, SWT.CHECK | SWT.RIGHT);
213         useDefaultsButton.setText(Policy.bind("TargetLocationSelectionDialog.useDefaultLabel")); //$NON-NLS-1$
214
useDefaultsButton.setSelection(this.useDefaults);
215         GridData buttonData = new GridData();
216         buttonData.horizontalSpan = 3;
217         useDefaultsButton.setLayoutData(buttonData);
218
219         createUserSpecifiedProjectLocationGroup(projectGroup, !this.useDefaults);
220
221         SelectionListener listener = new SelectionAdapter() {
222             public void widgetSelected(SelectionEvent e) {
223                 useDefaults = useDefaultsButton.getSelection();
224                 browseButton.setEnabled(!useDefaults);
225                 locationPathField.setEnabled(!useDefaults);
226                 locationLabel.setEnabled(!useDefaults);
227                 setLocationForSelection(true);
228             }
229         };
230         useDefaultsButton.addSelectionListener(listener);
231     }
232     
233     /**
234      * Creates the project location specification controls.
235      *
236      * @return the parent of the widgets created
237      * @param projectGroup the parent composite
238      * @param enabled - sets the initial enabled state of the widgets
239      */

240     private Composite createUserSpecifiedProjectLocationGroup(Composite projectGroup, boolean enabled) {
241     
242         // location label
243
locationLabel = new Label(projectGroup, SWT.NONE);
244         if (isSingleCheckout()) {
245             locationLabel.setText(Policy.bind("TargetLocationSelectionDialog.locationLabel")); //$NON-NLS-1$
246
} else {
247             locationLabel.setText(Policy.bind("TargetLocationSelectionDialog.parentDirectoryLabel")); //$NON-NLS-1$
248
}
249         locationLabel.setEnabled(enabled);
250
251         // project location entry field
252
locationPathField = new Text(projectGroup, SWT.BORDER);
253         GridData data = new GridData(GridData.FILL_HORIZONTAL);
254         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
255         locationPathField.setLayoutData(data);
256         locationPathField.setEnabled(enabled);
257
258         // browse button
259
this.browseButton = new Button(projectGroup, SWT.PUSH);
260         this.browseButton.setText(Policy.bind("TargetLocationSelectionDialog.browseLabel")); //$NON-NLS-1$
261
this.browseButton.addSelectionListener(new SelectionAdapter() {
262             public void widgetSelected(SelectionEvent event) {
263                 handleLocationBrowseButtonPressed();
264             }
265         });
266         this.browseButton.setEnabled(enabled);
267         setButtonLayoutData(this.browseButton);
268
269         // Set the initial value first before listener
270
// to avoid handling an event during the creation.
271
setLocationForSelection(true);
272         createLocationListener();
273         return projectGroup;
274
275     }
276     
277     /**
278      * Open an appropriate directory browser
279      */

280     private void handleLocationBrowseButtonPressed() {
281         DirectoryDialog dialog = new DirectoryDialog(locationPathField.getShell());
282         if (isSingleCheckout()) {
283             dialog.setMessage(Policy.bind("TargetLocationSelectionDialog.messageForSingle", newProjectName)); //$NON-NLS-1$
284
} else {
285             dialog.setMessage(Policy.bind("TargetLocationSelectionDialog.messageForMulti", new Integer JavaDoc(targetProjects.length).toString())); //$NON-NLS-1$
286
}
287     
288         String JavaDoc dirName = locationPathField.getText();
289         if (!dirName.equals("")) {//$NON-NLS-1$
290
File JavaDoc path = new File JavaDoc(dirName);
291             if (path.exists())
292                 dialog.setFilterPath(dirName);
293         }
294
295         String JavaDoc selectedDirectory = dialog.open();
296         if (selectedDirectory != null) {
297             if (targetProjects.length == 1) {
298                 locationPathField.setText(new Path(selectedDirectory).append(newProjectName).toOSString());
299             } else {
300                 locationPathField.setText(new Path(selectedDirectory).toOSString());
301             }
302         }
303         targetLocation = locationPathField.getText();
304     }
305     
306     /**
307      * Method isSingleCheckout.
308      * @return boolean
309      */

310     private boolean isSingleCheckout() {
311         return targetProjects.length == 1;
312     }
313
314     private IProject getSingleProject() {
315         if (newProjectName == null || newProjectName.length() == 0 || targetProjects[0].getName().equals(newProjectName))
316             return targetProjects[0];
317         else
318             return ResourcesPlugin.getWorkspace().getRoot().getProject(newProjectName);
319     }
320     
321     /**
322      * Create the listener that is used to validate the location entered by the iser
323      */

324     private void createLocationListener() {
325
326         Listener listener = new Listener() {
327             public void handleEvent(Event event) {
328                 applyValidationResult(checkValid());
329             }
330         };
331
332         this.locationPathField.addListener(SWT.Modify, listener);
333     }
334     
335     /**
336      * Check the message. If it is null then continue otherwise inform the user via the
337      * status value and disable the OK.
338      * @param message - the error message to show if it is not null.
339      */

340     private void applyValidationResult(String JavaDoc errorMsg) {
341
342         if (errorMsg == null) {
343             statusMessageLabel.setText("");//$NON-NLS-1$
344
getOkButton().setEnabled(true);
345         } else {
346             statusMessageLabel.setForeground(
347                 JFaceColors.getErrorText(
348                     statusMessageLabel.getDisplay()));
349             statusMessageLabel.setText(errorMsg);
350             getOkButton().setEnabled(false);
351         }
352     }
353     /**
354      * Check whether the entries are valid. If so return null. Otherwise
355      * return a string that indicates the problem.
356      */

357     private String JavaDoc checkValid() {
358         if (isSingleCheckout()) {
359             String JavaDoc valid = checkValidName();
360             if (valid != null)
361                 return valid;
362         }
363         return checkValidLocation();
364     }
365     /**
366      * Check if the entry in the widget location is valid. If it is valid return null. Otherwise
367      * return a string that indicates the problem.
368      */

369     private String JavaDoc checkValidLocation() {
370
371         if (useDefaults) {
372             targetLocation = null;
373             return null;
374         } else {
375             targetLocation = locationPathField.getText();
376             if (targetLocation.equals("")) {//$NON-NLS-1$
377
return(Policy.bind("TagetLocationSelectionDialog.locationEmpty")); //$NON-NLS-1$
378
}
379             else{
380                 IPath path = new Path("");//$NON-NLS-1$
381
if (!path.isValidPath(targetLocation)) {
382                     return Policy.bind("TagetLocationSelectionDialog.invalidLocation"); //$NON-NLS-1$
383
}
384             }
385
386             if (isSingleCheckout()) {
387                 IStatus locationStatus =
388                     ResourcesPlugin.getWorkspace().validateProjectLocation(
389                         getSingleProject(),
390                         new Path(targetLocation));
391     
392                 if (!locationStatus.isOK())
393                     return locationStatus.getMessage();
394             } else {
395                 for (int i = 0; i < targetProjects.length; i++) {
396                     ResourcesPlugin.getWorkspace().validateProjectLocation(
397                         targetProjects[i],
398                         new Path(targetLocation).append(targetProjects[i].getName()));
399                 }
400             }
401
402             return null;
403         }
404     }
405     /**
406      * Check if the entries in the widget are valid. If they are return null otherwise
407      * return a string that indicates the problem.
408      */

409     private String JavaDoc checkValidName() {
410
411         newProjectName = this.projectNameField.getText();
412         IWorkspace workspace = ResourcesPlugin.getWorkspace();
413         IStatus nameStatus = workspace.validateName(newProjectName, IResource.PROJECT);
414         if (!nameStatus.isOK())
415             return nameStatus.getMessage();
416 // IProject newProject = workspace.getRoot().getProject(newProjectName);
417
// if (newProject.exists()) {
418
// return Policy.bind("TagetLocationSelectionDialog.alreadyExists", newProjectName); //$NON-NLS-1$
419
// }
420

421         return null;
422     }
423     
424     /**
425      * @return String
426      */

427     public String JavaDoc getNewProjectName() {
428         return newProjectName;
429     }
430
431     /**
432      * @return String
433      */

434     public String JavaDoc getTargetLocation() {
435         return targetLocation;
436     }
437
438 }
439
Popular Tags