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