001: package com.xoetrope.swing;
002:
003: import java.awt.Color;
004: import java.awt.Component;
005: import java.awt.event.MouseAdapter;
006: import java.awt.event.MouseEvent;
007: import javax.swing.JCheckBox;
008: import javax.swing.JList;
009: import javax.swing.ListCellRenderer;
010: import javax.swing.ListModel;
011: import javax.swing.ListSelectionModel;
012: import javax.swing.UIManager;
013: import javax.swing.border.Border;
014: import javax.swing.border.EmptyBorder;
015: import net.xoetrope.swing.XList;
016: import net.xoetrope.xui.helper.XuiUtilities;
017: import com.xoetrope.swing.list.XCheckListCellRenderer;
018:
019: /**
020: * A list box that contains a list of check boxes
021: *
022: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
023: * the GNU Public License (GPL), please see license.txt for more details. If
024: * you make commercial use of this software you must purchase a commercial
025: * license from Xoetrope.</p>
026: * <p> $Revision: 1.8 $</p>
027: */
028: public class XCheckList extends XList {
029: private XCheckListCellRenderer renderer;
030:
031: /**
032: * Create a new checked list
033: */
034: public XCheckList() {
035: super ();
036: init();
037: }
038:
039: /**
040: * Create a new checked list
041: * @param items an array of items to add to the list
042: */
043: public XCheckList(Object[] items) {
044: super ();
045:
046: JCheckBox[] checkItems = new JCheckBox[items.length];
047: for (int i = 0; i < items.length; i++)
048: checkItems[i] = new JCheckBox(items[i].toString());
049:
050: setListData(checkItems);
051: init();
052: }
053:
054: void init() {
055: setCellRenderer(renderer = new XCheckListCellRenderer(this ));
056:
057: addMouseListener(new MouseAdapter() {
058: public void mousePressed(MouseEvent e) {
059: int index = locationToIndex(e.getPoint());
060:
061: if (index != -1) {
062: JCheckBox checkbox = (JCheckBox) getModel()
063: .getElementAt(index);
064: checkbox.setSelected(!checkbox.isSelected());
065: repaint();
066: }
067: }
068: });
069:
070: setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
071: }
072:
073: /**
074: * Set the list to its default selection state
075: */
076: public void setDefaultSelection() {
077: }
078:
079: /**
080: * Add an item to the listmodel
081: * @param s the item to be added
082: */
083: public void addItem(String s) {
084: listModel.add(listModel.getSize(), new JCheckBox(s));
085: }
086:
087: /**
088: * Set the colors for alternate ( odd ) row colors
089: * @param frgd the foreground color
090: * @param bkgd the background color
091: */
092: public void setAltUnselectedColors(Color frgd, Color bkgd) {
093: renderer.setAltUnselectedColors(frgd, bkgd);
094: }
095:
096: /**
097: * Sets the delegate that is used to paint each cell in the list.
098: * The job of a cell renderer is discussed in detail in the
099: * <a href="#renderer">class level documentation</a>.
100: * <p>
101: * If the {@code prototypeCellValue} property is {@code non-null},
102: * setting the cell renderer also causes the {@code fixedCellWidth} and
103: * {@code fixedCellHeight} properties to be re-calculated. Only one
104: * <code>PropertyChangeEvent</code> is generated however -
105: * for the <code>cellRenderer</code> property.
106: * <p>
107: * The default value of this property is provided by the {@code ListUI}
108: * delegate, i.e. by the look and feel implementation.
109: * <p>
110: * This is a JavaBeans bound property.
111: *
112: * @param cellRenderer the <code>ListCellRenderer</code>
113: * that paints list cells
114: * @see #getCellRenderer
115: * @beaninfo
116: * bound: true
117: * attribute: visualUpdate true
118: * description: The component used to draw the cells.
119: */
120: public void setCellRenderer(ListCellRenderer cellRenderer) {
121: if (cellRenderer instanceof XCheckListCellRenderer)
122: renderer = (XCheckListCellRenderer) cellRenderer;
123:
124: super .setCellRenderer(cellRenderer);
125: }
126:
127: /**
128: * @return the selected objects in the list
129: */
130: public Object[] getSelectedObjects() {
131: return getSelectedItems();
132: }
133:
134: /**
135: * Get a list of the checked items in the list
136: * @return the checked items
137: */
138: public String[] getSelectedItems() {
139: ListModel lm = getModel();
140: int listItems = lm.getSize();
141:
142: // Count the selected items
143: int numSelected = 0;
144: for (int i = 0; i < listItems; i++) {
145: if (((JCheckBox) lm.getElementAt(i)).isSelected())
146: numSelected++;
147: }
148:
149: // Store the selected texts.
150: String selItems[] = new String[numSelected];
151: int j = 0;
152: for (int i = 0; i < listItems; i++) {
153: JCheckBox cbi = (JCheckBox) lm.getElementAt(i);
154: if (cbi.isSelected())
155: selItems[j++] = cbi.getText();
156: }
157:
158: return selItems;
159: }
160:
161: /**
162: * Enable the selection of an item in the list
163: * @param idx the index of the item
164: * @param selected true to force selection of the item
165: */
166: public void enableItem(int idx, boolean selected) {
167: ((JCheckBox) getModel().getElementAt(idx)).setEnabled(selected);
168: }
169:
170: /**
171: * Force the selection of an item in the list
172: * @param idx the index of the item
173: * @param selected true to force selection of the item
174: */
175: public void selectItem(int idx, boolean selected) {
176: ((JCheckBox) getModel().getElementAt(idx))
177: .setSelected(selected);
178: }
179:
180: /**
181: * Get the checked state of the indexed item
182: * @param idx the index of the item
183: * @return true if the item is checked
184: */
185: public boolean isItemChecked(int idx) {
186: return ((JCheckBox) getModel().getElementAt(idx)).isSelected();
187: }
188:
189: /**
190: * Select/unselect all the items in the list
191: * @param select true to select the items, false to unselect
192: */
193: public void toggleAll(boolean select) {
194: ListModel lm = getModel();
195: int listItems = lm.getSize();
196:
197: for (int i = 0; i < listItems; i++)
198: ((JCheckBox) lm.getElementAt(i)).setSelected(select);
199:
200: if (invoker != null)
201: invoker.invoke();
202:
203: repaint();
204: }
205:
206: /**
207: * Get the list selection mode. This component returns 2. The selection
208: * corresponds to the items that are checked and not the items that are
209: * highlighted.
210: * @return
211: * <ul>
212: * <li>0=ListSelectionModel.SINGLE_SELECTION,</li>
213: * <li>1=ListSelectionModel.SINGLE_INTERVAL_SELECTION,</li>
214: * <li>2=ListSelectionModel.MULTIPLE_INTERVAL_SELECTION</li>
215: * </ul>
216: */
217: public int getSelectionMode() {
218: return 2;
219: }
220: }
|