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.JTable;
21 import javax.swing.table.TableCellRenderer;
22
23 /**
24 * <p>
25 * A specialized renderer implementation that is used for table columns that
26 * define their own custom renderer component.
27 * </p>
28 * <p>
29 * This class is the counterpart of {@link SwingTableCellEditor}
30 * for custom renderers. Its task is to extract the renderer component of a
31 * column from the table definition and to initialize it.
32 * </p>
33 *
34 * @author Oliver Heger
35 * @version $Id: SwingTableCellRenderer.java 205 2012-01-29 18:29:57Z oheger $
36 */
37 class SwingTableCellRenderer implements TableCellRenderer
38 {
39 /** Stores a reference to the associated table model. */
40 private final SwingTableModel model;
41
42 /**
43 * Creates a new instance of <code>SwingTableCellRenderer</code>.
44 *
45 * @param tabModel the associated table model
46 */
47 public SwingTableCellRenderer(SwingTableModel tabModel)
48 {
49 model = tabModel;
50 }
51
52 /**
53 * Returns a reference to the associated table model.
54 *
55 * @return the table model
56 */
57 public SwingTableModel getModel()
58 {
59 return model;
60 }
61
62 /**
63 * Returns the renderer component for the specified cell. This
64 * implementation fetches the <code>TableColumn</code> tag from the
65 * associated table model and obtains the renderer component from it. The
66 * {@link net.sf.jguiraffe.gui.builder.components.tags.table.TableSelectionHandler
67 * TableSelectionHandler} for renderers will be invoked to initialize the
68 * component. Note that there is no need to explicitly set the value
69 * because this was already done through the renderer <code>Form</code> of
70 * the table component.
71 *
72 * @param table the table object
73 * @param value the current value
74 * @param selected a flag whether the cell is selected
75 * @param focused a flag whether the cell has the focus
76 * @param row the current row
77 * @param col the current column
78 * @return the component to be used as cell renderer
79 */
80 public Component getTableCellRendererComponent(JTable table, Object value,
81 boolean selected, boolean focused, int row, int col)
82 {
83 Component renderer =
84 (Component) getModel().getTableTag().getColumn(col)
85 .getRendererComponent();
86 assert getModel().getTableTag().getRendererSelectionHandler() != null
87 : "No renderer selection handler set";
88 getModel()
89 .getTableTag()
90 .getRendererSelectionHandler()
91 .prepareComponent(table, getModel().getTableTag(), renderer,
92 selected, focused, row, col);
93 return renderer;
94 }
95 }