001: /*******************************************************************************
002: * Copyright (c) 2007 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.internal.provisional.views.markers;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.core.resources.IMarker;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IPath;
018: import org.eclipse.osgi.util.NLS;
019: import org.eclipse.ui.internal.provisional.views.markers.api.MarkerItem;
020: import org.eclipse.ui.internal.provisional.views.markers.api.MarkerSupportConstants;
021: import org.eclipse.ui.statushandlers.StatusManager;
022: import org.eclipse.ui.views.markers.MarkerViewUtil;
023: import org.eclipse.ui.views.markers.internal.MarkerMessages;
024: import org.eclipse.ui.views.markers.internal.MarkerTypesModel;
025:
026: import com.ibm.icu.text.CollationKey;
027: import com.ibm.icu.text.Collator;
028:
029: /**
030: * The MarkerEntry is the class that wrappers an {@link IMarker} for display in
031: * an {@link ExtendedMarkersView}.
032: *
033: * @since 3.4
034: *
035: */
036: public class MarkerEntry extends MarkerItem {
037:
038: Map attributeCache = new HashMap(0);
039: private MarkerCategory category;
040: Map collationKeys = new HashMap(0);
041: private String folder;
042: IMarker marker;
043:
044: /**
045: * Create a new instance of the receiver.
046: *
047: * @param marker
048: */
049: public MarkerEntry(IMarker marker) {
050: this .marker = marker;
051: }
052:
053: /*
054: * (non-Javadoc)
055: *
056: * @see org.eclipse.ui.internal.provisional.views.markers.MarkerItem#getAttributeValue(java.lang.String,
057: * boolean)
058: */
059: public boolean getAttributeValue(String attribute,
060: boolean defaultValue) {
061: if (!attributeCache.containsKey(attribute))
062: attributeCache.put(attribute, new Boolean(marker
063: .getAttribute(attribute, defaultValue)));
064: return ((Boolean) attributeCache.get(attribute)).booleanValue();
065: }
066:
067: /**
068: * Get the value of the attribute in the enclosed marker.
069: *
070: * @param attribute
071: * @param defaultValue
072: * the defaultValue if the value is not set
073: * @return int
074: */
075: public int getAttributeValue(String attribute, int defaultValue) {
076: if (!attributeCache.containsKey(attribute))
077: attributeCache.put(attribute, new Integer(marker
078: .getAttribute(attribute, defaultValue)));
079: return ((Integer) attributeCache.get(attribute)).intValue();
080:
081: }
082:
083: /**
084: * Get the String value of the attribute in the enclosed marker.
085: *
086: * @param attribute
087: * @param defaultValue
088: * the defaultValue if the value is not set
089: * @return String
090: */
091: public String getAttributeValue(String attribute,
092: String defaultValue) {
093: if (!attributeCache.containsKey(attribute))
094: attributeCache.put(attribute, marker.getAttribute(
095: attribute, defaultValue));
096: return (String) attributeCache.get(attribute);
097:
098: }
099:
100: /*
101: * (non-Javadoc)
102: *
103: * @see org.eclipse.ui.provisional.views.markers.MarkerItem#getChildren()
104: */
105: public MarkerItem[] getChildren() {
106: return MarkerSupportInternalUtilities.EMPTY_MARKER_ITEM_ARRAY;
107: }
108:
109: /**
110: * Get the CollationKey for the string attribute.
111: *
112: * @param attribute
113: * @param defaultValue
114: * the defaultValue if the value is not set
115: * @return
116: */
117: public CollationKey getCollationKey(String attribute,
118: String defaultValue) {
119: if (collationKeys.containsKey(attribute))
120: return (CollationKey) collationKeys.get(attribute);
121: String attributeValue = getAttributeValue(attribute,
122: defaultValue);
123: if (attributeValue.length() == 0)
124: return MarkerSupportInternalUtilities.EMPTY_COLLATION_KEY;
125: CollationKey key = Collator.getInstance().getCollationKey(
126: attributeValue);
127: collationKeys.put(attribute, key);
128: return key;
129: }
130:
131: /*
132: * (non-Javadoc)
133: *
134: * @see org.eclipse.ui.provisional.views.markers.MarkerItem#getConcreteRepresentative()
135: */
136: public MarkerEntry getConcreteRepresentative() {
137: return this ;
138: }
139:
140: /*
141: * (non-Javadoc)
142: *
143: * @see org.eclipse.ui.internal.provisional.views.markers.MarkerItem#getCreationTime()
144: */
145: public long getCreationTime() {
146: try {
147: return marker.getCreationTime();
148: } catch (CoreException e) {
149: StatusManager.getManager().handle(e.getStatus());
150: return -1;
151: }
152: }
153:
154: /*
155: * (non-Javadoc)
156: *
157: * @see org.eclipse.ui.provisional.views.markers.MarkerItem#getDescription()
158: */
159: public String getDescription() {
160: return getAttributeValue(IMarker.MESSAGE,
161: MarkerSupportConstants.EMPTY_STRING);
162: }
163:
164: /*
165: * (non-Javadoc)
166: *
167: * @see org.eclipse.ui.internal.provisional.views.markers.MarkerItem#getID()
168: */
169: public long getID() {
170: return marker.getId();
171: }
172:
173: /*
174: * (non-Javadoc)
175: *
176: * @see org.eclipse.ui.internal.provisional.views.markers.MarkerItem#getLocation()
177: */
178: public String getLocation() {
179: if (attributeCache.containsKey(IMarker.LOCATION))
180: return (String) attributeCache.get(IMarker.LOCATION);
181: try {
182: if (marker.getAttribute(IMarker.LOCATION) != null) {
183: String value = marker.getAttribute(IMarker.LOCATION,
184: MarkerSupportConstants.EMPTY_STRING);
185: attributeCache.put(IMarker.LOCATION, marker
186: .getAttribute(IMarker.LOCATION));
187: return value;
188: }
189: } catch (CoreException e) {
190: StatusManager.getManager().handle(e.getStatus());
191: }
192:
193: // No luck with the override so use line number
194: int lineNumber = marker.getAttribute(IMarker.LINE_NUMBER, -1);
195: String lineNumberString;
196: if (lineNumber < 0)
197: lineNumberString = MarkerMessages.Unknown;
198: else
199: lineNumberString = NLS.bind(
200: MarkerMessages.label_lineNumber, Integer
201: .toString(lineNumber));
202:
203: attributeCache.put(IMarker.LOCATION, lineNumberString);
204: return lineNumberString;
205:
206: }
207:
208: /**
209: * Return the Marker that the receiver is wrapping.
210: *
211: * @return {@link IMarker}
212: */
213: public IMarker getMarker() {
214: return marker;
215: }
216:
217: /*
218: * (non-Javadoc)
219: *
220: * @see org.eclipse.ui.internal.provisional.views.markers.MarkerItem#getMarkerTypeName()
221: */
222: public String getMarkerTypeName() {
223: try {
224: return MarkerTypesModel.getInstance().getType(
225: marker.getType()).getLabel();
226: } catch (CoreException e) {
227: return NLS.bind(MarkerMessages.FieldMessage_WrongType,
228: marker.toString());
229: }
230: }
231:
232: /*
233: * (non-Javadoc)
234: *
235: * @see org.eclipse.ui.provisional.views.markers.MarkerItem#getParent()
236: */
237: public MarkerItem getParent() {
238: return category;
239: }
240:
241: /*
242: * (non-Javadoc)
243: *
244: * @see org.eclipse.ui.internal.provisional.views.markers.MarkerItem#getPath()
245: */
246: public String getPath() {
247: if (folder == null) {
248: if (!marker.exists()) {
249: return super .getPath();
250: }
251:
252: // If the path attribute is set use it.
253: try {
254: Object pathAttribute = marker
255: .getAttribute(MarkerViewUtil.PATH_ATTRIBUTE);
256:
257: if (pathAttribute != null) {
258: folder = pathAttribute.toString();
259: return folder;
260: }
261: } catch (CoreException exception) {
262: // Log the exception and fall back.
263: StatusManager.getManager()
264: .handle(exception.getStatus());
265: }
266:
267: IPath path = marker.getResource().getFullPath();
268: int n = path.segmentCount() - 1; // n is the number of segments
269: // in container, not path
270: if (n <= 0) {
271: return super .getPath();
272: }
273: int len = 0;
274: for (int i = 0; i < n; ++i) {
275: len += path.segment(i).length();
276: }
277: // account for /'s
278: if (n > 1) {
279: len += n - 1;
280: }
281: StringBuffer sb = new StringBuffer(len);
282: for (int i = 0; i < n; ++i) {
283: if (i != 0) {
284: sb.append('/');
285: }
286: sb.append(path.segment(i));
287: }
288: folder = sb.toString();
289:
290: }
291: return folder;
292: }
293:
294: /*
295: * (non-Javadoc)
296: *
297: * @see org.eclipse.ui.provisional.views.markers.MarkerItem#isConcrete()
298: */
299: public boolean isConcrete() {
300: return true;
301: }
302:
303: /**
304: * Set the category to markerCategory.
305: *
306: * @param markerCategory
307: */
308: public void setCategory(MarkerCategory markerCategory) {
309: category = markerCategory;
310:
311: }
312:
313: /**
314: * Get the category of the receiver.
315: * @return {@link MarkerCategory}
316: */
317: public MarkerCategory getCategory() {
318: return category;
319: }
320:
321: }
|