1 /*
2 * Copyright 2006-2016 The JGUIraffe Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.jguiraffe.gui.platform.swing.builder.components.table;
17
18 import java.awt.Component;
19
20 import javax.swing.AbstractCellEditor;
21 import javax.swing.JTable;
22 import javax.swing.table.TableCellEditor;
23
24 /**
25 * <p>
26 * A specialized cell editor class for Swing tables.
27 * </p>
28 * <p>
29 * This class is used as editor class for table columns that define a custom
30 * editor. The component that is used as editor is obtained from the
31 * <code>TableTag</code> defining the table. Together with the special table
32 * model and the <code>Form</code> instance constructed for the table the
33 * current values are transfered into the editor component and input validation
34 * can be performed.
35 * </p>
36 * <p>
37 * An instance of this class is responsible for a complete table. The editor
38 * component to be used is obtained from the table definition based on the given
39 * column index. Setting and retrieving the editor's values is of less
40 * importance because this is handled by the table's editor form object.
41 * </p>
42 *
43 * @author Oliver Heger
44 * @version $Id: SwingTableCellEditor.java 205 2012-01-29 18:29:57Z oheger $
45 * @see SwingTableModel
46 */
47 class SwingTableCellEditor extends AbstractCellEditor implements
48 TableCellEditor
49 {
50 /**
51 * The serial version UID.
52 */
53 private static final long serialVersionUID = -5256155233784882938L;
54
55 /** Stores a reference to the model of the associated table. */
56 private SwingTableModel model;
57
58 /** Stores the current row index. */
59 private int currentRow;
60
61 /** Stores the current column index. */
62 private int currentCol;
63
64 /**
65 * Creates a new instance of <code>SwingTableCellEditor</code> and sets
66 * the associated table model.
67 *
68 * @param tabModel the table model
69 */
70 public SwingTableCellEditor(SwingTableModel tabModel)
71 {
72 model = tabModel;
73 }
74
75 /**
76 * Returns a reference to the associated table model.
77 *
78 * @return the table model
79 */
80 public SwingTableModel getModel()
81 {
82 return model;
83 }
84
85 /**
86 * Returns the editor component for the specified cell. This implementation
87 * will return the editor component that was specified in the table
88 * definition.
89 *
90 * @param table the affected table
91 * @param value the current value of this cell
92 * @param selected a flag if the cell is highlighted
93 * @param row the row index
94 * @param col the column index
95 * @return the editor component to be used
96 */
97 public Component getTableCellEditorComponent(JTable table, Object value,
98 boolean selected, int row, int col)
99 {
100 currentCol = col;
101 currentRow = row;
102 Component result = (Component) getModel().getTableTag().getColumn(col)
103 .getEditorComponent();
104 getModel().getTableTag().getEditorSelectionHandler().prepareComponent(
105 getModel().getTable(), getModel().getTableTag(), result, selected,
106 false, row, col);
107 return result;
108 }
109
110 /**
111 * Returns the current value of this editor. This implementation just
112 * returns <b>null</b>. The value will be processed by the table's editor
113 * form and directly written into the table model.
114 *
115 * @return the editor's current value
116 */
117 public Object getCellEditorValue()
118 {
119 return null;
120 }
121
122 /**
123 * Tests whether editing can be stopped. This implementation performs
124 * validation of the user input.
125 *
126 * @return a flag if editing can be stopped (this is the case if the input
127 * is valid)
128 */
129 @Override
130 public boolean stopCellEditing()
131 {
132 getModel().setValueAt(null, currentRow, currentCol);
133 return getModel().validateColumn(currentCol);
134 }
135 }