001: /*
002: * LabelCellEditor.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 javax.swing.JLabel;
026: import javax.swing.JTable;
027: import javax.swing.event.EventListenerList;
028: import javax.swing.event.ChangeEvent;
029: import javax.swing.event.CellEditorListener;
030: import javax.swing.table.TableCellEditor;
031:
032: import java.util.EventObject;
033:
034: /* ----------------------------------------------------------
035: * CVS NOTE: Changes to the CVS repository prior to the
036: * release of version 3.0.0beta1 has meant a
037: * resetting of CVS revision numbers.
038: * ----------------------------------------------------------
039: */
040:
041: /**
042: * A Class class.
043: * <P>
044: * @author Takis Diakoumis
045: * @version $Revision: 1.4 $
046: * @date $Date: 2006/05/14 06:56:07 $
047: */
048: public class LabelCellEditor extends JLabel implements TableCellEditor {
049:
050: protected EventListenerList listenerList = new EventListenerList();
051: protected ChangeEvent changeEvent = new ChangeEvent(this );
052:
053: /**
054: * Constructor
055: */
056: public LabelCellEditor(int align) {
057: super ();
058:
059: setHorizontalAlignment(align);
060: setBorder(null);
061: }
062:
063: public void addCellEditorListener(CellEditorListener listener) {
064: listenerList.add(CellEditorListener.class, listener);
065: }
066:
067: public void removeCellEditorListener(CellEditorListener listener) {
068: listenerList.remove(CellEditorListener.class, listener);
069: }
070:
071: protected void fireEditingStopped() {
072: CellEditorListener listener;
073: Object[] listeners = listenerList.getListenerList();
074: for (int i = 0; i < listeners.length; i++) {
075: if (listeners[i] == CellEditorListener.class) {
076: listener = (CellEditorListener) listeners[i + 1];
077: listener.editingStopped(changeEvent);
078: }
079: }
080: }
081:
082: protected void fireEditingCanceled() {
083: CellEditorListener listener;
084: Object[] listeners = listenerList.getListenerList();
085: for (int i = 0; i < listeners.length; i++) {
086: if (listeners[i] == CellEditorListener.class) {
087: listener = (CellEditorListener) listeners[i + 1];
088: listener.editingCanceled(changeEvent);
089: }
090: }
091: }
092:
093: public void cancelCellEditing() {
094: fireEditingCanceled();
095: }
096:
097: public boolean stopCellEditing() {
098: fireEditingStopped();
099: return true;
100: }
101:
102: public boolean isCellEditable(EventObject event) {
103: return true;
104: }
105:
106: public boolean shouldSelectCell(EventObject event) {
107: return true;
108: }
109:
110: public Object getCellEditorValue() {
111: return getText();
112: }
113:
114: public Component getTableCellEditorComponent(JTable table,
115: Object value, boolean isSelected, int row, int column) {
116: String type = (String) value;
117: setText(type);
118: return this;
119: }
120:
121: }
|