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

# · Java · 349 lines · 244 code · 39 blank · 66 comment · 19 complexity · 07c1c864386b50b686b8060aa0bb4468 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. * 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 = model.items.get(i);
  86. if(entry.checked && !entry.caption)
  87. {
  88. values.add(entry.value);
  89. }
  90. }
  91. Object[] retVal = new Object[values.size()];
  92. values.copyInto(retVal);
  93. return retVal;
  94. } //}}}
  95. //{{{ selectAll() method
  96. @Override
  97. public void selectAll()
  98. {
  99. CheckBoxListModel model = (CheckBoxListModel)getModel();
  100. for(int i = 0; i < model.items.size(); i++)
  101. {
  102. Entry entry = model.items.elementAt(i);
  103. if(!entry.caption)
  104. entry.checked = true;
  105. }
  106. model.fireTableRowsUpdated(0,model.getRowCount());
  107. } //}}}
  108. //{{{ getValues() method
  109. public Entry[] getValues()
  110. {
  111. CheckBoxListModel model = (CheckBoxListModel)getModel();
  112. Entry[] retVal = new Entry[model.items.size()];
  113. model.items.copyInto(retVal);
  114. return retVal;
  115. } //}}}
  116. //{{{ getSelectedValue() method
  117. public Object getSelectedValue()
  118. {
  119. int row = getSelectedRow();
  120. if(row == -1)
  121. {
  122. return null;
  123. }
  124. else
  125. {
  126. return getModel().getValueAt(row,1);
  127. }
  128. } //}}}
  129. //{{{ getCellRenderer() method
  130. @Override
  131. public TableCellRenderer getCellRenderer(int row, int column)
  132. {
  133. if(column == 0)
  134. {
  135. Entry entry = ((CheckBoxListModel)getModel()).items.get(row);
  136. if(entry.caption)
  137. return dummy;
  138. }
  139. return super.getCellRenderer(row,column);
  140. } //}}}
  141. //{{{ Private members
  142. private TableCellRenderer dummy;
  143. //{{{ init() method
  144. private void init()
  145. {
  146. dummy = new DummyRenderer();
  147. getSelectionModel().setSelectionMode(ListSelectionModel
  148. .SINGLE_SELECTION);
  149. setShowGrid(false);
  150. setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
  151. TableColumn column = getColumnModel().getColumn(0);
  152. int checkBoxWidth = new JCheckBox().getPreferredSize().width;
  153. column.setPreferredWidth(checkBoxWidth);
  154. column.setMinWidth(checkBoxWidth);
  155. column.setWidth(checkBoxWidth);
  156. column.setMaxWidth(checkBoxWidth);
  157. column.setResizable(false);
  158. column = getColumnModel().getColumn(1);
  159. column.setCellRenderer(new LabelRenderer());
  160. } //}}}
  161. //}}}
  162. //{{{ Entry class
  163. /**
  164. * A check box list entry.
  165. */
  166. public static class Entry
  167. {
  168. boolean checked;
  169. boolean caption;
  170. Object value;
  171. public Entry(Object value)
  172. {
  173. this.caption = true;
  174. this.value = value;
  175. }
  176. public Entry(boolean checked, Object value)
  177. {
  178. this.checked = checked;
  179. this.value = value;
  180. }
  181. public boolean isChecked()
  182. {
  183. return checked;
  184. }
  185. public Object getValue()
  186. {
  187. return value;
  188. }
  189. } //}}}
  190. //{{{ DummyRenderer class
  191. private class DummyRenderer extends DefaultTableCellRenderer
  192. {
  193. @Override
  194. public Component getTableCellRendererComponent(JTable table, Object value,
  195. boolean isSelected, boolean hasFocus, int row, int column)
  196. {
  197. return super.getTableCellRendererComponent(table,null /* value */,
  198. isSelected,false /* hasFocus */,row,column);
  199. }
  200. } //}}}
  201. //{{{ LabelRenderer class
  202. private class LabelRenderer extends DefaultTableCellRenderer
  203. {
  204. Font plainFont, boldFont;
  205. LabelRenderer()
  206. {
  207. plainFont = UIManager.getFont("Tree.font");
  208. boldFont = plainFont.deriveFont(Font.BOLD);
  209. }
  210. @Override
  211. public Component getTableCellRendererComponent(JTable table, Object value,
  212. boolean isSelected, boolean hasFocus, int row, int column)
  213. {
  214. super.getTableCellRendererComponent(table,value,isSelected,
  215. hasFocus,row,column);
  216. Entry entry = ((CheckBoxListModel)getModel()).items.get(row);
  217. if(entry.caption)
  218. setFont(boldFont);
  219. else
  220. setFont(plainFont);
  221. return this;
  222. }
  223. } //}}}
  224. }
  225. //{{{ CheckBoxListModel class
  226. class CheckBoxListModel extends AbstractTableModel
  227. {
  228. Vector<JCheckBoxList.Entry> items;
  229. CheckBoxListModel(Vector _items)
  230. {
  231. items = new Vector<JCheckBoxList.Entry>(_items.size());
  232. for(int i = 0; i < _items.size(); i++)
  233. {
  234. items.add(createEntry(_items.elementAt(i)));
  235. }
  236. }
  237. CheckBoxListModel(Object[] _items)
  238. {
  239. items = new Vector<JCheckBoxList.Entry>(_items.length);
  240. for(int i = 0; i < _items.length; i++)
  241. {
  242. items.add(createEntry(_items[i]));
  243. }
  244. }
  245. private JCheckBoxList.Entry createEntry(Object obj)
  246. {
  247. if(obj instanceof JCheckBoxList.Entry)
  248. return (JCheckBoxList.Entry)obj;
  249. else
  250. return new JCheckBoxList.Entry(false,obj);
  251. }
  252. public int getRowCount()
  253. {
  254. return items.size();
  255. }
  256. public int getColumnCount()
  257. {
  258. return 2;
  259. }
  260. @Override
  261. public String getColumnName(int col)
  262. {
  263. return null;
  264. }
  265. public Object getValueAt(int row, int col)
  266. {
  267. JCheckBoxList.Entry entry = items.get(row);
  268. switch(col)
  269. {
  270. case 0:
  271. return Boolean.valueOf(entry.checked);
  272. case 1:
  273. return entry.value;
  274. default:
  275. throw new InternalError();
  276. }
  277. }
  278. @Override
  279. public Class getColumnClass(int col)
  280. {
  281. switch(col)
  282. {
  283. case 0:
  284. return Boolean.class;
  285. case 1:
  286. return String.class;
  287. default:
  288. throw new InternalError();
  289. }
  290. }
  291. @Override
  292. public boolean isCellEditable(int row, int col)
  293. {
  294. JCheckBoxList.Entry entry = items.get(row);
  295. return col == 0 && !entry.caption;
  296. }
  297. @Override
  298. public void setValueAt(Object value, int row, int col)
  299. {
  300. if(col == 0)
  301. {
  302. JCheckBoxList.Entry entry = items.get(row);
  303. if(!entry.caption)
  304. {
  305. entry.checked = (value.equals(Boolean.TRUE));
  306. fireTableRowsUpdated(row,row);
  307. }
  308. }
  309. }
  310. } //}}}