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.util.ArrayList;
|
---|
7 | import java.util.Collections;
|
---|
8 |
|
---|
9 | import javax.swing.JLabel;
|
---|
10 | import javax.swing.JTable;
|
---|
11 | import javax.swing.table.TableCellRenderer;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * This is the {@see TableCellRenderer} used in the tables of {@see RelationMemberMerger}.
|
---|
17 | *
|
---|
18 | */
|
---|
19 | public abstract class MemberTableCellRenderer extends JLabel implements TableCellRenderer {
|
---|
20 | public final static Color BGCOLOR_SELECTED = new Color(143, 170, 255);
|
---|
21 | public final static Color BGCOLOR_EMPTY_ROW = new Color(234, 234, 234);
|
---|
22 |
|
---|
23 | public final static Color BGCOLOR_NOT_IN_OPPOSITE = new Color(255, 197, 197);
|
---|
24 | public final static Color BGCOLOR_DOUBLE_ENTRY = new Color(255, 234, 213);
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * constructor
|
---|
28 | */
|
---|
29 | public MemberTableCellRenderer() {
|
---|
30 | setIcon(null);
|
---|
31 | setOpaque(true);
|
---|
32 | }
|
---|
33 |
|
---|
34 | public String buildToolTipText(OsmPrimitive primitive) {
|
---|
35 | StringBuilder sb = new StringBuilder();
|
---|
36 | sb.append("<html>");
|
---|
37 | sb.append("<strong>id</strong>=").append(primitive.id).append("<br>");
|
---|
38 | ArrayList<String> keyList = new ArrayList<String>(primitive.keySet());
|
---|
39 | Collections.sort(keyList);
|
---|
40 | for (int i = 0; i < keyList.size(); i++) {
|
---|
41 | if (i > 0) {
|
---|
42 | sb.append("<br>");
|
---|
43 | }
|
---|
44 | String key = keyList.get(i);
|
---|
45 | sb.append("<strong>").append(key).append("</strong>").append("=");
|
---|
46 | String value = primitive.get(key);
|
---|
47 | while (value.length() != 0) {
|
---|
48 | sb.append(value.substring(0, Math.min(50, value.length())));
|
---|
49 | if (value.length() > 50) {
|
---|
50 | sb.append("<br>");
|
---|
51 | value = value.substring(50);
|
---|
52 | } else {
|
---|
53 | value = "";
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 | sb.append("</html>");
|
---|
58 | return sb.toString();
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * reset the renderer
|
---|
63 | */
|
---|
64 | protected void reset() {
|
---|
65 | setBackground(Color.WHITE);
|
---|
66 | setForeground(Color.BLACK);
|
---|
67 | setBorder(null);
|
---|
68 | setIcon(null);
|
---|
69 | setToolTipText(null);
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected void renderBackground(MemberTableModel model, OsmPrimitive primitive, boolean isSelected) {
|
---|
73 | Color bgc = Color.WHITE;
|
---|
74 | if (isSelected) {
|
---|
75 | bgc = BGCOLOR_SELECTED;
|
---|
76 | } else if (primitive != null && model.getNumMembersWithPrimitive(primitive) > 1) {
|
---|
77 | bgc = BGCOLOR_DOUBLE_ENTRY;
|
---|
78 | }
|
---|
79 | setBackground(bgc);
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected void renderForeground(boolean isSelected) {
|
---|
83 | Color fgc = Color.BLACK;
|
---|
84 | setForeground(fgc);
|
---|
85 | }
|
---|
86 |
|
---|
87 | abstract public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
|
---|
88 | boolean hasFocus, int row, int column);
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * replies the model
|
---|
92 | * @param table the table
|
---|
93 | * @return the table model
|
---|
94 | */
|
---|
95 | protected MemberTableModel getModel(JTable table) {
|
---|
96 | return (MemberTableModel) table.getModel();
|
---|
97 | }
|
---|
98 | }
|
---|