1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * ArffTableCellRenderer.java |
---|
19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.gui.arffviewer; |
---|
24 | |
---|
25 | import weka.core.Attribute; |
---|
26 | import java.awt.Color; |
---|
27 | import java.awt.Component; |
---|
28 | import javax.swing.JTable; |
---|
29 | import javax.swing.SwingConstants; |
---|
30 | import javax.swing.UIManager; |
---|
31 | import javax.swing.table.DefaultTableCellRenderer; |
---|
32 | |
---|
33 | /** |
---|
34 | * Handles the background colors for missing values differently than the |
---|
35 | * DefaultTableCellRenderer. |
---|
36 | * |
---|
37 | * |
---|
38 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
39 | * @version $Revision: 1.4 $ |
---|
40 | */ |
---|
41 | |
---|
42 | public class ArffTableCellRenderer |
---|
43 | extends DefaultTableCellRenderer { |
---|
44 | |
---|
45 | /** for serialization */ |
---|
46 | static final long serialVersionUID = 9195794493301191171L; |
---|
47 | |
---|
48 | /** the color for missing values */ |
---|
49 | private Color missingColor; |
---|
50 | /** the color for selected missing values */ |
---|
51 | private Color missingColorSelected; |
---|
52 | /** the color for highlighted values */ |
---|
53 | private Color highlightColor; |
---|
54 | /** the color for selected highlighted values */ |
---|
55 | private Color highlightColorSelected; |
---|
56 | |
---|
57 | /** |
---|
58 | * initializes the Renderer with a standard color |
---|
59 | */ |
---|
60 | public ArffTableCellRenderer() { |
---|
61 | this( new Color(223, 223, 223), |
---|
62 | new Color(192, 192, 192) ); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * initializes the Renderer with the given colors |
---|
67 | * |
---|
68 | * @param missingColor the color for missing values |
---|
69 | * @param missingColorSelected the color selected missing values |
---|
70 | */ |
---|
71 | public ArffTableCellRenderer( Color missingColor, |
---|
72 | Color missingColorSelected ) { |
---|
73 | this( missingColor, |
---|
74 | missingColorSelected, |
---|
75 | Color.RED, |
---|
76 | Color.RED.darker() ); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * initializes the Renderer with the given colors |
---|
81 | * |
---|
82 | * @param missingColor the color for missing values |
---|
83 | * @param missingColorSelected the color selected missing values |
---|
84 | * @param highlightColor the color for highlighted values |
---|
85 | * @param highlightColorSelected the color selected highlighted values |
---|
86 | */ |
---|
87 | public ArffTableCellRenderer( Color missingColor, |
---|
88 | Color missingColorSelected, |
---|
89 | Color highlightColor, |
---|
90 | Color highlightColorSelected ) { |
---|
91 | super(); |
---|
92 | |
---|
93 | this.missingColor = missingColor; |
---|
94 | this.missingColorSelected = missingColorSelected; |
---|
95 | this.highlightColor = highlightColor; |
---|
96 | this.highlightColorSelected = highlightColorSelected; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Returns the default table cell renderer. |
---|
101 | * stuff for the header is taken from <a href="http://www.chka.de/swing/table/faq.html">here</a> |
---|
102 | * |
---|
103 | * @param table the table this object belongs to |
---|
104 | * @param value the actual cell value |
---|
105 | * @param isSelected whether the cell is selected |
---|
106 | * @param hasFocus whether the cell has the focus |
---|
107 | * @param row the row in the table |
---|
108 | * @param column the column in the table |
---|
109 | * @return the rendering component |
---|
110 | */ |
---|
111 | public Component getTableCellRendererComponent( |
---|
112 | JTable table, Object value, boolean isSelected, |
---|
113 | boolean hasFocus, int row, int column ) { |
---|
114 | ArffSortedTableModel model; |
---|
115 | Component result; |
---|
116 | String searchString; |
---|
117 | boolean found; |
---|
118 | |
---|
119 | result = super.getTableCellRendererComponent( |
---|
120 | table, value, isSelected, hasFocus, row, column); |
---|
121 | |
---|
122 | // search |
---|
123 | if (table instanceof ArffTable) |
---|
124 | searchString = ((ArffTable) table).getSearchString(); |
---|
125 | else |
---|
126 | searchString = null; |
---|
127 | if ( (searchString != null) && (!searchString.equals("")) ) |
---|
128 | found = (searchString.equals(value.toString())); |
---|
129 | else |
---|
130 | found = false; |
---|
131 | |
---|
132 | if (table.getModel() instanceof ArffSortedTableModel) { |
---|
133 | model = (ArffSortedTableModel) table.getModel(); |
---|
134 | // normal cell |
---|
135 | if (row >= 0) { |
---|
136 | if (model.isMissingAt(row, column)) { |
---|
137 | setToolTipText("missing"); |
---|
138 | if (found) { |
---|
139 | if (isSelected) |
---|
140 | result.setBackground(highlightColorSelected); |
---|
141 | else |
---|
142 | result.setBackground(highlightColor); |
---|
143 | } |
---|
144 | else { |
---|
145 | if (isSelected) |
---|
146 | result.setBackground(missingColorSelected); |
---|
147 | else |
---|
148 | result.setBackground(missingColor); |
---|
149 | } |
---|
150 | } |
---|
151 | else { |
---|
152 | setToolTipText(null); |
---|
153 | if (found) { |
---|
154 | if (isSelected) |
---|
155 | result.setBackground(highlightColorSelected); |
---|
156 | else |
---|
157 | result.setBackground(highlightColor); |
---|
158 | } |
---|
159 | else { |
---|
160 | if (isSelected) |
---|
161 | result.setBackground(table.getSelectionBackground()); |
---|
162 | else |
---|
163 | result.setBackground(Color.WHITE); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | // alignment |
---|
168 | if (model.getType(row, column) == Attribute.NUMERIC) |
---|
169 | setHorizontalAlignment(SwingConstants.RIGHT); |
---|
170 | else |
---|
171 | setHorizontalAlignment(SwingConstants.LEFT); |
---|
172 | } |
---|
173 | // header |
---|
174 | else { |
---|
175 | setBorder(UIManager.getBorder("TableHeader.cellBorder")); |
---|
176 | setHorizontalAlignment(SwingConstants.CENTER); |
---|
177 | if (table.getColumnModel().getSelectionModel().isSelectedIndex(column)) |
---|
178 | result.setBackground(UIManager.getColor("TableHeader.background").darker()); |
---|
179 | else |
---|
180 | result.setBackground(UIManager.getColor("TableHeader.background")); |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | return result; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|