PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/JCheckBoxList.java

#
Java | 350 lines | 245 code | 39 blank | 66 comment | 19 complexity | 74eb3be59b6552fa8c03d0f52d97e82a MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import java.awt.Component;
 27import java.awt.Font;
 28import java.util.Vector;
 29import java.util.List;
 30import java.util.ArrayList;
 31import javax.swing.*;
 32import javax.swing.table.*;
 33//}}}
 34
 35/**
 36 * A list where items can be selected and checked off independently.
 37 * @since jEdit 3.2pre9
 38 */
 39public class JCheckBoxList extends JTable
 40{
 41	//{{{ JCheckBoxList constructor
 42	/**
 43	 * Creates a checkbox list with the given list of objects. The elements
 44	 * of this array can either be Entry instances, or other objects (if the
 45	 * latter, they will default to being unchecked).
 46	 */
 47	public JCheckBoxList(Object[] items)
 48	{
 49		setModel(items);
 50	} //}}}
 51
 52	//{{{ JCheckBoxList constructor
 53	/**
 54	 * Creates a checkbox list with the given list of objects. The elements
 55	 * of this vector can either be Entry instances, or other objects (if the
 56	 * latter, they will default to being unchecked).
 57	 */
 58	public JCheckBoxList(Vector items)
 59	{
 60		setModel(items);
 61	} //}}}
 62
 63	//{{{ setModel() method
 64	/**
 65	 * Sets the model to the given list of objects. The elements of this
 66	 * array can either be Entry instances, or other objects (if the
 67	 * latter, they will default to being unchecked).
 68	 */
 69	public void setModel(Object[] items)
 70	{
 71		setModel(new CheckBoxListModel(items));
 72		init();
 73	} //}}}
 74
 75	//{{{ setModel() method
 76	/**
 77	 * Sets the model to the given list of objects. The elements of this
 78	 * vector can either be Entry instances, or other objects (if the
 79	 * latter, they will default to being unchecked).
 80	 */
 81	public void setModel(Vector items)
 82	{
 83		setModel(new CheckBoxListModel(items));
 84		init();
 85	} //}}}
 86
 87	//{{{ getCheckedValues() method
 88	public Object[] getCheckedValues()
 89	{
 90		List<Object> values = new ArrayList<Object>();
 91		CheckBoxListModel model = (CheckBoxListModel)getModel();
 92		for(int i = 0; i < model.items.size(); i++)
 93		{
 94			Entry entry = model.items.get(i);
 95			if(entry.checked && !entry.caption)
 96			{
 97				values.add(entry.value);
 98			}
 99		}
100
101		Object[] retVal = new Object[values.size()];
102		return values.toArray(retVal);
103	} //}}}
104
105	//{{{ selectAll() method
106	@Override
107	public void selectAll()
108	{
109		CheckBoxListModel model = (CheckBoxListModel)getModel();
110		for(int i = 0; i < model.items.size(); i++)
111		{
112			Entry entry = model.items.elementAt(i);
113			if(!entry.caption)
114				entry.checked = true;
115		}
116
117		model.fireTableRowsUpdated(0,model.getRowCount());
118	} //}}}
119
120	//{{{ getValues() method
121	public Entry[] getValues()
122	{
123		CheckBoxListModel model = (CheckBoxListModel)getModel();
124		Entry[] retVal = new Entry[model.items.size()];
125		model.items.copyInto(retVal);
126		return retVal;
127	} //}}}
128
129	//{{{ getSelectedValue() method
130	public Object getSelectedValue()
131	{
132		int row = getSelectedRow();
133		if(row == -1)
134		{
135			return null;
136		}
137		else
138		{
139			return getModel().getValueAt(row,1);
140		}
141	} //}}}
142
143	//{{{ getCellRenderer() method
144	@Override
145	public TableCellRenderer getCellRenderer(int row, int column)
146	{
147		if(column == 0)
148		{
149			Entry entry = ((CheckBoxListModel)getModel()).items.get(row);
150			if(entry.caption)
151				return dummy;
152		}
153
154		return super.getCellRenderer(row,column);
155	} //}}}
156
157	//{{{ Private members
158	private TableCellRenderer dummy;
159
160	//{{{ init() method
161	private void init()
162	{
163		dummy = new DummyRenderer();
164		getSelectionModel().setSelectionMode(ListSelectionModel
165			.SINGLE_SELECTION);
166		setShowGrid(false);
167		setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
168		TableColumn column = getColumnModel().getColumn(0);
169		int checkBoxWidth = new JCheckBox().getPreferredSize().width;
170		column.setPreferredWidth(checkBoxWidth);
171		column.setMinWidth(checkBoxWidth);
172		column.setWidth(checkBoxWidth);
173		column.setMaxWidth(checkBoxWidth);
174		column.setResizable(false);
175
176		column = getColumnModel().getColumn(1);
177		column.setCellRenderer(new LabelRenderer());
178	} //}}}
179
180	//}}}
181
182	//{{{ Entry class
183	/**
184	 * A check box list entry.
185	 */
186	public static class Entry
187	{
188		boolean checked;
189		boolean caption;
190		Object value;
191
192		public Entry(Object value)
193		{
194			this.caption = true;
195			this.value = value;
196		}
197
198		public Entry(boolean checked, Object value)
199		{
200			this.checked = checked;
201			this.value = value;
202		}
203
204		public boolean isChecked()
205		{
206			return checked;
207		}
208
209		public Object getValue()
210		{
211			return value;
212		}
213	} //}}}
214
215	//{{{ DummyRenderer class
216	private static class DummyRenderer extends DefaultTableCellRenderer
217	{
218		@Override
219		public Component getTableCellRendererComponent(JTable table, Object value,
220			boolean isSelected, boolean hasFocus, int row, int column)
221		{
222			return super.getTableCellRendererComponent(table,null /* value */,
223				isSelected,false /* hasFocus */,row,column);
224		}
225	} //}}}
226
227	//{{{ LabelRenderer class
228	private class LabelRenderer extends DefaultTableCellRenderer
229	{
230		Font plainFont, boldFont;
231
232		LabelRenderer()
233		{
234			plainFont = UIManager.getFont("Tree.font");
235			boldFont = plainFont.deriveFont(Font.BOLD);
236		}
237
238		@Override
239		public Component getTableCellRendererComponent(JTable table, Object value,
240			boolean isSelected, boolean hasFocus, int row, int column)
241		{
242			super.getTableCellRendererComponent(table,value,isSelected,
243				hasFocus,row,column);
244
245			Entry entry = ((CheckBoxListModel)getModel()).items.get(row);
246			if(entry.caption)
247				setFont(boldFont);
248			else
249				setFont(plainFont);
250			return this;
251		}
252	} //}}}
253}
254
255//{{{ CheckBoxListModel class
256class CheckBoxListModel extends AbstractTableModel
257{
258	Vector<JCheckBoxList.Entry> items;
259
260	CheckBoxListModel(Vector _items)
261	{
262		items = new Vector<JCheckBoxList.Entry>(_items.size());
263		for(int i = 0; i < _items.size(); i++)
264		{
265			items.add(createEntry(_items.elementAt(i)));
266		}
267	}
268
269	CheckBoxListModel(Object[] _items)
270	{
271		items = new Vector<JCheckBoxList.Entry>(_items.length);
272		for(int i = 0; i < _items.length; i++)
273		{
274			items.add(createEntry(_items[i]));
275		}
276	}
277
278	private static JCheckBoxList.Entry createEntry(Object obj)
279	{
280		if(obj instanceof JCheckBoxList.Entry)
281			return (JCheckBoxList.Entry)obj;
282		else
283			return new JCheckBoxList.Entry(false,obj);
284	}
285
286	public int getRowCount()
287	{
288		return items.size();
289	}
290
291	public int getColumnCount()
292	{
293		return 2;
294	}
295
296	@Override
297	public String getColumnName(int col)
298	{
299		return null;
300	}
301
302	public Object getValueAt(int row, int col)
303	{
304		JCheckBoxList.Entry entry = items.get(row);
305		switch(col)
306		{
307		case 0:
308			return Boolean.valueOf(entry.checked);
309		case 1:
310			return entry.value;
311		default:
312			throw new InternalError();
313		}
314	}
315
316	@Override
317	public Class getColumnClass(int col)
318	{
319		switch(col)
320		{
321		case 0:
322			return Boolean.class;
323		case 1:
324			return String.class;
325		default:
326			throw new InternalError();
327		}
328	}
329
330	@Override
331	public boolean isCellEditable(int row, int col)
332	{
333		JCheckBoxList.Entry entry = items.get(row);
334		return col == 0 && !entry.caption;
335	}
336
337	@Override
338	public void setValueAt(Object value, int row, int col)
339	{
340		if(col == 0)
341		{
342			JCheckBoxList.Entry entry = items.get(row);
343			if(!entry.caption)
344			{
345				entry.checked = value.equals(Boolean.TRUE);
346				fireTableRowsUpdated(row,row);
347			}
348		}
349	}
350} //}}}