PageRenderTime 157ms CodeModel.GetById 132ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/JCheckBoxList.java

#
Java | 243 lines | 173 code | 28 blank | 42 comment | 11 complexity | e381b2a50ebc6257222c5092bdf8db98 MD5 | raw file
  1/*
  2 * JCheckBoxList.java - A list, each item can be checked or unchecked
  3 * Copyright (C) 2000, 2001 Slava Pestov
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License
  7 * as published by the Free Software Foundation; either version 2
  8 * of the License, or any later version.
  9 *
 10 * This program 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
 13 * GNU General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * along with this program; if not, write to the Free Software
 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18 */
 19
 20package org.gjt.sp.jedit.gui;
 21
 22import javax.swing.table.*;
 23import javax.swing.*;
 24import java.util.Vector;
 25
 26/**
 27 * @since jEdit 3.2pre9
 28 */
 29public class JCheckBoxList extends JTable
 30{
 31	/**
 32	 * Creates a checkbox list with the given list of objects. The elements
 33	 * of this array can either be Entry instances, or other objects (if the
 34	 * latter, they will default to being unchecked).
 35	 */
 36	public JCheckBoxList(Object[] items)
 37	{
 38		setModel(items);
 39	}
 40
 41	/**
 42	 * Creates a checkbox list with the given list of objects. The elements
 43	 * of this vector can either be Entry instances, or other objects (if the
 44	 * latter, they will default to being unchecked).
 45	 */
 46	public JCheckBoxList(Vector items)
 47	{
 48		setModel(items);
 49	}
 50
 51	/**
 52	 * Sets the model to the given list of objects. The elements of this
 53	 * array can either be Entry instances, or other objects (if the
 54	 * latter, they will default to being unchecked).
 55	 */
 56	public void setModel(Object[] items)
 57	{
 58		setModel(new CheckBoxListModel(items));
 59		init();
 60	}
 61
 62	/**
 63	 * Sets the model to the given list of objects. The elements of this
 64	 * vector can either be Entry instances, or other objects (if the
 65	 * latter, they will default to being unchecked).
 66	 */
 67	public void setModel(Vector items)
 68	{
 69		setModel(new CheckBoxListModel(items));
 70		init();
 71	}
 72
 73	public Object[] getCheckedValues()
 74	{
 75		Vector values = new Vector();
 76		CheckBoxListModel model = (CheckBoxListModel)getModel();
 77		for(int i = 0; i < model.items.size(); i++)
 78		{
 79			Entry entry = (Entry)model.items.elementAt(i);
 80			if(entry.checked)
 81				values.addElement(entry.value);
 82		}
 83
 84		Object[] retVal = new Object[values.size()];
 85		values.copyInto(retVal);
 86		return retVal;
 87	}
 88
 89	public void selectAll()
 90	{
 91		CheckBoxListModel model = (CheckBoxListModel)getModel();
 92		for(int i = 0; i < model.items.size(); i++)
 93		{
 94			Entry entry = (Entry)model.items.elementAt(i);
 95			entry.checked = true;
 96		}
 97
 98		model.fireTableRowsUpdated(0,model.getRowCount());
 99	}
100
101	public Entry[] getValues()
102	{
103		CheckBoxListModel model = (CheckBoxListModel)getModel();
104		Entry[] retVal = new Entry[model.items.size()];
105		model.items.copyInto(retVal);
106		return retVal;
107	}
108
109	public Object getSelectedValue()
110	{
111		int row = getSelectedRow();
112		if(row == -1)
113			return null;
114		else
115			return getModel().getValueAt(row,1);
116	}
117
118	// private members
119	private void init()
120	{
121		getSelectionModel().setSelectionMode(ListSelectionModel
122			.SINGLE_SELECTION);
123		setShowGrid(false);
124		setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
125		TableColumn column = getColumnModel().getColumn(0);
126		int checkBoxWidth = new JCheckBox().getPreferredSize().width;
127		column.setPreferredWidth(checkBoxWidth);
128		column.setMinWidth(checkBoxWidth);
129		column.setWidth(checkBoxWidth);
130		column.setMaxWidth(checkBoxWidth);
131		column.setResizable(false);
132	}
133
134	public static class Entry
135	{
136		boolean checked;
137		Object value;
138
139		public Entry(boolean checked, Object value)
140		{
141			this.checked = checked;
142			this.value = value;
143		}
144
145		public boolean isChecked()
146		{
147			return checked;
148		}
149
150		public Object getValue()
151		{
152			return value;
153		}
154	}
155}
156
157class CheckBoxListModel extends AbstractTableModel
158{
159	Vector items;
160
161	CheckBoxListModel(Vector _items)
162	{
163		items = new Vector(_items.size());
164		for(int i = 0; i < _items.size(); i++)
165		{
166			items.addElement(createEntry(_items.elementAt(i)));
167		}
168	}
169
170	CheckBoxListModel(Object[] _items)
171	{
172		items = new Vector(_items.length);
173		for(int i = 0; i < _items.length; i++)
174		{
175			items.addElement(createEntry(_items[i]));
176		}
177	}
178
179	private JCheckBoxList.Entry createEntry(Object obj)
180	{
181		if(obj instanceof JCheckBoxList.Entry)
182			return (JCheckBoxList.Entry)obj;
183		else
184			return new JCheckBoxList.Entry(false,obj);
185	}
186
187	public int getRowCount()
188	{
189		return items.size();
190	}
191
192	public int getColumnCount()
193	{
194		return 2;
195	}
196
197	public String getColumnName(int col)
198	{
199		return null;
200	}
201
202	public Object getValueAt(int row, int col)
203	{
204		JCheckBoxList.Entry entry = (JCheckBoxList.Entry)items.elementAt(row);
205		switch(col)
206		{
207		case 0:
208			return new Boolean(entry.checked);
209		case 1:
210			return entry.value;
211		default:
212			throw new InternalError();
213		}
214	}
215
216	public Class getColumnClass(int col)
217	{
218		switch(col)
219		{
220		case 0:
221			return Boolean.class;
222		case 1:
223			return String.class;
224		default:
225			throw new InternalError();
226		}
227	}
228
229	public boolean isCellEditable(int row, int col)
230	{
231		return col == 0;
232	}
233
234	public void setValueAt(Object value, int row, int col)
235	{
236		if(col == 0)
237		{
238			((JCheckBoxList.Entry)items.elementAt(row)).checked =
239				(value.equals(Boolean.TRUE));
240			fireTableRowsUpdated(row,row);
241		}
242	}
243}