001: /*
002: * FindBugs - Find Bugs in Java programs
003: * Copyright (C) 2006, University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
018: */
019:
020: package edu.umd.cs.findbugs.gui2;
021:
022: import java.awt.BorderLayout;
023: import java.awt.Color;
024: import java.awt.Component;
025: import java.awt.Insets;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.MouseAdapter;
029: import java.awt.event.MouseEvent;
030: import java.awt.event.WindowAdapter;
031: import java.awt.event.WindowEvent;
032: import java.util.ArrayList;
033:
034: import javax.swing.Box;
035: import javax.swing.BoxLayout;
036: import javax.swing.JButton;
037: import javax.swing.JComboBox;
038: import javax.swing.JLabel;
039: import javax.swing.JList;
040: import javax.swing.JPanel;
041: import javax.swing.JScrollPane;
042: import javax.swing.ListCellRenderer;
043:
044: import edu.umd.cs.findbugs.gui2.BugAspects.SortableValue;
045:
046: /**
047: *
048: * Lets you choose your new filter, shouldn't let you choose filters that wouldn't filter anything out
049: * including filters that you already have
050: *
051: */
052: @SuppressWarnings("serial")
053: public class NewFilterFrame extends FBDialog {
054:
055: private JList list = new JList();
056:
057: private static NewFilterFrame instance = null;
058:
059: public static void open() {
060: if (instance == null) {
061: instance = new NewFilterFrame();
062: instance.setVisible(true);
063: instance.toFront();
064: }
065: }
066:
067: public static void close() {
068: if (instance != null) {
069: instance.dispose();
070: instance = null;
071: }
072: }
073:
074: private NewFilterFrame() {
075: super (PreferencesFrame.getInstance());
076: setContentPane(new JPanel() {
077: @Override
078: public Insets getInsets() {
079: return new Insets(3, 3, 3, 3);
080: }
081: });
082: setLayout(new BorderLayout());
083:
084: JPanel north = new JPanel();
085: north.setLayout(new BoxLayout(north, BoxLayout.X_AXIS));
086: north.add(Box.createHorizontalStrut(3));
087: north.add(new JLabel(edu.umd.cs.findbugs.L10N.getLocalString(
088: "dlg.filter_bugs_lbl", "Filter out all bugs whose")
089: + " "));
090:
091: //Argh divider
092: Sortables[] valuesWithoutDivider = new Sortables[Sortables
093: .values().length - 1];
094: int index = 0;
095:
096: for (int x = 0; x < Sortables.values().length; x++) {
097: if (Sortables.values()[x] != Sortables.DIVIDER) {
098: valuesWithoutDivider[index] = Sortables.values()[x];
099: index++;
100: }
101: }
102:
103: final JComboBox comboBox = new JComboBox(valuesWithoutDivider);
104: comboBox.setRenderer(new ListCellRenderer() {
105: final Color SELECTED_BACKGROUND = new Color(183, 184, 204);
106:
107: public Component getListCellRendererComponent(JList list,
108: Object value, int index, boolean isSelected,
109: boolean cellHasFocus) {
110: JLabel result = new JLabel();
111: result.setFont(result.getFont().deriveFont(
112: Driver.getFontSize()));
113: result.setOpaque(true);
114: result.setText(value.toString().toLowerCase());
115: if (isSelected)
116: result.setBackground(SELECTED_BACKGROUND);
117: return result;
118: }
119: });
120: comboBox.addActionListener(new ActionListener() {
121: public void actionPerformed(ActionEvent evt) {
122: Sortables filterBy = (Sortables) comboBox
123: .getSelectedItem();
124: String[] listData = filterBy.getAllSorted();
125: for (int i = 0; i < listData.length; i++)
126: listData[i] = filterBy.formatValue(listData[i]);
127: list.setListData(listData);
128: }
129: });
130: comboBox.validate();
131: north.add(comboBox);
132: north.add(new JLabel(" "
133: + edu.umd.cs.findbugs.L10N.getLocalString("dlg.is",
134: "is") + " "));
135: String[] filterModes = {
136: edu.umd.cs.findbugs.L10N.getLocalString(
137: "mode.equal_to", "equal to"),
138: edu.umd.cs.findbugs.L10N.getLocalString(
139: "mode.at_or_after", "at or after"),
140: edu.umd.cs.findbugs.L10N.getLocalString(
141: "mode.at_or_before", "at or before") };
142: final JComboBox filterModeComboBox = new JComboBox(filterModes);
143: north.add(filterModeComboBox);
144: north.add(new JLabel(":"));
145: north.add(Box.createHorizontalGlue());
146: JPanel south = new JPanel();
147: JButton okButton = new JButton(edu.umd.cs.findbugs.L10N
148: .getLocalString("dlg.ok_btn", "OK"));
149: okButton.addActionListener(new ActionListener() {
150: public void actionPerformed(ActionEvent evt) {
151: Sortables key = (Sortables) comboBox.getSelectedItem();
152: String[] values = key.getAllSorted();
153:
154: ArrayList<SortableValue> filterMe = new ArrayList<SortableValue>();
155: for (int i : list.getSelectedIndices()) {
156: filterMe.add(new BugAspects.SortableValue(key,
157: values[i]));
158: }
159: MainFrame.getInstance().getProject()
160: .getSuppressionFilter().addChild(
161: FilterFactory.makeOrMatcher(filterMe));
162: FilterActivity.notifyListeners(
163: FilterListener.Action.FILTERING, null);
164: PreferencesFrame.getInstance().updateFilterPanel();
165: MainFrame.getInstance().setProjectChanged(true);
166: close();
167: }
168: });
169: JButton cancelButton = new JButton(edu.umd.cs.findbugs.L10N
170: .getLocalString("dlg.cancel_btn", "Cancel"));
171: cancelButton.addActionListener(new ActionListener() {
172: public void actionPerformed(ActionEvent evt) {
173: close();
174: }
175: });
176: Util.addOkAndCancelButtons(south, okButton, cancelButton);
177:
178: list.addMouseListener(new MouseAdapter() {
179: @Override
180: public void mouseClicked(MouseEvent evt) {
181: if (evt.getClickCount() == 2) {
182: // Dupe from OK button's ActionListener
183: Sortables key = (Sortables) comboBox
184: .getSelectedItem();
185: String[] values = key.getAllSorted();
186: // for (int i : list.getSelectedIndices())
187: // {
188: // FilterMatcher fm=new FilterMatcher(key,values[i]);
189: // if (!ProjectSettings.getInstance().getAllMatchers().contains(fm))
190: // ProjectSettings.getInstance().addFilter(fm);
191: // }
192: FilterMatcher[] newFilters = new FilterMatcher[list
193: .getSelectedIndices().length];
194: for (int i = 0; i < newFilters.length; i++)
195: newFilters[i] = new FilterMatcher(key,
196: values[list.getSelectedIndices()[i]]);
197: ProjectSettings.getInstance()
198: .addFilters(newFilters);
199: PreferencesFrame.getInstance().updateFilterPanel();
200: close();
201: }
202: }
203: });
204:
205: add(north, BorderLayout.NORTH);
206: add(Box.createHorizontalStrut(2), BorderLayout.WEST);
207: add(new JScrollPane(list), BorderLayout.CENTER);
208: add(Box.createHorizontalStrut(2), BorderLayout.EAST);
209: add(south, BorderLayout.SOUTH);
210:
211: // Populate the box with initial values
212: comboBox.getActionListeners()[0].actionPerformed(null);
213:
214: setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
215: addWindowListener(new WindowAdapter() {
216: @Override
217: public void windowClosing(WindowEvent arg0) {
218: close();
219: }
220: });
221:
222: setTitle(edu.umd.cs.findbugs.L10N.getLocalString(
223: "dlg.create_new_filter_ttl", "Create New Filter"));
224: pack();
225: }
226:
227: public static void main(String[] args) {
228: new NewFilterFrame().setDefaultCloseOperation(EXIT_ON_CLOSE);
229: }
230: }
|