1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.correction;
|
---|
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 | /**
|
---|
13 | * Abstract correction table.
|
---|
14 | * @param <T> type of table model
|
---|
15 | */
|
---|
16 | public abstract class CorrectionTable<T extends CorrectionTableModel<?>> extends JTable {
|
---|
17 |
|
---|
18 | private static final int MAX_VISIBLE_LINES = 10;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Renders text in bold.
|
---|
22 | */
|
---|
23 | public static class BoldRenderer extends JLabel implements TableCellRenderer {
|
---|
24 |
|
---|
25 | @Override
|
---|
26 | public Component getTableCellRendererComponent(JTable table,
|
---|
27 | Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
28 | Font f = getFont();
|
---|
29 | setFont(new Font(f.getName(), f.getStyle() | Font.BOLD, f.getSize()));
|
---|
30 | setText((String) value);
|
---|
31 | return this;
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | private static volatile BoldRenderer boldRenderer;
|
---|
36 |
|
---|
37 | protected CorrectionTable(T correctionTableModel) {
|
---|
38 | super(correctionTableModel);
|
---|
39 |
|
---|
40 | final int correctionsSize = correctionTableModel.getCorrections().size();
|
---|
41 | final int lines = correctionsSize > MAX_VISIBLE_LINES ? MAX_VISIBLE_LINES : correctionsSize;
|
---|
42 | setPreferredScrollableViewportSize(new Dimension(400, lines * getRowHeight()));
|
---|
43 | getColumnModel().getColumn(correctionTableModel.getApplyColumn()).setPreferredWidth(40);
|
---|
44 | setRowSelectionAllowed(false);
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
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 | /**
|
---|
58 | * Returns correction table model.
|
---|
59 | * @return correction table model
|
---|
60 | */
|
---|
61 | @SuppressWarnings("unchecked")
|
---|
62 | public T getCorrectionTableModel() {
|
---|
63 | return (T) getModel();
|
---|
64 | }
|
---|
65 | }
|
---|