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.Dimension;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.util.ArrayList;
027:
028: import javax.swing.JButton;
029: import javax.swing.JCheckBox;
030: import javax.swing.JPanel;
031: import javax.swing.JScrollPane;
032: import javax.swing.JTable;
033: import javax.swing.event.ChangeEvent;
034: import javax.swing.event.ChangeListener;
035: import javax.swing.table.DefaultTableModel;
036: import javax.swing.table.JTableHeader;
037:
038: /**
039: * This is the window that pops up when the user double clicks on the sorting table
040: * Its also available from the menu if they remove all Sortables.
041: *
042: * The user can choose what Sortables he wants to sort by,
043: * sort them into the order he wants to see
044: * and then click apply to move
045: * his choices onto the sorting table
046: * @author Dan
047: *
048: */
049: public class SorterDialog extends FBDialog {
050:
051: private JTableHeader preview;
052: private ArrayList<JCheckBox> checkBoxSortList = new ArrayList<JCheckBox>();
053: private CheckBoxList chBList;
054: JButton sortApply;
055: private static SorterDialog instance = new SorterDialog();
056:
057: public static SorterDialog getInstance() {
058: return instance;
059: }
060:
061: @Override
062: public void setVisible(boolean visible) {
063: super .setVisible(visible);
064:
065: if (visible) {
066: ((SorterTableColumnModel) (preview.getColumnModel()))
067: .createFrom(MainFrame.getInstance().getSorter());
068: setSorterCheckBoxes();
069: }
070: }
071:
072: private SorterDialog() {
073: setTitle("Sort By...");
074: add(createSorterPane());
075: pack();
076: setLocationByPlatform(true);
077: setResizable(false);
078: preview.setColumnModel(new SorterTableColumnModel(MainFrame
079: .getInstance().getSorter().getOrder()));
080: }
081:
082: /**
083: * Creates JPanel with checkboxes of different things to
084: * sort by. List is: priority, class, package, category,
085: * bugcode, status, and type.
086: * @return
087: */
088: private JPanel createSorterPane() {
089: JPanel sorter = new JPanel();
090: JPanel insidePanel = new JPanel();
091: insidePanel.setLayout(new BorderLayout());
092: sorter.setLayout(new BorderLayout());
093: preview = new JTableHeader();
094: preview.setColumnModel(new SorterTableColumnModel(Sortables
095: .values()));
096:
097: Sortables[] sortList = Sortables.values();
098:
099: for (Sortables s : Sortables.values()) {
100: if (s == Sortables.DIVIDER)
101: checkBoxSortList.add(new JCheckBox(
102: edu.umd.cs.findbugs.L10N.getLocalString(
103: "sort.divider", "[divider]")));
104: else
105: checkBoxSortList.add(new JCheckBox(s.toString()));
106: }
107:
108: setSorterCheckBoxes();
109:
110: for (int i = 0; i < sortList.length; i++) {
111: checkBoxSortList.get(i).addChangeListener(
112: new CheckBoxChangedListener(i));
113: }
114:
115: chBList = new CheckBoxList(checkBoxSortList
116: .toArray(new JCheckBox[checkBoxSortList.size()]));
117:
118: insidePanel.add(chBList, BorderLayout.NORTH);
119:
120: //insidePanel.add(sorterInfoLabel(), BorderLayout.CENTER);
121:
122: JPanel bottomPanel = new JPanel();
123: bottomPanel.setLayout(new BorderLayout());
124: //bottomPanel.add(previewLabel,BorderLayout.NORTH);
125: //New code to fix problem in Windows
126: JTable t = new JTable(new DefaultTableModel(0, Sortables
127: .values().length));
128: t.setTableHeader(preview);
129: JScrollPane sp = new JScrollPane(t);
130: //This sets the height of the scrollpane so it is dependent on the fontsize.
131: int num = (int) (Driver.getFontSize() * 1.2);
132: sp.setPreferredSize(new Dimension(670, 10 + num));
133: //End of new code.
134: //Changed code.
135: bottomPanel.add(sp, BorderLayout.CENTER);
136: //bottomPanel.add(preview, BorderLayout.CENTER);
137: //End of changed code.
138:
139: insidePanel.add(bottomPanel, BorderLayout.SOUTH);
140:
141: sortApply = new JButton(edu.umd.cs.findbugs.L10N
142: .getLocalString("dlg.apply_btn", "Apply"));
143: sortApply.addActionListener(new ActionListener() {
144: public void actionPerformed(ActionEvent e) {
145: MainFrame.getInstance().getSorter().createFrom(
146: (SorterTableColumnModel) preview
147: .getColumnModel());
148: ((BugTreeModel) MainFrame.getInstance().getTree()
149: .getModel()).checkSorter();
150: instance.setVisible(false); //close window
151: }
152: });
153: bottomPanel.add(sortApply, BorderLayout.SOUTH);
154: sorter.add(new JScrollPane(insidePanel), BorderLayout.CENTER);
155:
156: return sorter;
157: }
158:
159: private class CheckBoxChangedListener implements ChangeListener {
160:
161: int indexOfCheckBox;
162:
163: public CheckBoxChangedListener(int index) {
164: indexOfCheckBox = index;
165: }
166:
167: public void stateChanged(ChangeEvent e) {
168: ((SorterTableColumnModel) preview.getColumnModel())
169: .setIndexChanged(indexOfCheckBox);
170: }
171: }
172:
173: /**
174: * Sets the checkboxes in the sorter panel to what is shown in
175: * the MainFrame. This assumes that sorterTableColumnModel will
176: * return the list of which box is checked in the same order as
177: * the order that sorter panel has.
178: */
179: private void setSorterCheckBoxes() {
180: boolean[] chBoxSorterBooleans = MainFrame.getInstance()
181: .getSorter().getVisibleColumns();
182: if (chBoxSorterBooleans.length != checkBoxSortList.size())
183: return;
184:
185: for (int i = 0; i < checkBoxSortList.size(); i++) {
186: checkBoxSortList.get(i).setSelected(chBoxSorterBooleans[i]);
187: }
188: }
189:
190: void freeze() {
191: sortApply.setEnabled(false);
192: }
193:
194: void thaw() {
195: sortApply.setEnabled(true);
196: }
197: }
|