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