PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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