1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.conflict.tags;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Font;
|
---|
8 |
|
---|
9 | import javax.swing.ImageIcon;
|
---|
10 | import javax.swing.JLabel;
|
---|
11 | import javax.swing.JTable;
|
---|
12 | import javax.swing.UIManager;
|
---|
13 | import javax.swing.table.TableCellRenderer;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.gui.conflict.ConflictColors;
|
---|
16 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
17 | import org.openstreetmap.josm.gui.widgets.JosmComboBoxModel;
|
---|
18 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
19 | import org.openstreetmap.josm.tools.Logging;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * This is a {@link TableCellRenderer} for {@link MultiValueResolutionDecision}s.
|
---|
23 | *
|
---|
24 | */
|
---|
25 | public class MultiValueCellRenderer extends JLabel implements TableCellRenderer {
|
---|
26 |
|
---|
27 | private final ImageIcon iconDecided;
|
---|
28 | private final ImageIcon iconUndecided;
|
---|
29 | private final JosmComboBoxModel<Object> model;
|
---|
30 | private final JosmComboBox<Object> cbDecisionRenderer;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Constructs a new {@code MultiValueCellRenderer}.
|
---|
34 | */
|
---|
35 | public MultiValueCellRenderer() {
|
---|
36 | setOpaque(true);
|
---|
37 | iconDecided = ImageProvider.get("dialogs/conflict", "tagconflictresolved");
|
---|
38 | iconUndecided = ImageProvider.get("dialogs/conflict", "tagconflictunresolved");
|
---|
39 | model = new JosmComboBoxModel<>();
|
---|
40 | cbDecisionRenderer = new JosmComboBox<>(model);
|
---|
41 | }
|
---|
42 |
|
---|
43 | protected void renderColors(MultiValueResolutionDecision decision, boolean selected, boolean conflict) {
|
---|
44 | if (selected) {
|
---|
45 | setForeground(UIManager.getColor("Table.selectionForeground"));
|
---|
46 | setBackground(UIManager.getColor("Table.selectionBackground"));
|
---|
47 | } else {
|
---|
48 | switch (decision.getDecisionType()) {
|
---|
49 | case UNDECIDED:
|
---|
50 | setForeground(ConflictColors.FGCOLOR_UNDECIDED.get());
|
---|
51 | setBackground(ConflictColors.BGCOLOR_UNDECIDED.get());
|
---|
52 | break;
|
---|
53 | case KEEP_NONE:
|
---|
54 | setForeground(ConflictColors.FGCOLOR_TAG_KEEP_NONE.get());
|
---|
55 | setBackground(ConflictColors.BGCOLOR_TAG_KEEP_NONE.get());
|
---|
56 | break;
|
---|
57 | default:
|
---|
58 | if (conflict) {
|
---|
59 | switch (decision.getDecisionType()) {
|
---|
60 | case KEEP_ONE:
|
---|
61 | setForeground(ConflictColors.FGCOLOR_TAG_KEEP_ONE.get());
|
---|
62 | setBackground(ConflictColors.BGCOLOR_TAG_KEEP_ONE.get());
|
---|
63 | break;
|
---|
64 | case KEEP_ALL:
|
---|
65 | setForeground(ConflictColors.FGCOLOR_TAG_KEEP_ALL.get());
|
---|
66 | setBackground(ConflictColors.BGCOLOR_TAG_KEEP_ALL.get());
|
---|
67 | break;
|
---|
68 | case SUM_ALL_NUMERIC:
|
---|
69 | setForeground(ConflictColors.FGCOLOR_TAG_SUM_ALL_NUM.get());
|
---|
70 | setBackground(ConflictColors.BGCOLOR_TAG_SUM_ALL_NUM.get());
|
---|
71 | break;
|
---|
72 | default:
|
---|
73 | Logging.error("Unknown decision type in renderColors(): "+decision.getDecisionType());
|
---|
74 | }
|
---|
75 | } else {
|
---|
76 | setForeground(UIManager.getColor("Table.foreground"));
|
---|
77 | setBackground(UIManager.getColor("Table.background"));
|
---|
78 | }
|
---|
79 | break;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected void renderValue(MultiValueResolutionDecision decision) {
|
---|
85 | model.removeAllElements();
|
---|
86 | switch (decision.getDecisionType()) {
|
---|
87 | case UNDECIDED:
|
---|
88 | model.addElement(tr("Choose a value"));
|
---|
89 | cbDecisionRenderer.setFont(getFont().deriveFont(Font.ITALIC));
|
---|
90 | cbDecisionRenderer.setSelectedIndex(0);
|
---|
91 | break;
|
---|
92 | case KEEP_NONE:
|
---|
93 | model.addElement(tr("deleted"));
|
---|
94 | cbDecisionRenderer.setFont(getFont().deriveFont(Font.ITALIC));
|
---|
95 | cbDecisionRenderer.setSelectedIndex(0);
|
---|
96 | break;
|
---|
97 | case KEEP_ONE:
|
---|
98 | case KEEP_ALL:
|
---|
99 | case SUM_ALL_NUMERIC:
|
---|
100 | model.addElement(decision.getChosenValue());
|
---|
101 | cbDecisionRenderer.setFont(getFont());
|
---|
102 | cbDecisionRenderer.setSelectedIndex(0);
|
---|
103 | break;
|
---|
104 | default:
|
---|
105 | Logging.error("Unknown decision type in renderValue(): "+decision.getDecisionType());
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Sets the text of the tooltip for both renderers, this (the JLabel) and the combobox renderer.
|
---|
111 | * @param decision conflict resolution decision
|
---|
112 | */
|
---|
113 | protected void renderToolTipText(MultiValueResolutionDecision decision) {
|
---|
114 | String toolTipText = null;
|
---|
115 | switch (decision.getDecisionType()) {
|
---|
116 | case UNDECIDED:
|
---|
117 | toolTipText = tr("Please decide which values to keep");
|
---|
118 | break;
|
---|
119 | case KEEP_ONE:
|
---|
120 | toolTipText = tr("Value ''{0}'' is going to be applied for key ''{1}''",
|
---|
121 | decision.getChosenValue(), decision.getKey());
|
---|
122 | break;
|
---|
123 | case SUM_ALL_NUMERIC:
|
---|
124 | toolTipText = tr("All numeric values sumed as ''{0}'' are going to be applied for key ''{1}''",
|
---|
125 | decision.getChosenValue(), decision.getKey());
|
---|
126 | break;
|
---|
127 | case KEEP_NONE:
|
---|
128 | toolTipText = tr("The key ''{0}'' and all its values are going to be removed", decision.getKey());
|
---|
129 | break;
|
---|
130 | case KEEP_ALL:
|
---|
131 | toolTipText = tr("All values joined as ''{0}'' are going to be applied for key ''{1}''",
|
---|
132 | decision.getChosenValue(), decision.getKey());
|
---|
133 | break;
|
---|
134 | }
|
---|
135 | setToolTipText(toolTipText);
|
---|
136 | cbDecisionRenderer.setToolTipText(toolTipText);
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected void reset() {
|
---|
140 | setFont(UIManager.getFont("Table.font"));
|
---|
141 | setIcon(null);
|
---|
142 | setText("");
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Override
|
---|
146 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
147 |
|
---|
148 | reset();
|
---|
149 | if (value == null)
|
---|
150 | return this;
|
---|
151 |
|
---|
152 | MultiValueResolutionDecision decision = (MultiValueResolutionDecision) value;
|
---|
153 | TagConflictResolverModel tagModel = (TagConflictResolverModel) table.getModel();
|
---|
154 | boolean conflict = tagModel.getKeysWithConflicts().contains(tagModel.getKey(row));
|
---|
155 | renderColors(decision, isSelected, conflict);
|
---|
156 | renderToolTipText(decision);
|
---|
157 | switch(column) {
|
---|
158 | case 0:
|
---|
159 | if (decision.isDecided()) {
|
---|
160 | setIcon(iconDecided);
|
---|
161 | } else {
|
---|
162 | setIcon(iconUndecided);
|
---|
163 | }
|
---|
164 | return this;
|
---|
165 |
|
---|
166 | case 1:
|
---|
167 | setText(decision.getKey());
|
---|
168 | return this;
|
---|
169 |
|
---|
170 | case 2:
|
---|
171 | renderValue(decision);
|
---|
172 | return cbDecisionRenderer;
|
---|
173 | }
|
---|
174 | return this;
|
---|
175 | }
|
---|
176 | }
|
---|