1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Component;
|
---|
6 | import java.awt.Rectangle;
|
---|
7 |
|
---|
8 | import javax.swing.JLabel;
|
---|
9 | import javax.swing.JTable;
|
---|
10 | import javax.swing.UIManager;
|
---|
11 | import javax.swing.table.TableCellRenderer;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
15 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * This is the {@link TableCellRenderer} used in {@link SelectionTable}.
|
---|
19 | *
|
---|
20 | */
|
---|
21 | public class SelectionTableCellRenderer extends JLabel implements TableCellRenderer {
|
---|
22 | public static final Color BGCOLOR_DOUBLE_ENTRY = new Color(254, 226, 214);
|
---|
23 | public static final Color BGCOLOR_SINGLE_ENTRY = new Color(235, 255, 177);
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * reference to the member table model; required, in order to check whether a
|
---|
27 | * selected primitive is already used in the member list of the currently edited
|
---|
28 | * relation
|
---|
29 | */
|
---|
30 | private MemberTableModel model;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * constructor
|
---|
34 | */
|
---|
35 | public SelectionTableCellRenderer() {
|
---|
36 | setIcon(null);
|
---|
37 | setOpaque(true);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * reset the renderer
|
---|
42 | */
|
---|
43 | protected void reset() {
|
---|
44 | setBackground(UIManager.getColor("Table.background"));
|
---|
45 | setForeground(UIManager.getColor("Table.foreground"));
|
---|
46 | setBorder(null);
|
---|
47 | setIcon(null);
|
---|
48 | setToolTipText(null);
|
---|
49 | }
|
---|
50 |
|
---|
51 | protected void renderBackground(OsmPrimitive primitive) {
|
---|
52 | Color bgc = UIManager.getColor("Table.background");
|
---|
53 | if (primitive != null && model != null && model.getNumMembersWithPrimitive(primitive) == 1) {
|
---|
54 | bgc = BGCOLOR_SINGLE_ENTRY;
|
---|
55 | } else if (primitive != null && model != null && model.getNumMembersWithPrimitive(primitive) > 1) {
|
---|
56 | bgc = BGCOLOR_DOUBLE_ENTRY;
|
---|
57 | }
|
---|
58 | setBackground(bgc);
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected void renderPrimitive(OsmPrimitive primitive, Rectangle cellSize) {
|
---|
62 | setIcon(ImageProvider.getPadded(primitive, cellSize));
|
---|
63 | setText(primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
64 | setToolTipText(DefaultNameFormatter.getInstance().buildDefaultToolTip(primitive));
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
|
---|
69 | int row, int column) {
|
---|
70 |
|
---|
71 | reset();
|
---|
72 | if (value == null)
|
---|
73 | return this;
|
---|
74 |
|
---|
75 | Rectangle cellSize = table.getCellRect(row, column, false);
|
---|
76 |
|
---|
77 | renderBackground((OsmPrimitive) value);
|
---|
78 | renderPrimitive((OsmPrimitive) value, cellSize);
|
---|
79 | return this;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void setMemberTableModel(MemberTableModel model) {
|
---|
83 | this.model = model;
|
---|
84 | }
|
---|
85 | }
|
---|