KickJava   Java API By Example, From Geeks To Geeks.

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


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.markers.internal;
13
14 import com.ibm.icu.text.DateFormat;
15 import java.util.Date JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.core.resources.IMarker;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jface.resource.JFaceResources;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages;
28 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
29 import org.eclipse.ui.views.markers.MarkerViewUtil;
30
31 /**
32  * The Util class is the class of general utilities used by the marker support.
33  *
34  */

35 public final class Util {
36
37     static String JavaDoc EMPTY_STRING = "";//$NON-NLS-1$
38

39     static String JavaDoc TWO_LINE_FEED = "\n\n";//$NON-NLS-1$
40

41     static String JavaDoc LINE_FEED_AND_TAB = "\n\t";//$NON-NLS-1$
42

43     private static DateFormat format;
44
45     static final MarkerNode[] EMPTY_MARKER_ARRAY = new MarkerNode[0];
46
47     /**
48      * Get the propery called property from the marker. If it is not found
49      * return the empty string.
50      *
51      * @param property
52      * @param marker
53      * @return String
54      */

55     public static String JavaDoc getProperty(String JavaDoc property, IMarker marker) {
56         if (marker == null || !marker.exists()) {
57             return EMPTY_STRING;
58         }
59         try {
60             Object JavaDoc obj = marker.getAttribute(property);
61             if (obj != null) {
62                 return obj.toString();
63             }
64             return EMPTY_STRING;
65         } catch (CoreException e) {
66             log(e);
67             return EMPTY_STRING;
68         }
69     }
70
71     /**
72      * Get the human readable creation time from the timestamp
73      *
74      * @param timestamp
75      * @return String
76      */

77     public static String JavaDoc getCreationTime(long timestamp) {
78         if (format == null) {
79             format = DateFormat.getDateTimeInstance(DateFormat.LONG,
80                     DateFormat.MEDIUM);
81         }
82         return format.format(new Date JavaDoc(timestamp));
83     }
84
85     /**
86      * Get the human readable creation time from the marker.
87      *
88      * @param marker
89      * @return String
90      */

91     public static String JavaDoc getCreationTime(IMarker marker) {
92         try {
93             return getCreationTime(marker.getCreationTime());
94         } catch (CoreException e) {
95             log(e);
96             return EMPTY_STRING;
97         }
98     }
99
100     /**
101      * Get the name of the container. If the marker has the
102      * MarkerViewUtil#PATH_ATTRIBUTE set use that. Otherwise use the path of the
103      * parent resource.
104      *
105      * @param marker
106      * @return String
107      */

108     public static String JavaDoc getContainerName(IMarker marker) {
109
110         if (!marker.exists())
111             return Util.EMPTY_STRING;
112
113         try {
114             Object JavaDoc pathAttribute = marker
115                     .getAttribute(MarkerViewUtil.PATH_ATTRIBUTE);
116
117             if (pathAttribute != null) {
118                 return pathAttribute.toString();
119             }
120         } catch (CoreException exception) {
121             // Log the exception and fall back.
122
log(exception);
123         }
124
125         IPath path = marker.getResource().getFullPath();
126         int n = path.segmentCount() - 1; // n is the number of segments in
127
// container, not path
128
if (n <= 0) {
129             return Util.EMPTY_STRING;
130         }
131         int len = 0;
132         for (int i = 0; i < n; ++i) {
133             len += path.segment(i).length();
134         }
135         // account for /'s
136
if (n > 1) {
137             len += n - 1;
138         }
139         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(len);
140         for (int i = 0; i < n; ++i) {
141             if (i != 0) {
142                 sb.append('/');
143             }
144             sb.append(path.segment(i));
145         }
146         return sb.toString();
147     }
148
149     /**
150      * Log the exception.
151      *
152      * @param exception
153      */

154     public static void log(CoreException exception) {
155         IDEWorkbenchPlugin.log(exception.getLocalizedMessage(), exception);
156     }
157
158     /**
159      * Get the name of the element. If the marker has the
160      * MarkerViewUtil#NAME_ATTRIBUTE set use that. Otherwise use the name of the
161      * resource.
162      *
163      * @param marker
164      * @return String
165      */

166     public static String JavaDoc getResourceName(IMarker marker) {
167
168         if (!marker.exists())
169             return Util.EMPTY_STRING;
170
171         try {
172             Object JavaDoc nameAttribute = marker
173                     .getAttribute(MarkerViewUtil.NAME_ATTRIBUTE);
174
175             if (nameAttribute != null) {
176                 return nameAttribute.toString();
177             }
178         } catch (CoreException exception) {
179             log(exception);
180         }
181
182         return marker.getResource().getName();
183     }
184
185     /**
186      * Return whether or not the marker is editable.
187      *
188      * @param marker
189      * @return boolean <code>true</code> if it is editable
190      */

191     public static boolean isEditable(IMarker marker) {
192         if (marker == null) {
193             return false;
194         }
195         try {
196             return marker.isSubtypeOf(IMarker.BOOKMARK)
197                     || (marker.isSubtypeOf(IMarker.TASK) && marker
198                             .getAttribute(IMarker.USER_EDITABLE, true));
199         } catch (CoreException e) {
200             return false;
201         }
202     }
203
204     /**
205      * Return an error status for the given exception.
206      *
207      * @param exception
208      * @return IStatus
209      */

210     public static IStatus errorStatus(Throwable JavaDoc exception) {
211         String JavaDoc message = exception.getLocalizedMessage();
212         if (message == null) {
213             message = EMPTY_STRING;
214         }
215         return new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH,
216                 IStatus.ERROR, message, exception);
217     }
218
219     static final int SHORT_DELAY = 100;// The 100 ms short delay for scheduling
220

221     static final int LONG_DELAY = 30000;// The 30s long delay to run without a
222

223     // builder update
224

225     private Util() {
226         super();
227     }
228
229     /**
230      * Get the image for the severity if it can be identified.
231      *
232      * @param severity
233      * @return Image or <code>null</code>
234      */

235     public static Image getImage(int severity) {
236
237         if (severity == IMarker.SEVERITY_ERROR) {
238             return getIDEImage(IDEInternalWorkbenchImages.IMG_OBJS_ERROR_PATH);
239         }
240         if (severity == IMarker.SEVERITY_WARNING) {
241             return getIDEImage(IDEInternalWorkbenchImages.IMG_OBJS_WARNING_PATH);
242         }
243         if (severity == IMarker.SEVERITY_INFO) {
244             return getIDEImage(IDEInternalWorkbenchImages.IMG_OBJS_INFO_PATH);
245         }
246
247         return null;
248     }
249
250     /**
251      * Get the IDE image at path.
252      *
253      * @param path
254      * @return Image
255      */

256     private static Image getIDEImage(String JavaDoc constantName) {
257
258         return JFaceResources.getResources().createImageWithDefault(
259                 IDEInternalWorkbenchImages.getImageDescriptor(constantName));
260
261     }
262
263     /**
264      * Get the short name for the container
265      *
266      * @param marker
267      * @return String
268      */

269     public static String JavaDoc getShortContainerName(IMarker marker) {
270
271         if (!marker.exists())
272             return Util.EMPTY_STRING;
273
274         try {
275             Object JavaDoc pathAttribute = marker
276                     .getAttribute(MarkerViewUtil.PATH_ATTRIBUTE);
277
278             if (pathAttribute != null) {
279                 return pathAttribute.toString();
280             }
281         } catch (CoreException exception) {
282             // Log the exception and fall back.
283
log(exception);
284         }
285
286         IResource resource = marker.getResource();
287         int type = resource.getType();
288
289         // Cannot be project relative if it is the root or a project
290
if (type == IResource.PROJECT) {
291             return resource.getName();
292         }
293
294         if (type == IResource.ROOT) {
295             return MarkerMessages.Util_WorkspaceRoot;
296         }
297
298         String JavaDoc result = marker.getResource().getProjectRelativePath()
299                 .removeLastSegments(1).toOSString();
300         if (result.trim().length() == 0) {
301             return MarkerMessages.Util_ProjectRoot;
302         }
303         return result;
304     }
305
306     /**
307      * Return whether or not the selection has one element that is concrete.
308      *
309      * @param selection
310      * @return <true>code</true> if the selection has one element that is
311      * concrete.
312      */

313     static boolean isSingleConcreteSelection(IStructuredSelection selection) {
314         if (selection != null && selection.size() == 1) {
315             Object JavaDoc first = selection.getFirstElement();
316             if (first instanceof MarkerNode) {
317                 return ((MarkerNode) first).isConcrete();
318             }
319         }
320         return false;
321     }
322
323     /**
324      * Return whether or not all of the elements in the selection are concrete.
325      *
326      * @param selection
327      * @return <true>code</true> if all of the elements are concrete.
328      */

329     public static boolean allConcreteSelection(IStructuredSelection selection) {
330         if (selection != null && selection.size() > 0) {
331             Iterator JavaDoc nodes = selection.iterator();
332             while (nodes.hasNext()) {
333                 if (((MarkerNode) nodes.next()).isConcrete()) {
334                     continue;
335                 }
336                 return false;
337             }
338             return true;
339         }
340         return false;
341     }
342 }
343
Popular Tags