KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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 java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.swt.graphics.Image;
23
24 /**
25  * @since 3.2
26  *
27  */

28 public class FieldMarkerGroup implements IField {
29
30     class EntryMapping {
31         MarkerGroupingEntry groupingEntry;
32
33         /**
34          * Create an entry mapping for the receiver.
35          *
36          * @param entry
37          */

38         EntryMapping(MarkerGroupingEntry entry) {
39             groupingEntry = entry;
40         }
41
42         /**
43          * Return whether or not the receiver tests attributes.
44          *
45          * @return boolean
46          */

47         public boolean hasAttributes() {
48             return false;
49         }
50
51         /**
52          * Test the attribute of the marker to find a grouping.
53          *
54          * @param marker
55          * @return MarkerGroupingEntry or <code>null</code> if there is not
56          * entry.
57          */

58         public MarkerGroupingEntry testAttribute(ConcreteMarker marker) {
59             return null;
60         }
61     }
62
63     private static MarkerGroupingEntry undefinedEntry = new MarkerGroupingEntry(
64             MarkerMessages.FieldCategory_Uncategorized, null, 0);
65
66     class AttributeMapping extends EntryMapping {
67
68         String JavaDoc attribute;
69
70         String JavaDoc attributeValue;
71
72         /**
73          * Create a mapping for an attribute with name attributeName and value
74          * value to the supplied entry.
75          *
76          * @param entry
77          * @param attributeName
78          * @param value
79          */

80         AttributeMapping(MarkerGroupingEntry entry, String JavaDoc attributeName,
81                 String JavaDoc value) {
82             super(entry);
83             attribute = attributeName;
84             attributeValue = value;
85         }
86
87         /*
88          * (non-Javadoc)
89          *
90          * @see org.eclipse.ui.views.markers.internal.FieldMarkerGroup.EntryMapping#hasAttributes()
91          */

92         public boolean hasAttributes() {
93             return true;
94         }
95
96         /*
97          * (non-Javadoc)
98          *
99          * @see org.eclipse.ui.views.markers.internal.FieldMarkerGroup.EntryMapping#testAttribute(org.eclipse.ui.views.markers.internal.ConcreteMarker)
100          */

101         public MarkerGroupingEntry testAttribute(ConcreteMarker marker) {
102             Object JavaDoc value;
103             
104             if(!marker.getMarker().exists())
105                 return null;//If the marker was deleted during the update drop it
106

107             try {
108                 value = marker.getMarker().getAttribute(attribute);
109             } catch (CoreException e) {
110                 Util.log(e);
111                 return null;
112             }
113             
114             if (value != null && attributeValue.equals(value.toString())) {
115                 return groupingEntry;
116             }
117             return null;
118         }
119     }
120
121     private String JavaDoc title;
122
123     private String JavaDoc id;
124
125     private Map JavaDoc typesToMappings = new HashMap JavaDoc();
126
127     private boolean showing;
128
129     /**
130      * Create a new instance of the receiver called name with id identifier.
131      *
132      * @param name
133      * @param identifier
134      */

135     public FieldMarkerGroup(String JavaDoc name, String JavaDoc identifier) {
136         title = name;
137         id = identifier;
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.eclipse.ui.views.markers.internal.IField#getDescription()
144      */

145     public String JavaDoc getDescription() {
146         return title;
147     }
148
149     /*
150      * (non-Javadoc)
151      *
152      * @see org.eclipse.ui.views.markers.internal.IField#getDescriptionImage()
153      */

154     public Image getDescriptionImage() {
155         return null;
156     }
157
158     /*
159      * (non-Javadoc)
160      *
161      * @see org.eclipse.ui.views.markers.internal.IField#getColumnHeaderText()
162      */

163     public String JavaDoc getColumnHeaderText() {
164         return title;
165     }
166
167     /*
168      * (non-Javadoc)
169      *
170      * @see org.eclipse.ui.views.markers.internal.IField#getColumnHeaderImage()
171      */

172     public Image getColumnHeaderImage() {
173         return null;
174     }
175
176     /*
177      * (non-Javadoc)
178      *
179      * @see org.eclipse.ui.views.markers.internal.IField#getValue(java.lang.Object)
180      */

181     public String JavaDoc getValue(Object JavaDoc obj) {
182         MarkerNode node = (MarkerNode) obj;
183
184         if (node.isConcrete()) {
185             MarkerGroupingEntry groupingEntry = getMapping((ConcreteMarker) node);
186             return groupingEntry.getLabel();
187         }
188         return node.getDescription();
189     }
190
191     /**
192      * Get the attribute mapping for the marker
193      *
194      * @param marker
195      * @return MarkerGroupingEntry
196      */

197     private MarkerGroupingEntry getMapping(ConcreteMarker marker) {
198
199         if (marker.getGroup() == null) {
200             marker.setGroup(findGroupValue(marker));
201         }
202         return (MarkerGroupingEntry) marker.getGroup();
203     }
204
205     /**
206      * Find the group value. If it cannot be found in an attribute mapping then
207      * return null;
208      *
209      * @param marker
210      * @return String or <code>null</code>
211      */

212     private MarkerGroupingEntry findGroupValue(ConcreteMarker marker) {
213
214         if (typesToMappings.containsKey(marker.getType())) {
215             EntryMapping defaultMapping = null;
216             Iterator JavaDoc mappings = ((Collection JavaDoc) typesToMappings.get(marker
217                     .getType())).iterator();
218             while (mappings.hasNext()) {
219                 EntryMapping mapping = (EntryMapping) mappings.next();
220                 if (mapping.hasAttributes()) {
221                     MarkerGroupingEntry entry = mapping.testAttribute(marker);
222                     if (entry != null) {
223                         return entry;
224                     }
225                 } else {
226                     // If it has no attributes it is our default
227
defaultMapping = mapping;
228                 }
229             }
230             if (defaultMapping != null) {
231                 return defaultMapping.groupingEntry;
232             }
233
234         }
235
236         return undefinedEntry;
237
238     } /*
239          * (non-Javadoc)
240          *
241          * @see org.eclipse.ui.views.markers.internal.IField#getImage(java.lang.Object)
242          */

243
244     public Image getImage(Object JavaDoc obj) {
245         return null;
246     }
247
248     /*
249      * (non-Javadoc)
250      *
251      * @see org.eclipse.ui.views.markers.internal.IField#compare(java.lang.Object,
252      * java.lang.Object)
253      */

254     public int compare(Object JavaDoc obj1, Object JavaDoc obj2) {
255
256         MarkerGroupingEntry entry1 = getMapping(((MarkerNode) obj1)
257                 .getConcreteRepresentative());
258         MarkerGroupingEntry entry2 = getMapping(((MarkerNode) obj2)
259                 .getConcreteRepresentative());
260         return entry2.getPriority() - entry1.getPriority();
261
262     }
263
264     /*
265      * (non-Javadoc)
266      *
267      * @see org.eclipse.ui.views.markers.internal.IField#getDefaultDirection()
268      */

269     public int getDefaultDirection() {
270         return TableComparator.ASCENDING;
271     }
272
273     /*
274      * (non-Javadoc)
275      *
276      * @see org.eclipse.ui.views.markers.internal.IField#getPreferredWidth()
277      */

278     public int getPreferredWidth() {
279         return 75;
280     }
281
282     /*
283      * (non-Javadoc)
284      *
285      * @see org.eclipse.ui.views.markers.internal.IField#isShowing()
286      */

287     public boolean isShowing() {
288         return showing;
289     }
290
291     /*
292      * (non-Javadoc)
293      *
294      * @see org.eclipse.ui.views.markers.internal.IField#setShowing(boolean)
295      */

296     public void setShowing(boolean showing) {
297         this.showing = showing;
298
299     }
300
301     /**
302      * Set entry and the default entry for the supplied markerType.
303      *
304      * @param markerType
305      * @param entry
306      */

307
308     public void setAsDefault(String JavaDoc markerType, MarkerGroupingEntry entry) {
309         addEntry(markerType, new EntryMapping(entry));
310
311     }
312
313     /**
314      * Add the entry for the markerType.
315      *
316      * @param markerType
317      * @param entry
318      */

319     private void addEntry(String JavaDoc markerType, EntryMapping entry) {
320
321         MarkerType[] allDerived = getMarkerTypes(markerType);
322
323         for (int i = 0; i < allDerived.length; i++) {
324             Collection JavaDoc entries = new HashSet JavaDoc();
325             MarkerType type = allDerived[i];
326             if (typesToMappings.containsKey(type.getId())) {
327                 entries = (Collection JavaDoc) typesToMappings.get(markerType);
328             } else {
329                 entries = new HashSet JavaDoc();
330             }
331
332             entries.add(entry);
333             typesToMappings.put(type.getId(), entries);
334         }
335
336     }
337
338     /**
339      * Return the marker types that match and are subtypes of markerType.
340      * @param markerType
341      * @return MarkerType[]
342      */

343     private MarkerType[] getMarkerTypes(String JavaDoc markerType) {
344         MarkerTypesModel model = MarkerTypesModel.getInstance();
345         Collection JavaDoc types = new HashSet JavaDoc();
346
347         MarkerType type = model.getType(markerType);
348         if (type != null) {
349             types.add(type);
350             MarkerType[] subs = type.getAllSubTypes();
351             for (int j = 0; j < subs.length; j++) {
352                 types.add(subs[j]);
353             }
354         }
355
356         if (types.isEmpty()) {
357             return new MarkerType[0];
358         }
359
360         MarkerType[] typesArray = new MarkerType[types.size()];
361         types.toArray(typesArray);
362         return typesArray;
363     }
364
365     /**
366      * Add an attributeMapping for the markerType.
367      *
368      * @param markerType
369      * @param attribute
370      * @param attributeValue
371      * @param entry
372      */

373     public void mapAttribute(String JavaDoc markerType, String JavaDoc attribute,
374             String JavaDoc attributeValue, MarkerGroupingEntry entry) {
375         addEntry(markerType, new AttributeMapping(entry, attribute,
376                 attributeValue));
377
378     }
379
380     /**
381      * Return the id of the receiver.
382      *
383      * @return String
384      */

385     public String JavaDoc getId() {
386         return id;
387     }
388
389     /**
390      * Remove the entry from all of the entries in the receiver.
391      * @param entry
392      */

393     public void remove(MarkerGroupingEntry entry) {
394         Iterator JavaDoc entries = typesToMappings.values().iterator();
395         Collection JavaDoc removeCollection = new ArrayList JavaDoc();
396         while(entries.hasNext()){
397             Collection JavaDoc mappings = (Collection JavaDoc) entries.next();
398             Iterator JavaDoc mappingsIterator = mappings.iterator();
399             while(mappingsIterator.hasNext()){
400                 EntryMapping next = (EntryMapping) mappingsIterator.next();
401                 if(next.groupingEntry.equals(entry)){
402                     removeCollection.add(next);
403                 }
404                     
405             }
406             mappings.removeAll(removeCollection);
407             removeCollection.clear();
408         }
409         
410     }
411 }
412
Popular Tags