KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > gui2 > Sortables


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
18  */

19
20 package edu.umd.cs.findbugs.gui2;
21
22 import java.sql.Timestamp JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import edu.umd.cs.findbugs.AppVersion;
30 import edu.umd.cs.findbugs.BugCollection;
31 import edu.umd.cs.findbugs.BugInstance;
32 import edu.umd.cs.findbugs.BugPattern;
33 import edu.umd.cs.findbugs.Detector;
34 import edu.umd.cs.findbugs.I18N;
35 import edu.umd.cs.findbugs.L10N;
36 import edu.umd.cs.findbugs.gui2.BugAspects.StringPair;
37
38 /**
39  * A useful enum for dealing with all the types of filterable and sortable data in BugInstances
40  * This is the preferred way for getting the information out of a BugInstance and formatting it for display
41  * It also has the comparators for the different types of data
42  *
43  * @author Reuven
44  */

45
46 public enum Sortables implements Comparator JavaDoc<StringPair>
47 {
48
49     FIRSTVERSION(edu.umd.cs.findbugs.L10N.getLocalString("sort.first_version", "First Version"))
50     {
51         public String JavaDoc getFrom(BugInstance bug)
52         {
53             return Long.toString(bug.getFirstVersion());
54         }
55         
56         @Override JavaDoc
57         public String JavaDoc formatValue(String JavaDoc value)
58         {
59             //System.out.println("Formatting first version value");
60
if(value.equals("0"))
61                 return edu.umd.cs.findbugs.L10N.getLocalString("sort.first_version_not_defined", "First version not defined");
62             int seqNum = Integer.parseInt(value);
63             AppVersion appVersion = BugLoader.mainBugCollection.getAppVersionFromSequenceNumber(seqNum);
64             String JavaDoc appendItem = "";
65             if(appVersion != null)
66             {
67                 String JavaDoc timestamp = new Timestamp JavaDoc(appVersion.getTimestamp()).toString();
68                 appendItem = appVersion.getReleaseName() + " (" + timestamp.substring(0, timestamp.indexOf(' ')) + ")";
69             }
70             if(appendItem == "")
71                 appendItem = "AppVersion not found, sequence number=" + seqNum;
72             return (edu.umd.cs.findbugs.L10N.getLocalString("sort.first_version", "First Version") + ": " + appendItem);
73         }
74         
75         @Override JavaDoc
76         public int compare(StringPair one, StringPair two)
77         {
78             // Numerical (zero is first)
79
return Integer.valueOf(one.value).compareTo(Integer.valueOf(two.value));
80         }
81     },
82     
83     LASTVERSION(edu.umd.cs.findbugs.L10N.getLocalString("sort.last_version", "Last Version"))
84     {
85         public String JavaDoc getFrom(BugInstance bug)
86         {
87             return Long.toString(bug.getLastVersion());
88         }
89         
90         @Override JavaDoc
91         public String JavaDoc formatValue(String JavaDoc value)
92         {
93             //System.out.println("Formatting last version value");
94
if(value.equals("-1"))
95                 return edu.umd.cs.findbugs.L10N.getLocalString("sort.last_version_not_defined", "Last version not defined");
96             int seqNum = Integer.parseInt(value);
97             AppVersion appVersion = BugLoader.mainBugCollection.getAppVersionFromSequenceNumber(seqNum);
98             String JavaDoc appendItem = "";
99             if(appVersion != null)
100             {
101                 String JavaDoc timestamp = new Timestamp JavaDoc(appVersion.getTimestamp()).toString();
102                 appendItem = appVersion.getReleaseName().toString() + " (" + timestamp.substring(0, timestamp.indexOf(' ')) + ")";
103             }
104             if(appendItem == "")
105                 appendItem = "AppVersion not found, sequence number=" + seqNum;
106             return (edu.umd.cs.findbugs.L10N.getLocalString("sort.last_version", "Last Version") + ": " + appendItem);
107             }
108         
109         @Override JavaDoc
110         public int compare(StringPair one, StringPair two)
111         {
112             if (one.value.equals(two.value)) return 0;
113             
114             // Numerical (except that -1 is last)
115
int first = Integer.valueOf(one.value);
116             int second = Integer.valueOf(two.value);
117             if (first == second) return 0;
118             if (first < 0) return 1;
119             if (second < 0) return -1;
120             if (first < second) return -1;
121             return 1;
122         }
123     },
124     
125     PRIORITY(edu.umd.cs.findbugs.L10N.getLocalString("sort.priority", "Priority"))
126     {
127         public String JavaDoc getFrom(BugInstance bug)
128         {
129             return String.valueOf(bug.getPriority());
130         }
131         
132         @Override JavaDoc
133         public String JavaDoc formatValue(String JavaDoc value)
134         {
135             if (value.equals(String.valueOf(Detector.HIGH_PRIORITY)))
136                 return edu.umd.cs.findbugs.L10N.getLocalString("sort.priority_high", "High");
137             if (value.equals(String.valueOf(Detector.NORMAL_PRIORITY)))
138                 return edu.umd.cs.findbugs.L10N.getLocalString("sort.priority_normal", "Normal");
139             if (value.equals(String.valueOf(Detector.LOW_PRIORITY)))
140                 return edu.umd.cs.findbugs.L10N.getLocalString("sort.priority_low", "Low");
141             if (value.equals(String.valueOf(Detector.EXP_PRIORITY)))
142                 return edu.umd.cs.findbugs.L10N.getLocalString("sort.priority_experimental", "Experimental");
143             return edu.umd.cs.findbugs.L10N.getLocalString("sort.priority_ignore", "Ignore"); // This probably shouldn't ever happen, but what the hell, let's be complete
144

145         }
146         
147         @Override JavaDoc
148         public int compare(StringPair one, StringPair two)
149         {
150             // Numerical
151
return Integer.valueOf(one.value).compareTo(Integer.valueOf(two.value));
152         }
153     },
154     CLASS(edu.umd.cs.findbugs.L10N.getLocalString("sort.class", "Class"))
155     {
156         public String JavaDoc getFrom(BugInstance bug)
157         {
158             return bug.getPrimarySourceLineAnnotation().getClassName();
159         }
160                 
161         @Override JavaDoc
162         public int compare(StringPair one, StringPair two)
163         {
164             // If both have dollar signs and are of the same outer class, compare the numbers after the dollar signs.
165
try
166             {
167                 if (one.value.contains("$") && two.value.contains("$")
168                         && one.value.substring(0, one.value.lastIndexOf("$")).equals(two.value.substring(0, two.value.lastIndexOf("$"))))
169                     return Integer.valueOf(one.value.substring(one.value.lastIndexOf("$"))).compareTo(Integer.valueOf(two.value.substring(two.value.lastIndexOf("$"))));
170             }
171             catch (NumberFormatException JavaDoc e) {} // Somebody's playing silly buggers with dollar signs, just do it lexicographically
172

173             // Otherwise, lexicographicalify it
174
return one.value.compareTo(two.value);
175         }
176     },
177     PACKAGE(edu.umd.cs.findbugs.L10N.getLocalString("sort.package", "Package"))
178     {
179         public String JavaDoc getFrom(BugInstance bug)
180         {
181             return bug.getPrimarySourceLineAnnotation().getPackageName();
182         }
183         
184         public String JavaDoc formatValue(String JavaDoc value)
185         {
186             if (value.equals(""))
187                 return "(Default)";
188             return value;
189         }
190     },
191     CATEGORY(edu.umd.cs.findbugs.L10N.getLocalString("sort.category", "Category"))
192     {
193         public String JavaDoc getFrom(BugInstance bug)
194         {
195             return bug.getBugPattern().getCategory();
196         }
197         
198         @Override JavaDoc
199         public String JavaDoc formatValue(String JavaDoc value)
200         {
201             return I18N.instance().getBugCategoryDescription(value);
202         }
203     },
204     DESIGNATION(edu.umd.cs.findbugs.L10N.getLocalString("sort.designation", "Designation"))
205     {
206         public String JavaDoc getFrom(BugInstance bug)
207         {
208             return bug.getUserDesignationKey();
209         }
210                         
211         /**
212          * value is the key of the designations.
213          * @param value
214          * @return
215          */

216         @Override JavaDoc
217         public String JavaDoc formatValue(String JavaDoc value)
218         {
219             return I18N.instance().getUserDesignation(value);
220         }
221         
222         public String JavaDoc[] getAllSorted()
223         {//FIXME I think we always want user to see all possible designations, not just the ones he has set in his project, Agreement? -Dan
224
List JavaDoc<String JavaDoc> sortedDesignations=I18N.instance().getUserDesignationKeys(true);
225             return sortedDesignations.toArray(new String JavaDoc[sortedDesignations.size()]);
226         }
227     },
228     BUGCODE(edu.umd.cs.findbugs.L10N.getLocalString("sort.bug_kind", "Bug Kind"))
229     {
230         public String JavaDoc getFrom(BugInstance bug)
231         {
232             return bug.getBugPattern().getAbbrev();
233         }
234                 
235         public String JavaDoc formatValue(String JavaDoc value)
236         {
237             return I18N.instance().getBugTypeDescription(value);
238         }
239         
240         @Override JavaDoc
241         public int compare(StringPair one, StringPair two)
242         {
243             return formatValue(one.value).compareTo(formatValue(two.value));
244         }
245     },
246     TYPE(edu.umd.cs.findbugs.L10N.getLocalString("sort.bug_pattern", "Bug Pattern"))
247     {
248         public String JavaDoc getFrom(BugInstance bug)
249         {
250             if((bug.getBugPattern()) == null)
251                 return "Missing bug pattern";
252             else return bug.getBugPattern().getType();
253         }
254                 
255         public String JavaDoc formatValue(String JavaDoc value)
256         {
257             return I18N.instance().getShortMessageWithoutCode(value);
258         }
259     },
260     DIVIDER(" ")
261     {
262         @Override JavaDoc
263         public String JavaDoc getFrom(BugInstance bug)
264         {
265             throw new UnsupportedOperationException JavaDoc();
266         }
267         
268         @Override JavaDoc
269         public String JavaDoc[] getAll()
270         {
271             throw new UnsupportedOperationException JavaDoc();
272         }
273         
274         @Override JavaDoc
275         public String JavaDoc formatValue(String JavaDoc value)
276         {
277             throw new UnsupportedOperationException JavaDoc();
278         }
279         
280         @Override JavaDoc
281         public int compare(StringPair one, StringPair two)
282         {
283             throw new UnsupportedOperationException JavaDoc();
284         }
285     };
286     
287     String JavaDoc prettyName;
288
289     Sortables(String JavaDoc prettyName)
290     {
291         this.prettyName = prettyName;
292     }
293
294     public String JavaDoc toString()
295     {
296         return prettyName;
297     }
298
299     public abstract String JavaDoc getFrom(BugInstance bug);
300     public String JavaDoc[] getAll()
301     {
302         return getAll(BugSet.getMainBugSet());
303     }
304     public String JavaDoc[] getAll(BugSet set)
305     {
306         return set.getAll(this);
307     }
308     
309     public String JavaDoc formatValue(String JavaDoc value)
310     {
311         return value;
312     }
313     
314     public int compare(StringPair one, StringPair two)
315     {
316         // Lexicographical by default
317
return one.value.compareTo(two.value);
318     }
319     
320     public String JavaDoc[] getAllSorted()
321     {
322         return getAllSorted(BugSet.getMainBugSet());
323     }
324     
325     public String JavaDoc[] getAllSorted(BugSet set)
326     {
327         String JavaDoc[] values = getAll(set);
328         StringPair[] pairs = new StringPair[values.length];
329         for (int i = 0; i < values.length; i++)
330             pairs[i] = new StringPair(this, values[i]);
331         Arrays.sort(pairs, this);
332         for (int i = 0; i < values.length; i++)
333             values[i] = pairs[i].value;
334         return values;
335     }
336     
337     public Comparator JavaDoc<BugLeafNode> getBugLeafNodeComparator()
338     {
339         final Sortables key = this;
340         return new Comparator JavaDoc<BugLeafNode>()
341         {
342             public int compare(BugLeafNode one, BugLeafNode two)
343             {
344                 return key.compare(new StringPair(key, key.getFrom(one.getBug())), new StringPair(key, key.getFrom(two.getBug())));
345             }
346         };
347     }
348     
349     public static Sortables getSortableByPrettyName(String JavaDoc name)
350     {
351         for (Sortables s: Sortables.values())
352         {
353             if (s.prettyName.equals(name))
354                 return s;
355         }
356             return null;
357     }
358 }
359
Popular Tags