1 11 package org.eclipse.ui.internal.ide.misc; 12 13 import org.eclipse.core.resources.IContainer; 14 import org.eclipse.core.resources.IResource; 15 import org.eclipse.core.resources.IWorkspace; 16 import org.eclipse.core.resources.IWorkspaceRoot; 17 import org.eclipse.core.resources.ResourcesPlugin; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Path; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.events.FocusAdapter; 24 import org.eclipse.swt.events.FocusEvent; 25 import org.eclipse.swt.graphics.Font; 26 import org.eclipse.swt.layout.GridData; 27 import org.eclipse.swt.layout.GridLayout; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Event; 30 import org.eclipse.swt.widgets.Label; 31 import org.eclipse.swt.widgets.Listener; 32 import org.eclipse.swt.widgets.Text; 33 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 34 35 41 public class ResourceAndContainerGroup implements Listener { 42 public static final int PROBLEM_NONE = 0; 44 45 public static final int PROBLEM_RESOURCE_EMPTY = 1; 46 47 public static final int PROBLEM_RESOURCE_EXIST = 2; 48 49 public static final int PROBLEM_PATH_INVALID = 4; 50 51 public static final int PROBLEM_CONTAINER_EMPTY = 5; 52 53 public static final int PROBLEM_PROJECT_DOES_NOT_EXIST = 6; 54 55 public static final int PROBLEM_NAME_INVALID = 7; 56 57 public static final int PROBLEM_PATH_OCCUPIED = 8; 58 59 private Listener client; 61 62 private boolean allowExistingResources = false; 64 65 private String resourceType = IDEWorkbenchMessages.ResourceGroup_resource; 67 68 private boolean showClosedProjects = true; 70 71 private String problemMessage = ""; 74 private int problemType = PROBLEM_NONE; 75 76 private ContainerSelectionGroup containerGroup; 78 79 private Text resourceNameField; 80 81 86 private String resourceExtension; 87 88 private static final int SIZING_TEXT_FIELD_WIDTH = 250; 90 91 101 public ResourceAndContainerGroup(Composite parent, Listener client, 102 String resourceFieldLabel, String resourceType) { 103 this(parent, client, resourceFieldLabel, resourceType, true); 104 } 105 106 117 public ResourceAndContainerGroup(Composite parent, Listener client, 118 String resourceFieldLabel, String resourceType, 119 boolean showClosedProjects) { 120 this(parent, client, resourceFieldLabel, resourceType, 121 showClosedProjects, SWT.DEFAULT); 122 } 123 124 136 public ResourceAndContainerGroup(Composite parent, Listener client, 137 String resourceFieldLabel, String resourceType, 138 boolean showClosedProjects, int heightHint) { 139 super(); 140 this.resourceType = resourceType; 141 this.showClosedProjects = showClosedProjects; 142 createContents(parent, resourceFieldLabel, heightHint); 143 this.client = client; 144 } 145 146 152 public boolean areAllValuesValid() { 153 return problemType == PROBLEM_NONE; 154 } 155 156 162 protected void createContents(Composite parent, String resourceLabelString, 163 int heightHint) { 164 165 Font font = parent.getFont(); 166 Composite composite = new Composite(parent, SWT.NONE); 168 GridLayout layout = new GridLayout(); 169 layout.marginWidth = 0; 170 layout.marginHeight = 0; 171 composite.setLayout(layout); 172 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 173 composite.setFont(font); 174 175 if (heightHint == SWT.DEFAULT) { 177 containerGroup = new ContainerSelectionGroup(composite, this, true, 178 null, showClosedProjects); 179 } else { 180 containerGroup = new ContainerSelectionGroup(composite, this, true, 181 null, showClosedProjects, heightHint, SIZING_TEXT_FIELD_WIDTH); 182 } 183 184 Composite nameGroup = new Composite(composite, SWT.NONE); 186 layout = new GridLayout(); 187 layout.numColumns = 2; 188 layout.marginWidth = 0; 189 nameGroup.setLayout(layout); 190 nameGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL 191 | GridData.GRAB_HORIZONTAL)); 192 nameGroup.setFont(font); 193 194 Label label = new Label(nameGroup, SWT.NONE); 195 label.setText(resourceLabelString); 196 label.setFont(font); 197 198 resourceNameField = new Text(nameGroup, SWT.BORDER); 200 resourceNameField.addListener(SWT.Modify, this); 201 resourceNameField.addFocusListener(new FocusAdapter() { 202 public void focusLost(FocusEvent e) { 203 handleResourceNameFocusLostEvent(); 204 } 205 }); 206 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL 207 | GridData.GRAB_HORIZONTAL); 208 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 209 resourceNameField.setLayoutData(data); 210 resourceNameField.setFont(font); 211 validateControls(); 212 } 213 214 221 public IPath getContainerFullPath() { 222 return containerGroup.getContainerFullPath(); 223 } 224 225 232 public String getProblemMessage() { 233 return problemMessage; 234 } 235 236 242 public int getProblemType() { 243 return problemType; 244 } 245 246 257 public String getResource() { 258 String resource = resourceNameField.getText(); 259 if (useResourceExtension()) { 260 return resource + '.' + resourceExtension; 261 } 262 return resource; 263 } 264 265 272 public String getResourceExtension() { 273 return resourceExtension; 274 } 275 276 285 private boolean useResourceExtension() { 286 String resource = resourceNameField.getText(); 287 if ((resourceExtension != null) && 288 (resourceExtension.length() > 0) && 289 (resource.length() > 0) && 290 (resource.endsWith('.' + resourceExtension) == false)) { 291 return true; 292 } 293 return false; 294 } 295 296 305 private void handleResourceNameFocusLostEvent() { 306 if (useResourceExtension()) { 307 setResource(resourceNameField.getText() + '.' + resourceExtension); 308 } 309 } 310 311 316 public void handleEvent(Event e) { 317 validateControls(); 318 if (client != null) { 319 client.handleEvent(e); 320 } 321 } 322 323 326 public void setAllowExistingResources(boolean value) { 327 allowExistingResources = value; 328 } 329 330 335 public void setContainerFullPath(IPath path) { 336 IResource initial = ResourcesPlugin.getWorkspace().getRoot() 337 .findMember(path); 338 if (initial != null) { 339 if (!(initial instanceof IContainer)) { 340 initial = initial.getParent(); 341 } 342 containerGroup.setSelectedContainer((IContainer) initial); 343 } 344 validateControls(); 345 } 346 347 350 public void setFocus() { 351 resourceNameField.setSelection(0, resourceNameField.getText().length()); 353 resourceNameField.setFocus(); 354 } 355 356 361 public void setResource(String value) { 362 resourceNameField.setText(value); 363 validateControls(); 364 } 365 366 388 public void setResourceExtension(String value) { 389 resourceExtension = value; 390 validateControls(); 391 } 392 393 400 protected boolean validateContainer() { 401 IPath path = containerGroup.getContainerFullPath(); 402 if (path == null) { 403 problemType = PROBLEM_CONTAINER_EMPTY; 404 problemMessage = IDEWorkbenchMessages.ResourceGroup_folderEmpty; 405 return false; 406 } 407 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 408 String projectName = path.segment(0); 409 if (projectName == null 410 || !workspace.getRoot().getProject(projectName).exists()) { 411 problemType = PROBLEM_PROJECT_DOES_NOT_EXIST; 412 problemMessage = IDEWorkbenchMessages.ResourceGroup_noProject; 413 return false; 414 } 415 IWorkspaceRoot root = workspace.getRoot(); 417 while (path.segmentCount() > 1) { 418 if (root.getFile(path).exists()) { 419 problemType = PROBLEM_PATH_OCCUPIED; 420 problemMessage = NLS.bind(IDEWorkbenchMessages.ResourceGroup_pathOccupied, path.makeRelative()); 421 return false; 422 } 423 path = path.removeLastSegments(1); 424 } 425 return true; 426 } 427 428 434 protected boolean validateControls() { 435 if (containerGroup == null) { 437 return false; 438 } 439 problemType = PROBLEM_NONE; 440 problemMessage = ""; 442 if (!validateContainer() || !validateResourceName()) { 443 return false; 444 } 445 446 IPath path = containerGroup.getContainerFullPath().append( 447 getResource()); 448 return validateFullResourcePath(path); 449 } 450 451 460 protected boolean validateFullResourcePath(IPath resourcePath) { 461 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 462 463 IStatus result = workspace.validatePath(resourcePath.toString(), 464 IResource.FOLDER); 465 if (!result.isOK()) { 466 problemType = PROBLEM_PATH_INVALID; 467 problemMessage = result.getMessage(); 468 return false; 469 } 470 471 if (!allowExistingResources 472 && (workspace.getRoot().getFolder(resourcePath).exists() || workspace 473 .getRoot().getFile(resourcePath).exists())) { 474 problemType = PROBLEM_RESOURCE_EXIST; 475 problemMessage = NLS.bind(IDEWorkbenchMessages.ResourceGroup_nameExists, getResource()); 476 return false; 477 } 478 return true; 479 } 480 481 488 protected boolean validateResourceName() { 489 String resourceName = getResource(); 490 491 if (resourceName.length() == 0) { 492 problemType = PROBLEM_RESOURCE_EMPTY; 493 problemMessage = NLS.bind(IDEWorkbenchMessages.ResourceGroup_emptyName, resourceType); 494 return false; 495 } 496 497 if (!Path.ROOT.isValidPath(resourceName)) { 498 problemType = PROBLEM_NAME_INVALID; 499 problemMessage = NLS.bind(IDEWorkbenchMessages.ResourceGroup_invalidFilename, resourceName); 500 return false; 501 } 502 return true; 503 } 504 505 } 506 | Popular Tags |