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<TM 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 | public Component getTableCellRendererComponent(JTable table,
|
---|
21 | Object value, boolean isSelected, boolean hasFocus, int row,
|
---|
22 | int column) {
|
---|
23 |
|
---|
24 | Font f = getFont();
|
---|
25 | setFont(new Font(f.getName(), f.getStyle() | Font.BOLD, f.getSize()));
|
---|
26 |
|
---|
27 | setText((String)value);
|
---|
28 |
|
---|
29 | return this;
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | private static BoldRenderer boldRenderer = null;
|
---|
34 |
|
---|
35 | protected CorrectionTable(TM correctionTableModel) {
|
---|
36 | super(correctionTableModel);
|
---|
37 |
|
---|
38 | final int correctionsSize = correctionTableModel.getCorrections().size();
|
---|
39 | final int lines = correctionsSize > MAX_VISIBLE_LINES ? MAX_VISIBLE_LINES
|
---|
40 | : correctionsSize;
|
---|
41 | setPreferredScrollableViewportSize(new Dimension(400, lines
|
---|
42 | * getRowHeight()));
|
---|
43 | getColumnModel().getColumn(correctionTableModel.getApplyColumn())
|
---|
44 | .setPreferredWidth(40);
|
---|
45 | setRowSelectionAllowed(false);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public TableCellRenderer getCellRenderer(int row, int column) {
|
---|
49 | if (getCorrectionTableModel().isBoldCell(row, column)) {
|
---|
50 | if (boldRenderer == null)
|
---|
51 | boldRenderer = new BoldRenderer();
|
---|
52 | return boldRenderer;
|
---|
53 | }
|
---|
54 | return super.getCellRenderer(row, column);
|
---|
55 | }
|
---|
56 |
|
---|
57 | @SuppressWarnings("unchecked")
|
---|
58 | public TM getCorrectionTableModel() {
|
---|
59 | return (TM)getModel();
|
---|
60 | }
|
---|
61 |
|
---|
62 | }
|
---|