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 javax.swing.BorderFactory;
19 import javax.swing.JComponent;
20 import javax.swing.JTable;
21
22 import net.sf.jguiraffe.gui.builder.components.tags.table.TableSelectionHandler;
23 import net.sf.jguiraffe.gui.builder.components.tags.table.TableTag;
24
25 /**
26 * <p>
27 * A Swing-specific default implementation of the
28 * <code>TableSelectionHandler</code> interface.
29 * </p>
30 * <p>
31 * This implementation expects that the component passed in to the
32 * <code>prepareComponent()</code> method is a
33 * <code>javax.swing.JComponent</code>. Depending on the selected or focused
34 * flags the color of this component will be set, and eventually a border will
35 * be drawn.
36 * </p>
37 *
38 * @author Oliver Heger
39 * @version $Id: SwingTableSelectionHandler.java 205 2012-01-29 18:29:57Z oheger $
40 */
41 public class SwingTableSelectionHandler implements TableSelectionHandler
42 {
43 /**
44 * Prepares the renderer or editor component. This implementation will set
45 * the correct colors and a border for focused cells.
46 *
47 * @param table the table component
48 * @param tableTag the table tag
49 * @param component the component to be prepared
50 * @param selected a flag whether this cell is selected
51 * @param hasFocus a flag whether this cell has the focus
52 * @param row the current row index
53 * @param col the current column index
54 */
55 public void prepareComponent(Object table, TableTag tableTag,
56 Object component, boolean selected, boolean hasFocus, int row,
57 int col)
58 {
59 JComponent c = (JComponent) component;
60 JTable tab = (JTable) table;
61 if (selected)
62 {
63 c.setForeground(tab.getSelectionForeground());
64 c.setBackground(tab.getSelectionBackground());
65 }
66 else
67 {
68 c.setForeground(tab.getForeground());
69 c.setBackground(tab.getBackground());
70 }
71
72 if (hasFocus)
73 {
74 // TODO determine correct color
75 c.setBorder(BorderFactory.createLineBorder(tab
76 .getSelectionBackground()));
77 }
78 else
79 {
80 c.setBorder(null);
81 }
82 }
83 }