001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.views.markers.internal;
011:
012: import com.ibm.icu.text.DateFormat;
013: import java.util.Date;
014: import java.util.Iterator;
015:
016: import org.eclipse.core.resources.IMarker;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.IStatus;
021: import org.eclipse.core.runtime.Status;
022: import org.eclipse.jface.resource.JFaceResources;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.swt.graphics.Image;
025: import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages;
026: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
027: import org.eclipse.ui.views.markers.MarkerViewUtil;
028:
029: /**
030: * The Util class is the class of general utilities used by the marker support.
031: *
032: */
033: public final class Util {
034:
035: static String EMPTY_STRING = "";//$NON-NLS-1$
036:
037: static String TWO_LINE_FEED = "\n\n";//$NON-NLS-1$
038:
039: static String LINE_FEED_AND_TAB = "\n\t";//$NON-NLS-1$
040:
041: private static DateFormat format;
042:
043: static final MarkerNode[] EMPTY_MARKER_ARRAY = new MarkerNode[0];
044:
045: static final String TYPE_MARKER_GROUPING_ID = "org.eclipse.ui.ide.type"; //$NON-NLS-1$
046:
047: /**
048: * Get the propery called property from the marker. If it is not found
049: * return the empty string.
050: *
051: * @param property
052: * @param marker
053: * @return String
054: */
055: public static String getProperty(String property, IMarker marker) {
056: if (marker == null || !marker.exists()) {
057: return EMPTY_STRING;
058: }
059: try {
060: Object obj = marker.getAttribute(property);
061: if (obj != null) {
062: return obj.toString();
063: }
064: return EMPTY_STRING;
065: } catch (CoreException e) {
066: log(e);
067: return EMPTY_STRING;
068: }
069: }
070:
071: /**
072: * Get the human readable creation time from the timestamp
073: *
074: * @param timestamp
075: * @return String
076: */
077: public static String getCreationTime(long timestamp) {
078: if (format == null) {
079: format = DateFormat.getDateTimeInstance(DateFormat.LONG,
080: DateFormat.MEDIUM);
081: }
082: return format.format(new Date(timestamp));
083: }
084:
085: /**
086: * Get the human readable creation time from the marker.
087: *
088: * @param marker
089: * @return String
090: */
091: public static String getCreationTime(IMarker marker) {
092: try {
093: return getCreationTime(marker.getCreationTime());
094: } catch (CoreException e) {
095: log(e);
096: return EMPTY_STRING;
097: }
098: }
099:
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 getContainerName(IMarker marker) {
109:
110: if (!marker.exists())
111: return Util.EMPTY_STRING;
112:
113: try {
114: Object 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 sb = new StringBuffer(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(),
156: exception);
157: }
158:
159: /**
160: * Get the name of the element. If the marker has the
161: * MarkerViewUtil#NAME_ATTRIBUTE set use that. Otherwise use the name of the
162: * resource.
163: *
164: * @param marker
165: * @return String
166: */
167: public static String getResourceName(IMarker marker) {
168:
169: if (!marker.exists())
170: return Util.EMPTY_STRING;
171:
172: try {
173: Object nameAttribute = marker
174: .getAttribute(MarkerViewUtil.NAME_ATTRIBUTE);
175:
176: if (nameAttribute != null) {
177: return nameAttribute.toString();
178: }
179: } catch (CoreException exception) {
180: log(exception);
181: }
182:
183: return marker.getResource().getName();
184: }
185:
186: /**
187: * Return whether or not the marker is editable.
188: *
189: * @param marker
190: * @return boolean <code>true</code> if it is editable
191: */
192: public static boolean isEditable(IMarker marker) {
193: if (marker == null) {
194: return false;
195: }
196: try {
197: return marker.isSubtypeOf(IMarker.BOOKMARK)
198: || (marker.isSubtypeOf(IMarker.TASK) && marker
199: .getAttribute(IMarker.USER_EDITABLE, true));
200: } catch (CoreException e) {
201: return false;
202: }
203: }
204:
205: /**
206: * Return an error status for the given exception.
207: *
208: * @param exception
209: * @return IStatus
210: */
211: public static IStatus errorStatus(Throwable exception) {
212: String message = exception.getLocalizedMessage();
213: if (message == null) {
214: message = EMPTY_STRING;
215: }
216: return new Status(IStatus.ERROR,
217: IDEWorkbenchPlugin.IDE_WORKBENCH, IStatus.ERROR,
218: message, exception);
219: }
220:
221: static final int SHORT_DELAY = 100;// The 100 ms short delay for scheduling
222:
223: static final int LONG_DELAY = 30000;// The 30s long delay to run without a
224:
225: // builder update
226:
227: private Util() {
228: super ();
229: }
230:
231: /**
232: * Get the image for the severity if it can be identified.
233: *
234: * @param severity
235: * @return Image or <code>null</code>
236: */
237: public static Image getImage(int severity) {
238:
239: if (severity == IMarker.SEVERITY_ERROR) {
240: return getIDEImage(IDEInternalWorkbenchImages.IMG_OBJS_ERROR_PATH);
241: }
242: if (severity == IMarker.SEVERITY_WARNING) {
243: return getIDEImage(IDEInternalWorkbenchImages.IMG_OBJS_WARNING_PATH);
244: }
245: if (severity == IMarker.SEVERITY_INFO) {
246: return getIDEImage(IDEInternalWorkbenchImages.IMG_OBJS_INFO_PATH);
247: }
248:
249: return null;
250: }
251:
252: /**
253: * Get the IDE image at path.
254: *
255: * @param path
256: * @return Image
257: */
258: private static Image getIDEImage(String constantName) {
259:
260: return JFaceResources.getResources().createImageWithDefault(
261: IDEInternalWorkbenchImages
262: .getImageDescriptor(constantName));
263:
264: }
265:
266: /**
267: * Get the short name for the container
268: *
269: * @param marker
270: * @return String
271: */
272: public static String getShortContainerName(IMarker marker) {
273:
274: if (!marker.exists())
275: return Util.EMPTY_STRING;
276:
277: try {
278: Object pathAttribute = marker
279: .getAttribute(MarkerViewUtil.PATH_ATTRIBUTE);
280:
281: if (pathAttribute != null) {
282: return pathAttribute.toString();
283: }
284: } catch (CoreException exception) {
285: // Log the exception and fall back.
286: log(exception);
287: }
288:
289: IResource resource = marker.getResource();
290: int type = resource.getType();
291:
292: // Cannot be project relative if it is the root or a project
293: if (type == IResource.PROJECT) {
294: return resource.getName();
295: }
296:
297: if (type == IResource.ROOT) {
298: return MarkerMessages.Util_WorkspaceRoot;
299: }
300:
301: String result = marker.getResource().getProjectRelativePath()
302: .removeLastSegments(1).toOSString();
303: if (result.trim().length() == 0) {
304: return MarkerMessages.Util_ProjectRoot;
305: }
306: return result;
307: }
308:
309: /**
310: * Return whether or not the selection has one element that is concrete.
311: *
312: * @param selection
313: * @return <true>code</true> if the selection has one element that is
314: * concrete.
315: */
316: static boolean isSingleConcreteSelection(
317: IStructuredSelection selection) {
318: if (selection != null && selection.size() == 1) {
319: Object first = selection.getFirstElement();
320: if (first instanceof MarkerNode) {
321: return ((MarkerNode) first).isConcrete();
322: }
323: }
324: return false;
325: }
326:
327: /**
328: * Return whether or not all of the elements in the selection are concrete.
329: *
330: * @param selection
331: * @return <true>code</true> if all of the elements are concrete.
332: */
333: public static boolean allConcreteSelection(
334: IStructuredSelection selection) {
335: if (selection != null && selection.size() > 0) {
336: Iterator nodes = selection.iterator();
337: while (nodes.hasNext()) {
338: if (((MarkerNode) nodes.next()).isConcrete()) {
339: continue;
340: }
341: return false;
342: }
343: return true;
344: }
345: return false;
346: }
347: }
|