📄 findbugsframe.java
字号:
/* * FindBugs - Find bugs in Java programs * Copyright (C) 2003,2004 University of Maryland * Copyright (C) 2004 Dave Brosius <dbrosius@users.sourceforge.net> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* * FindBugsFrame.java * * Created on March 30, 2003, 12:05 PM */package edu.umd.cs.findbugs.gui;import java.lang.reflect.Method;import java.awt.*;import java.io.*;import java.util.*;import java.util.List;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeEvent;import javax.swing.*;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;import javax.swing.filechooser.FileFilter;import javax.swing.text.*;import javax.swing.tree.*;import java.text.MessageFormat;import java.awt.event.KeyEvent;import java.awt.datatransfer.*;import edu.umd.cs.findbugs.*;import edu.umd.cs.findbugs.ba.SourceFinder;import edu.umd.cs.findbugs.config.ProjectFilterSettings;import edu.umd.cs.findbugs.config.UserPreferences;/** * The main GUI frame for FindBugs. * * @author David Hovemeyer */public class FindBugsFrame extends javax.swing.JFrame { /* ---------------------------------------------------------------------- * Helper classes * ---------------------------------------------------------------------- */ private static final Color HIGH_PRIORITY_COLOR = new Color(0xff0000); private static final Color NORMAL_PRIORITY_COLOR = new Color(0x9f0000); private static final Color LOW_PRIORITY_COLOR = Color.BLACK; private static final Color EXP_PRIORITY_COLOR = Color.BLACK; /** * Custom cell renderer for the bug tree. * We use this to select the tree icons, and to set the * text color based on the bug priority. */ private static class BugCellRenderer extends DefaultTreeCellRenderer { private ImageIcon bugGroupIcon; private ImageIcon packageIcon; private ImageIcon bugIcon; private ImageIcon classIcon; private ImageIcon methodIcon; private ImageIcon fieldIcon; private ImageIcon sourceFileIcon; private Object value; public BugCellRenderer() { ClassLoader classLoader = this.getClass().getClassLoader(); bugGroupIcon = new ImageIcon(classLoader.getResource("edu/umd/cs/findbugs/gui/bug.png")); packageIcon = new ImageIcon(classLoader.getResource("edu/umd/cs/findbugs/gui/package.png")); bugIcon = new ImageIcon(classLoader.getResource("edu/umd/cs/findbugs/gui/bug2.png")); classIcon = new ImageIcon(classLoader.getResource("edu/umd/cs/findbugs/gui/class.png")); methodIcon = new ImageIcon(classLoader.getResource("edu/umd/cs/findbugs/gui/method.png")); fieldIcon = new ImageIcon(classLoader.getResource("edu/umd/cs/findbugs/gui/field.png")); sourceFileIcon = new ImageIcon(classLoader.getResource("edu/umd/cs/findbugs/gui/sourcefile.png")); } public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; Object obj = node.getUserObject(); this.value = obj; super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); // Set the icon, depending on what kind of node it is if (obj instanceof BugInstance) { setIcon(bugIcon); } else if (obj instanceof ClassAnnotation) { setIcon(classIcon); } else if (obj instanceof MethodAnnotation) { setIcon(methodIcon); } else if (obj instanceof FieldAnnotation) { setIcon(fieldIcon); } else if (obj instanceof SourceLineAnnotation) { setIcon(sourceFileIcon); } else if (obj instanceof BugInstanceGroup) { // This is a "group" node BugInstanceGroup groupNode = (BugInstanceGroup) obj; String groupType = groupNode.getGroupType(); if (groupType == GROUP_BY_CLASS) { setIcon(classIcon); } else if (groupType == GROUP_BY_PACKAGE) { setIcon(packageIcon); } else if (groupType == GROUP_BY_BUG_TYPE) { setIcon(bugGroupIcon); } } else { setIcon(null); } return this; } public Color getTextNonSelectionColor() { return getCellTextColor(); } private Color getCellTextColor() { // Based on the priority, color-code the bug instance. Color color = Color.BLACK; if (value instanceof BugInstance) { BugInstance bugInstance = (BugInstance) value; switch (bugInstance.getPriority()) { case Detector.EXP_PRIORITY: color = EXP_PRIORITY_COLOR; break; case Detector.LOW_PRIORITY: color = LOW_PRIORITY_COLOR; break; case Detector.NORMAL_PRIORITY: color = NORMAL_PRIORITY_COLOR; break; case Detector.HIGH_PRIORITY: color = HIGH_PRIORITY_COLOR; break; } } return color; } } /** * The instance of BugCellRenderer. */ private static final FindBugsFrame.BugCellRenderer bugCellRenderer = new FindBugsFrame.BugCellRenderer(); /** * Tree node type for BugInstances. * We use this instead of plain DefaultMutableTreeNodes in order to * get more control over the exact text that is shown in the tree. */ private class BugTreeNode extends DefaultMutableTreeNode { private int count; public BugTreeNode(BugInstance bugInstance) { super(bugInstance); count = -1; } public void setCount(int count) { this.count = count; } public String toString() { try { BugInstance bugInstance = (BugInstance) getUserObject(); StringBuffer result = new StringBuffer(); if (count >= 0) { result.append(count); result.append(": "); } if (bugInstance.isExperimental()) result.append(L10N.getLocalString("msg.exp_txt", "EXP: ")); result.append(fullDescriptionsItem.isSelected() ? bugInstance.getMessage() : bugInstance.toString()); return result.toString(); } catch (Exception e) { return MessageFormat.format(L10N.getLocalString("msg.errorformatting_txt", "Error formatting message for bug: "), new Object[]{e.toString()}); } } } /** * Compare BugInstance class names. * This is useful for grouping bug instances by class. * Note that all instances with the same class name will compare * as equal. */ private static class BugInstanceClassComparator implements Comparator<BugInstance> { public int compare(BugInstance lhs, BugInstance rhs) { return lhs.getPrimaryClass().compareTo(rhs.getPrimaryClass()); } } /** * The instance of BugInstanceClassComparator. */ private static final Comparator<BugInstance> bugInstanceClassComparator = new BugInstanceClassComparator(); /** * Compare BugInstance package names. * This is useful for grouping bug instances by package. * Note that all instances with the same package name will compare * as equal. */ private static class BugInstancePackageComparator implements Comparator<BugInstance> { public int compare(BugInstance lhs, BugInstance rhs) { return lhs.getPrimaryClass().getPackageName().compareTo(rhs.getPrimaryClass().getPackageName()); } } /** * The instance of BugInstancePackageComparator. */ private static final Comparator<BugInstance> bugInstancePackageComparator = new BugInstancePackageComparator(); /** * Compare BugInstance bug types. * This is useful for grouping bug instances by bug type. * Note that all instances with the same bug type will compare * as equal. */ private static class BugInstanceTypeComparator implements Comparator<BugInstance> { public int compare(BugInstance lhs, BugInstance rhs) { String lhsString = lhs.toString(); String rhsString = rhs.toString(); return lhsString.substring(0, lhsString.indexOf(':')).compareTo(rhsString.substring(0, rhsString.indexOf(':'))); } } /** * The instance of BugInstanceTypeComparator. */ private static final Comparator<BugInstance> bugInstanceTypeComparator = new BugInstanceTypeComparator(); /** * Two-level comparison of bug instances by class name and * BugInstance natural ordering. */ private static class BugInstanceByClassComparator implements Comparator<BugInstance> { public int compare(BugInstance a, BugInstance b) { int cmp = bugInstanceClassComparator.compare(a, b); if (cmp != 0) return cmp; return a.compareTo(b); } } /** * The instance of BugInstanceByClassComparator. */ private static final Comparator<BugInstance> bugInstanceByClassComparator = new FindBugsFrame.BugInstanceByClassComparator(); /** * Two-level comparison of bug instances by package and * BugInstance natural ordering. */ private static class BugInstanceByPackageComparator implements Comparator<BugInstance> { public int compare(BugInstance a, BugInstance b) { int cmp = bugInstancePackageComparator.compare(a, b); if (cmp != 0) return cmp; return a.compareTo(b); } } /** * The instance of BugInstanceByPackageComparator. */ private static final Comparator<BugInstance> bugInstanceByPackageComparator = new FindBugsFrame.BugInstanceByPackageComparator(); /** * Two-level comparison of bug instances by bug type and * BugInstance natural ordering. */ private static class BugInstanceByTypeComparator implements Comparator<BugInstance> { public int compare(BugInstance a, BugInstance b) { int cmp = bugInstanceTypeComparator.compare(a, b); if (cmp != 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -