001: /*
002: * JCheckBoxList.java - A list, each item can be checked or unchecked
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2001, 2002 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.gui;
024:
025: //{{{ Imports
026: import java.awt.Component;
027: import java.awt.Font;
028: import java.util.Vector;
029: import javax.swing.*;
030: import javax.swing.table.*;
031:
032: //}}}
033:
034: /**
035: * A list where items can be selected and checked off independently.
036: * @since jEdit 3.2pre9
037: */
038: public class JCheckBoxList extends JTable {
039: //{{{ JCheckBoxList constructor
040: /**
041: * Creates a checkbox list with the given list of objects. The elements
042: * of this array can either be Entry instances, or other objects (if the
043: * latter, they will default to being unchecked).
044: */
045: public JCheckBoxList(Object[] items) {
046: setModel(items);
047: } //}}}
048:
049: //{{{ JCheckBoxList constructor
050: /**
051: * Creates a checkbox list with the given list of objects. The elements
052: * of this vector can either be Entry instances, or other objects (if the
053: * latter, they will default to being unchecked).
054: */
055: public JCheckBoxList(Vector items) {
056: setModel(items);
057: } //}}}
058:
059: //{{{ setModel() method
060: /**
061: * Sets the model to the given list of objects. The elements of this
062: * array can either be Entry instances, or other objects (if the
063: * latter, they will default to being unchecked).
064: */
065: public void setModel(Object[] items) {
066: setModel(new CheckBoxListModel(items));
067: init();
068: } //}}}
069:
070: //{{{ setModel() method
071: /**
072: * Sets the model to the given list of objects. The elements of this
073: * vector can either be Entry instances, or other objects (if the
074: * latter, they will default to being unchecked).
075: */
076: public void setModel(Vector items) {
077: setModel(new CheckBoxListModel(items));
078: init();
079: } //}}}
080:
081: //{{{ getCheckedValues() method
082: public Object[] getCheckedValues() {
083: Vector values = new Vector();
084: CheckBoxListModel model = (CheckBoxListModel) getModel();
085: for (int i = 0; i < model.items.size(); i++) {
086: Entry entry = (Entry) model.items.elementAt(i);
087: if (entry.checked && !entry.caption) {
088: values.addElement(entry.value);
089: }
090: }
091:
092: Object[] retVal = new Object[values.size()];
093: values.copyInto(retVal);
094: return retVal;
095: } //}}}
096:
097: //{{{ selectAll() method
098: public void selectAll() {
099: CheckBoxListModel model = (CheckBoxListModel) getModel();
100: for (int i = 0; i < model.items.size(); i++) {
101: Entry entry = (Entry) model.items.elementAt(i);
102: if (!entry.caption)
103: entry.checked = true;
104: }
105:
106: model.fireTableRowsUpdated(0, model.getRowCount());
107: } //}}}
108:
109: //{{{ getValues() method
110: public Entry[] getValues() {
111: CheckBoxListModel model = (CheckBoxListModel) getModel();
112: Entry[] retVal = new Entry[model.items.size()];
113: model.items.copyInto(retVal);
114: return retVal;
115: } //}}}
116:
117: //{{{ getSelectedValue() method
118: public Object getSelectedValue() {
119: int row = getSelectedRow();
120: if (row == -1) {
121: return null;
122: } else {
123: return getModel().getValueAt(row, 1);
124: }
125: } //}}}
126:
127: //{{{ getCellRenderer() method
128: public TableCellRenderer getCellRenderer(int row, int column) {
129: if (column == 0) {
130: Entry entry = (Entry) ((CheckBoxListModel) getModel()).items
131: .get(row);
132: if (entry.caption)
133: return dummy;
134: }
135:
136: return super .getCellRenderer(row, column);
137: } //}}}
138:
139: //{{{ Private members
140: private TableCellRenderer dummy;
141:
142: //{{{ init() method
143: private void init() {
144: dummy = new DummyRenderer();
145: getSelectionModel().setSelectionMode(
146: ListSelectionModel.SINGLE_SELECTION);
147: setShowGrid(false);
148: setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
149: TableColumn column = getColumnModel().getColumn(0);
150: int checkBoxWidth = new JCheckBox().getPreferredSize().width;
151: column.setPreferredWidth(checkBoxWidth);
152: column.setMinWidth(checkBoxWidth);
153: column.setWidth(checkBoxWidth);
154: column.setMaxWidth(checkBoxWidth);
155: column.setResizable(false);
156:
157: column = getColumnModel().getColumn(1);
158: column.setCellRenderer(new LabelRenderer());
159: } //}}}
160:
161: //}}}
162:
163: //{{{ Entry class
164: /**
165: * A check box list entry.
166: */
167: public static class Entry {
168: boolean checked;
169: boolean caption;
170: Object value;
171:
172: public Entry(Object value) {
173: this .caption = true;
174: this .value = value;
175: }
176:
177: public Entry(boolean checked, Object value) {
178: this .checked = checked;
179: this .value = value;
180: }
181:
182: public boolean isChecked() {
183: return checked;
184: }
185:
186: public Object getValue() {
187: return value;
188: }
189: } //}}}
190:
191: //{{{ DummyRenderer class
192: private class DummyRenderer extends DefaultTableCellRenderer {
193: public Component getTableCellRendererComponent(JTable table,
194: Object value, boolean isSelected, boolean hasFocus,
195: int row, int column) {
196: return super .getTableCellRendererComponent(table,
197: null /* value */, isSelected,
198: false /* hasFocus */, row, column);
199: }
200: } //}}}
201:
202: //{{{ LabelRenderer class
203: private class LabelRenderer extends DefaultTableCellRenderer {
204: Font plainFont, boldFont;
205:
206: LabelRenderer() {
207: plainFont = UIManager.getFont("Tree.font");
208: boldFont = plainFont.deriveFont(Font.BOLD);
209: }
210:
211: public Component getTableCellRendererComponent(JTable table,
212: Object value, boolean isSelected, boolean hasFocus,
213: int row, int column) {
214: super .getTableCellRendererComponent(table, value,
215: isSelected, hasFocus, row, column);
216:
217: Entry entry = (Entry) ((CheckBoxListModel) getModel()).items
218: .get(row);
219: if (entry.caption)
220: setFont(boldFont);
221: else
222: setFont(plainFont);
223: return this ;
224: }
225: } //}}}
226: }
227:
228: //{{{ CheckBoxListModel class
229: class CheckBoxListModel extends AbstractTableModel {
230: Vector items;
231:
232: CheckBoxListModel(Vector _items) {
233: items = new Vector(_items.size());
234: for (int i = 0; i < _items.size(); i++) {
235: items.addElement(createEntry(_items.elementAt(i)));
236: }
237: }
238:
239: CheckBoxListModel(Object[] _items) {
240: items = new Vector(_items.length);
241: for (int i = 0; i < _items.length; i++) {
242: items.addElement(createEntry(_items[i]));
243: }
244: }
245:
246: private JCheckBoxList.Entry createEntry(Object obj) {
247: if (obj instanceof JCheckBoxList.Entry)
248: return (JCheckBoxList.Entry) obj;
249: else
250: return new JCheckBoxList.Entry(false, obj);
251: }
252:
253: public int getRowCount() {
254: return items.size();
255: }
256:
257: public int getColumnCount() {
258: return 2;
259: }
260:
261: public String getColumnName(int col) {
262: return null;
263: }
264:
265: public Object getValueAt(int row, int col) {
266: JCheckBoxList.Entry entry = (JCheckBoxList.Entry) items
267: .elementAt(row);
268: switch (col) {
269: case 0:
270: return new Boolean(entry.checked);
271: case 1:
272: return entry.value;
273: default:
274: throw new InternalError();
275: }
276: }
277:
278: public Class getColumnClass(int col) {
279: switch (col) {
280: case 0:
281: return Boolean.class;
282: case 1:
283: return String.class;
284: default:
285: throw new InternalError();
286: }
287: }
288:
289: public boolean isCellEditable(int row, int col) {
290: JCheckBoxList.Entry entry = (JCheckBoxList.Entry) items
291: .elementAt(row);
292: return col == 0 && !entry.caption;
293: }
294:
295: public void setValueAt(Object value, int row, int col) {
296: if (col == 0) {
297: JCheckBoxList.Entry entry = (JCheckBoxList.Entry) items
298: .elementAt(row);
299: if (!entry.caption) {
300: entry.checked = (value.equals(Boolean.TRUE));
301: fireTableRowsUpdated(row, row);
302: }
303: }
304: }
305: } //}}}
|