KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > gui > BugCellRenderer


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

20 package edu.umd.cs.findbugs.gui;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24
25 import javax.swing.ImageIcon JavaDoc;
26 import javax.swing.JTree JavaDoc;
27 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
28 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
29
30 import edu.umd.cs.findbugs.BugInstance;
31 import edu.umd.cs.findbugs.ClassAnnotation;
32 import edu.umd.cs.findbugs.Detector;
33 import edu.umd.cs.findbugs.FieldAnnotation;
34 import edu.umd.cs.findbugs.MethodAnnotation;
35 import edu.umd.cs.findbugs.SourceLineAnnotation;
36
37 /**
38  * Custom cell renderer for the bug tree.
39  * We use this to select the tree icons, and to set the
40  * text color based on the bug priority.
41  */

42 public class BugCellRenderer extends DefaultTreeCellRenderer JavaDoc {
43     
44     private static final BugCellRenderer theInstance = new BugCellRenderer();
45     
46     /**
47      * Get the single instance.
48      *
49      * @return the instance
50      */

51     public static BugCellRenderer instance() {
52         return theInstance;
53     }
54     
55     private static final long serialVersionUID = 1L;
56     private ImageIcon JavaDoc bugGroupIcon;
57     private ImageIcon JavaDoc packageIcon;
58     private ImageIcon JavaDoc bugIcon;
59     private ImageIcon JavaDoc classIcon;
60     private ImageIcon JavaDoc methodIcon;
61     private ImageIcon JavaDoc fieldIcon;
62     private ImageIcon JavaDoc sourceFileIcon;
63     private Object JavaDoc value;
64
65     private BugCellRenderer() {
66         ClassLoader JavaDoc classLoader = this.getClass().getClassLoader();
67         bugGroupIcon = new ImageIcon JavaDoc(classLoader.getResource("edu/umd/cs/findbugs/gui/bug.png"));
68         packageIcon = new ImageIcon JavaDoc(classLoader.getResource("edu/umd/cs/findbugs/gui/package.png"));
69         bugIcon = new ImageIcon JavaDoc(classLoader.getResource("edu/umd/cs/findbugs/gui/bug2.png"));
70         classIcon = new ImageIcon JavaDoc(classLoader.getResource("edu/umd/cs/findbugs/gui/class.png"));
71         methodIcon = new ImageIcon JavaDoc(classLoader.getResource("edu/umd/cs/findbugs/gui/method.png"));
72         fieldIcon = new ImageIcon JavaDoc(classLoader.getResource("edu/umd/cs/findbugs/gui/field.png"));
73         sourceFileIcon = new ImageIcon JavaDoc(classLoader.getResource("edu/umd/cs/findbugs/gui/sourcefile.png"));
74     }
75
76     @Override JavaDoc
77     public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value, boolean sel,
78                                                   boolean expanded, boolean leaf, int row, boolean hasFocus) {
79         DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) value;
80         Object JavaDoc obj = node.getUserObject();
81
82         this.value = obj;
83
84         super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
85
86         // Set the icon, depending on what kind of node it is
87
if (obj instanceof BugInstance) {
88             setIcon(bugIcon);
89         } else if (obj instanceof ClassAnnotation) {
90             setIcon(classIcon);
91         } else if (obj instanceof MethodAnnotation) {
92             setIcon(methodIcon);
93         } else if (obj instanceof FieldAnnotation) {
94             setIcon(fieldIcon);
95         } else if (obj instanceof SourceLineAnnotation) {
96             setIcon(sourceFileIcon);
97         } else if (obj instanceof BugInstanceGroup) {
98             // This is a "group" node
99
BugInstanceGroup groupNode = (BugInstanceGroup) obj;
100             String JavaDoc groupType = groupNode.getGroupType();
101             if (groupType == FindBugsFrame.GROUP_BY_CLASS) {
102                 setIcon(classIcon);
103             } else if (groupType == FindBugsFrame.GROUP_BY_PACKAGE) {
104                 setIcon(packageIcon);
105             } else if (groupType == FindBugsFrame.GROUP_BY_BUG_TYPE) {
106                 setIcon(bugGroupIcon);
107                             } else if (groupType == FindBugsFrame.GROUP_BY_BUG_CATEGORY) {
108                 setIcon(bugGroupIcon);
109             }
110         } else {
111             setIcon(null);
112         }
113
114         return this;
115     }
116
117     @Override JavaDoc
118     public Color JavaDoc getTextNonSelectionColor() {
119         return getCellTextColor();
120     }
121
122     private Color JavaDoc getCellTextColor() {
123         // Based on the priority, color-code the bug instance.
124
Color JavaDoc color = Color.BLACK;
125         if (value instanceof BugInstance) {
126             BugInstance bugInstance = (BugInstance) value;
127             switch (bugInstance.getPriority()) {
128             case Detector.EXP_PRIORITY:
129                 color = FindBugsFrame.EXP_PRIORITY_COLOR;
130                 break;
131             case Detector.LOW_PRIORITY:
132                 color = FindBugsFrame.LOW_PRIORITY_COLOR;
133                 break;
134             case Detector.NORMAL_PRIORITY:
135                 color = FindBugsFrame.NORMAL_PRIORITY_COLOR;
136                 break;
137             case Detector.HIGH_PRIORITY:
138                 color = FindBugsFrame.HIGH_PRIORITY_COLOR;
139                 break;
140             }
141         }
142         return color;
143     }
144 }
Popular Tags