1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 import java.io.File ; 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 50 public class TagetLocationSelectionDialog extends SelectionDialog { 51 52 private Text projectNameField; 54 private Text locationPathField; 55 private Label locationLabel; 56 private Label statusMessageLabel; 57 private Button browseButton; 58 59 private boolean useDefaults = true; 61 private IProject[] targetProjects; 62 private String newProjectName; 63 private String targetLocation; 64 65 private static final int SIZING_TEXT_FIELD_WIDTH = 250; 67 68 72 public TagetLocationSelectionDialog(Shell parentShell, String title, IProject targetProject) { 73 this(parentShell, title, new IProject[] { targetProject }); 74 } 75 76 80 public TagetLocationSelectionDialog(Shell parentShell, String 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 90 protected Control createDialogArea(Composite parent) { 91 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 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 113 private void createProjectNameGroup(Composite parent) { 114 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 Label projectLabel = new Label(projectGroup,SWT.NONE); 123 projectLabel.setText(Policy.bind("TargetLocationSelectionDialog.projectNameLabel")); 125 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 projectNameField.setText(getNewProjectName()); 134 projectNameField.selectAll(); 135 136 createNameListener(); 137 138 } 139 140 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 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 } 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 } 183 if (location == null) { 184 targetLocation = null; 185 locationPathField.setText(""); } 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 202 private final void createProjectLocationGroup(Composite parent) { 203 204 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")); 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 240 private Composite createUserSpecifiedProjectLocationGroup(Composite projectGroup, boolean enabled) { 241 242 locationLabel = new Label(projectGroup, SWT.NONE); 244 if (isSingleCheckout()) { 245 locationLabel.setText(Policy.bind("TargetLocationSelectionDialog.locationLabel")); } else { 247 locationLabel.setText(Policy.bind("TargetLocationSelectionDialog.parentDirectoryLabel")); } 249 locationLabel.setEnabled(enabled); 250 251 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 this.browseButton = new Button(projectGroup, SWT.PUSH); 260 this.browseButton.setText(Policy.bind("TargetLocationSelectionDialog.browseLabel")); 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 setLocationForSelection(true); 272 createLocationListener(); 273 return projectGroup; 274 275 } 276 277 280 private void handleLocationBrowseButtonPressed() { 281 DirectoryDialog dialog = new DirectoryDialog(locationPathField.getShell()); 282 if (isSingleCheckout()) { 283 dialog.setMessage(Policy.bind("TargetLocationSelectionDialog.messageForSingle", newProjectName)); } else { 285 dialog.setMessage(Policy.bind("TargetLocationSelectionDialog.messageForMulti", new Integer (targetProjects.length).toString())); } 287 288 String dirName = locationPathField.getText(); 289 if (!dirName.equals("")) { File path = new File (dirName); 291 if (path.exists()) 292 dialog.setFilterPath(dirName); 293 } 294 295 String 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 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 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 340 private void applyValidationResult(String errorMsg) { 341 342 if (errorMsg == null) { 343 statusMessageLabel.setText(""); 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 357 private String checkValid() { 358 if (isSingleCheckout()) { 359 String valid = checkValidName(); 360 if (valid != null) 361 return valid; 362 } 363 return checkValidLocation(); 364 } 365 369 private String checkValidLocation() { 370 371 if (useDefaults) { 372 targetLocation = null; 373 return null; 374 } else { 375 targetLocation = locationPathField.getText(); 376 if (targetLocation.equals("")) { return(Policy.bind("TagetLocationSelectionDialog.locationEmpty")); } 379 else{ 380 IPath path = new Path(""); if (!path.isValidPath(targetLocation)) { 382 return Policy.bind("TagetLocationSelectionDialog.invalidLocation"); } 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 409 private String 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 421 return null; 422 } 423 424 427 public String getNewProjectName() { 428 return newProjectName; 429 } 430 431 434 public String getTargetLocation() { 435 return targetLocation; 436 } 437 438 } 439 | Popular Tags |