001: package snow.sortabletable;
002:
003: import snow.utils.gui.FontSelector;
004:
005: import javax.swing.table.*;
006: import javax.swing.*;
007: import javax.swing.event.*;
008: import javax.swing.border.*;
009: import java.awt.*;
010: import java.text.*;
011: import javax.swing.text.*;
012: import java.awt.font.*;
013: import java.awt.event.*;
014: import java.awt.geom.*;
015: import java.nio.*;
016: import java.nio.charset.*;
017: import java.util.*;
018:
019: public class UniversalTableEditor extends AbstractCellEditor implements
020: TableCellEditor {
021: protected final ColorCellEditor colorCellEditor = new ColorCellEditor();
022: protected final DefaultCellEditor textFieldDefaultCellEditor = new DefaultCellEditor(
023: new JTextField());
024: protected final DefaultCellEditor comboBoxDefaultCellEditor = new DefaultCellEditor(
025: new JComboBox());
026: protected final DefaultCellEditor checkBoxDefaultCellEditor = new DefaultCellEditor(
027: new JCheckBox());
028: protected final FontCellEditor fontCellEditor = new FontCellEditor();
029: protected final NumberTableCellEditor numberTableCellEditor = new NumberTableCellEditor();
030:
031: private CellEditor selectedEditor = null;
032:
033: public UniversalTableEditor() {
034:
035: } // Constructor
036:
037: // CELL EDITOR INTERFACE
038: //
039: @Override
040: public void addCellEditorListener(CellEditorListener l) { /*
041: colorCellEditor.addCellEditorListener(l);
042: textFieldDefaultCellEditor.addCellEditorListener(l);
043: comboBoxDefaultCellEditor.addCellEditorListener(l);
044: checkBoxDefaultCellEditor.addCellEditorListener(l);
045: fontCellEditor.addCellEditorListener(l);
046: numberTableCellEditor.addCellEditorListener(l);*/
047:
048: if (selectedEditor != null) {
049: selectedEditor.addCellEditorListener(l);
050: }
051:
052: }
053:
054: @Override
055: public void removeCellEditorListener(CellEditorListener l) { /*
056: colorCellEditor.removeCellEditorListener(l);
057: textFieldDefaultCellEditor.removeCellEditorListener(l);
058: comboBoxDefaultCellEditor.removeCellEditorListener(l);
059: checkBoxDefaultCellEditor.removeCellEditorListener(l);
060: fontCellEditor.removeCellEditorListener(l);
061: numberTableCellEditor.removeCellEditorListener(l); */
062: if (selectedEditor != null) {
063: selectedEditor.removeCellEditorListener(l);
064: }
065:
066: }
067:
068: @Override
069: public void cancelCellEditing() { /*
070: colorCellEditor.cancelCellEditing();
071: textFieldDefaultCellEditor.cancelCellEditing();
072: comboBoxDefaultCellEditor.cancelCellEditing();
073: checkBoxDefaultCellEditor.cancelCellEditing();
074: fontCellEditor.cancelCellEditing();
075: numberTableCellEditor.cancelCellEditing(); */
076: if (selectedEditor != null) {
077: selectedEditor.cancelCellEditing();
078: }
079:
080: }
081:
082: @Override
083: public boolean stopCellEditing() {
084: /*
085: colorCellEditor.stopCellEditing();
086: textFieldDefaultCellEditor.stopCellEditing();
087: comboBoxDefaultCellEditor.stopCellEditing();
088: checkBoxDefaultCellEditor.stopCellEditing();
089: fontCellEditor.stopCellEditing();
090: numberTableCellEditor.stopCellEditing();
091: return true;
092: */
093: if (selectedEditor != null) {
094: return selectedEditor.stopCellEditing();
095: }
096: return true;
097: }
098:
099: public Object getCellEditorValue() {
100: if (selectedEditor != null) {
101: return selectedEditor.getCellEditorValue();
102: }
103: return null;
104: //textFieldDefaultCellEditor.getCellEditorValue();
105: }
106:
107: @Override
108: public boolean isCellEditable(EventObject anEvent) {
109: //System.out.println(""+anEvent);
110: if (selectedEditor != null) {
111: return selectedEditor.isCellEditable(anEvent);
112: }
113: // if false, nothing will be editable !!
114: return true;
115: }
116:
117: @Override
118: public boolean shouldSelectCell(EventObject anEvent) {
119: if (selectedEditor != null) {
120: return selectedEditor.shouldSelectCell(anEvent);
121: }
122: return true; // normally true, maybe no for checkboxes...
123: }
124:
125: /** TableCellEditor interface impl
126: */
127: public Component getTableCellEditorComponent(JTable table,
128: Object value, boolean isSelected, int row, int column) {
129: // is null allowed ?? yes, may be casted for example to color
130: // if(value==null) return new JLabel("No editor for null value");
131:
132: if (value instanceof Color) {
133: selectedEditor = colorCellEditor;
134: return colorCellEditor.getTableCellEditorComponent(table,
135: (Color) value, isSelected, row, column);
136: } else if (value instanceof Boolean) {
137: selectedEditor = checkBoxDefaultCellEditor;
138: return checkBoxDefaultCellEditor
139: .getTableCellEditorComponent(table, value,
140: isSelected, row, column);
141: } else if (value instanceof Font) {
142: selectedEditor = fontCellEditor;
143: return fontCellEditor.getTableCellEditorComponent(table,
144: value, isSelected, row, column);
145: } else if (value instanceof Double || value instanceof Float
146: || value instanceof Integer) {
147: selectedEditor = numberTableCellEditor;
148: return numberTableCellEditor.getTableCellEditorComponent(
149: table, value, isSelected, row, column);
150: } else {
151: // default
152: selectedEditor = textFieldDefaultCellEditor;
153: return textFieldDefaultCellEditor
154: .getTableCellEditorComponent(table, value,
155: isSelected, row, column);
156: }
157:
158: // return new JLabel("NO EDITOR FOR "+value.getClass().getName());
159: }
160:
161: class FontCellEditor extends AbstractCellEditor implements
162: TableCellEditor, ActionListener {
163: Font currentFont;
164: JButton button;
165: //FontSelector fontChooser;
166: JDialog dialog;
167: protected static final String EDIT = "edit";
168:
169: public FontCellEditor() {
170: button = new JButton();
171: button.setActionCommand(EDIT);
172: button.addActionListener(this );
173: button.setBorderPainted(false);
174:
175: //Set up the dialog that the button brings up.
176: //fontChooser = new FontSelector.();
177: }
178:
179: public void actionPerformed(ActionEvent e) {
180: if (EDIT.equals(e.getActionCommand())) {
181: //The user has clicked the cell, so
182: //bring up the dialog.
183: //button.setBackground(currentColor);
184: //colorChooser.setColor(currentColor);
185: //dialog.setVisible(true);
186:
187: //fontChooser.setFont(currentFont);
188: currentFont = FontSelector.chooseFont(button,
189: currentFont);
190:
191: fireEditingStopped(); //Make the renderer reappear.
192:
193: } else { //User pressed dialog's "OK" button.
194: //currentFont = fontChooser.getFont();
195: }
196: }
197:
198: //Implement the one CellEditor method that AbstractCellEditor doesn't.
199: public Object getCellEditorValue() {
200: return currentFont;
201: }
202:
203: //Implement the one method defined by TableCellEditor.
204: public Component getTableCellEditorComponent(JTable table,
205: Object value, boolean isSelected, int row, int column) {
206: if (value instanceof Font) {
207: currentFont = (Font) value;
208: }
209: return button;
210: }
211: }
212:
213: }
|