KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > wizards > CheckoutAsLocationSelectionPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Philippe Ombredanne - bug 84808
11  *******************************************************************************/

12 package org.eclipse.team.internal.ccvs.ui.wizards;
13
14 import java.io.File JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.core.runtime.Platform;
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.dialogs.IDialogSettings;
28 import org.eclipse.jface.resource.ImageDescriptor;
29 import org.eclipse.osgi.util.NLS;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.ModifyEvent;
32 import org.eclipse.swt.events.ModifyListener;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.events.SelectionListener;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Combo;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.DirectoryDialog;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.team.internal.ccvs.core.ICVSRemoteFolder;
43 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
44 import org.eclipse.team.internal.ccvs.ui.IHelpContextIds;
45 import org.eclipse.ui.PlatformUI;
46
47 /**
48  * @author Administrator
49  *
50  * To change the template for this generated type comment go to
51  * Window>Preferences>Java>Code Generation>Code and Comments
52  */

53 public class CheckoutAsLocationSelectionPage extends CVSWizardPage {
54
55     public static final String JavaDoc NAME = "CheckoutAsLocationSelectionPage"; //$NON-NLS-1$
56

57     private Button browseButton;
58     private Combo locationPathField;
59     private Label locationLabel;
60     private boolean useDefaults = true;
61     private ICVSRemoteFolder[] remoteFolders;
62     private String JavaDoc targetLocation;
63     private IProject singleProject;
64     
65     // constants
66
private static final int SIZING_TEXT_FIELD_WIDTH = 250;
67     private static final int COMBO_HISTORY_LENGTH = 5;
68     
69     // store id constants
70
private static final String JavaDoc STORE_PREVIOUS_LOCATIONS =
71         "CheckoutAsLocationSelectionPage.STORE_PREVIOUS_LOCATIONS";//$NON-NLS-1$
72

73     /**
74      * @param pageName
75      * @param title
76      * @param titleImage
77      * @param description
78      */

79     public CheckoutAsLocationSelectionPage(ImageDescriptor titleImage, ICVSRemoteFolder[] remoteFolders) {
80         super(NAME, CVSUIMessages.CheckoutAsLocationSelectionPage_title, titleImage, CVSUIMessages.CheckoutAsLocationSelectionPage_description); //
81
this.remoteFolders = remoteFolders;
82     }
83
84     /**
85      * @return
86      */

87     private boolean isSingleFolder() {
88         return remoteFolders.length == 1;
89     }
90     
91     /**
92      * @param string
93      */

94     public void setProject(IProject project) {
95         singleProject = project;
96         setLocationForSelection(true);
97     }
98     
99     /**
100      * @param string
101      */

102     public void setProjectName(String JavaDoc string) {
103         if (string == null || string.equals(".")) return; //$NON-NLS-1$
104
if (singleProject != null && singleProject.getName().equals(string)) return;
105         setProject(ResourcesPlugin.getWorkspace().getRoot().getProject(string));
106     }
107     
108     private IProject getSingleProject() {
109         if (singleProject == null) {
110             setProjectName(getPreferredFolderName(remoteFolders[0]));
111         }
112         return singleProject;
113     }
114     
115     /* (non-Javadoc)
116      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
117      */

118     public void createControl(Composite parent) {
119         Composite composite= createComposite(parent, 1, false);
120         setControl(composite);
121         // required in order to use setButtonLayoutData
122
initializeDialogUnits(composite);
123         
124         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IHelpContextIds.CHECKOUT_LOCATION_SELECTION_PAGE);
125
126         final Button useDefaultsButton =
127             new Button(composite, SWT.CHECK | SWT.RIGHT);
128         useDefaultsButton.setText(CVSUIMessages.CheckoutAsLocationSelectionPage_useDefaultLabel);
129         useDefaultsButton.setSelection(this.useDefaults);
130
131         createUserSpecifiedProjectLocationGroup(composite, !this.useDefaults);
132         initializeValues();
133
134         SelectionListener listener = new SelectionAdapter() {
135             public void widgetSelected(SelectionEvent e) {
136                 useDefaults = useDefaultsButton.getSelection();
137                 browseButton.setEnabled(!useDefaults);
138                 locationPathField.setEnabled(!useDefaults);
139                 locationLabel.setEnabled(!useDefaults);
140                 setLocationForSelection(true);
141                 setErrorMessage(useDefaults ? null : checkValidLocation());
142             }
143         };
144         useDefaultsButton.addSelectionListener(listener);
145         Dialog.applyDialogFont(parent);
146     }
147     
148     /**
149      * Creates the project location specification controls.
150      *
151      * @return the parent of the widgets created
152      * @param projectGroup the parent composite
153      * @param enabled - sets the initial enabled state of the widgets
154      */

155     private Composite createUserSpecifiedProjectLocationGroup(Composite parent, boolean enabled) {
156     
157         // This group needs 3 columns
158
Composite projectGroup = createComposite(parent, 3, false);
159         
160         // location label
161
locationLabel = new Label(projectGroup, SWT.NONE);
162         if (isSingleFolder()) {
163             locationLabel.setText(CVSUIMessages.CheckoutAsLocationSelectionPage_locationLabel);
164         } else {
165             locationLabel.setText(CVSUIMessages.CheckoutAsLocationSelectionPage_parentDirectoryLabel);
166         }
167         locationLabel.setEnabled(enabled);
168
169         // project location entry field
170
locationPathField = new Combo(projectGroup, SWT.DROP_DOWN);
171         GridData data = new GridData(GridData.FILL_HORIZONTAL);
172         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
173         locationPathField.setLayoutData(data);
174         locationPathField.setEnabled(enabled);
175
176         // browse button
177
this.browseButton = new Button(projectGroup, SWT.PUSH);
178         this.browseButton.setText(CVSUIMessages.CheckoutAsLocationSelectionPage_browseLabel);
179         this.browseButton.addSelectionListener(new SelectionAdapter() {
180             public void widgetSelected(SelectionEvent event) {
181                 handleLocationBrowseButtonPressed();
182             }
183         });
184         this.browseButton.setEnabled(enabled);
185         setButtonLayoutData(this.browseButton);
186
187         // Set the initial value first before listener
188
// to avoid handling an event during the creation.
189
setLocationForSelection(true);
190         locationPathField.addModifyListener(new ModifyListener() {
191             public void modifyText(ModifyEvent e) {
192                 setErrorMessage(checkValidLocation());
193             }
194         });
195         return projectGroup;
196     }
197     
198     /**
199      * Initializes states of the controls.
200      */

201     private void initializeValues() {
202         // Set remembered values
203
IDialogSettings settings = getDialogSettings();
204         if (settings != null) {
205             String JavaDoc[] previouseLocations = settings.getArray(STORE_PREVIOUS_LOCATIONS);
206             if (previouseLocations != null) {
207                 for (int i = 0; i < previouseLocations.length; i++) {
208                     if(isSingleFolder())
209                         locationPathField.add(new Path(previouseLocations[i]).append(getSingleProject().getName()).toOSString());
210                     else
211                         locationPathField.add(previouseLocations[i]);
212                 }
213             }
214         }
215     }
216     
217     /**
218      * Saves the widget values
219      */

220     private void saveWidgetValues() {
221         // Update history
222
IDialogSettings settings = getDialogSettings();
223         if (settings != null) {
224             String JavaDoc[] previouseLocations = settings.getArray(STORE_PREVIOUS_LOCATIONS);
225             if (previouseLocations == null) previouseLocations = new String JavaDoc[0];
226             if(isSingleFolder())
227                 previouseLocations = addToHistory(previouseLocations, new Path(locationPathField.getText()).removeLastSegments(1).toOSString());
228             else
229                 previouseLocations = addToHistory(previouseLocations, locationPathField.getText());
230             settings.put(STORE_PREVIOUS_LOCATIONS, previouseLocations);
231         }
232     }
233
234     /**
235      * Adds an entry to a history, while taking care of duplicate history items
236      * and excessively long histories. The assumption is made that all histories
237      * should be of length <code>CheckoutAsLocationSelectionPage.COMBO_HISTORY_LENGTH</code>.
238      *
239      * @param history the current history
240      * @param newEntry the entry to add to the history
241      * @return the history with the new entry appended
242      */

243     private String JavaDoc[] addToHistory(String JavaDoc[] history, String JavaDoc newEntry) {
244         ArrayList JavaDoc l = new ArrayList JavaDoc(Arrays.asList(history));
245         addToHistory(l, newEntry);
246         String JavaDoc[] r = new String JavaDoc[l.size()];
247         l.toArray(r);
248         return r;
249     }
250     
251     /**
252      * Adds an entry to a history, while taking care of duplicate history items
253      * and excessively long histories. The assumption is made that all histories
254      * should be of length <code>CheckoutAsLocationSelectionPage.COMBO_HISTORY_LENGTH</code>.
255      *
256      * @param history the current history
257      * @param newEntry the entry to add to the history
258      */

259     private void addToHistory(List JavaDoc history, String JavaDoc newEntry) {
260         history.remove(newEntry);
261         history.add(0,newEntry);
262     
263         // since only one new item was added, we can be over the limit
264
// by at most one item
265
if (history.size() > COMBO_HISTORY_LENGTH)
266             history.remove(COMBO_HISTORY_LENGTH);
267     }
268     
269     /**
270      * Check if the entry in the widget location is valid. If it is valid return null. Otherwise
271      * return a string that indicates the problem.
272      */

273     private String JavaDoc checkValidLocation() {
274
275         if (useDefaults) {
276             targetLocation = null;
277             return null;
278         } else {
279             targetLocation = locationPathField.getText();
280             if (targetLocation.equals("")) {//$NON-NLS-1$
281
return(CVSUIMessages.CheckoutAsLocationSelectionPage_locationEmpty);
282             }
283             else{
284                 IPath path = new Path("");//$NON-NLS-1$
285
if (!path.isValidPath(targetLocation)) {
286                     return CVSUIMessages.CheckoutAsLocationSelectionPage_invalidLocation;
287                 }
288             }
289
290             if (isSingleFolder()) {
291                 IStatus locationStatus =
292                     ResourcesPlugin.getWorkspace().validateProjectLocation(
293                         getSingleProject(),
294                         new Path(targetLocation));
295     
296                 if (!locationStatus.isOK())
297                     return locationStatus.getMessage();
298             } else {
299                 for (int i = 0; i < remoteFolders.length; i++) {
300                     String JavaDoc projectName = getPreferredFolderName(remoteFolders[i]);
301                     IStatus locationStatus = ResourcesPlugin.getWorkspace().validateProjectLocation(
302                         ResourcesPlugin.getWorkspace().getRoot().getProject(projectName),
303                         new Path(targetLocation).append(projectName));
304                     if (!locationStatus.isOK())
305                         return locationStatus.getMessage();
306                 }
307             }
308
309             return null;
310         }
311     }
312     
313     /**
314      * Set the location to the default location if we are set to useDefaults.
315      */

316     private void setLocationForSelection(boolean changed) {
317         if (useDefaults) {
318             IPath defaultPath = null;
319             if (isSingleFolder()) {
320                 IProject singleProject = getSingleProject();
321                 if (singleProject != null) {
322                     try {
323                         defaultPath = singleProject.getDescription().getLocation();
324                     } catch (CoreException e) {
325                         // ignore
326
}
327                     if (defaultPath == null) {
328                         defaultPath = Platform.getLocation().append(singleProject.getName());
329                     }
330                 }
331             } else {
332                 defaultPath = Platform.getLocation();
333             }
334             if (defaultPath != null) {
335                 locationPathField.setText(defaultPath.toOSString());
336             }
337             targetLocation = null;
338         } else if (changed) {
339             IPath location = null;
340             IProject project = getSingleProject();
341             if (project != null) {
342                 try {
343                     location = project.getDescription().getLocation();
344                 } catch (CoreException e) {
345                     // ignore the exception
346
}
347             }
348             if (location == null) {
349                 targetLocation = null;
350                 locationPathField.setText(""); //$NON-NLS-1$
351
} else {
352                 if (isSingleFolder()) {
353                     targetLocation = location.toOSString();
354                 } else {
355                     targetLocation = location.removeLastSegments(1).toOSString();
356                 }
357                 locationPathField.setText(targetLocation);
358             }
359         }
360     }
361     
362     /**
363      * Open an appropriate directory browser
364      */

365     private void handleLocationBrowseButtonPressed() {
366         DirectoryDialog dialog = new DirectoryDialog(locationPathField.getShell());
367         if (isSingleFolder()) {
368             dialog.setMessage(NLS.bind(CVSUIMessages.CheckoutAsLocationSelectionPage_messageForSingle, new String JavaDoc[] { getSingleProject().getName() }));
369         } else {
370             dialog.setMessage(NLS.bind(CVSUIMessages.CheckoutAsLocationSelectionPage_messageForMulti, new String JavaDoc[] { new Integer JavaDoc(remoteFolders.length).toString() }));
371         }
372     
373         String JavaDoc dirName = locationPathField.getText();
374         if (!dirName.equals("")) {//$NON-NLS-1$
375
File JavaDoc path = new File JavaDoc(dirName);
376             if (path.exists())
377                 dialog.setFilterPath(dirName);
378         }
379
380         String JavaDoc selectedDirectory = dialog.open();
381         if (selectedDirectory != null) {
382             if (isSingleFolder()) {
383                 locationPathField.setText(new Path(selectedDirectory).append(getSingleProject().getName()).toOSString());
384             } else {
385                 locationPathField.setText(new Path(selectedDirectory).toOSString());
386             }
387         }
388         targetLocation = locationPathField.getText();
389     }
390     
391     /**
392      * Return the custom location for a single project. In this case, the specified
393      * location is used as the location of the project.
394      *
395      * @param project
396      * @return
397      */

398     public String JavaDoc getTargetLocation() {
399         if (isCustomLocationSpecified()) {
400             saveWidgetValues();
401             return targetLocation;
402         }
403         return null;
404     }
405
406     /**
407      * @return
408      */

409     private boolean isCustomLocationSpecified() {
410         return !useDefaults;
411     }
412
413 }
414
Popular Tags