KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 import javax.swing.JButton JavaDoc;
28 import javax.swing.JCheckBox JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.JScrollPane JavaDoc;
32 import javax.swing.event.ChangeEvent JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
34 import javax.swing.event.TableColumnModelEvent JavaDoc;
35 import javax.swing.event.TableColumnModelListener JavaDoc;
36 import javax.swing.table.JTableHeader JavaDoc;
37
38 /**
39  * This is the window that pops up when the user double clicks on the sorting table
40  * Its also available from the menu if they remove all Sortables.
41  *
42  * The user can choose what Sortables he wants to sort by,
43  * sort them into the order he wants to see
44  * and then click apply to move
45  * his choices onto the sorting table
46  * @author Dan
47  *
48  */

49 public class SorterDialog extends FBDialog {
50
51     private JLabel JavaDoc previewLabel=new JLabel JavaDoc("Preview:");
52     private JTableHeader JavaDoc preview;
53     private ArrayList JavaDoc<JCheckBox JavaDoc> checkBoxSortList = new ArrayList JavaDoc<JCheckBox JavaDoc>();
54     private CheckBoxList chBList;
55     JButton JavaDoc sortApply;
56     private static SorterDialog instance;
57     
58     public static SorterDialog getInstance()
59     {
60         if (instance==null)
61             instance=new SorterDialog();
62         return instance;
63     }
64     
65     @Override JavaDoc
66     public void setVisible(boolean visible)
67     {
68         super.setVisible(visible);
69         
70         if (visible)
71             ((SorterTableColumnModel)(preview.getColumnModel())).createFrom(MainFrame.getInstance().getSorter());
72     }
73     
74     private SorterDialog()
75     {
76         setTitle("Sort By...");
77         add(createSorterPane());
78         pack();
79         setLocationByPlatform(true);
80         setResizable(false);
81         preview.setColumnModel(new SorterTableColumnModel(MainFrame.getInstance().getSorter().getOrder()));
82     }
83     /**
84      * Creates JPanel with checkboxes of different things to
85      * sort by. List is: priority, class, package, category,
86      * bugcode, status, and type.
87      * @return
88      */

89     private JPanel JavaDoc createSorterPane() {
90         JPanel JavaDoc sorter = new JPanel JavaDoc();
91         JPanel JavaDoc insidePanel = new JPanel JavaDoc();
92         insidePanel.setLayout(new BorderLayout JavaDoc());
93         sorter.setLayout(new BorderLayout JavaDoc());
94         preview=new JTableHeader JavaDoc();
95         preview.setColumnModel(new SorterTableColumnModel(Sortables.values()));
96         
97         Sortables[] sortList = Sortables.values();
98         
99         for(Sortables s : Sortables.values()){
100             if (s == Sortables.DIVIDER)
101                 checkBoxSortList.add(new JCheckBox JavaDoc(edu.umd.cs.findbugs.L10N.getLocalString("sort.divider", "[divider]")));
102             else
103                 checkBoxSortList.add(new JCheckBox JavaDoc(s.toString()));
104         }
105         
106         setSorterCheckBoxes();
107         
108         for(int i = 0; i < sortList.length; i++){
109             checkBoxSortList.get(i).addChangeListener(new CheckBoxChangedListener(i));
110         }
111         
112         chBList = new CheckBoxList(checkBoxSortList.toArray(
113                 new JCheckBox JavaDoc[checkBoxSortList.size()]));
114                 
115         insidePanel.add(chBList, BorderLayout.NORTH);
116         
117         //insidePanel.add(sorterInfoLabel(), BorderLayout.CENTER);
118

119         
120         JPanel JavaDoc bottomPanel=new JPanel JavaDoc();
121         bottomPanel.setLayout(new BorderLayout JavaDoc());
122         //bottomPanel.add(previewLabel,BorderLayout.NORTH);
123
bottomPanel.add(preview, BorderLayout.CENTER);
124
125         insidePanel.add(bottomPanel,BorderLayout.SOUTH);
126         
127         
128         sortApply=new JButton JavaDoc(edu.umd.cs.findbugs.L10N.getLocalString("dlg.apply_btn", "Apply"));
129         sortApply.addActionListener(new ActionListener JavaDoc(){
130             public void actionPerformed(ActionEvent JavaDoc e)
131             {
132                 MainFrame.getInstance().getSorter().createFrom((SorterTableColumnModel)preview.getColumnModel());
133                 ((BugTreeModel)MainFrame.getInstance().getTree().getModel()).checkSorter();
134                 instance.setVisible(false); //close window
135
}
136         });
137         bottomPanel.add(sortApply,BorderLayout.SOUTH);
138         sorter.add(new JScrollPane JavaDoc(insidePanel), BorderLayout.CENTER);
139         
140         return sorter;
141     }
142     
143     private class CheckBoxChangedListener implements ChangeListener JavaDoc{
144
145         int indexOfCheckBox;
146         
147         public CheckBoxChangedListener(int index){
148             indexOfCheckBox = index;
149         }
150         
151         public void stateChanged(ChangeEvent JavaDoc e) {
152                 ((SorterTableColumnModel)preview.getColumnModel()).setIndexChanged(indexOfCheckBox);
153         }
154     }
155     
156     /**
157      * Sets the checkboxes in the sorter panel to what is shown in
158      * the MainFrame. This assumes that sorterTableColumnModel will
159      * return the list of which box is checked in the same order as
160      * the order that sorter panel has.
161      */

162     private void setSorterCheckBoxes() {
163         boolean[] chBoxSorterBooleans = MainFrame.getInstance().getSorter().getVisibleColumns();
164         if(chBoxSorterBooleans.length != checkBoxSortList.size())
165             return;
166         
167         for(int i = 0; i < checkBoxSortList.size(); i++){
168             checkBoxSortList.get(i).setSelected(chBoxSorterBooleans[i]);
169         }
170     }
171
172     void freeze()
173     {
174         sortApply.setEnabled(false);
175     }
176     
177     void thaw()
178     {
179         sortApply.setEnabled(true);
180     }
181 }
182
Popular Tags