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 | int size = Math.min(cellSize.width, cellSize.height);
|
---|
64 | setIcon(ImageProvider.getPadded(primitive, new Dimension(size, size)));
|
---|
65 | setText(primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
66 | setToolTipText(DefaultNameFormatter.getInstance().buildDefaultToolTip(primitive));
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
|
---|
71 | int row, int column) {
|
---|
72 |
|
---|
73 | reset();
|
---|
74 | if (value == null)
|
---|
75 | return this;
|
---|
76 |
|
---|
77 | Dimension cellSize = table.getCellRect(row, column, false).getSize();
|
---|
78 |
|
---|
79 | renderBackground((OsmPrimitive) value);
|
---|
80 | renderPrimitive((OsmPrimitive) value, cellSize);
|
---|
81 | return this;
|
---|
82 | }
|
---|
83 |
|
---|
84 | public void setMemberTableModel(MemberTableModel model) {
|
---|
85 | this.model = model;
|
---|
86 | }
|
---|
87 | }
|
---|