KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > markers > internal > DialogMarkerProperties


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Sebastian Davids <sdavids@gmx.de> - bug 77332 - [Markers] Add task dialog improvements
11  *******************************************************************************/

12
13 package org.eclipse.ui.views.markers.internal;
14
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
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 /**
48  * Shows the properties of a new or existing marker
49  *
50  * In 3.3, this class was refactored to allow pre-existing public dialog classes
51  * to share the implementation. Note that certain methods are exposed as API
52  * in public subclasses, so changes to the methods in this class should be
53  * treated carefully as they may affect API methods in subclasses. The specific
54  * methods affected are documented in the method comment.
55  */

56 public class DialogMarkerProperties extends TrayDialog {
57
58     private static final String JavaDoc DIALOG_SETTINGS_SECTION = "DialogMarkerPropertiesDialogSettings"; //$NON-NLS-1$
59

60     /**
61      * The marker being shown, or <code>null</code> for a new marker
62      */

63     private IMarker marker = null;
64
65     /**
66      * The resource on which to create a new marker
67      */

68     private IResource resource = null;
69
70     /**
71      * The type of marker to be created
72      */

73     private String JavaDoc type = IMarker.MARKER;
74
75     /**
76      * The initial attributes to use when creating a new marker
77      */

78     private Map JavaDoc initialAttributes = null;
79
80     /**
81      * The text control for the Description field.
82      */

83     private Text descriptionText;
84
85     /**
86      * The control for the Creation Time field.
87      */

88     private Label creationTime;
89
90     /**
91      * The text control for the Resource field.
92      */

93     private Text resourceText;
94
95     /**
96      * The text control for the Folder field.
97      */

98     private Text folderText;
99
100     /**
101      * The text control for the Location field.
102      */

103     private Text locationText;
104
105     /**
106      * Dirty flag. True if any changes have been made.
107      */

108     private boolean dirty;
109
110     private String JavaDoc title;
111     
112     /**
113      * The name used to describe the specific kind of marker. Used when
114      * creating an undo command for the dialog, so that a specific name such
115      * as "Undo Create Task" or "Undo Modify Bookmark" can be used.
116      */

117     private String JavaDoc markerName;
118
119     /**
120      * Creates the dialog. By default this dialog creates a new marker. To set
121      * the resource and initial attributes for the new marker, use
122      * <code>setResource</code> and <code>setInitialAttributes</code>. To
123      * show or modify an existing marker, use <code>setMarker</code>.
124      *
125      * @param parentShell
126      * the parent shell
127      */

128     public DialogMarkerProperties(Shell parentShell) {
129         super(parentShell);
130         setShellStyle(getShellStyle() | SWT.RESIZE);
131     }
132
133     /**
134      * Creates the dialog. By default this dialog creates a new marker. To set
135      * the resource and initial attributes for the new marker, use
136      * <code>setResource</code> and <code>setInitialAttributes</code>. To
137      * show or modify an existing marker, use <code>setMarker</code>.
138      *
139      * @param parentShell
140      * the parent shell
141      * @param title
142      * the title of the dialog
143      */

144     public DialogMarkerProperties(Shell parentShell, String JavaDoc title) {
145         super(parentShell);
146         setShellStyle(getShellStyle() | SWT.RESIZE);
147         this.title = title;
148     }
149     
150     /**
151      * Creates the dialog. By default this dialog creates a new marker. To set
152      * the resource and initial attributes for the new marker, use
153      * <code>setResource</code> and <code>setInitialAttributes</code>. To
154      * show or modify an existing marker, use <code>setMarker</code>.
155      *
156      * @param parentShell
157      * the parent shell
158      * @param title
159      * the title of the dialog
160      * @param markerName
161      * the name used to describe the specific kind of marker shown
162      *
163      * @since 3.3
164      */

165     public DialogMarkerProperties(Shell parentShell, String JavaDoc title, String JavaDoc markerName) {
166         super(parentShell);
167         setShellStyle(getShellStyle() | SWT.RESIZE);
168         this.title = title;
169         this.markerName = markerName;
170     }
171
172     /**
173      * Sets the marker to show or modify.
174      * <p>IMPORTANT: Although this class is internal, there are public
175      * subclasses that expose this method as API. Changes in
176      * this implementation should be treated as API changes.
177      *
178      * @param marker the marker, or <code>null</code> to create a new marker
179      *
180      * @since 3.3
181      */

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     /**
193      * Returns the marker being created or modified.
194      * For a new marker, this returns <code>null</code> until
195      * the dialog returns, but is non-null after.
196      * <p>IMPORTANT: Although this method is protected and the class is
197      * internal, there are public subclasses that expose this method as API.
198      * Changes in this implementation should be treated as API changes.
199      *
200      * @return the marker
201      *
202      * @since 3.3
203      */

204     protected IMarker getMarker() {
205         return marker;
206     }
207
208     /**
209      * Sets the resource to use when creating a new task.
210      * If not set, the new task is created on the workspace root.
211      * <p>IMPORTANT: Although this class is internal, there are public
212      * subclasses that expose this method as API. Changes in
213      * this implementation should be treated as API changes.
214      *
215      * @param resource the resource
216      */

217     public void setResource(IResource resource) {
218         this.resource = resource;
219     }
220
221     /**
222      * Returns the resource to use when creating a new task,
223      * or <code>null</code> if none has been set.
224      * If not set, the new task is created on the workspace root.
225      * <p>IMPORTANT: Although this method is protected and the class is
226      * internal, there are public subclasses that expose this method as API.
227      * Changes in this implementation should be treated as API changes.
228      *
229      * @return the resource
230      *
231      * @since 3.3
232      */

233     protected IResource getResource() {
234         return resource;
235     }
236
237     /**
238      * Sets initial attributes to use when creating a new task.
239      * If not set, the new task is created with default attributes.
240      * <p>IMPORTANT: Although this method is protected and the class is
241      * internal, there are public subclasses that expose this method as API.
242      * Changes in this implementation should be treated as API changes.
243      *
244      * @param initialAttributes the initial attributes
245      *
246      * @since 3.3
247      */

248     protected void setInitialAttributes(Map JavaDoc initialAttributes) {
249         this.initialAttributes = initialAttributes;
250     }
251
252     /**
253      * Returns the initial attributes to use when creating a new task,
254      * or <code>null</code> if not set.
255      * If not set, the new task is created with default attributes.
256      * <p>IMPORTANT: Although this method is protected and the class is
257      * internal, there are public subclasses that expose this method as API.
258      * Changes in this implementation should be treated as API changes.
259      *
260      * @return the initial attributes
261      *
262      * @since 3.3
263      */

264     protected Map JavaDoc getInitialAttributes() {
265         if (initialAttributes == null) {
266             initialAttributes = new HashMap JavaDoc();
267         }
268         return initialAttributes;
269     }
270
271     /**
272      * Method declared on Window.
273      */

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     /**
284      * Method declared on Dialog.
285      */

286     protected Control createDialogArea(Composite parent) {
287         // initialize resources/properties
288
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     /**
327      * Creates a seperator.
328      */

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     /**
337      * Method createCreationTimeArea.
338      * @param parent
339      */

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     /**
349      * Creates the OK and Cancel buttons.
350      */

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     /**
359      * Creates the area for the Description field.
360      */

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     /**
377      * This method is intended to be overridden by subclasses. The attributes
378      * area is created between the creation time area and the resource area.
379      *
380      * @param parent
381      * the parent composite
382      */

383     protected void createAttributesArea(Composite parent) {
384     }
385
386     /**
387      * Creates the area for the Resource field.
388      */

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     /**
413      * Updates the dialog from the marker state.
414      */

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 JavaDoc line = Util.getProperty(IMarker.LINE_NUMBER, marker);
432             if (line.equals("")) { //$NON-NLS-1$
433
locationText.setText(""); //$NON-NLS-1$
434
} else {
435                 locationText.setText(NLS.bind(MarkerMessages.label_lineNumber, line));
436             }
437         }
438
439         descriptionText.selectAll();
440     }
441     
442     /**
443      * Updates the dialog from the predefined attributes.
444      */

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; // n is the number of segments in container, not path
451
if (n > 0) {
452                 int len = 0;
453                 for (int i = 0; i < n; ++i) {
454                     len += path.segment(i).length();
455                 }
456                 // account for /'s
457
if (n > 1) {
458                     len += n - 1;
459                 }
460                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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 JavaDoc description = initialAttributes.get(IMarker.MESSAGE);
473             if (description != null && description instanceof String JavaDoc) {
474                 descriptionText.setText((String JavaDoc) description);
475             }
476             descriptionText.selectAll();
477
478             Object JavaDoc line = initialAttributes.get(IMarker.LINE_NUMBER);
479             if (line != null && line instanceof Integer JavaDoc && locationText != null) {
480                 locationText.setText(
481                     NLS.bind(MarkerMessages.label_lineNumber, line));
482             }
483         }
484     }
485     
486     /**
487      * Method declared on Dialog
488      */

489     protected void okPressed() {
490         if (marker == null || Util.isEditable(marker)) {
491             saveChanges();
492         }
493         super.okPressed();
494     }
495
496     /**
497      * Sets the dialog's dirty flag to <code>true</code>
498      */

499     protected void markDirty() {
500         dirty = true;
501     }
502
503     /**
504      * @return
505      * <ul>
506      * <li><code>true</code> if the dirty flag has been set to true.</li>
507      * <li><code>false</code> otherwise.</li>
508      * </ul>
509      */

510     protected boolean isDirty() {
511         return dirty;
512     }
513
514     /**
515      * Saves the changes made in the dialog if needed. Creates a new marker if
516      * needed. Updates the existing marker only if there have been changes.
517      */

518     private void saveChanges() {
519         Map JavaDoc 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     /**
550      * Returns the marker attributes to save back to the marker, based on the
551      * current dialog fields.
552      */

553     protected Map JavaDoc getMarkerAttributes() {
554         Map JavaDoc attrs = getInitialAttributes();
555         attrs.put(IMarker.MESSAGE, descriptionText.getText());
556         return attrs;
557     }
558     
559     /**
560      * Updates widget enablement for the dialog. Should be overridden by
561      * subclasses.
562      */

563     protected void updateEnablement() {
564         descriptionText.setEditable(isEditable());
565     }
566
567     /**
568      * @return
569      * <ul>
570      * <li><code>true</code> if the marker is editable or the dialog is
571      * creating a new marker.</li>
572      * <li><code>false</code> if the marker is not editable.</li>
573      * </ul>
574      */

575     protected boolean isEditable() {
576         if (marker == null) {
577             return true;
578         }
579         return Util.isEditable(marker);
580     }
581
582     /**
583      * Sets the marker type when creating a new marker.
584      *
585      * @param type
586      * the marker type
587      *
588      * @since 3.3 this method is protected.
589      */

590     protected void setType(String JavaDoc type) {
591         this.type = type;
592     }
593     
594     /* (non-Javadoc)
595      * @see org.eclipse.jface.window.Dialog#getDialogBoundsSettings()
596      *
597      * @since 3.2
598      */

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     /**
609      * Return the string that describes a modify marker operation.
610      * Subclasses may override to more specifically describe the marker.
611      *
612      * @since 3.3
613      */

614     protected String JavaDoc getModifyOperationTitle() {
615         if (markerName == null) {
616             // we don't know what kind of marker is being modified
617
return MarkerMessages.DialogMarkerProperties_ModifyMarker;
618         }
619         return NLS.bind(MarkerMessages.qualifiedMarkerCommand_title,
620                 MarkerMessages.DialogMarkerProperties_Modify, markerName);
621     }
622     
623     /**
624      * Return the string that describes a create marker operation.
625      * Subclasses may override to more specifically describe the marker.
626      *
627      * @since 3.3
628      */

629     protected String JavaDoc getCreateOperationTitle() {
630         if (markerName == null) {
631             // we don't know what kind of marker is being created
632
return MarkerMessages.DialogMarkerProperties_CreateMarker;
633         }
634         return NLS.bind(MarkerMessages.qualifiedMarkerCommand_title,
635                 MarkerMessages.DialogMarkerProperties_Create, markerName);
636         
637     }
638 }
639
Popular Tags