PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 243 lines | 173 code | 28 blank | 42 comment | 11 complexity | e381b2a50ebc6257222c5092bdf8db98 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. * Copyright (C) 2000, 2001 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package org.gjt.sp.jedit.gui;
  20. import javax.swing.table.*;
  21. import javax.swing.*;
  22. import java.util.Vector;
  23. /**
  24. * @since jEdit 3.2pre9
  25. */
  26. public class JCheckBoxList extends JTable
  27. {
  28. /**
  29. * Creates a checkbox list with the given list of objects. The elements
  30. * of this array can either be Entry instances, or other objects (if the
  31. * latter, they will default to being unchecked).
  32. */
  33. public JCheckBoxList(Object[] items)
  34. {
  35. setModel(items);
  36. }
  37. /**
  38. * Creates a checkbox list with the given list of objects. The elements
  39. * of this vector can either be Entry instances, or other objects (if the
  40. * latter, they will default to being unchecked).
  41. */
  42. public JCheckBoxList(Vector items)
  43. {
  44. setModel(items);
  45. }
  46. /**
  47. * Sets the model to the given list of objects. The elements of this
  48. * array can either be Entry instances, or other objects (if the
  49. * latter, they will default to being unchecked).
  50. */
  51. public void setModel(Object[] items)
  52. {
  53. setModel(new CheckBoxListModel(items));
  54. init();
  55. }
  56. /**
  57. * Sets the model to the given list of objects. The elements of this
  58. * vector can either be Entry instances, or other objects (if the
  59. * latter, they will default to being unchecked).
  60. */
  61. public void setModel(Vector items)
  62. {
  63. setModel(new CheckBoxListModel(items));
  64. init();
  65. }
  66. public Object[] getCheckedValues()
  67. {
  68. Vector values = new Vector();
  69. CheckBoxListModel model = (CheckBoxListModel)getModel();
  70. for(int i = 0; i < model.items.size(); i++)
  71. {
  72. Entry entry = (Entry)model.items.elementAt(i);
  73. if(entry.checked)
  74. values.addElement(entry.value);
  75. }
  76. Object[] retVal = new Object[values.size()];
  77. values.copyInto(retVal);
  78. return retVal;
  79. }
  80. public void selectAll()
  81. {
  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. entry.checked = true;
  87. }
  88. model.fireTableRowsUpdated(0,model.getRowCount());
  89. }
  90. public Entry[] getValues()
  91. {
  92. CheckBoxListModel model = (CheckBoxListModel)getModel();
  93. Entry[] retVal = new Entry[model.items.size()];
  94. model.items.copyInto(retVal);
  95. return retVal;
  96. }
  97. public Object getSelectedValue()
  98. {
  99. int row = getSelectedRow();
  100. if(row == -1)
  101. return null;
  102. else
  103. return getModel().getValueAt(row,1);
  104. }
  105. // private members
  106. private void init()
  107. {
  108. getSelectionModel().setSelectionMode(ListSelectionModel
  109. .SINGLE_SELECTION);
  110. setShowGrid(false);
  111. setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
  112. TableColumn column = getColumnModel().getColumn(0);
  113. int checkBoxWidth = new JCheckBox().getPreferredSize().width;
  114. column.setPreferredWidth(checkBoxWidth);
  115. column.setMinWidth(checkBoxWidth);
  116. column.setWidth(checkBoxWidth);
  117. column.setMaxWidth(checkBoxWidth);
  118. column.setResizable(false);
  119. }
  120. public static class Entry
  121. {
  122. boolean checked;
  123. Object value;
  124. public Entry(boolean checked, Object value)
  125. {
  126. this.checked = checked;
  127. this.value = value;
  128. }
  129. public boolean isChecked()
  130. {
  131. return checked;
  132. }
  133. public Object getValue()
  134. {
  135. return value;
  136. }
  137. }
  138. }
  139. class CheckBoxListModel extends AbstractTableModel
  140. {
  141. Vector items;
  142. CheckBoxListModel(Vector _items)
  143. {
  144. items = new Vector(_items.size());
  145. for(int i = 0; i < _items.size(); i++)
  146. {
  147. items.addElement(createEntry(_items.elementAt(i)));
  148. }
  149. }
  150. CheckBoxListModel(Object[] _items)
  151. {
  152. items = new Vector(_items.length);
  153. for(int i = 0; i < _items.length; i++)
  154. {
  155. items.addElement(createEntry(_items[i]));
  156. }
  157. }
  158. private JCheckBoxList.Entry createEntry(Object obj)
  159. {
  160. if(obj instanceof JCheckBoxList.Entry)
  161. return (JCheckBoxList.Entry)obj;
  162. else
  163. return new JCheckBoxList.Entry(false,obj);
  164. }
  165. public int getRowCount()
  166. {
  167. return items.size();
  168. }
  169. public int getColumnCount()
  170. {
  171. return 2;
  172. }
  173. public String getColumnName(int col)
  174. {
  175. return null;
  176. }
  177. public Object getValueAt(int row, int col)
  178. {
  179. JCheckBoxList.Entry entry = (JCheckBoxList.Entry)items.elementAt(row);
  180. switch(col)
  181. {
  182. case 0:
  183. return new Boolean(entry.checked);
  184. case 1:
  185. return entry.value;
  186. default:
  187. throw new InternalError();
  188. }
  189. }
  190. public Class getColumnClass(int col)
  191. {
  192. switch(col)
  193. {
  194. case 0:
  195. return Boolean.class;
  196. case 1:
  197. return String.class;
  198. default:
  199. throw new InternalError();
  200. }
  201. }
  202. public boolean isCellEditable(int row, int col)
  203. {
  204. return col == 0;
  205. }
  206. public void setValueAt(Object value, int row, int col)
  207. {
  208. if(col == 0)
  209. {
  210. ((JCheckBoxList.Entry)items.elementAt(row)).checked =
  211. (value.equals(Boolean.TRUE));
  212. fireTableRowsUpdated(row,row);
  213. }
  214. }
  215. }