KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.GridBagConstraints JavaDoc;
26 import java.awt.GridBagLayout JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
30 import java.awt.event.MouseAdapter JavaDoc;
31 import java.awt.event.MouseEvent JavaDoc;
32 import java.awt.event.MouseListener JavaDoc;
33 import java.awt.event.WindowAdapter JavaDoc;
34 import java.awt.event.WindowEvent JavaDoc;
35 import java.awt.event.WindowListener JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Arrays JavaDoc;
38
39 import javax.swing.Box JavaDoc;
40 import javax.swing.BoxLayout JavaDoc;
41 import javax.swing.ComboBoxModel JavaDoc;
42 import javax.swing.JButton JavaDoc;
43 import javax.swing.JComboBox JavaDoc;
44 import javax.swing.JFrame JavaDoc;
45 import javax.swing.JLabel JavaDoc;
46 import javax.swing.JList JavaDoc;
47 import javax.swing.JPanel JavaDoc;
48 import javax.swing.JScrollPane JavaDoc;
49 import javax.swing.ListCellRenderer JavaDoc;
50
51 import edu.umd.cs.findbugs.gui2.BugAspects.StringPair;
52
53 /**
54  *
55  * Lets you choose your new filter, shouldn't let you choose filters that wouldn't filter anything out
56  * including filters that you already have
57  *
58  */

59 @SuppressWarnings JavaDoc("serial")
60 public class NewFilterFrame extends FBDialog
61 {
62     
63     private JList JavaDoc list = new JList JavaDoc();
64     
65     private static NewFilterFrame instance;
66     public static void open()
67     {
68         if (instance == null)
69         {
70             instance = new NewFilterFrame();
71             instance.setVisible(true);
72         }
73         else
74         {
75             instance.toFront();
76         }
77     }
78     public static void close()
79     {
80         instance.dispose();
81         instance = null;
82     }
83     
84     private NewFilterFrame()
85     {
86         super(PreferencesFrame.getInstance());
87         setContentPane(new JPanel JavaDoc()
88         {
89             public Insets JavaDoc getInsets()
90             {
91                 return new Insets JavaDoc(3, 3, 3, 3);
92             }
93         });
94         setLayout(new BorderLayout JavaDoc());
95         
96         JPanel JavaDoc north = new JPanel JavaDoc();
97         north.setLayout(new BoxLayout JavaDoc(north, BoxLayout.X_AXIS));
98         north.add(Box.createHorizontalStrut(3));
99         north.add(new JLabel JavaDoc(edu.umd.cs.findbugs.L10N.getLocalString("dlg.filter_bugs_lbl", "Filter out all bugs whose") + " "));
100         
101         //Argh divider
102
Sortables[] valuesWithoutDivider=new Sortables[Sortables.values().length-1];
103         int index=0;
104
105         for (int x=0; x<Sortables.values().length;x++)
106         {
107             if (Sortables.values()[x]!=Sortables.DIVIDER)
108             {
109                 valuesWithoutDivider[index]=Sortables.values()[x];
110                 index++;
111             }
112         }
113         
114         final JComboBox JavaDoc comboBox = new JComboBox JavaDoc(valuesWithoutDivider);
115         comboBox.setRenderer(new ListCellRenderer JavaDoc()
116         {
117             final Color JavaDoc SELECTED_BACKGROUND = new Color JavaDoc(183, 184, 204);
118             
119             public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus)
120             {
121                 JLabel JavaDoc result = new JLabel JavaDoc();
122                 result.setFont(result.getFont().deriveFont(Driver.getFontSize()));
123                 result.setOpaque(true);
124                 result.setText(value.toString().toLowerCase());
125                 if (isSelected)
126                     result.setBackground(SELECTED_BACKGROUND);
127                 return result;
128             }
129         });
130         comboBox.addActionListener(new ActionListener JavaDoc()
131         {
132             public void actionPerformed(ActionEvent JavaDoc evt)
133             {
134                 Sortables filterBy = (Sortables) comboBox.getSelectedItem();
135                 String JavaDoc[] listData = filterBy.getAllSorted();
136                 for (int i = 0; i < listData.length; i++)
137                     listData[i] = filterBy.formatValue(listData[i]);
138                 list.setListData(listData);
139             }
140         });
141         comboBox.validate();
142         north.add(comboBox);
143         north.add(new JLabel JavaDoc(" " + edu.umd.cs.findbugs.L10N.getLocalString("dlg.is", "is") + " "));
144         String JavaDoc[] filterModes = {edu.umd.cs.findbugs.L10N.getLocalString("mode.equal_to", "equal to"), edu.umd.cs.findbugs.L10N.getLocalString("mode.at_or_after", "at or after"), edu.umd.cs.findbugs.L10N.getLocalString("mode.at_or_before", "at or before")};
145         final JComboBox JavaDoc filterModeComboBox = new JComboBox JavaDoc(filterModes);
146         north.add(filterModeComboBox);
147         north.add(new JLabel JavaDoc(":"));
148         north.add(Box.createHorizontalGlue());
149         JPanel JavaDoc south = new JPanel JavaDoc();
150         JButton JavaDoc okButton = new JButton JavaDoc(edu.umd.cs.findbugs.L10N.getLocalString("dlg.ok_btn", "OK"));
151         okButton.addActionListener(new ActionListener JavaDoc()
152         {
153             public void actionPerformed(ActionEvent JavaDoc evt)
154             {
155                 Sortables key = (Sortables) comboBox.getSelectedItem();
156                 String JavaDoc[] values = key.getAllSorted();
157 // for (int i : list.getSelectedIndices())
158
// {
159
// FilterMatcher fm=new FilterMatcher(key,values[i]);
160
// if (!ProjectSettings.getInstance().getAllMatchers().contains(fm))
161
// ProjectSettings.getInstance().addFilter(fm);
162
// }
163
FilterMatcher[] newFilters = new FilterMatcher[list.getSelectedIndices().length];
164                 for (int i = 0; i < newFilters.length; i++)
165                     newFilters[i] = new FilterMatcher(key, values[list.getSelectedIndices()[i]], filterModeComboBox.getSelectedIndex());
166                 ProjectSettings.getInstance().addFilters(newFilters);
167                 PreferencesFrame.getInstance().updateFilterPanel();
168                 MainFrame.getInstance().setProjectChanged(newFilters.length > 0);
169                 close();
170             }
171         });
172         JButton JavaDoc cancelButton = new JButton JavaDoc(edu.umd.cs.findbugs.L10N.getLocalString("dlg.cancel_btn", "Cancel"));
173         cancelButton.addActionListener(new ActionListener JavaDoc()
174         {
175             public void actionPerformed(ActionEvent JavaDoc evt)
176             {
177                 close();
178             }
179         });
180         south.setLayout(new BoxLayout JavaDoc(south, BoxLayout.X_AXIS));
181         south.add(Box.createHorizontalGlue());
182         south.add(okButton);
183         south.add(Box.createHorizontalStrut(2));
184         south.add(cancelButton);
185         south.add(Box.createHorizontalStrut(3));
186         
187         list.addMouseListener(new MouseAdapter JavaDoc()
188         {
189             public void mouseClicked(MouseEvent JavaDoc evt)
190             {
191                 if (evt.getClickCount() == 2)
192                 {
193                     // Dupe from OK button's ActionListener
194
Sortables key = (Sortables) comboBox.getSelectedItem();
195                     String JavaDoc[] values = key.getAllSorted();
196 // for (int i : list.getSelectedIndices())
197
// {
198
// FilterMatcher fm=new FilterMatcher(key,values[i]);
199
// if (!ProjectSettings.getInstance().getAllMatchers().contains(fm))
200
// ProjectSettings.getInstance().addFilter(fm);
201
// }
202
FilterMatcher[] newFilters = new FilterMatcher[list.getSelectedIndices().length];
203                     for (int i = 0; i < newFilters.length; i++)
204                         newFilters[i] = new FilterMatcher(key, values[list.getSelectedIndices()[i]]);
205                     ProjectSettings.getInstance().addFilters(newFilters);
206                     PreferencesFrame.getInstance().updateFilterPanel();
207                     close();
208                 }
209             }
210         });
211         
212         add(north, BorderLayout.NORTH);
213         add(Box.createHorizontalStrut(2), BorderLayout.WEST);
214         add(new JScrollPane JavaDoc(list), BorderLayout.CENTER);
215         add(Box.createHorizontalStrut(2), BorderLayout.EAST);
216         add(south, BorderLayout.SOUTH);
217         
218         // Populate the box with initial values
219
comboBox.getActionListeners()[0].actionPerformed(null);
220         
221         setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
222         addWindowListener(new WindowAdapter JavaDoc()
223         {
224             @Override JavaDoc
225             public void windowClosing(WindowEvent JavaDoc arg0)
226             {
227                 close();
228             }
229         });
230         
231         setTitle(edu.umd.cs.findbugs.L10N.getLocalString("dlg.create_new_filter_ttl", "Create New Filter"));
232         pack();
233     }
234     
235     public static void main(String JavaDoc[] args)
236     {
237         new NewFilterFrame().setDefaultCloseOperation(EXIT_ON_CLOSE);
238     }
239 }
240
Popular Tags