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.CollationKey;
013: import com.ibm.icu.text.Collator;
014:
015: import org.eclipse.core.resources.IMarker;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.runtime.CoreException;
018:
019: /**
020: * This is a concrete class that stores the same type of information as the IMarkers
021: * used by the IDE. This class exists as an optimization. The various get* methods
022: * on IMarker are extremely slow, which makes it very slow to sort markers (for example,
023: * in the problems view). This marker class stores the fields in the most efficient form
024: * for sorting and display, but necessarily removes some generality from IMarker.
025: */
026: public class ConcreteMarker extends MarkerNode {
027:
028: private String description;
029:
030: private String resourceName;
031:
032: private String inFolder;
033:
034: private CollationKey descriptionKey;
035:
036: private CollationKey resourceNameKey;
037:
038: private int line;
039:
040: private String locationString;
041:
042: private long creationTime;
043:
044: private String type;
045:
046: private IMarker marker;
047:
048: /**
049: * Cache for the marker ID.
050: */
051: private long id = -1L;
052:
053: private MarkerNode markerCategory;
054:
055: private String shortFolder;
056:
057: private Object group;
058:
059: public ConcreteMarker(IMarker toCopy) {
060: marker = toCopy;
061: refresh();
062: }
063:
064: /**
065: * Clears any cached information. This frees up some memory, but will slow down
066: * the next comparison operation. It is a good idea to call this on a set of markers
067: * after sorting them, in order to reduce their memory cost.
068: */
069: public void clearCache() {
070: resourceNameKey = null;
071: descriptionKey = null;
072: }
073:
074: /**
075: * Refresh the properties of this marker from the underlying IMarker instance
076: */
077: public void refresh() {
078: clearCache();
079:
080: description = Util.getProperty(IMarker.MESSAGE, marker);
081: resourceName = Util.getResourceName(marker);
082: inFolder = Util.getContainerName(marker);
083: shortFolder = null;
084: line = marker.getAttribute(IMarker.LINE_NUMBER, -1);
085: locationString = marker.getAttribute(IMarker.LOCATION,
086: Util.EMPTY_STRING);
087:
088: try {
089: creationTime = marker.getCreationTime();
090: } catch (CoreException e) {
091: creationTime = 0;
092: }
093:
094: try {
095: type = marker.getType();
096: } catch (CoreException e1) {
097: type = Util.EMPTY_STRING;
098: }
099:
100: // store the marker ID locally
101: id = marker.getId();
102: }
103:
104: public IResource getResource() {
105: return marker.getResource();
106: }
107:
108: public String getType() {
109: return type;
110: }
111:
112: /* (non-Javadoc)
113: * @see org.eclipse.ui.views.markers.internal.MarkerNode#getDescription()
114: */
115: public String getDescription() {
116: return description;
117: }
118:
119: public CollationKey getDescriptionKey() {
120: if (descriptionKey == null) {
121: descriptionKey = Collator.getInstance().getCollationKey(
122: description);
123: }
124:
125: return descriptionKey;
126: }
127:
128: public String getResourceName() {
129: return resourceName;
130: }
131:
132: public CollationKey getResourceNameKey() {
133: if (resourceNameKey == null) {
134: resourceNameKey = Collator.getInstance().getCollationKey(
135: resourceName);
136: }
137: return resourceNameKey;
138: }
139:
140: public int getLine() {
141: return line;
142: }
143:
144: public String getFolder() {
145: return inFolder;
146: }
147:
148: public long getCreationTime() {
149: return creationTime;
150: }
151:
152: /**
153: * The underlying marker ID value.
154: * @return the marker's ID.
155: */
156: public long getId() {
157: return id;
158: }
159:
160: public IMarker getMarker() {
161: return marker;
162: }
163:
164: public boolean equals(Object object) {
165: if (!(object instanceof ConcreteMarker)) {
166: return false;
167: }
168:
169: ConcreteMarker other = (ConcreteMarker) object;
170:
171: return other.getMarker().equals(getMarker());
172: }
173:
174: public int hashCode() {
175: return getMarker().hashCode();
176: }
177:
178: /**
179: * Set the category the receiver is in.
180: * @param category
181: */
182: public void setCategory(MarkerNode category) {
183: markerCategory = category;
184:
185: }
186:
187: /* (non-Javadoc)
188: * @see org.eclipse.ui.views.markers.internal.MarkerNode#getChildren()
189: */
190: public MarkerNode[] getChildren() {
191: return Util.EMPTY_MARKER_ARRAY;
192: }
193:
194: /* (non-Javadoc)
195: * @see org.eclipse.ui.views.markers.internal.MarkerNode#getParent()
196: */
197: public MarkerNode getParent() {
198: return markerCategory;
199: }
200:
201: /* (non-Javadoc)
202: * @see org.eclipse.ui.views.markers.internal.MarkerNode#isConcrete()
203: */
204: public boolean isConcrete() {
205: return true;
206: }
207:
208: /**
209: * Return the short name for the folder.
210: * @return String
211: */
212: public String getShortFolder() {
213: if (shortFolder == null) {
214: shortFolder = Util.getShortContainerName(marker);
215: }
216: return shortFolder;
217: }
218:
219: /**
220: * Get the location string. If the {@link IMarker#LOCATION }
221: * attribute was not set then return an empty String.
222: * @return String
223: */
224: public String getLocationString() {
225: return locationString;
226: }
227:
228: /**
229: * Get the group for the reciever.
230: * @return Returns the group.
231: */
232: public Object getGroup() {
233: return group;
234: }
235:
236: /**
237: * Set the group name.
238: * @param group the group name
239: */
240: public void setGroup(Object group) {
241: this .group = group;
242: }
243:
244: /* (non-Javadoc)
245: * @see org.eclipse.ui.views.markers.internal.MarkerNode#getConcreteRepresentative()
246: */
247: public ConcreteMarker getConcreteRepresentative() {
248: return this;
249: }
250: }
|