1 12 13 package org.eclipse.ui.views.markers.internal; 14 15 import java.util.HashMap ; 16 import java.util.Map ; 17 18 import org.eclipse.core.commands.ExecutionException; 19 import org.eclipse.core.commands.operations.IUndoableOperation; 20 import org.eclipse.core.resources.IMarker; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.resources.ResourcesPlugin; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.jface.dialogs.Dialog; 26 import org.eclipse.jface.dialogs.ErrorDialog; 27 import org.eclipse.jface.dialogs.IDialogConstants; 28 import org.eclipse.jface.dialogs.IDialogSettings; 29 import org.eclipse.jface.dialogs.TrayDialog; 30 import org.eclipse.osgi.util.NLS; 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.events.ModifyEvent; 33 import org.eclipse.swt.events.ModifyListener; 34 import org.eclipse.swt.layout.GridData; 35 import org.eclipse.swt.layout.GridLayout; 36 import org.eclipse.swt.widgets.Composite; 37 import org.eclipse.swt.widgets.Control; 38 import org.eclipse.swt.widgets.Label; 39 import org.eclipse.swt.widgets.Shell; 40 import org.eclipse.swt.widgets.Text; 41 import org.eclipse.ui.PlatformUI; 42 import org.eclipse.ui.ide.undo.CreateMarkersOperation; 43 import org.eclipse.ui.ide.undo.UpdateMarkersOperation; 44 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil; 45 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 46 47 56 public class DialogMarkerProperties extends TrayDialog { 57 58 private static final String DIALOG_SETTINGS_SECTION = "DialogMarkerPropertiesDialogSettings"; 60 63 private IMarker marker = null; 64 65 68 private IResource resource = null; 69 70 73 private String type = IMarker.MARKER; 74 75 78 private Map initialAttributes = null; 79 80 83 private Text descriptionText; 84 85 88 private Label creationTime; 89 90 93 private Text resourceText; 94 95 98 private Text folderText; 99 100 103 private Text locationText; 104 105 108 private boolean dirty; 109 110 private String title; 111 112 117 private String markerName; 118 119 128 public DialogMarkerProperties(Shell parentShell) { 129 super(parentShell); 130 setShellStyle(getShellStyle() | SWT.RESIZE); 131 } 132 133 144 public DialogMarkerProperties(Shell parentShell, String title) { 145 super(parentShell); 146 setShellStyle(getShellStyle() | SWT.RESIZE); 147 this.title = title; 148 } 149 150 165 public DialogMarkerProperties(Shell parentShell, String title, String markerName) { 166 super(parentShell); 167 setShellStyle(getShellStyle() | SWT.RESIZE); 168 this.title = title; 169 this.markerName = markerName; 170 } 171 172 182 public void setMarker(IMarker marker) { 183 this.marker = marker; 184 if (marker != null) { 185 try { 186 type = marker.getType(); 187 } catch (CoreException e) { 188 } 189 } 190 } 191 192 204 protected IMarker getMarker() { 205 return marker; 206 } 207 208 217 public void setResource(IResource resource) { 218 this.resource = resource; 219 } 220 221 233 protected IResource getResource() { 234 return resource; 235 } 236 237 248 protected void setInitialAttributes(Map initialAttributes) { 249 this.initialAttributes = initialAttributes; 250 } 251 252 264 protected Map getInitialAttributes() { 265 if (initialAttributes == null) { 266 initialAttributes = new HashMap (); 267 } 268 return initialAttributes; 269 } 270 271 274 protected void configureShell(Shell newShell) { 275 super.configureShell(newShell); 276 if (title == null) { 277 newShell.setText(MarkerMessages.propertiesDialog_title); 278 } else { 279 newShell.setText(title); 280 } 281 } 282 283 286 protected Control createDialogArea(Composite parent) { 287 if (marker != null) { 289 resource = marker.getResource(); 290 try { 291 initialAttributes = marker.getAttributes(); 292 } catch (CoreException e) { 293 } 294 } else if (resource == null) { 295 resource = ResourcesPlugin.getWorkspace().getRoot(); 296 } 297 298 Composite comp = (Composite) super.createDialogArea(parent); 299 Composite composite = new Composite(comp, SWT.NULL); 300 GridLayout layout = new GridLayout(2, false); 301 layout.marginWidth = 0; 302 layout.marginHeight = 0; 303 composite.setLayout(layout); 304 GridData gridData = new GridData(GridData.FILL_HORIZONTAL); 305 composite.setLayoutData(gridData); 306 307 initializeDialogUnits(composite); 308 createDescriptionArea(composite); 309 if (marker != null) { 310 createSeperator(composite); 311 createCreationTimeArea(composite); 312 } 313 createAttributesArea(composite); 314 if (resource != null) { 315 createSeperator(composite); 316 createResourceArea(composite); 317 } 318 updateDialogFromMarker(); 319 updateEnablement(); 320 321 Dialog.applyDialogFont(composite); 322 323 return composite; 324 } 325 326 329 protected void createSeperator(Composite parent) { 330 Label seperator = new Label(parent, SWT.NULL); 331 GridData gridData = new GridData(GridData.FILL_HORIZONTAL); 332 gridData.horizontalSpan = 2; 333 seperator.setLayoutData(gridData); 334 } 335 336 340 private void createCreationTimeArea(Composite parent) { 341 Label label = new Label(parent, SWT.NONE); 342 label.setText(MarkerMessages 343 .propertiesDialog_creationTime_text); 344 345 creationTime = new Label(parent, SWT.NONE); 346 } 347 348 351 protected void createButtonsForButtonBar(Composite parent) { 352 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, 353 true); 354 createButton(parent, IDialogConstants.CANCEL_ID, 355 IDialogConstants.CANCEL_LABEL, false); 356 } 357 358 361 private void createDescriptionArea(Composite parent) { 362 Label label = new Label(parent, SWT.NONE); 363 label.setText(MarkerMessages.propertiesDialog_description_text); 364 descriptionText = new Text(parent, (SWT.SINGLE | SWT.BORDER)); 365 GridData gridData = new GridData(GridData.FILL_HORIZONTAL); 366 gridData.widthHint = convertHorizontalDLUsToPixels(400); 367 descriptionText.setLayoutData(gridData); 368 369 descriptionText.addModifyListener(new ModifyListener() { 370 public void modifyText(ModifyEvent e) { 371 markDirty(); 372 } 373 }); 374 } 375 376 383 protected void createAttributesArea(Composite parent) { 384 } 385 386 389 private void createResourceArea(Composite parent) { 390 Label resourceLabel = new Label(parent, SWT.NONE); 391 resourceLabel.setText(MarkerMessages.propertiesDialog_resource_text); 392 resourceText = new Text(parent, SWT.SINGLE | SWT.WRAP 393 | SWT.READ_ONLY | SWT.BORDER); 394 GridData gridData = new GridData(GridData.FILL_HORIZONTAL); 395 resourceText.setLayoutData(gridData); 396 397 Label folderLabel = new Label(parent, SWT.NONE); 398 folderLabel.setText(MarkerMessages.propertiesDialog_folder_text); 399 folderText = new Text(parent, SWT.SINGLE | SWT.WRAP | SWT.READ_ONLY 400 | SWT.BORDER); 401 gridData = new GridData(GridData.FILL_HORIZONTAL); 402 folderText.setLayoutData(gridData); 403 404 Label locationLabel = new Label(parent, SWT.NONE); 405 locationLabel.setText(MarkerMessages.propertiesDialog_location_text); 406 locationText = new Text(parent, SWT.SINGLE | SWT.WRAP 407 | SWT.READ_ONLY | SWT.BORDER); 408 gridData = new GridData(GridData.FILL_HORIZONTAL); 409 locationText.setLayoutData(gridData); 410 } 411 412 415 protected void updateDialogFromMarker() { 416 if (marker == null) { 417 updateDialogForNewMarker(); 418 return; 419 } 420 descriptionText.setText(Util.getProperty(IMarker.MESSAGE, marker)); 421 if (creationTime != null) { 422 creationTime.setText(Util.getCreationTime(marker)); 423 } 424 if (resourceText != null) { 425 resourceText.setText(Util.getResourceName(marker)); 426 } 427 if (folderText != null) { 428 folderText.setText(Util.getContainerName(marker)); 429 } 430 if (locationText != null) { 431 String line = Util.getProperty(IMarker.LINE_NUMBER, marker); 432 if (line.equals("")) { locationText.setText(""); } else { 435 locationText.setText(NLS.bind(MarkerMessages.label_lineNumber, line)); 436 } 437 } 438 439 descriptionText.selectAll(); 440 } 441 442 445 protected void updateDialogForNewMarker() { 446 if (resource != null && resourceText != null && folderText != null) { 447 resourceText.setText(resource.getName()); 448 449 IPath path = resource.getFullPath(); 450 int n = path.segmentCount() - 1; if (n > 0) { 452 int len = 0; 453 for (int i = 0; i < n; ++i) { 454 len += path.segment(i).length(); 455 } 456 if (n > 1) { 458 len += n - 1; 459 } 460 StringBuffer sb = new StringBuffer (len); 461 for (int i = 0; i < n; ++i) { 462 if (i != 0) { 463 sb.append('/'); 464 } 465 sb.append(path.segment(i)); 466 } 467 folderText.setText(sb.toString()); 468 } 469 } 470 471 if (initialAttributes != null) { 472 Object description = initialAttributes.get(IMarker.MESSAGE); 473 if (description != null && description instanceof String ) { 474 descriptionText.setText((String ) description); 475 } 476 descriptionText.selectAll(); 477 478 Object line = initialAttributes.get(IMarker.LINE_NUMBER); 479 if (line != null && line instanceof Integer && locationText != null) { 480 locationText.setText( 481 NLS.bind(MarkerMessages.label_lineNumber, line)); 482 } 483 } 484 } 485 486 489 protected void okPressed() { 490 if (marker == null || Util.isEditable(marker)) { 491 saveChanges(); 492 } 493 super.okPressed(); 494 } 495 496 499 protected void markDirty() { 500 dirty = true; 501 } 502 503 510 protected boolean isDirty() { 511 return dirty; 512 } 513 514 518 private void saveChanges() { 519 Map attrs = getMarkerAttributes(); 520 IUndoableOperation op = null; 521 if (marker == null) { 522 if (resource == null) 523 return; 524 op = new CreateMarkersOperation(type, attrs, 525 resource, getCreateOperationTitle()); 526 } else { 527 if (isDirty()) { 528 op = new UpdateMarkersOperation(marker, attrs, 529 getModifyOperationTitle(), true); 530 } 531 } 532 if (op != null) { 533 try { 534 PlatformUI.getWorkbench() 535 .getOperationSupport() 536 .getOperationHistory().execute(op, 537 null, WorkspaceUndoUtil.getUIInfoAdapter(getShell())); 538 } catch (ExecutionException e) { 539 if (e.getCause() instanceof CoreException) { 540 ErrorDialog.openError( 541 getShell(), 542 MarkerMessages.Error, null, ((CoreException)e.getCause()).getStatus()); 543 } else 544 IDEWorkbenchPlugin.log(e.getMessage(), e); 545 } 546 } 547 } 548 549 553 protected Map getMarkerAttributes() { 554 Map attrs = getInitialAttributes(); 555 attrs.put(IMarker.MESSAGE, descriptionText.getText()); 556 return attrs; 557 } 558 559 563 protected void updateEnablement() { 564 descriptionText.setEditable(isEditable()); 565 } 566 567 575 protected boolean isEditable() { 576 if (marker == null) { 577 return true; 578 } 579 return Util.isEditable(marker); 580 } 581 582 590 protected void setType(String type) { 591 this.type = type; 592 } 593 594 599 protected IDialogSettings getDialogBoundsSettings() { 600 IDialogSettings settings = IDEWorkbenchPlugin.getDefault().getDialogSettings(); 601 IDialogSettings section = settings.getSection(DIALOG_SETTINGS_SECTION); 602 if (section == null) { 603 section = settings.addNewSection(DIALOG_SETTINGS_SECTION); 604 } 605 return section; 606 } 607 608 614 protected String getModifyOperationTitle() { 615 if (markerName == null) { 616 return MarkerMessages.DialogMarkerProperties_ModifyMarker; 618 } 619 return NLS.bind(MarkerMessages.qualifiedMarkerCommand_title, 620 MarkerMessages.DialogMarkerProperties_Modify, markerName); 621 } 622 623 629 protected String getCreateOperationTitle() { 630 if (markerName == null) { 631 return MarkerMessages.DialogMarkerProperties_CreateMarker; 633 } 634 return NLS.bind(MarkerMessages.qualifiedMarkerCommand_title, 635 MarkerMessages.DialogMarkerProperties_Create, markerName); 636 637 } 638 } 639 | Popular Tags |