Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8413813545df041f2273b3efdc3fdf98bc256f07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Philippe Ombredanne - bug 84808
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui.wizards;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;
import org.eclipse.team.internal.ccvs.core.ICVSRemoteFolder;
import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
import org.eclipse.team.internal.ccvs.ui.IHelpContextIds;
import org.eclipse.ui.PlatformUI;

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class CheckoutAsLocationSelectionPage extends CVSWizardPage {

	public static final String NAME = "CheckoutAsLocationSelectionPage"; //$NON-NLS-1$
	
	private Button browseButton;
	private Combo locationPathField;
	private Label locationLabel;
	private boolean useDefaults = true;
	private ICVSRemoteFolder[] remoteFolders;
	private String targetLocation;
	private IProject singleProject;
	
	// constants
	private static final int SIZING_TEXT_FIELD_WIDTH = 250;
    private static final int COMBO_HISTORY_LENGTH = 5;
	
    // store id constants
    private static final String STORE_PREVIOUS_LOCATIONS =
        "CheckoutAsLocationSelectionPage.STORE_PREVIOUS_LOCATIONS";//$NON-NLS-1$
    
	/**
	 * @param pageName
	 * @param title
	 * @param titleImage
	 * @param description
	 */
	public CheckoutAsLocationSelectionPage(ImageDescriptor titleImage, ICVSRemoteFolder[] remoteFolders) {
		super(NAME, CVSUIMessages.CheckoutAsLocationSelectionPage_title, titleImage, CVSUIMessages.CheckoutAsLocationSelectionPage_description); // 
		this.remoteFolders = remoteFolders;
	}

	/**
	 * @return
	 */
	private boolean isSingleFolder() {
		return remoteFolders.length == 1;
	}
	
	/**
	 * @param string
	 */
	public void setProject(IProject project) {
		singleProject = project;
		setLocationForSelection(true);
	}
	
	/**
	 * @param string
	 */
	public void setProjectName(String string) {
		if (string == null || string.equals(".")) return; //$NON-NLS-1$
		if (singleProject != null && singleProject.getName().equals(string)) return;
		setProject(ResourcesPlugin.getWorkspace().getRoot().getProject(string));
	}
	
	private IProject getSingleProject() {
		if (singleProject == null) {
			setProjectName(getPreferredFolderName(remoteFolders[0]));
		}
		return singleProject;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createControl(Composite parent) {
		Composite composite= createComposite(parent, 1, false);
		setControl(composite);
		// required in order to use setButtonLayoutData
		initializeDialogUnits(composite);
		
        PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IHelpContextIds.CHECKOUT_LOCATION_SELECTION_PAGE);

		final Button useDefaultsButton =
			new Button(composite, SWT.CHECK | SWT.RIGHT);
		useDefaultsButton.setText(CVSUIMessages.CheckoutAsLocationSelectionPage_useDefaultLabel); 
		useDefaultsButton.setSelection(this.useDefaults);

		createUserSpecifiedProjectLocationGroup(composite, !this.useDefaults);
        initializeValues();

		SelectionListener listener = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				useDefaults = useDefaultsButton.getSelection();
				browseButton.setEnabled(!useDefaults);
				locationPathField.setEnabled(!useDefaults);
				locationLabel.setEnabled(!useDefaults);
				setLocationForSelection(true);
				setErrorMessage(useDefaults ? null : checkValidLocation());
			}
		};
		useDefaultsButton.addSelectionListener(listener);
        Dialog.applyDialogFont(parent);
	}
	
	/**
	 * Creates the project location specification controls.
	 *
	 * @return the parent of the widgets created
	 * @param projectGroup the parent composite
	 * @param enabled - sets the initial enabled state of the widgets
	 */
	private Composite createUserSpecifiedProjectLocationGroup(Composite parent, boolean enabled) {
	
		// This group needs 3 columns
		Composite projectGroup = createComposite(parent, 3, true);
		
		// location label
		locationLabel = new Label(projectGroup, SWT.NONE);
		if (isSingleFolder()) {
			locationLabel.setText(CVSUIMessages.CheckoutAsLocationSelectionPage_locationLabel); 
		} else {
			locationLabel.setText(CVSUIMessages.CheckoutAsLocationSelectionPage_parentDirectoryLabel); 
		}
		locationLabel.setEnabled(enabled);

		// project location entry field
		locationPathField = new Combo(projectGroup, SWT.DROP_DOWN);
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.widthHint = SIZING_TEXT_FIELD_WIDTH;
		locationPathField.setLayoutData(data);
		locationPathField.setEnabled(enabled);

		// browse button
		this.browseButton = new Button(projectGroup, SWT.PUSH);
		this.browseButton.setText(CVSUIMessages.CheckoutAsLocationSelectionPage_browseLabel); 
		this.browseButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				handleLocationBrowseButtonPressed();
			}
		});
		this.browseButton.setEnabled(enabled);
		setButtonLayoutData(this.browseButton);

		// Set the initial value first before listener
		// to avoid handling an event during the creation.
		setLocationForSelection(true);
		locationPathField.addModifyListener(e -> setErrorMessage(checkValidLocation()));
		return projectGroup;
	}
	
    /**
     * Initializes states of the controls.
     */
    private void initializeValues() {
        // Set remembered values
        IDialogSettings settings = getDialogSettings();
        if (settings != null) {
            String[] previouseLocations = settings.getArray(STORE_PREVIOUS_LOCATIONS);
            if (previouseLocations != null) {
                for (int i = 0; i < previouseLocations.length; i++) {
                    if(isSingleFolder())
                        locationPathField.add(new Path(previouseLocations[i]).append(getSingleProject().getName()).toOSString());
                    else
                        locationPathField.add(previouseLocations[i]);
                }
            }
        }
    }
    
    /**
     * Saves the widget values
     */
    private void saveWidgetValues() {
        // Update history
        IDialogSettings settings = getDialogSettings();
        if (settings != null) {
            String[] previouseLocations = settings.getArray(STORE_PREVIOUS_LOCATIONS);
            if (previouseLocations == null) previouseLocations = new String[0];
            if(isSingleFolder())
                previouseLocations = addToHistory(previouseLocations, new Path(locationPathField.getText()).removeLastSegments(1).toOSString());
            else
                previouseLocations = addToHistory(previouseLocations, locationPathField.getText());
            settings.put(STORE_PREVIOUS_LOCATIONS, previouseLocations);
        }
    }

    /**
     * Adds an entry to a history, while taking care of duplicate history items
     * and excessively long histories.  The assumption is made that all histories
     * should be of length <code>CheckoutAsLocationSelectionPage.COMBO_HISTORY_LENGTH</code>.
     *
     * @param history the current history
     * @param newEntry the entry to add to the history
     * @return the history with the new entry appended
     */
    private String[] addToHistory(String[] history, String newEntry) {
		ArrayList<String> l = new ArrayList<>(Arrays.asList(history));
        addToHistory(l, newEntry);
        String[] r = new String[l.size()];
        l.toArray(r);
        return r;
    }
    
    /**
     * Adds an entry to a history, while taking care of duplicate history items
     * and excessively long histories.  The assumption is made that all histories
     * should be of length <code>CheckoutAsLocationSelectionPage.COMBO_HISTORY_LENGTH</code>.
     *
     * @param history the current history
     * @param newEntry the entry to add to the history
     */
	private void addToHistory(List<String> history, String newEntry) {
        history.remove(newEntry);
        history.add(0,newEntry);
    
        // since only one new item was added, we can be over the limit
        // by at most one item
        if (history.size() > COMBO_HISTORY_LENGTH)
            history.remove(COMBO_HISTORY_LENGTH);
    }
    
	/**
	 * Check if the entry in the widget location is valid. If it is valid return null. Otherwise
	 * return a string that indicates the problem.
	 */
	private String checkValidLocation() {

		if (useDefaults) {
			targetLocation = null;
			return null;
		} else {
			targetLocation = locationPathField.getText();
			if (targetLocation.equals("")) {//$NON-NLS-1$
				return(CVSUIMessages.CheckoutAsLocationSelectionPage_locationEmpty); 
			}
			else{
				IPath path = new Path("");//$NON-NLS-1$
				if (!path.isValidPath(targetLocation)) {
					return CVSUIMessages.CheckoutAsLocationSelectionPage_invalidLocation; 
				}
			}

			if (isSingleFolder()) {
				IStatus locationStatus =
					ResourcesPlugin.getWorkspace().validateProjectLocation(
						getSingleProject(),
						new Path(targetLocation));
	
				if (!locationStatus.isOK())
					return locationStatus.getMessage();
			} else {
				for (int i = 0; i < remoteFolders.length; i++) {
					String projectName = getPreferredFolderName(remoteFolders[i]);
					IStatus locationStatus = ResourcesPlugin.getWorkspace().validateProjectLocation(
						ResourcesPlugin.getWorkspace().getRoot().getProject(projectName),
						new Path(targetLocation).append(projectName));
					if (!locationStatus.isOK())
						return locationStatus.getMessage();
				}
			}

			return null;
		}
	}
	
	/**
	 * Set the location to the default location if we are set to useDefaults.
	 */
	private void setLocationForSelection(boolean changed) {
		if (useDefaults) {
			IPath defaultPath = null;
			if (isSingleFolder()) {
				IProject singleProject = getSingleProject();
				if (singleProject != null) {
					try {
						defaultPath = singleProject.getDescription().getLocation();
					} catch (CoreException e) {
						// ignore
					}
					if (defaultPath == null) {
						defaultPath = Platform.getLocation().append(singleProject.getName());
					}
				}
			} else {
				defaultPath = Platform.getLocation();
			}
			if (defaultPath != null) {
				locationPathField.setText(defaultPath.toOSString());
			}
			targetLocation = null;
		} else if (changed) {
			IPath location = null;
			IProject project = getSingleProject();
			if (project != null) {
				try {
					location = project.getDescription().getLocation();
				} catch (CoreException e) {
					// ignore the exception
				}
			}
			if (location == null) {
				targetLocation = null;
				locationPathField.setText(""); //$NON-NLS-1$
			} else {
				if (isSingleFolder()) {
					targetLocation = location.toOSString();
				} else {
					targetLocation = location.removeLastSegments(1).toOSString();
				}
				locationPathField.setText(targetLocation);
			}
		}
	}
	
	/**
	 *	Open an appropriate directory browser
	 */
	private void handleLocationBrowseButtonPressed() {
		DirectoryDialog dialog = new DirectoryDialog(locationPathField.getShell());
		if (isSingleFolder()) {
			dialog.setMessage(NLS.bind(CVSUIMessages.CheckoutAsLocationSelectionPage_messageForSingle, new String[] { getSingleProject().getName() })); 
		} else {
			dialog.setMessage(NLS.bind(CVSUIMessages.CheckoutAsLocationSelectionPage_messageForMulti, new String[] { Integer.valueOf(remoteFolders.length).toString() })); 
		}
	
		String dirName = locationPathField.getText();
		if (!dirName.equals("")) {//$NON-NLS-1$
			File path = new File(dirName);
			if (path.exists())
				dialog.setFilterPath(dirName);
		}

		String selectedDirectory = dialog.open();
		if (selectedDirectory != null) {
			if (isSingleFolder()) {
				locationPathField.setText(new Path(selectedDirectory).append(getSingleProject().getName()).toOSString());
			} else {
				locationPathField.setText(new Path(selectedDirectory).toOSString());
			}
		}
		targetLocation = locationPathField.getText();
	}
	
	/**
	 * Return the custom location for a single project. In this case, the specified
	 * location is used as the location of the project.
	 * 
	 * @param project
	 * @return
	 */
	public String getTargetLocation() {
		if (isCustomLocationSpecified()) {
            saveWidgetValues();
            return targetLocation;
        }
		return null;
	}

	/**
	 * @return
	 */
	private boolean isCustomLocationSpecified() {
		return !useDefaults;
	}

}

Back to the top