1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.corrector;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.Font;
|
---|
7 |
|
---|
8 | import javax.swing.JLabel;
|
---|
9 | import javax.swing.JTable;
|
---|
10 | import javax.swing.table.TableCellRenderer;
|
---|
11 |
|
---|
12 | public abstract class CorrectionTable<T extends CorrectionTableModel<?>>
|
---|
13 | extends JTable {
|
---|
14 |
|
---|
15 | private static final int MAX_VISIBLE_LINES = 10;
|
---|
16 |
|
---|
17 | public static class BoldRenderer extends JLabel implements
|
---|
18 | TableCellRenderer {
|
---|
19 |
|
---|
20 | @Override
|
---|
21 | public Component getTableCellRendererComponent(JTable table,
|
---|
22 | Object value, boolean isSelected, boolean hasFocus, int row,
|
---|
23 | int column) {
|
---|
24 |
|
---|
25 | Font f = getFont();
|
---|
26 | setFont(new Font(f.getName(), f.getStyle() | Font.BOLD, f.getSize()));
|
---|
27 |
|
---|
28 | setText((String)value);
|
---|
29 |
|
---|
30 | return this;
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | private static BoldRenderer boldRenderer = null;
|
---|
35 |
|
---|
36 | protected CorrectionTable(T correctionTableModel) {
|
---|
37 | super(correctionTableModel);
|
---|
38 |
|
---|
39 | final int correctionsSize = correctionTableModel.getCorrections().size();
|
---|
40 | final int lines = correctionsSize > MAX_VISIBLE_LINES ? MAX_VISIBLE_LINES
|
---|
41 | : correctionsSize;
|
---|
42 | setPreferredScrollableViewportSize(new Dimension(400, lines
|
---|
43 | * getRowHeight()));
|
---|
44 | getColumnModel().getColumn(correctionTableModel.getApplyColumn())
|
---|
45 | .setPreferredWidth(40);
|
---|
46 | setRowSelectionAllowed(false);
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public TableCellRenderer getCellRenderer(int row, int column) {
|
---|
51 | if (getCorrectionTableModel().isBoldCell(row, column)) {
|
---|
52 | if (boldRenderer == null)
|
---|
53 | boldRenderer = new BoldRenderer();
|
---|
54 | return boldRenderer;
|
---|
55 | }
|
---|
56 | return super.getCellRenderer(row, column);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @SuppressWarnings("unchecked")
|
---|
60 | public T getCorrectionTableModel() {
|
---|
61 | return (T)getModel();
|
---|
62 | }
|
---|
63 |
|
---|
64 | }
|
---|