KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > tasklist > MarkerUtil


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  *******************************************************************************/

11
12 package org.eclipse.ui.views.tasklist;
13
14 import com.ibm.icu.text.DateFormat;
15 import com.ibm.icu.text.MessageFormat;
16 import java.util.Date JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.resources.IMarker;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.jface.resource.ImageRegistry;
25 import org.eclipse.jface.viewers.IBasicPropertyConstants;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
28 import org.eclipse.ui.internal.views.tasklist.TaskListMessages;
29
30 /**
31  * Utility class for accessing marker attributes.
32  */

33 class MarkerUtil implements IMarkerConstants {
34     
35    private static Map JavaDoc imageDescriptors;
36
37     private static ImageRegistry imageRegistry = new ImageRegistry();
38
39     private static MessageFormat line = new MessageFormat(TaskListMessages.TaskList_line);
40
41     private static MessageFormat lineAndLocation = new MessageFormat(
42             TaskListMessages.TaskList_lineAndLocation);
43
44     static {
45         createImageDescriptors();
46     }
47
48     /**
49      * Don't allow instantiation.
50      */

51     private MarkerUtil() {
52     }
53
54     /**
55      * Creates the map of image descriptors.
56      */

57     static void createImageDescriptors() {
58     
59
60         imageDescriptors = new HashMap JavaDoc(51);
61         imageDescriptors
62                 .put(
63                         "header_complete", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/header_complete.gif"));//$NON-NLS-2$//$NON-NLS-1$
64
imageDescriptors
65                 .put(
66                         "header_priority", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/header_priority.gif"));//$NON-NLS-2$//$NON-NLS-1$
67
imageDescriptors.put(
68                 "task", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/taskmrk_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
69
imageDescriptors.put(
70                 "error", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/error_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
71
imageDescriptors.put(
72                 "warn", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/warn_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
73
imageDescriptors.put(
74                 "info", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/info_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
75
imageDescriptors.put(
76                 "hprio", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/hprio_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
77
imageDescriptors.put(
78                 "lprio", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/lprio_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
79
imageDescriptors
80                 .put(
81                         "complete_tsk", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/complete_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
82
imageDescriptors
83                 .put(
84                         "incomplete_tsk", IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/incomplete_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
85
imageDescriptors.put(
86                 "gotoobj", IDEWorkbenchPlugin.getIDEImageDescriptor("elcl16/gotoobj_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
87
imageDescriptors.put(
88                 "addtsk", IDEWorkbenchPlugin.getIDEImageDescriptor("elcl16/addtsk_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
89
imageDescriptors
90                 .put(
91                         "addtsk_disabled", IDEWorkbenchPlugin.getIDEImageDescriptor("dlcl16/addtsk_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
92
imageDescriptors
93                 .put(
94                         "showcomplete", IDEWorkbenchPlugin.getIDEImageDescriptor("elcl16/showcomplete_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
95
imageDescriptors
96                 .put(
97                         "selected_mode", IDEWorkbenchPlugin.getIDEImageDescriptor("elcl16/selected_mode.gif"));//$NON-NLS-2$//$NON-NLS-1$
98
imageDescriptors
99                 .put(
100                         "selected_mode_disabled", IDEWorkbenchPlugin.getIDEImageDescriptor("dlcl16/selected_mode.gif"));//$NON-NLS-2$//$NON-NLS-1$
101
imageDescriptors
102                 .put(
103                         "showchild_mode", IDEWorkbenchPlugin.getIDEImageDescriptor("elcl16/showchild_mode.gif"));//$NON-NLS-2$//$NON-NLS-1$
104
imageDescriptors
105                 .put(
106                         "showchild_mode_disabled", IDEWorkbenchPlugin.getIDEImageDescriptor("dlcl16/showchild_mode.gif"));//$NON-NLS-2$//$NON-NLS-1$
107
imageDescriptors
108                 .put(
109                         "showerr_disabled", IDEWorkbenchPlugin.getIDEImageDescriptor("dlcl16/showerr_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
110
imageDescriptors
111                 .put(
112                         "showwarn_disabled", IDEWorkbenchPlugin.getIDEImageDescriptor("dlcl16/showwarn_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
113
imageDescriptors
114                 .put(
115                         "showtsk_disabled", IDEWorkbenchPlugin.getIDEImageDescriptor("dlcl16/showtsk_tsk.gif"));//$NON-NLS-2$//$NON-NLS-1$
116
imageDescriptors.put(
117                 "filter", IDEWorkbenchPlugin.getIDEImageDescriptor("elcl16/filter_ps.gif"));//$NON-NLS-2$//$NON-NLS-1$
118
}
119
120     /**
121      * Returns the ending character offset of the given marker.
122      */

123     public static int getCharEnd(IMarker marker) {
124         return marker.getAttribute(IMarker.CHAR_END, -1);
125     }
126
127     /**
128      * Returns the starting character offset of the given marker.
129      */

130     public static int getCharStart(IMarker marker) {
131         return marker.getAttribute(IMarker.CHAR_START, -1);
132     }
133
134     /**
135      * Returns the icon to be used in the tasklist
136      * for the complete state of a task. Returns null
137      * for markers that are not tasks.
138      */

139     public static Image getCompleteImage(IMarker marker) {
140         if (isMarkerType(marker, IMarker.TASK)) {
141             if (isComplete(marker)) {
142                 return getImage("complete_tsk");//$NON-NLS-1$
143
} else if (!isReadOnly(marker)) {
144                 return getImage("incomplete_tsk");//$NON-NLS-1$
145
}
146         }
147         return null;
148     }
149
150     /**
151      * Returns the text to be used for the complete state of a task.
152      * Returns the empty string for markers that are not tasks.
153      */

154     public static String JavaDoc getCompleteText(IMarker marker) {
155         if (isMarkerType(marker, IMarker.TASK)) {
156             if (isComplete(marker)) {
157                 return TaskListMessages.TaskList_completed;
158             } else {
159                 return TaskListMessages.TaskList_notCompleted;
160             }
161         }
162         return ""; //$NON-NLS-1$
163
}
164
165     /**
166      * Returns the text to be used for the kind of marker.
167      */

168     public static String JavaDoc getKindText(IMarker marker) {
169         if (isMarkerType(marker, IMarker.TASK)) {
170             return TaskListMessages.TaskList_task;
171         }
172         switch (getSeverity(marker)) {
173         case IMarker.SEVERITY_ERROR:
174             return TaskListMessages.TaskList_error;
175         case IMarker.SEVERITY_WARNING:
176             return TaskListMessages.TaskList_warning;
177         case IMarker.SEVERITY_INFO:
178             return TaskListMessages.TaskList_info;
179         }
180         return ""; //$NON-NLS-1$
181
}
182
183     /**
184      * Returns the container name if it is defined, or empty string if not.
185      */

186     public static String JavaDoc getContainerName(IMarker marker) {
187         IPath path = marker.getResource().getFullPath();
188         int n = path.segmentCount() - 1; // n is the number of segments in container, not path
189
if (n <= 0) {
190             return ""; //$NON-NLS-1$
191
}
192         int len = 0;
193         for (int i = 0; i < n; ++i) {
194             len += path.segment(i).length();
195         }
196         // account for /'s
197
if (n > 1) {
198             len += n - 1;
199         }
200         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(len);
201         for (int i = 0; i < n; ++i) {
202             if (i != 0) {
203                 sb.append('/');
204             }
205             sb.append(path.segment(i));
206         }
207         return sb.toString();
208     }
209
210     /**
211      * Returns the image with the given key, or <code>null</code> if not found.
212      */

213     static Image getImage(String JavaDoc key) {
214         Image image = imageRegistry.get(key);
215         if (image == null) {
216             ImageDescriptor desc = getImageDescriptor(key);
217             if (desc != null) {
218                 image = desc.createImage(false);
219                 if (image == null) {
220                     System.err
221                             .println("TaskList: Error creating image for " + key);//$NON-NLS-1$
222
}
223                 imageRegistry.put(key, image);
224             }
225         }
226         return image;
227     }
228
229     /**
230      * Returns the image that should be used to visually represent
231      * the marker, based on its type and priority.
232      */

233     static public Image getImage(IMarker marker) {
234         if (isMarkerType(marker, IMarker.PROBLEM)) {
235             switch (getSeverity(marker)) {
236             case IMarker.SEVERITY_ERROR:
237                 return getImage("error");//$NON-NLS-1$
238
case IMarker.SEVERITY_WARNING:
239                 return getImage("warn");//$NON-NLS-1$
240
case IMarker.SEVERITY_INFO:
241                 return getImage("info");//$NON-NLS-1$
242
}
243         } else if (isMarkerType(marker, IMarker.TASK)) {
244             return getImage("task");//$NON-NLS-1$
245
}
246         return null;
247     }
248
249     /**
250      * Returns the image descriptor with the given key, or <code>null</code> if not found.
251      */

252     static ImageDescriptor getImageDescriptor(String JavaDoc key) {
253         ImageDescriptor desc = (ImageDescriptor) imageDescriptors.get(key);
254         if (desc == null) {
255                 IDEWorkbenchPlugin.log("TaskList: No image descriptor for " + key); //$NON-NLS-1$
256
}
257         return desc;
258     }
259
260     /**
261      * Returns the text for the line and location column given the marker.
262      */

263     public static String JavaDoc getLineAndLocation(IMarker marker) {
264         int lineNumber = getLineNumber(marker);
265         String JavaDoc location = getLocation(marker);
266         return getLineAndLocation(lineNumber, location);
267     }
268
269     /**
270      * Returns the text for the line and location column given the line number and location text.
271      */

272     public static String JavaDoc getLineAndLocation(int lineNumber, String JavaDoc location) {
273         if (lineNumber == -1) {
274             if (location.equals("")) {//$NON-NLS-1$
275
return "";//$NON-NLS-1$
276
} else {
277                 return location;
278             }
279         } else {
280             if (location.equals("")) {//$NON-NLS-1$
281
return line
282                         .format(new Object JavaDoc[] { Integer.toString(lineNumber) });
283             } else {
284                 return lineAndLocation.format(new Object JavaDoc[] {
285                         Integer.toString(lineNumber), location });
286             }
287         }
288     }
289
290     /**
291      * Returns the line number of the given marker.
292      */

293     public static int getLineNumber(IMarker marker) {
294         return marker.getAttribute(IMarker.LINE_NUMBER, -1);
295     }
296
297     /**
298      * Returns the text for the location field.
299      */

300     public static String JavaDoc getLocation(IMarker marker) {
301         return marker.getAttribute(IMarker.LOCATION, "");//$NON-NLS-1$
302
}
303
304     /**
305      * Returns the message attribute of the given marker,
306      * or the empty string if the message attribute is not defined.
307      */

308     public static String JavaDoc getMessage(IMarker marker) {
309         return marker.getAttribute(IMarker.MESSAGE, "");//$NON-NLS-1$
310
}
311
312     /**
313      * Returns the numeric value of the given string, which is assumed to represent a numeric value.
314      *
315      * @return <code>true</code> if numeric, <code>false</code> if not
316      */

317     static public int getNumericValue(String JavaDoc value) {
318         boolean negative = false;
319         int i = 0;
320         int len = value.length();
321
322         // skip any leading '#'
323
// workaround for 1GCE69U: ITPJCORE:ALL - Java problems should not have '#' in location.
324
if (i < len && value.charAt(i) == '#') {
325             ++i;
326         }
327
328         if (i < len && value.charAt(i) == '-') {
329             negative = true;
330             ++i;
331         }
332
333         int result = 0;
334         while (i < len) {
335             int digit = Character.digit(value.charAt(i++), 10);
336             if (digit < 0) {
337                 return result;
338             }
339             result = result * 10 + digit;
340         }
341         if (negative) {
342             result = -result;
343         }
344         return result;
345     }
346
347     /**
348      * Returns the priority of the given marker. Default is PRIORITY_NORMAL.
349      */

350     public static int getPriority(IMarker marker) {
351         return marker.getAttribute(IMarker.PRIORITY, IMarker.PRIORITY_NORMAL);
352     }
353
354     /**
355      * Returns an image that should visually represent marker's priority
356      * in the tasklist.
357      */

358     public static Image getPriorityImage(IMarker marker) {
359         switch (getPriority(marker)) {
360         case IMarker.PRIORITY_HIGH:
361             return getImage("hprio");//$NON-NLS-1$
362
case IMarker.PRIORITY_NORMAL:
363             return null; // no image for normal priority
364
case IMarker.PRIORITY_LOW:
365             return getImage("lprio");//$NON-NLS-1$
366
}
367         return null;
368     }
369
370     /**
371      * Returns the text for the given marker's priority.
372      */

373     public static String JavaDoc getPriorityText(IMarker marker) {
374         if (!isMarkerType(marker, IMarker.TASK)) {
375             return ""; //$NON-NLS-1$
376
}
377
378         switch (getPriority(marker)) {
379         case IMarker.PRIORITY_HIGH:
380             return TaskListMessages.TaskList_high;
381         case IMarker.PRIORITY_NORMAL:
382             return TaskListMessages.TaskList_normal;
383         case IMarker.PRIORITY_LOW:
384             return TaskListMessages.TaskList_low;
385         }
386         return ""; //$NON-NLS-1$
387
}
388
389     /**
390      * Implements IProvider interface by supporting a number of
391      * properties required for visual representation of markers
392      * in the tasklist.
393      */

394     static public Object JavaDoc getProperty(Object JavaDoc object, String JavaDoc key) {
395         IMarker marker = (IMarker) object;
396
397         // optimizations:
398
// - check properties needed for TaskListLabelProvider first,
399
// then those needed for cell modifiers
400
// - use == instead of equals for efficiency
401
if (IBasicPropertyConstants.P_IMAGE == key) {
402             return getImage(marker);
403         }
404         if (IMarkerConstants.P_COMPLETE_IMAGE == key) {
405             return getCompleteImage(marker);
406         }
407         if (IMarkerConstants.P_PRIORITY_IMAGE == key) {
408             return getPriorityImage(marker);
409         }
410         if (IMarker.MESSAGE == key) {
411             return getMessage(marker);
412         }
413         if (IMarkerConstants.P_RESOURCE_NAME == key) {
414             return getResourceName(marker);
415         }
416         if (IMarkerConstants.P_CONTAINER_NAME == key) {
417             return getContainerName(marker);
418         }
419         if (IMarkerConstants.P_LINE_AND_LOCATION == key) {
420             return getLineAndLocation(marker);
421         }
422         if (IMarker.PRIORITY == key) {
423             // this property is used only by cell editor, where order is High, Normal, Low
424
return new Integer JavaDoc(IMarker.PRIORITY_HIGH - getPriority(marker));
425         }
426         if (IMarker.DONE == key) {
427             return isComplete(marker) ? Boolean.TRUE : Boolean.FALSE;
428         }
429         if (IBasicPropertyConstants.P_TEXT == key) {
430             return getMessage(marker);
431         }
432         return null;
433     }
434
435     /**
436      * Returns name if it is defined, or
437      * blank string if not.
438      */

439     public static String JavaDoc getResourceName(IMarker marker) {
440         return marker.getResource().getName();
441     }
442
443     /**
444      * Returns the severity of the given marker. Default is SEVERITY_WARNING.
445      */

446     public static int getSeverity(IMarker marker) {
447         return marker.getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
448     }
449
450     /**
451      * A helper method that returns true if the given
452      * marker is marked as complete in the tasklist.
453      * Only tasks can be complete.
454      */

455     public static boolean isComplete(IMarker marker) {
456         return marker.getAttribute(IMarker.DONE, false);
457     }
458
459     /**
460      * Returns whether the given marker is editable.
461      * Only tasks which have not been marked as read-only are editable.
462      */

463     public static boolean isEditable(IMarker marker) {
464         return isMarkerType(marker, IMarker.TASK) && !isReadOnly(marker);
465     }
466
467     /**
468      * Returns whether the given marker is of the given type (either directly or indirectly).
469      */

470     public static boolean isMarkerType(IMarker marker, String JavaDoc type) {
471         try {
472             return marker.isSubtypeOf(type);
473         } catch (CoreException e) {
474             return false;
475         }
476     }
477
478     /**
479      * Returns whether the given marker is read-only.
480      */

481     public static boolean isReadOnly(IMarker marker) {
482         return !marker.getAttribute(IMarker.USER_EDITABLE, true);
483     }
484
485     /**
486      * Returns the creation time of the marker as a string.
487      */

488     public static String JavaDoc getCreationTime(IMarker marker) {
489         try {
490             return DateFormat.getDateTimeInstance(DateFormat.LONG,
491                     DateFormat.MEDIUM).format(
492                     new Date JavaDoc(marker.getCreationTime()));
493         } catch (CoreException e) {
494             return null;
495         }
496     }
497 }
498
Popular Tags