KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > JCheckBoxList


1 /*
2  * JCheckBoxList.java - A list, each item can be checked or unchecked
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2000, 2001, 2002 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import java.awt.Component JavaDoc;
27 import java.awt.Font JavaDoc;
28 import java.util.Vector JavaDoc;
29 import javax.swing.*;
30 import javax.swing.table.*;
31 //}}}
32

33 /**
34  * A list where items can be selected and checked off independently.
35  * @since jEdit 3.2pre9
36  */

37 public class JCheckBoxList extends JTable
38 {
39     //{{{ JCheckBoxList constructor
40
/**
41      * Creates a checkbox list with the given list of objects. The elements
42      * of this array can either be Entry instances, or other objects (if the
43      * latter, they will default to being unchecked).
44      */

45     public JCheckBoxList(Object JavaDoc[] items)
46     {
47         setModel(items);
48     } //}}}
49

50     //{{{ JCheckBoxList constructor
51
/**
52      * Creates a checkbox list with the given list of objects. The elements
53      * of this vector can either be Entry instances, or other objects (if the
54      * latter, they will default to being unchecked).
55      */

56     public JCheckBoxList(Vector JavaDoc items)
57     {
58         setModel(items);
59     } //}}}
60

61     //{{{ setModel() method
62
/**
63      * Sets the model to the given list of objects. The elements of this
64      * array can either be Entry instances, or other objects (if the
65      * latter, they will default to being unchecked).
66      */

67     public void setModel(Object JavaDoc[] items)
68     {
69         setModel(new CheckBoxListModel(items));
70         init();
71     } //}}}
72

73     //{{{ setModel() method
74
/**
75      * Sets the model to the given list of objects. The elements of this
76      * vector can either be Entry instances, or other objects (if the
77      * latter, they will default to being unchecked).
78      */

79     public void setModel(Vector JavaDoc items)
80     {
81         setModel(new CheckBoxListModel(items));
82         init();
83     } //}}}
84

85     //{{{ getCheckedValues() method
86
public Object JavaDoc[] getCheckedValues()
87     {
88         Vector JavaDoc values = new Vector JavaDoc();
89         CheckBoxListModel model = (CheckBoxListModel)getModel();
90         for(int i = 0; i < model.items.size(); i++)
91         {
92             Entry entry = (Entry)model.items.elementAt(i);
93             if(entry.checked && !entry.caption)
94                 values.addElement(entry.value);
95         }
96
97         Object JavaDoc[] retVal = new Object JavaDoc[values.size()];
98         values.copyInto(retVal);
99         return retVal;
100     } //}}}
101

102     //{{{ selectAll() method
103
public void selectAll()
104     {
105         CheckBoxListModel model = (CheckBoxListModel)getModel();
106         for(int i = 0; i < model.items.size(); i++)
107         {
108             Entry entry = (Entry)model.items.elementAt(i);
109             if(!entry.caption)
110                 entry.checked = true;
111         }
112
113         model.fireTableRowsUpdated(0,model.getRowCount());
114     } //}}}
115

116     //{{{ getValues() method
117
public Entry[] getValues()
118     {
119         CheckBoxListModel model = (CheckBoxListModel)getModel();
120         Entry[] retVal = new Entry[model.items.size()];
121         model.items.copyInto(retVal);
122         return retVal;
123     } //}}}
124

125     //{{{ getSelectedValue() method
126
public Object JavaDoc getSelectedValue()
127     {
128         int row = getSelectedRow();
129         if(row == -1)
130             return null;
131         else
132             return getModel().getValueAt(row,1);
133     } //}}}
134

135     //{{{ getCellRenderer() method
136
public TableCellRenderer getCellRenderer(int row, int column)
137     {
138         if(column == 0)
139         {
140             Entry entry = (Entry)((CheckBoxListModel)getModel()).items.get(row);
141             if(entry.caption)
142                 return dummy;
143         }
144
145         return super.getCellRenderer(row,column);
146     } //}}}
147

148     //{{{ Private members
149
private TableCellRenderer dummy;
150
151     //{{{ init() method
152
private void init()
153     {
154         dummy = new DummyRenderer();
155         getSelectionModel().setSelectionMode(ListSelectionModel
156             .SINGLE_SELECTION);
157         setShowGrid(false);
158         setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
159         TableColumn column = getColumnModel().getColumn(0);
160         int checkBoxWidth = new JCheckBox().getPreferredSize().width;
161         column.setPreferredWidth(checkBoxWidth);
162         column.setMinWidth(checkBoxWidth);
163         column.setWidth(checkBoxWidth);
164         column.setMaxWidth(checkBoxWidth);
165         column.setResizable(false);
166
167         column = getColumnModel().getColumn(1);
168         column.setCellRenderer(new LabelRenderer());
169     } //}}}
170

171     //}}}
172

173     //{{{ Entry class
174
/**
175      * A check box list entry.
176      */

177     public static class Entry
178     {
179         boolean checked;
180         boolean caption;
181         Object JavaDoc value;
182
183         public Entry(Object JavaDoc value)
184         {
185             this.caption = true;
186             this.value = value;
187         }
188
189         public Entry(boolean checked, Object JavaDoc value)
190         {
191             this.checked = checked;
192             this.value = value;
193         }
194
195         public boolean isChecked()
196         {
197             return checked;
198         }
199
200         public Object JavaDoc getValue()
201         {
202             return value;
203         }
204     } //}}}
205

206     //{{{ DummyRenderer class
207
private class DummyRenderer extends DefaultTableCellRenderer
208     {
209         public Component JavaDoc getTableCellRendererComponent(JTable table, Object JavaDoc value,
210             boolean isSelected, boolean hasFocus, int row, int column)
211         {
212             return super.getTableCellRendererComponent(table,null /* value */,
213                 isSelected,false /* hasFocus */,row,column);
214         }
215     } //}}}
216

217     //{{{ LabelRenderer class
218
private class LabelRenderer extends DefaultTableCellRenderer
219     {
220         Font JavaDoc plainFont, boldFont;
221
222         LabelRenderer()
223         {
224             plainFont = UIManager.getFont("Tree.font");
225             boldFont = plainFont.deriveFont(Font.BOLD);
226         }
227
228         public Component JavaDoc getTableCellRendererComponent(JTable table, Object JavaDoc value,
229             boolean isSelected, boolean hasFocus, int row, int column)
230         {
231             super.getTableCellRendererComponent(table,value,isSelected,
232                 hasFocus,row,column);
233
234             Entry entry = (Entry)((CheckBoxListModel)getModel()).items.get(row);
235             if(entry.caption)
236                 setFont(boldFont);
237             else
238                 setFont(plainFont);
239             return this;
240         }
241     } //}}}
242
}
243
244 class CheckBoxListModel extends AbstractTableModel
245 {
246     Vector JavaDoc items;
247
248     CheckBoxListModel(Vector JavaDoc _items)
249     {
250         items = new Vector JavaDoc(_items.size());
251         for(int i = 0; i < _items.size(); i++)
252         {
253             items.addElement(createEntry(_items.elementAt(i)));
254         }
255     }
256
257     CheckBoxListModel(Object JavaDoc[] _items)
258     {
259         items = new Vector JavaDoc(_items.length);
260         for(int i = 0; i < _items.length; i++)
261         {
262             items.addElement(createEntry(_items[i]));
263         }
264     }
265
266     private JCheckBoxList.Entry createEntry(Object JavaDoc obj)
267     {
268         if(obj instanceof JCheckBoxList.Entry)
269             return (JCheckBoxList.Entry)obj;
270         else
271             return new JCheckBoxList.Entry(false,obj);
272     }
273
274     public int getRowCount()
275     {
276         return items.size();
277     }
278
279     public int getColumnCount()
280     {
281         return 2;
282     }
283
284     public String JavaDoc getColumnName(int col)
285     {
286         return null;
287     }
288
289     public Object JavaDoc getValueAt(int row, int col)
290     {
291         JCheckBoxList.Entry entry = (JCheckBoxList.Entry)items.elementAt(row);
292         switch(col)
293         {
294         case 0:
295             return new Boolean JavaDoc(entry.checked);
296         case 1:
297             return entry.value;
298         default:
299             throw new InternalError JavaDoc();
300         }
301     }
302
303     public Class JavaDoc getColumnClass(int col)
304     {
305         switch(col)
306         {
307         case 0:
308             return Boolean JavaDoc.class;
309         case 1:
310             return String JavaDoc.class;
311         default:
312             throw new InternalError JavaDoc();
313         }
314     }
315
316     public boolean isCellEditable(int row, int col)
317     {
318         JCheckBoxList.Entry entry = (JCheckBoxList.Entry)items.elementAt(row);
319         return col == 0 && !entry.caption;
320     }
321
322     public void setValueAt(Object JavaDoc value, int row, int col)
323     {
324         if(col == 0)
325         {
326             JCheckBoxList.Entry entry = (JCheckBoxList.Entry)items.elementAt(row);
327             if(!entry.caption)
328             {
329                 entry.checked = (value.equals(Boolean.TRUE));
330                 fireTableRowsUpdated(row,row);
331             }
332         }
333     }
334 }
335
Popular Tags