1 11 12 package org.eclipse.ui.internal.ide.dialogs; 13 14 import java.net.URI ; 15 import java.net.URISyntaxException ; 16 import org.eclipse.core.filesystem.EFS; 17 import org.eclipse.core.filesystem.IFileInfo; 18 import org.eclipse.core.filesystem.IFileStore; 19 import org.eclipse.core.filesystem.URIUtil; 20 import org.eclipse.core.resources.IPathVariableManager; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.resources.IWorkspace; 23 import org.eclipse.core.resources.ResourcesPlugin; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.core.runtime.IStatus; 26 import org.eclipse.core.runtime.Path; 27 import org.eclipse.core.runtime.Status; 28 import org.eclipse.jface.dialogs.Dialog; 29 import org.eclipse.jface.dialogs.IDialogConstants; 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.graphics.Font; 37 import org.eclipse.swt.graphics.FontMetrics; 38 import org.eclipse.swt.graphics.GC; 39 import org.eclipse.swt.layout.GridData; 40 import org.eclipse.swt.layout.GridLayout; 41 import org.eclipse.swt.widgets.Button; 42 import org.eclipse.swt.widgets.Composite; 43 import org.eclipse.swt.widgets.Control; 44 import org.eclipse.swt.widgets.DirectoryDialog; 45 import org.eclipse.swt.widgets.Event; 46 import org.eclipse.swt.widgets.FileDialog; 47 import org.eclipse.swt.widgets.Label; 48 import org.eclipse.swt.widgets.Listener; 49 import org.eclipse.swt.widgets.Text; 50 import org.eclipse.ui.ide.dialogs.PathVariableSelectionDialog; 51 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 52 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 53 import org.eclipse.ui.internal.ide.filesystem.FileSystemConfiguration; 54 import org.eclipse.ui.internal.ide.filesystem.FileSystemSupportRegistry; 55 56 61 public class CreateLinkedResourceGroup { 62 private Listener listener; 63 64 private String linkTarget = ""; 66 private int type; 67 68 private boolean createLink = false; 69 70 private FontMetrics fontMetrics; 72 73 private Composite groupComposite; 75 76 private Text linkTargetField; 77 78 private Button browseButton; 79 80 private Button variablesButton; 81 82 private Label resolvedPathLabelText; 83 84 private Label resolvedPathLabelData; 85 86 private final IStringValue updatableResourceName; 87 88 94 public static interface IStringValue { 95 101 void setValue(String string); 102 103 108 String getValue(); 109 } 110 111 private String lastUpdatedValue; 112 113 private FileSystemSelectionArea fileSystemSelectionArea; 114 115 130 public CreateLinkedResourceGroup(int type, Listener listener, 131 IStringValue updatableResourceName) { 132 this.type = type; 133 this.listener = listener; 134 this.updatableResourceName = updatableResourceName; 135 if (updatableResourceName != null) { 136 lastUpdatedValue = updatableResourceName.getValue(); 137 } 138 } 139 140 147 public Composite createContents(Composite parent) { 148 Font font = parent.getFont(); 149 initializeDialogUnits(parent); 150 groupComposite = new Composite(parent, SWT.NONE); 152 GridLayout layout = new GridLayout(); 153 groupComposite.setLayout(layout); 154 groupComposite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL 155 | GridData.FILL_HORIZONTAL)); 156 groupComposite.setFont(font); 157 158 final Button createLinkButton = new Button(groupComposite, SWT.CHECK); 159 if (type == IResource.FILE) { 160 createLinkButton 161 .setText(IDEWorkbenchMessages.CreateLinkedResourceGroup_linkFileButton); 162 } else { 163 createLinkButton 164 .setText(IDEWorkbenchMessages.CreateLinkedResourceGroup_linkFolderButton); 165 } 166 createLinkButton.setSelection(createLink); 167 createLinkButton.setFont(font); 168 SelectionListener selectionListener = new SelectionAdapter() { 169 public void widgetSelected(SelectionEvent e) { 170 createLink = createLinkButton.getSelection(); 171 browseButton.setEnabled(createLink); 172 variablesButton.setEnabled(createLink); 173 linkTargetField.setEnabled(createLink); 175 if (fileSystemSelectionArea != null) 176 fileSystemSelectionArea.setEnabled(createLink); 177 178 if (listener != null) { 179 listener.handleEvent(new Event()); 180 } 181 } 182 }; 183 createLinkButton.addSelectionListener(selectionListener); 184 185 createLinkLocationGroup(groupComposite, createLink); 186 return groupComposite; 187 } 188 189 197 private void createLinkLocationGroup(Composite locationGroup, 198 boolean enabled) { 199 Button button = new Button(locationGroup, SWT.CHECK); 200 int indent = button.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; 201 202 button.dispose(); 203 204 Composite linkTargetGroup = new Composite(locationGroup, SWT.NONE); 207 GridLayout layout = new GridLayout(); 208 layout.numColumns = 4; 209 layout.marginHeight = 0; 210 layout.marginWidth = 0; 211 linkTargetGroup.setLayout(layout); 212 GridData data = new GridData(GridData.FILL_HORIZONTAL); 213 data.horizontalIndent = indent; 214 linkTargetGroup.setLayoutData(data); 215 216 linkTargetField = new Text(linkTargetGroup, SWT.BORDER); 218 data = new GridData(GridData.FILL_HORIZONTAL); 219 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; 220 data.horizontalSpan = 2; 221 linkTargetField.setLayoutData(data); 222 linkTargetField.setEnabled(enabled); 223 linkTargetField.addModifyListener(new ModifyListener() { 224 public void modifyText(ModifyEvent e) { 225 linkTarget = linkTargetField.getText(); 226 resolveVariable(); 227 if (updatableResourceName != null) { 228 String value = updatableResourceName.getValue(); 229 if (value == null 230 || value.equals("") || value.equals(lastUpdatedValue)) { IPath linkTargetPath = new Path(linkTarget); 232 String lastSegment = linkTargetPath.lastSegment(); 233 if (lastSegment != null) { 234 lastUpdatedValue = lastSegment; 235 updatableResourceName.setValue(lastSegment); 236 } 237 } 238 } 239 if (listener != null) { 240 listener.handleEvent(new Event()); 241 } 242 } 243 }); 244 245 browseButton = new Button(linkTargetGroup, SWT.PUSH); 247 browseButton 248 .setText(IDEWorkbenchMessages.CreateLinkedResourceGroup_browseButton); 249 browseButton.addSelectionListener(new SelectionAdapter() { 250 public void widgetSelected(SelectionEvent event) { 251 handleLinkTargetBrowseButtonPressed(); 252 } 253 }); 254 browseButton.setEnabled(enabled); 255 setButtonLayoutData(browseButton); 256 257 variablesButton = new Button(linkTargetGroup, SWT.PUSH); 259 variablesButton 260 .setText(IDEWorkbenchMessages.CreateLinkedResourceGroup_variablesButton); 261 variablesButton.addSelectionListener(new SelectionAdapter() { 262 public void widgetSelected(SelectionEvent event) { 263 handleVariablesButtonPressed(); 264 } 265 }); 266 variablesButton.setEnabled(enabled); 267 setButtonLayoutData(variablesButton); 268 269 createFileSystemSelection(linkTargetGroup, enabled); 270 271 createResolvedPathGroup(locationGroup, indent); 272 273 if (linkTarget != null) { 274 linkTargetField.setText(linkTarget); 275 } 276 } 277 278 285 private void createFileSystemSelection(Composite composite, boolean enabled) { 286 287 if (FileSystemSupportRegistry.getInstance().hasOneFileSystem()) { 289 return; 290 } 291 292 fileSystemSelectionArea = new FileSystemSelectionArea(); 293 fileSystemSelectionArea.createContents(composite); 294 fileSystemSelectionArea.setEnabled(enabled); 295 } 296 297 303 private void createResolvedPathGroup(Composite locationGroup, int indent) { 304 GridLayout layout; 305 GridData data; 306 Composite resolvedPathGroup = new Composite(locationGroup, SWT.NONE); 307 layout = new GridLayout(); 308 layout.numColumns = 2; 309 layout.marginHeight = 0; 310 layout.marginWidth = 0; 311 resolvedPathGroup.setLayout(layout); 312 data = new GridData(GridData.FILL_HORIZONTAL); 313 data.horizontalIndent = indent; 314 resolvedPathGroup.setLayoutData(data); 315 316 resolvedPathLabelText = new Label(resolvedPathGroup, SWT.SINGLE); 317 resolvedPathLabelText 318 .setText(IDEWorkbenchMessages.CreateLinkedResourceGroup_resolvedPathLabel); 319 resolvedPathLabelText.setVisible(false); 320 321 resolvedPathLabelData = new Label(resolvedPathGroup, SWT.SINGLE); 322 data = new GridData(GridData.FILL_HORIZONTAL); 323 resolvedPathLabelData.setLayoutData(data); 324 resolvedPathLabelData.setVisible(false); 325 } 326 327 332 private IStatus createStatus(int severity, String message) { 333 return new Status(severity, IDEWorkbenchPlugin.getDefault().getBundle() 334 .getSymbolicName(), severity, message, null); 335 } 336 337 340 public void dispose() { 341 if (groupComposite != null && groupComposite.isDisposed() == false) { 342 groupComposite.dispose(); 343 } 344 } 345 346 352 public URI getLinkTargetURI() { 353 if (!createLink) 354 return null; 355 if (!linkTarget.startsWith("/")) { IPathVariableManager pathVariableManager = ResourcesPlugin 358 .getWorkspace().getPathVariableManager(); 359 try { 360 361 URI path = new URI (linkTarget.replace(java.io.File.separatorChar, '/')); 362 URI resolved = pathVariableManager.resolveURI(path); 363 if (path != resolved) { 364 return path; 367 } 368 } catch (URISyntaxException e) { 369 } 372 } 373 374 FileSystemConfiguration configuration = getSelectedConfiguration(); 375 if (configuration == null) { 376 return URIUtil.toURI(linkTarget); 377 } 378 return configuration.getContributor().getURI(linkTarget); 380 } 381 382 385 private void handleLinkTargetBrowseButtonPressed() { 386 IFileStore store = null; 387 String selection = null; 388 FileSystemConfiguration config = getSelectedConfiguration(); 389 boolean isDefault = config == null 390 || (FileSystemSupportRegistry.getInstance() 391 .getDefaultConfiguration()).equals(config); 392 393 if (linkTarget.length() > 0) { 394 store = IDEResourceInfoUtils.getFileStore(linkTarget); 395 if (!store.fetchInfo().exists()) { 396 store = null; 397 } 398 } 399 if (type == IResource.FILE) { 400 if (isDefault) { 401 FileDialog dialog = new FileDialog(linkTargetField.getShell()); 402 if (store != null) { 403 if (store.fetchInfo().isDirectory()) { 404 dialog.setFilterPath(linkTarget); 405 } else { 406 dialog.setFileName(linkTarget); 407 } 408 } 409 selection = dialog.open(); 410 } else { 411 URI uri = config.getContributor().browseFileSystem(linkTarget, 412 linkTargetField.getShell()); 413 if (uri != null) 414 selection = uri.toString(); 415 } 416 } else { 417 String filterPath = null; 418 if (store != null) { 419 IFileStore path = store; 420 if (!store.fetchInfo().isDirectory()) { 421 path = store.getParent(); 422 } 423 if (path != null) { 424 filterPath = store.toString(); 425 } 426 } 427 428 if (isDefault) { 429 DirectoryDialog dialog = new DirectoryDialog(linkTargetField 430 .getShell()); 431 dialog 432 .setMessage(IDEWorkbenchMessages.CreateLinkedResourceGroup_targetSelectionLabel); 433 if (filterPath != null) 434 dialog.setFilterPath(filterPath); 435 selection = dialog.open(); 436 } else { 437 String initialPath = IDEResourceInfoUtils.EMPTY_STRING; 438 if (filterPath != null) 439 initialPath = filterPath; 440 URI uri = config.getContributor().browseFileSystem(initialPath, 441 linkTargetField.getShell()); 442 if (uri != null) 443 selection = uri.toString(); 444 } 445 } 446 if (selection != null) { 447 linkTargetField.setText(selection); 448 } 449 } 450 451 457 private FileSystemConfiguration getSelectedConfiguration() { 458 if (fileSystemSelectionArea == null) 459 return null; 460 return fileSystemSelectionArea.getSelectedConfiguration(); 461 } 462 463 466 private void handleVariablesButtonPressed() { 467 int variableTypes = IResource.FOLDER; 468 469 if (type == IResource.FILE) { 472 variableTypes |= IResource.FILE; 473 } 474 475 PathVariableSelectionDialog dialog = new PathVariableSelectionDialog( 476 linkTargetField.getShell(), variableTypes); 477 if (dialog.open() == IDialogConstants.OK_ID) { 478 String [] variableNames = (String []) dialog.getResult(); 479 if (variableNames != null && variableNames.length == 1) { 480 linkTargetField.setText(variableNames[0]); 481 } 482 } 483 } 484 485 496 protected void initializeDialogUnits(Control control) { 497 GC gc = new GC(control); 499 gc.setFont(control.getFont()); 500 fontMetrics = gc.getFontMetrics(); 501 gc.dispose(); 502 } 503 504 509 private void resolveVariable() { 510 IPathVariableManager pathVariableManager = ResourcesPlugin 511 .getWorkspace().getPathVariableManager(); 512 IPath path = new Path(linkTarget); 513 IPath resolvedPath = pathVariableManager.resolvePath(path); 514 515 if (path.equals(resolvedPath)) { 516 resolvedPathLabelText.setVisible(false); 517 resolvedPathLabelData.setVisible(false); 518 } else { 519 resolvedPathLabelText.setVisible(true); 520 resolvedPathLabelData.setVisible(true); 521 } 522 resolvedPathLabelData.setText(resolvedPath.toOSString()); 523 } 524 525 535 private GridData setButtonLayoutData(Button button) { 536 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 537 int widthHint = Dialog.convertHorizontalDLUsToPixels(fontMetrics, 538 IDialogConstants.BUTTON_WIDTH); 539 data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT, 540 SWT.DEFAULT, true).x); 541 button.setLayoutData(data); 542 return data; 543 } 544 545 551 public void setLinkTarget(String target) { 552 linkTarget = target; 553 if (linkTargetField != null && linkTargetField.isDisposed() == false) { 554 linkTargetField.setText(target); 555 } 556 } 557 558 567 private IStatus validateFileType(IFileInfo linkTargetFile) { 568 if (type == IResource.FILE && linkTargetFile.isDirectory()) { 569 return createStatus( 570 IStatus.ERROR, 571 IDEWorkbenchMessages.CreateLinkedResourceGroup_linkTargetNotFile); 572 } else if (type == IResource.FOLDER && !linkTargetFile.isDirectory()) { 573 return createStatus( 574 IStatus.ERROR, 575 IDEWorkbenchMessages.CreateLinkedResourceGroup_linkTargetNotFolder); 576 } 577 return Status.OK_STATUS; 578 } 579 580 589 public IStatus validateLinkLocation(IResource linkHandle) { 590 if (linkTargetField == null || linkTargetField.isDisposed() 591 || !createLink) { 592 return Status.OK_STATUS; 593 } 594 IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace(); 595 FileSystemConfiguration configuration = getSelectedConfiguration(); 596 if (configuration == null 597 || EFS.SCHEME_FILE.equals(configuration.getScheme())) { 598 IPath location = new Path(linkTarget); 600 if (location.isUNC()) { 601 return createStatus( 602 IStatus.WARNING, 603 IDEWorkbenchMessages.CreateLinkedResourceGroup_unableToValidateLinkTarget); 604 } 605 } 606 URI locationURI = getLinkTargetURI(); 607 IStatus locationStatus = workspace.validateLinkLocationURI(linkHandle, 608 locationURI); 609 if (locationStatus.getSeverity() == IStatus.ERROR) { 610 return locationStatus; 611 } 612 613 URI resolved = workspace.getPathVariableManager().resolveURI( 615 locationURI); 616 IFileInfo linkTargetFile = IDEResourceInfoUtils.getFileInfo(resolved); 617 if (linkTargetFile != null && linkTargetFile.exists()) { 618 IStatus fileTypeStatus = validateFileType(linkTargetFile); 619 if (!fileTypeStatus.isOK()) { 620 return fileTypeStatus; 621 } 622 } else if (locationStatus.isOK()) { 623 return createStatus( 625 IStatus.WARNING, 626 IDEWorkbenchMessages.CreateLinkedResourceGroup_linkTargetNonExistent); 627 } 628 return locationStatus; 629 } 630 } 631 | Popular Tags |