1 12 package org.eclipse.team.internal.ccvs.ui.wizards; 13 14 import java.io.File ; 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.List ; 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 53 public class CheckoutAsLocationSelectionPage extends CVSWizardPage { 54 55 public static final String NAME = "CheckoutAsLocationSelectionPage"; 57 private Button browseButton; 58 private Combo locationPathField; 59 private Label locationLabel; 60 private boolean useDefaults = true; 61 private ICVSRemoteFolder[] remoteFolders; 62 private String targetLocation; 63 private IProject singleProject; 64 65 private static final int SIZING_TEXT_FIELD_WIDTH = 250; 67 private static final int COMBO_HISTORY_LENGTH = 5; 68 69 private static final String STORE_PREVIOUS_LOCATIONS = 71 "CheckoutAsLocationSelectionPage.STORE_PREVIOUS_LOCATIONS"; 73 79 public CheckoutAsLocationSelectionPage(ImageDescriptor titleImage, ICVSRemoteFolder[] remoteFolders) { 80 super(NAME, CVSUIMessages.CheckoutAsLocationSelectionPage_title, titleImage, CVSUIMessages.CheckoutAsLocationSelectionPage_description); this.remoteFolders = remoteFolders; 82 } 83 84 87 private boolean isSingleFolder() { 88 return remoteFolders.length == 1; 89 } 90 91 94 public void setProject(IProject project) { 95 singleProject = project; 96 setLocationForSelection(true); 97 } 98 99 102 public void setProjectName(String string) { 103 if (string == null || string.equals(".")) return; 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 118 public void createControl(Composite parent) { 119 Composite composite= createComposite(parent, 1, false); 120 setControl(composite); 121 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 155 private Composite createUserSpecifiedProjectLocationGroup(Composite parent, boolean enabled) { 156 157 Composite projectGroup = createComposite(parent, 3, false); 159 160 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 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 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 setLocationForSelection(true); 190 locationPathField.addModifyListener(new ModifyListener() { 191 public void modifyText(ModifyEvent e) { 192 setErrorMessage(checkValidLocation()); 193 } 194 }); 195 return projectGroup; 196 } 197 198 201 private void initializeValues() { 202 IDialogSettings settings = getDialogSettings(); 204 if (settings != null) { 205 String [] 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 220 private void saveWidgetValues() { 221 IDialogSettings settings = getDialogSettings(); 223 if (settings != null) { 224 String [] previouseLocations = settings.getArray(STORE_PREVIOUS_LOCATIONS); 225 if (previouseLocations == null) previouseLocations = new String [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 243 private String [] addToHistory(String [] history, String newEntry) { 244 ArrayList l = new ArrayList (Arrays.asList(history)); 245 addToHistory(l, newEntry); 246 String [] r = new String [l.size()]; 247 l.toArray(r); 248 return r; 249 } 250 251 259 private void addToHistory(List history, String newEntry) { 260 history.remove(newEntry); 261 history.add(0,newEntry); 262 263 if (history.size() > COMBO_HISTORY_LENGTH) 266 history.remove(COMBO_HISTORY_LENGTH); 267 } 268 269 273 private String checkValidLocation() { 274 275 if (useDefaults) { 276 targetLocation = null; 277 return null; 278 } else { 279 targetLocation = locationPathField.getText(); 280 if (targetLocation.equals("")) { return(CVSUIMessages.CheckoutAsLocationSelectionPage_locationEmpty); 282 } 283 else{ 284 IPath path = new Path(""); 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 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 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 } 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 } 347 } 348 if (location == null) { 349 targetLocation = null; 350 locationPathField.setText(""); } 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 365 private void handleLocationBrowseButtonPressed() { 366 DirectoryDialog dialog = new DirectoryDialog(locationPathField.getShell()); 367 if (isSingleFolder()) { 368 dialog.setMessage(NLS.bind(CVSUIMessages.CheckoutAsLocationSelectionPage_messageForSingle, new String [] { getSingleProject().getName() })); 369 } else { 370 dialog.setMessage(NLS.bind(CVSUIMessages.CheckoutAsLocationSelectionPage_messageForMulti, new String [] { new Integer (remoteFolders.length).toString() })); 371 } 372 373 String dirName = locationPathField.getText(); 374 if (!dirName.equals("")) { File path = new File (dirName); 376 if (path.exists()) 377 dialog.setFilterPath(dirName); 378 } 379 380 String 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 398 public String getTargetLocation() { 399 if (isCustomLocationSpecified()) { 400 saveWidgetValues(); 401 return targetLocation; 402 } 403 return null; 404 } 405 406 409 private boolean isCustomLocationSpecified() { 410 return !useDefaults; 411 } 412 413 } 414 | Popular Tags |