KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003,2004 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
21 /*
22  * ConfigureDetectorsDialog.java
23  *
24  * Created on June 3, 2003, 3:52 PM
25  */

26
27 package edu.umd.cs.findbugs.gui;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javax.swing.ListSelectionModel JavaDoc;
34 import javax.swing.event.ListSelectionEvent JavaDoc;
35 import javax.swing.event.ListSelectionListener JavaDoc;
36 import javax.swing.table.DefaultTableModel JavaDoc;
37 import javax.swing.table.TableModel JavaDoc;
38
39 import edu.umd.cs.findbugs.BugPattern;
40 import edu.umd.cs.findbugs.DetectorFactory;
41 import edu.umd.cs.findbugs.DetectorFactoryCollection;
42 import edu.umd.cs.findbugs.L10N;
43 import edu.umd.cs.findbugs.config.UserPreferences;
44
45 /**
46  * Configure Detectors by enabling/disabling them.
47  *
48  * @author David Hovemeyer
49  */

50 public class ConfigureDetectorsDialog extends javax.swing.JDialog JavaDoc {
51     private static final long serialVersionUID = 1L;
52
53     private static final int SPEED_COLUMN = 1;
54     private static final int ENABLED_COLUMN = 2;
55
56     /**
57      * Creates new form ConfigureDetectorsDialog
58      */

59     public ConfigureDetectorsDialog(java.awt.Frame JavaDoc parent, boolean modal) {
60         super(parent, modal);
61         initComponents();
62         postInitComponents();
63     }
64
65     /**
66      * This method is called from within the constructor to
67      * initialize the form.
68      * WARNING: Do NOT modify this code. The content of this method is
69      * always regenerated by the Form Editor.
70      */

71     private void initComponents() {//GEN-BEGIN:initComponents
72
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
73
74         detectorTableScrollPane = new javax.swing.JScrollPane JavaDoc();
75         detectorTable = new javax.swing.JTable JavaDoc();
76         detectorDescriptionScrollPane = new javax.swing.JScrollPane JavaDoc();
77         detectorDescription = new javax.swing.JEditorPane JavaDoc();
78         jSeparator1 = new javax.swing.JSeparator JavaDoc();
79         okButton = new javax.swing.JButton JavaDoc();
80         cancelButton = new javax.swing.JButton JavaDoc();
81         spacer = new javax.swing.JLabel JavaDoc();
82         restoreDefaultsButton = new javax.swing.JButton JavaDoc();
83
84         getContentPane().setLayout(new java.awt.GridBagLayout JavaDoc());
85
86         setTitle("Configure Detectors");
87         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
88             @Override JavaDoc
89             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
90                 closeDialog(evt);
91             }
92             @Override JavaDoc
93             public void windowOpened(java.awt.event.WindowEvent JavaDoc evt) {
94                 formWindowOpened(evt);
95             }
96         });
97
98         detectorTableScrollPane.setBorder(new javax.swing.border.BevelBorder JavaDoc(javax.swing.border.BevelBorder.LOWERED));
99         detectorTable.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
100             new Object JavaDoc [][] {
101
102             },
103             new String JavaDoc [] {
104                 "Bug Detector", "Speed", "Enabled"
105             }
106         ) {
107             private static final long serialVersionUID = 1L;
108             Class JavaDoc[] types = new Class JavaDoc [] {
109                 java.lang.String JavaDoc.class, java.lang.String JavaDoc.class, java.lang.Boolean JavaDoc.class
110             };
111             boolean[] canEdit = new boolean [] {
112                 false, false, true
113             };
114
115             @Override JavaDoc
116             public Class JavaDoc<?> getColumnClass(int columnIndex) {
117                 return types [columnIndex];
118             }
119
120             @Override JavaDoc
121             public boolean isCellEditable(int rowIndex, int columnIndex) {
122                 return canEdit [columnIndex];
123             }
124         });
125         populateTable();
126         detectorTable.getColumnModel().getColumn(ENABLED_COLUMN).setMaxWidth(60);
127         detectorTable.getColumnModel().getColumn(SPEED_COLUMN).setMaxWidth(60);
128         detectorTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
129
130         {
131             DefaultTableModel JavaDoc m = (DefaultTableModel JavaDoc)detectorTable.getModel();
132             m.setColumnIdentifiers( new String JavaDoc[]
133                 {
134                     L10N.getLocalString("dlg.bugdetector_lbl", "Bug Detector"),
135                     L10N.getLocalString("dlg.speed_lbl", "Speed"),
136                     L10N.getLocalString("dlg.enabled_lbl", "Enabled"),
137                 });
138
139                 DefaultSortedTableModel sortedModel = new DefaultSortedTableModel(m, detectorTable.getTableHeader());
140                 detectorTable.setModel(sortedModel);
141             }
142
143             detectorTableScrollPane.setViewportView(detectorTable);
144
145             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
146             gridBagConstraints.gridwidth = 4;
147             gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
148             gridBagConstraints.weightx = 1.0;
149             gridBagConstraints.weighty = 0.8;
150             gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 2, 6);
151             getContentPane().add(detectorTableScrollPane, gridBagConstraints);
152
153             detectorDescriptionScrollPane.setBorder(new javax.swing.border.BevelBorder JavaDoc(javax.swing.border.BevelBorder.LOWERED));
154             detectorDescriptionScrollPane.setPreferredSize(new java.awt.Dimension JavaDoc(110, 120));
155             detectorDescriptionScrollPane.setViewportView(detectorDescription);
156
157             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
158             gridBagConstraints.gridx = 0;
159             gridBagConstraints.gridy = 1;
160             gridBagConstraints.gridwidth = 4;
161             gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
162             gridBagConstraints.weighty = 0.3;
163             gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 6, 2, 6);
164             getContentPane().add(detectorDescriptionScrollPane, gridBagConstraints);
165
166             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
167             gridBagConstraints.gridx = 0;
168             gridBagConstraints.gridy = 2;
169             gridBagConstraints.gridwidth = 4;
170             gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
171             gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 0, 3, 0);
172             getContentPane().add(jSeparator1, gridBagConstraints);
173
174             okButton.setMnemonic('O');
175             okButton.setText("OK");
176             okButton.setText(L10N.getLocalString("dlg.ok_btn","OK"));
177             okButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
178                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
179                     okButtonActionPerformed(evt);
180                 }
181             });
182
183             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
184             gridBagConstraints.gridx = 2;
185             gridBagConstraints.gridy = 3;
186             gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 4, 2);
187             getContentPane().add(okButton, gridBagConstraints);
188
189             cancelButton.setMnemonic('C');
190             cancelButton.setText("Cancel");
191             cancelButton.setText(L10N.getLocalString("dlg.cancel_btn", "Cancel"));
192             cancelButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
193                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
194                     cancelButtonActionPerformed(evt);
195                 }
196             });
197
198             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
199             gridBagConstraints.gridx = 3;
200             gridBagConstraints.gridy = 3;
201             gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 2, 4, 6);
202             getContentPane().add(cancelButton, gridBagConstraints);
203
204             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
205             gridBagConstraints.gridx = 1;
206             gridBagConstraints.gridy = 3;
207             gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
208             gridBagConstraints.weightx = 1.0;
209             getContentPane().add(spacer, gridBagConstraints);
210
211             restoreDefaultsButton.setText("Restore Defaults");
212             restoreDefaultsButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
213             restoreDefaultsButton.setText(L10N.getLocalString("dlg.restoredefaults_btn", "Restore Defaults"));
214             restoreDefaultsButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
215                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
216                     restoreDefaultsButtonActionPerformed(evt);
217                 }
218             });
219
220             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
221             gridBagConstraints.gridx = 0;
222             gridBagConstraints.gridy = 3;
223             gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 4, 0);
224             getContentPane().add(restoreDefaultsButton, gridBagConstraints);
225
226             pack();
227         }//GEN-END:initComponents
228

229     private void formWindowOpened(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_formWindowOpened
230
setTitle(L10N.getLocalString("dlg.configuredetectors_ttl", "Configure Detectors"));
231     }//GEN-LAST:event_formWindowOpened
232

233     /**
234      * reverts the selected state of all the detectors to their defaults as specified in the findbugs.xml file
235      *
236      * @param evt the swing event corresponding to the mouse click of the Restore Defaults button
237      */

238     private void restoreDefaultsButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_restoreDefaultsButtonActionPerformed
239
Iterator JavaDoc<DetectorFactory> i = DetectorFactoryCollection.instance().factoryIterator();
240         DefaultSortedTableModel sorter = (DefaultSortedTableModel) detectorTable.getModel();
241         TableModel JavaDoc model = sorter.getBaseTableModel();
242         int row = 0;
243         while (i.hasNext()) {
244             DetectorFactory factory = i.next();
245             if (factory.isHidden())
246                 continue;
247             model.setValueAt(factory.isDefaultEnabled() ? Boolean.TRUE : Boolean.FALSE, row++, ENABLED_COLUMN);
248         }
249     }//GEN-LAST:event_restoreDefaultsButtonActionPerformed
250

251     private void cancelButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_cancelButtonActionPerformed
252
closeDialog();
253     }//GEN-LAST:event_cancelButtonActionPerformed
254

255     private void okButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_okButtonActionPerformed
256
// Update new enabled/disabled status for the Detectors
257
int num = factoryList.size();
258         DefaultSortedTableModel sorter = (DefaultSortedTableModel) detectorTable.getModel();
259         TableModel JavaDoc model = sorter.getBaseTableModel();
260         for (int i = 0; i < num; ++i) {
261             DetectorFactory factory = factoryList.get(i);
262             Boolean JavaDoc enabled = (Boolean JavaDoc) model.getValueAt(i, ENABLED_COLUMN);
263             UserPreferences.getUserPreferences().enableDetector(
264                     factory, enabled.booleanValue());
265         }
266         closeDialog();
267     }//GEN-LAST:event_okButtonActionPerformed
268

269     /**
270      * Closes the dialog
271      */

272     private void closeDialog(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_closeDialog
273
setVisible(false);
274         dispose();
275     }//GEN-LAST:event_closeDialog
276

277     /**
278      * installs a list selection listener to populate the bottom details page based on selection changes in top grid.
279      * A conversion from the table sorter index to the base model index is done to get the correct details
280      */

281     private void postInitComponents() {
282         // Listen to detector table selections so we can (hopefully)
283
// display the description of the detector
284

285         ListSelectionModel JavaDoc rowSM = detectorTable.getSelectionModel();
286         rowSM.addListSelectionListener(new ListSelectionListener JavaDoc() {
287             public void valueChanged(ListSelectionEvent JavaDoc e) {
288                 if (e.getValueIsAdjusting()) return;
289
290                 ListSelectionModel JavaDoc lsm = (ListSelectionModel JavaDoc) e.getSource();
291                 if (!lsm.isSelectionEmpty()) {
292                     int selectedRow = lsm.getMinSelectionIndex();
293                     DefaultSortedTableModel sorter = (DefaultSortedTableModel) detectorTable.getModel();
294                     viewDetectorDetails(factoryList.get(sorter.getBaseModelIndex(selectedRow)));
295                 }
296             }
297         });
298     }
299
300     /**
301      * populates the bottom detector details pane based on the detector selected
302      *
303      * @param factory the detector that is currently selected
304      */

305     private void viewDetectorDetails(DetectorFactory factory) {
306         String JavaDoc detailHTML = factory.getDetailHTML();
307         if (detailHTML == null) {
308             detectorDescription.setText("");
309         } else {
310             detectorDescription.setContentType("text/html");
311             detectorDescription.setText(detailHTML);
312             StringBuffer JavaDoc toolTip = new StringBuffer JavaDoc(100);
313             toolTip.append("<html><body><b>");
314             toolTip.append(factory.getFullName());
315             toolTip.append("</b><br><br><table border='1' width='100%'><tr><th>");
316             toolTip.append(L10N.getLocalString("msg.bugpatternsreported_txt", "Bug Patterns Reported"));
317             toolTip.append("</th></tr>");
318             
319             Collection JavaDoc<BugPattern> patterns = factory.getReportedBugPatterns();
320             for (BugPattern pattern : patterns) {
321                 toolTip.append("<tr><td align='center'>");
322                 toolTip.append("[");
323                 toolTip.append(pattern.getAbbrev());
324                 toolTip.append("] ");
325                 toolTip.append(pattern.getType());
326                 toolTip.append("</td></tr>");
327             }
328             toolTip.append("</body></html>");
329             detectorDescription.setToolTipText(toolTip.toString());
330         }
331     }
332
333     /**
334      * populates the Detector JTable model with all available detectors
335      * Due to Netbeans form builder, populate table gets called before the tablesorter is installed,
336      * so it is correct for the model retrieved from the table to be assumed to be the base DefaultTableModel.
337      */

338     private void populateTable() {
339         Iterator JavaDoc<DetectorFactory> i = DetectorFactoryCollection.instance().factoryIterator();
340         while (i.hasNext()) {
341             DetectorFactory factory = i.next();
342             if (factory.isHidden())
343                 continue;
344             DefaultTableModel JavaDoc model = (DefaultTableModel JavaDoc) detectorTable.getModel();
345             model.addRow(new Object JavaDoc[]{
346                     factory.getShortName(),
347                     factory.getSpeed(),
348                     UserPreferences.getUserPreferences().isDetectorEnabled(factory)
349                         ? Boolean.TRUE : Boolean.FALSE
350                     });
351             factoryList.add(factory);
352         }
353     }
354
355     private void closeDialog() {
356         setVisible(false);
357         dispose();
358     }
359
360     /**
361      * @param args the command line arguments
362      */

363     public static void main(String JavaDoc args[]) {
364         new ConfigureDetectorsDialog(new javax.swing.JFrame JavaDoc(), true).setVisible(true);
365     }
366
367
368     // Variables declaration - do not modify//GEN-BEGIN:variables
369
private javax.swing.JButton JavaDoc cancelButton;
370     private javax.swing.JEditorPane JavaDoc detectorDescription;
371     private javax.swing.JScrollPane JavaDoc detectorDescriptionScrollPane;
372     private javax.swing.JTable JavaDoc detectorTable;
373     private javax.swing.JScrollPane JavaDoc detectorTableScrollPane;
374     private javax.swing.JSeparator JavaDoc jSeparator1;
375     private javax.swing.JButton JavaDoc okButton;
376     private javax.swing.JButton JavaDoc restoreDefaultsButton;
377     private javax.swing.JLabel JavaDoc spacer;
378     // End of variables declaration//GEN-END:variables
379

380     // My variables
381
private ArrayList JavaDoc<DetectorFactory> factoryList = new ArrayList JavaDoc<DetectorFactory>();
382 }
383
Popular Tags