43 |
import java.awt.Dimension; |
import java.awt.Dimension; |
44 |
import java.awt.Point; |
import java.awt.Point; |
45 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
46 |
|
import java.text.DateFormat; |
47 |
|
import java.text.NumberFormat; |
48 |
|
import java.util.Date; |
49 |
import java.util.Hashtable; |
import java.util.Hashtable; |
50 |
import java.util.Vector; |
import java.util.Vector; |
51 |
|
|
74 |
implements TableModelListener, Scrollable, TableColumnModelListener, |
implements TableModelListener, Scrollable, TableColumnModelListener, |
75 |
ListSelectionListener, CellEditorListener, Accessible |
ListSelectionListener, CellEditorListener, Accessible |
76 |
{ |
{ |
77 |
|
|
78 |
|
/** |
79 |
|
* A cell renderer for boolean values. |
80 |
|
*/ |
81 |
|
private class BooleanCellRenderer |
82 |
|
extends DefaultTableCellRenderer |
83 |
|
{ |
84 |
|
|
85 |
|
/** |
86 |
|
* The CheckBox that is used for rendering. |
87 |
|
*/ |
88 |
|
private JCheckBox checkBox = new JCheckBox(); |
89 |
|
|
90 |
|
/** |
91 |
|
* Returns the component that is used for rendering the value. |
92 |
|
* |
93 |
|
* @param table the JTable |
94 |
|
* @param value the value of the object |
95 |
|
* @param isSelected is the cell selected? |
96 |
|
* @param hasFocus has the cell the focus? |
97 |
|
* @param row the row to render |
98 |
|
* @param column the cell to render |
99 |
|
* |
100 |
|
* @return this component (the default table cell renderer) |
101 |
|
*/ |
102 |
|
public Component getTableCellRendererComponent(JTable table, Object value, |
103 |
|
boolean isSelected, |
104 |
|
boolean hasFocus, int row, |
105 |
|
int column) |
106 |
|
{ |
107 |
|
Boolean boolValue = (Boolean) value; |
108 |
|
checkBox.setSelected(boolValue.booleanValue()); |
109 |
|
return checkBox; |
110 |
|
} |
111 |
|
} |
112 |
|
|
113 |
|
/** |
114 |
|
* A cell renderer for Date values. |
115 |
|
*/ |
116 |
|
private class DateCellRenderer |
117 |
|
extends DefaultTableCellRenderer |
118 |
|
{ |
119 |
|
/** |
120 |
|
* Returns the component that is used for rendering the value. |
121 |
|
* |
122 |
|
* @param table the JTable |
123 |
|
* @param value the value of the object |
124 |
|
* @param isSelected is the cell selected? |
125 |
|
* @param hasFocus has the cell the focus? |
126 |
|
* @param row the row to render |
127 |
|
* @param column the cell to render |
128 |
|
* |
129 |
|
* @return this component (the default table cell renderer) |
130 |
|
*/ |
131 |
|
public Component getTableCellRendererComponent(JTable table, Object value, |
132 |
|
boolean isSelected, |
133 |
|
boolean hasFocus, int row, |
134 |
|
int column) |
135 |
|
{ |
136 |
|
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, |
137 |
|
row, column); |
138 |
|
if (value instanceof Date) |
139 |
|
{ |
140 |
|
Date dateValue = (Date) value; |
141 |
|
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); |
142 |
|
setText(df.format(dateValue)); |
143 |
|
} |
144 |
|
return this; |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
|
* A cell renderer for Double values. |
150 |
|
*/ |
151 |
|
private class DoubleCellRenderer |
152 |
|
extends DefaultTableCellRenderer |
153 |
|
{ |
154 |
|
/** |
155 |
|
* Creates a new instance of NumberCellRenderer. |
156 |
|
*/ |
157 |
|
public DoubleCellRenderer() |
158 |
|
{ |
159 |
|
setHorizontalAlignment(JLabel.RIGHT); |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
|
* Returns the component that is used for rendering the value. |
164 |
|
* |
165 |
|
* @param table the JTable |
166 |
|
* @param value the value of the object |
167 |
|
* @param isSelected is the cell selected? |
168 |
|
* @param hasFocus has the cell the focus? |
169 |
|
* @param row the row to render |
170 |
|
* @param column the cell to render |
171 |
|
* |
172 |
|
* @return this component (the default table cell renderer) |
173 |
|
*/ |
174 |
|
public Component getTableCellRendererComponent(JTable table, Object value, |
175 |
|
boolean isSelected, |
176 |
|
boolean hasFocus, int row, |
177 |
|
int column) |
178 |
|
{ |
179 |
|
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, |
180 |
|
row, column); |
181 |
|
if (value instanceof Double) |
182 |
|
{ |
183 |
|
Double doubleValue = (Double) value; |
184 |
|
NumberFormat nf = NumberFormat.getInstance(); |
185 |
|
setText(nf.format(doubleValue.doubleValue())); |
186 |
|
} |
187 |
|
return this; |
188 |
|
} |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
|
* A cell renderer for Float values. |
193 |
|
*/ |
194 |
|
private class FloatCellRenderer |
195 |
|
extends DefaultTableCellRenderer |
196 |
|
{ |
197 |
|
/** |
198 |
|
* Creates a new instance of NumberCellRenderer. |
199 |
|
*/ |
200 |
|
public FloatCellRenderer() |
201 |
|
{ |
202 |
|
setHorizontalAlignment(JLabel.RIGHT); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
206 |
|
* Returns the component that is used for rendering the value. |
207 |
|
* |
208 |
|
* @param table the JTable |
209 |
|
* @param value the value of the object |
210 |
|
* @param isSelected is the cell selected? |
211 |
|
* @param hasFocus has the cell the focus? |
212 |
|
* @param row the row to render |
213 |
|
* @param column the cell to render |
214 |
|
* |
215 |
|
* @return this component (the default table cell renderer) |
216 |
|
*/ |
217 |
|
public Component getTableCellRendererComponent(JTable table, Object value, |
218 |
|
boolean isSelected, |
219 |
|
boolean hasFocus, int row, |
220 |
|
int column) |
221 |
|
{ |
222 |
|
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, |
223 |
|
row, column); |
224 |
|
if (value instanceof Float) |
225 |
|
{ |
226 |
|
Float floatValue = (Float) value; |
227 |
|
NumberFormat nf = NumberFormat.getInstance(); |
228 |
|
setText(nf.format(floatValue.floatValue())); |
229 |
|
} |
230 |
|
return this; |
231 |
|
} |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
|
* A cell renderer for Number values. |
236 |
|
*/ |
237 |
|
private class NumberCellRenderer |
238 |
|
extends DefaultTableCellRenderer |
239 |
|
{ |
240 |
|
/** |
241 |
|
* Creates a new instance of NumberCellRenderer. |
242 |
|
*/ |
243 |
|
public NumberCellRenderer() |
244 |
|
{ |
245 |
|
setHorizontalAlignment(JLabel.RIGHT); |
246 |
|
} |
247 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
|
* A cell renderer for Icon values. |
251 |
|
*/ |
252 |
|
private class IconCellRenderer |
253 |
|
extends DefaultTableCellRenderer |
254 |
|
{ |
255 |
|
/** |
256 |
|
* Returns the component that is used for rendering the value. |
257 |
|
* |
258 |
|
* @param table the JTable |
259 |
|
* @param value the value of the object |
260 |
|
* @param isSelected is the cell selected? |
261 |
|
* @param hasFocus has the cell the focus? |
262 |
|
* @param row the row to render |
263 |
|
* @param column the cell to render |
264 |
|
* |
265 |
|
* @return this component (the default table cell renderer) |
266 |
|
*/ |
267 |
|
public Component getTableCellRendererComponent(JTable table, Object value, |
268 |
|
boolean isSelected, |
269 |
|
boolean hasFocus, int row, |
270 |
|
int column) |
271 |
|
{ |
272 |
|
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, |
273 |
|
row, column); |
274 |
|
if (value instanceof Icon) |
275 |
|
{ |
276 |
|
Icon iconValue = (Icon) value; |
277 |
|
setIcon(iconValue); |
278 |
|
} |
279 |
|
return this; |
280 |
|
} |
281 |
|
} |
282 |
|
|
283 |
private static final long serialVersionUID = 3876025080382781659L; |
private static final long serialVersionUID = 3876025080382781659L; |
284 |
|
|
285 |
|
|
685 |
|
|
686 |
protected void createDefaultRenderers() |
protected void createDefaultRenderers() |
687 |
{ |
{ |
688 |
//FIXME: Create the renderer object. |
setDefaultRenderer(Boolean.class, new BooleanCellRenderer()); |
689 |
|
setDefaultRenderer(Number.class, new NumberCellRenderer()); |
690 |
|
setDefaultRenderer(Double.class, new DoubleCellRenderer()); |
691 |
|
setDefaultRenderer(Double.class, new FloatCellRenderer()); |
692 |
|
setDefaultRenderer(Date.class, new DateCellRenderer()); |
693 |
|
setDefaultRenderer(Icon.class, new IconCellRenderer()); |
694 |
} |
} |
695 |
|
|
696 |
/** |
/** |
1794 |
|
|
1795 |
|
|
1796 |
/** |
/** |
1797 |
* Sun javadocs describe an unusual implementation of |
* This distributes the superfluous width in a table evenly on its columns. |
1798 |
* <code>doLayout</code> which involves some private interfaces. We try |
* |
1799 |
* to implement the same algorithm as is documented, but using the |
* The implementation used here is different to that one described in |
1800 |
* columnModel directly. We still use a private helper method, but it has |
* the JavaDocs. It is much simpler, and seems to work very well. |
1801 |
* a simpler signature. |
* |
1802 |
|
* TODO: correctly implement the algorithm described in the JavaDoc |
1803 |
*/ |
*/ |
|
|
|
1804 |
private void distributeSpill(TableColumn[] cols, int spill) |
private void distributeSpill(TableColumn[] cols, int spill) |
1805 |
{ |
{ |
1806 |
int MIN = 0; |
int average = spill / cols.length; |
1807 |
int MAX = 0; |
for (int i = 0; i < cols.length; i++) |
|
int PREF = 0; |
|
|
|
|
|
int[] min = new int[cols.length]; |
|
|
int[] max = new int[cols.length]; |
|
|
int[] pref = new int[cols.length]; |
|
|
|
|
|
for (int i = 0; i < cols.length; ++i) |
|
1808 |
{ |
{ |
1809 |
pref[i] = cols[i].getPreferredWidth(); |
cols[i].setWidth(cols[i].getWidth() + average); |
|
min[i] = cols[i].getMinWidth(); |
|
|
max[i] = cols[i].getMaxWidth(); |
|
|
PREF += pref[i]; |
|
|
MIN += min[i]; |
|
|
MAX += max[i]; |
|
1810 |
} |
} |
|
|
|
|
for (int i = 0; i < cols.length; ++i) |
|
|
{ |
|
|
int adj = 0; |
|
|
if (spill > 0) |
|
|
adj = (spill * (pref[i] - min[i])) / (PREF - MIN); |
|
|
else |
|
|
adj = (spill * (max[i] - pref[i])) / (MAX - PREF); |
|
|
cols[i].setWidth(pref[i] + adj); |
|
|
} |
|
1811 |
} |
} |
1812 |
|
|
1813 |
public void doLayout() |
public void doLayout() |
1814 |
{ |
{ |
1815 |
TableColumn resizingColumn = null; |
TableColumn resizingColumn = null; |
1835 |
rCol = i; |
rCol = i; |
1836 |
} |
} |
1837 |
|
|
1838 |
int spill = prefSum - getWidth(); |
int spill = getWidth() - prefSum; |
1839 |
|
|
1840 |
if (resizingColumn != null) |
if (resizingColumn != null) |
1841 |
{ |
{ |