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

# · Java · 330 lines · 230 code · 39 blank · 61 comment · 19 complexity · 766b99ffcef43cac83884561dcf1b49d MD5 · raw file

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