001: package com.xoetrope.swing;
002:
003: import java.awt.Component;
004: import java.awt.event.MouseAdapter;
005: import java.awt.event.MouseEvent;
006: import javax.swing.ComboBoxEditor;
007: import javax.swing.DefaultComboBoxModel;
008: import javax.swing.DefaultListCellRenderer;
009: import javax.swing.JCheckBox;
010: import javax.swing.JLabel;
011: import javax.swing.JList;
012: import javax.swing.JTextField;
013: import javax.swing.ListModel;
014: import javax.swing.UIManager;
015: import javax.swing.border.Border;
016: import javax.swing.border.EmptyBorder;
017: import net.xoetrope.swing.XComboBox;
018: import net.xoetrope.xui.XAttributedComponent;
019:
020: /**
021: * A Combo box that contains a Combo of check boxes
022: *
023: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
024: * the GNU Public License (GPL), please see license.txt for more details. If
025: * you make commercial use of this software you must purchase a commercial
026: * license from Xoetrope.</p>
027: * <p> $Revision: 1.5 $</p>
028: */
029: public class XCheckCombo extends XComboBox implements
030: XAttributedComponent {
031: protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
032: private DefaultComboBoxModel model;
033: private String itemSeparator;
034: private String editorText;
035:
036: /**
037: * Create a new checked Combo
038: */
039: public XCheckCombo() {
040: super ();
041: itemSeparator = " ";
042:
043: init();
044: }
045:
046: /**
047: * Create a new checked Combo
048: * @param items an array of items to add to the Combo
049: */
050: public XCheckCombo(Object[] items) {
051: super ();
052:
053: for (int i = 0; i < items.length; i++)
054: addItem(items[i].toString());
055:
056: init();
057: }
058:
059: /**
060: * Add an item to the listmodel
061: * @param s the item to be added
062: */
063: public void addItem(String s) {
064: model.addElement(new JCheckBox(s));
065: }
066:
067: // public void setListData( final Object[] listData )
068: // {
069: // setModel( new ComboBoxModel()
070: // {
071: // private Object selection;
072: //
073: // public int getSize()
074: // {
075: // return listData.length;
076: // }
077: //
078: // public Object getElementAt( int i )
079: // {
080: // return listData[ i ];
081: // }
082: //
083: // public void setSelectedItem( Object anItem )
084: // {
085: // selection = anItem;
086: // }
087: //
088: // public Object getSelectedItem()
089: // {
090: // return selection;
091: // }
092: // });
093: // }
094:
095: void init() {
096: model = new DefaultComboBoxModel();
097: setModel(model);
098:
099: setEditable(true);
100: getEditor().getEditorComponent().setEnabled(false);
101:
102: setRenderer(new XCheckComboCellRenderer());
103: }
104:
105: /**
106: * Initializes the editor with the specified item.
107: *
108: * @param anEditor the <code>ComboBoxEditor</code> that displays
109: * the list item in the
110: * combo box field and allows it to be edited
111: * @param anItem the object to display and edit in the field
112: */
113: public void configureEditor(ComboBoxEditor anEditor, Object anItem) {
114: editorText = "";
115: int items = model.getSize();
116: for (int i = 0; i < items; i++) {
117: JCheckBox cb = (JCheckBox) model.getElementAt(i);
118: if (cb.isSelected()) {
119: if (editorText.length() > 0)
120: editorText += itemSeparator;
121: editorText += cb.getText();
122: }
123: }
124:
125: anEditor.setItem(editorText);
126: }
127:
128: /**
129: * Returns the first item in the list that matches the given item.
130: * The result is not always defined if the <code>JComboBox</code>
131: * allows selected items that are not in the list.
132: * Returns -1 if there is no selected item or if the user specified
133: * an item which is not in the list.
134:
135: * @return an integer specifying the currently selected list item,
136: * where 0 specifies
137: * the first item in the list;
138: * or -1 if no item is selected or if
139: * the currently selected item is not in the list
140: */
141: public int getSelectedIndex() {
142: return -1;
143: }
144:
145: public void setSelectedItem(Object selObj) {
146: if (selObj instanceof JCheckBox) {
147: JCheckBox checkbox = (JCheckBox) selObj;
148: checkbox.setSelected(!checkbox.isSelected());
149: //((JCheckBox)selObj).setSelected( true );
150: return;
151: }
152:
153: String s = null;
154: String[] selections = null;
155: if (selObj != null) {
156: s = selObj.toString();
157: selections = s.split(itemSeparator);
158: }
159:
160: int items = model.getSize();
161: for (int i = 0; i < items; i++) {
162: JCheckBox cb = (JCheckBox) model.getElementAt(i);
163: boolean isSelected = false;
164: if (s != null) {
165: String caption = cb.getText();
166: for (int j = 0; j < selections.length; j++) {
167: if (selections[j].equals(caption)) {
168: isSelected = true;
169: break;
170: }
171: }
172: }
173: cb.setSelected(isSelected);
174: }
175:
176: configureEditor(getEditor(), null);
177: }
178:
179: /**
180: * Clear the list selections
181: */
182: public void clearSelection() {
183: int items = model.getSize();
184: for (int i = 0; i < items; i++) {
185: JCheckBox cb = (JCheckBox) model.getElementAt(i);
186: cb.setSelected(false);
187: }
188:
189: configureEditor(getEditor(), null);
190: }
191:
192: // /**
193: // * Add an item to the Combomodel
194: // * @param s the item to be added
195: // */
196: // public void addItem( String s )
197: // {
198: // listModel.add( listModel.getSize(), new JCheckBox( s ));
199: // }
200:
201: protected class XCheckComboCellRenderer extends
202: DefaultListCellRenderer {
203: private JLabel label = new JLabel();
204:
205: public Component getListCellRendererComponent(JList list,
206: Object value, int index, boolean isSelected,
207: boolean cellHasFocus) {
208: if (value instanceof JCheckBox) {
209: JCheckBox checkbox = (JCheckBox) value;
210: checkbox.setBackground(isSelected ? list
211: .getSelectionBackground() : list
212: .getBackground());
213: checkbox.setForeground(isSelected ? list
214: .getSelectionForeground() : list
215: .getForeground());
216: checkbox.setEnabled(isEnabled());
217: checkbox.setFont(getFont());
218: checkbox.setFocusPainted(false);
219: checkbox.setBorderPainted(true);
220: checkbox.setBorder(isSelected ? UIManager
221: .getBorder("Combo.focusCellHighlightBorder")
222: : noFocusBorder);
223:
224: return checkbox;
225: }
226:
227: return super .getListCellRendererComponent(list, value,
228: index, isSelected, cellHasFocus);
229: }
230: }
231:
232: /**
233: * Get a Combo of the checked items in the Combo
234: * @return the checked items
235: */
236: public String[] getSelectedItems() {
237: ListModel lm = getModel();
238: int ComboItems = lm.getSize();
239:
240: // Count the selected items
241: int numSelected = 0;
242: for (int i = 0; i < ComboItems; i++) {
243: if (((JCheckBox) lm.getElementAt(i)).isSelected())
244: numSelected++;
245: }
246:
247: // Store the selected texts.
248: String selItems[] = new String[numSelected];
249: int j = 0;
250: for (int i = 0; i < ComboItems; i++) {
251: JCheckBox cbi = (JCheckBox) lm.getElementAt(i);
252: if (cbi.isSelected())
253: selItems[j++] = cbi.getText();
254: }
255:
256: return selItems;
257: }
258:
259: /**
260: * Force the selection of an item in the Combo
261: * @param idx the index of the item
262: * @param selected true to force selection of the item
263: */
264: public void selectItem(int idx, boolean selected) {
265: ((JCheckBox) getModel().getElementAt(idx))
266: .setSelected(selected);
267: }
268:
269: /**
270: * Set the separator text
271: * @param s the separator text
272: */
273: public void setSeparator(String s) {
274: itemSeparator = s;
275: }
276:
277: /**
278: * Set one or more attributes of the component.
279: * <OL>
280: * <LI>separator, value=the text used to separate item once selected</LI>
281: * </OL>
282: * @param attribName the name of the attribute
283: * @param attribValue the value of the attribute
284: * @return 0 for success, non zero for failure or to require some further action
285: */
286: public int setAttribute(String attribName, Object attribValue) {
287: if (attribName.equals("separator"))
288: setSeparator((String) attribValue);
289:
290: return 0;
291: }
292: }
|