001: /*
002: * EachRowEditor.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.MouseEvent;
026: import java.util.EventObject;
027: import java.util.Hashtable;
028: import javax.swing.DefaultCellEditor;
029: import javax.swing.JTable;
030: import javax.swing.JTextField;
031: import javax.swing.event.CellEditorListener;
032: import javax.swing.table.TableCellEditor;
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: *
043: * @author Takis Diakoumis
044: * @version $Revision: 1.4 $
045: * @date $Date: 2006/05/14 06:56:07 $
046: */
047: public class EachRowEditor implements TableCellEditor {
048:
049: protected Hashtable editors;
050: protected TableCellEditor editor;
051: protected TableCellEditor defaultEditor;
052: protected JTable table;
053:
054: /**
055: * Constructs a EachRowEditor.
056: * create default editor
057: *
058: * @see TableCellEditor
059: * @see DefaultCellEditor
060: */
061: public EachRowEditor(JTable table) {
062: this .table = table;
063: editors = new Hashtable();
064: JTextField defaultField = new JTextField();
065: defaultField.setBorder(null);
066: defaultEditor = new DefaultCellEditor(defaultField);
067: }
068:
069: /**
070: * @param row table row
071: * @param editor table cell editor
072: */
073: public void setEditorAt(int row, TableCellEditor editor) {
074: editors.put(new Integer(row), editor);
075: }
076:
077: public Component getTableCellEditorComponent(JTable table,
078: Object value, boolean isSelected, int row, int column) {
079:
080: Component _editor = editor.getTableCellEditorComponent(table,
081: value, isSelected, row, column);
082: _editor.setFont(table.getFont());
083: return _editor;
084: }
085:
086: public Object getCellEditorValue() {
087: return editor.getCellEditorValue();
088: }
089:
090: public boolean stopCellEditing() {
091: return editor.stopCellEditing();
092: }
093:
094: public void cancelCellEditing() {
095: editor.cancelCellEditing();
096: }
097:
098: public boolean isCellEditable(EventObject anEvent) {
099: selectEditor((MouseEvent) anEvent);
100: return editor.isCellEditable(anEvent);
101: }
102:
103: public void addCellEditorListener(CellEditorListener l) {
104: editor.addCellEditorListener(l);
105: }
106:
107: public void removeCellEditorListener(CellEditorListener l) {
108: editor.removeCellEditorListener(l);
109: }
110:
111: public boolean shouldSelectCell(EventObject anEvent) {
112: selectEditor((MouseEvent) anEvent);
113: return editor.shouldSelectCell(anEvent);
114: }
115:
116: protected void selectEditor(MouseEvent e) {
117: int row;
118: if (e == null) {
119: row = table.getSelectionModel().getAnchorSelectionIndex();
120: } else {
121: row = table.rowAtPoint(e.getPoint());
122: }
123: editor = (TableCellEditor) editors.get(new Integer(row));
124: if (editor == null) {
125: editor = defaultEditor;
126: }
127: }
128: }
|