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