[2512] | 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;
|
---|
[2563] | 9 | import javax.swing.UIManager;
|
---|
[2512] | 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 | /**
|
---|
[5266] | 17 | * This is the {@link TableCellRenderer} used in {@link SelectionTable}.
|
---|
[2512] | 18 | *
|
---|
| 19 | */
|
---|
[6476] | 20 | public class SelectionTableCellRenderer extends JLabel implements TableCellRenderer {
|
---|
[6889] | 21 | public static final Color BGCOLOR_DOUBLE_ENTRY = new Color(254,226,214);
|
---|
| 22 | public static final Color BGCOLOR_SINGLE_ENTRY = new Color(235,255,177);
|
---|
[2512] | 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() {
|
---|
[2563] | 43 | setBackground(UIManager.getColor("Table.background"));
|
---|
| 44 | setForeground(UIManager.getColor("Table.foreground"));
|
---|
[2512] | 45 | setBorder(null);
|
---|
| 46 | setIcon(null);
|
---|
[6101] | 47 | setToolTipText(null);
|
---|
[2512] | 48 | }
|
---|
| 49 |
|
---|
| 50 | protected void renderBackground(OsmPrimitive primitive, boolean isSelected) {
|
---|
[2563] | 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) {
|
---|
[2512] | 55 | bgc = BGCOLOR_DOUBLE_ENTRY;
|
---|
| 56 | }
|
---|
| 57 | setBackground(bgc);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | protected void renderPrimitive(OsmPrimitive primitive) {
|
---|
[6476] | 61 | setIcon(ImageProvider.get(primitive.getDisplayType()));
|
---|
[2512] | 62 | setText(primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
[2563] | 63 | setToolTipText(DefaultNameFormatter.getInstance().buildDefaultToolTip(primitive));
|
---|
[2512] | 64 | }
|
---|
| 65 |
|
---|
[6084] | 66 | @Override
|
---|
[2512] | 67 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
|
---|
| 68 | int row, int column) {
|
---|
| 69 |
|
---|
| 70 | reset();
|
---|
[4072] | 71 | if (value == null)
|
---|
| 72 | return this;
|
---|
| 73 |
|
---|
[2512] | 74 | renderBackground((OsmPrimitive)value, isSelected);
|
---|
| 75 | renderPrimitive((OsmPrimitive)value);
|
---|
| 76 | return this;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public void setMemberTableModel(MemberTableModel model) {
|
---|
| 80 | this.model = model;
|
---|
| 81 | }
|
---|
| 82 | }
|
---|