001: /*
002: * ComboBoxCellEditor.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.table;
023:
024: import java.awt.Component;
025: import java.awt.event.ActionListener;
026: import java.awt.event.ActionEvent;
027: import javax.swing.JComboBox;
028: import javax.swing.JTable;
029: import javax.swing.event.EventListenerList;
030: import javax.swing.event.ChangeEvent;
031: import javax.swing.event.CellEditorListener;
032: import javax.swing.table.TableCellEditor;
033:
034: import java.util.EventObject;
035: import java.util.Vector;
036: import javax.swing.DefaultComboBoxModel;
037:
038: /* ----------------------------------------------------------
039: * CVS NOTE: Changes to the CVS repository prior to the
040: * release of version 3.0.0beta1 has meant a
041: * resetting of CVS revision numbers.
042: * ----------------------------------------------------------
043: */
044:
045: /**
046: * A Class class.
047: * <P>
048: * @author Takis Diakoumis
049: * @version $Revision: 1.4 $
050: * @date $Date: 2006/05/14 06:56:07 $
051: */
052: public class ComboBoxCellEditor extends JComboBox implements
053: TableCellEditor {
054:
055: protected EventListenerList listenerList = new EventListenerList();
056: protected ChangeEvent changeEvent = new ChangeEvent(this );
057:
058: public static final int INTEGER = 0;
059: public static final int STRING = 1;
060:
061: private int d_type;
062:
063: public ComboBoxCellEditor() {
064: super ();
065: d_type = STRING;
066: addActionListener(new ActionListener() {
067: public void actionPerformed(ActionEvent event) {
068: fireEditingStopped();
069: }
070: });
071: }
072:
073: public ComboBoxCellEditor(String[] values) {
074: super (values);
075: d_type = STRING;
076: addActionListener(new ActionListener() {
077: public void actionPerformed(ActionEvent event) {
078: fireEditingStopped();
079: }
080: });
081: }
082:
083: public ComboBoxCellEditor(Object[] values) {
084: super (values);
085: d_type = STRING;
086: addActionListener(new ActionListener() {
087: public void actionPerformed(ActionEvent event) {
088: fireEditingStopped();
089: }
090: });
091: }
092:
093: public ComboBoxCellEditor(Vector values) {
094: super (values);
095: d_type = STRING;
096: addActionListener(new ActionListener() {
097: public void actionPerformed(ActionEvent event) {
098: fireEditingStopped();
099: }
100: });
101: }
102:
103: public ComboBoxCellEditor(Vector values, int type) {
104: super (values);
105: d_type = type;
106: addActionListener(new ActionListener() {
107: public void actionPerformed(ActionEvent event) {
108: fireEditingStopped();
109: }
110: });
111: }
112:
113: public void addCellEditorListener(CellEditorListener listener) {
114: listenerList.add(CellEditorListener.class, listener);
115: }
116:
117: public void removeCellEditorListener(CellEditorListener listener) {
118: listenerList.remove(CellEditorListener.class, listener);
119: }
120:
121: protected void fireEditingStopped() {
122: CellEditorListener listener;
123: Object[] listeners = listenerList.getListenerList();
124: for (int i = 0; i < listeners.length; i++) {
125: if (listeners[i] == CellEditorListener.class) {
126: listener = (CellEditorListener) listeners[i + 1];
127: listener.editingStopped(changeEvent);
128: }
129: }
130: }
131:
132: protected void fireEditingCanceled() {
133: CellEditorListener listener;
134: Object[] listeners = listenerList.getListenerList();
135: for (int i = 0; i < listeners.length; i++) {
136: if (listeners[i] == CellEditorListener.class) {
137: listener = (CellEditorListener) listeners[i + 1];
138: listener.editingCanceled(changeEvent);
139: }
140: }
141: }
142:
143: public void cancelCellEditing() {
144: fireEditingCanceled();
145: }
146:
147: public boolean stopCellEditing() {
148: fireEditingStopped();
149: return true;
150: }
151:
152: public boolean isCellEditable(EventObject event) {
153: return true;
154: }
155:
156: public boolean shouldSelectCell(EventObject event) {
157: return true;
158: }
159:
160: public Object getCellEditorValue() {
161: return getSelectedItem();
162: }
163:
164: public void setSelectionValues(Object[] values) {
165: DefaultComboBoxModel model = (DefaultComboBoxModel) getModel();
166: model.removeAllElements();
167: for (int i = 0; i < values.length; i++) {
168: model.addElement(values[i]);
169: }
170: // select the first item
171: model.setSelectedItem(values[0]);
172: }
173:
174: public Component getTableCellEditorComponent(JTable table,
175: Object value, boolean isSelected, int row, int column) {
176: if (d_type == STRING) {
177: String type = (String) value;
178: setSelectedItem(type);
179: } else if (d_type == INTEGER) {
180: Integer type = (Integer) value;
181: setSelectedItem(type);
182: }
183: return this ;
184: }
185:
186: public void setSelection(int i) {
187: setSelectedIndex(i);
188: }
189:
190: }
|