source: josm/trunk/src/org/openstreetmap/josm/gui/conflict/tags/MultiValueCellRenderer.java@ 9078

Last change on this file since 9078 was 9078, checked in by Don-vip, 8 years ago

sonar - Immutable Field

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.conflict.tags;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.Font;
8
9import javax.swing.DefaultComboBoxModel;
10import javax.swing.ImageIcon;
11import javax.swing.JLabel;
12import javax.swing.JTable;
13import javax.swing.UIManager;
14import javax.swing.table.TableCellRenderer;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.gui.conflict.ConflictColors;
18import org.openstreetmap.josm.gui.widgets.JosmComboBox;
19import org.openstreetmap.josm.tools.ImageProvider;
20
21/**
22 * This is a {@link TableCellRenderer} for {@link MultiValueResolutionDecision}s.
23 *
24 */
25public class MultiValueCellRenderer extends JLabel implements TableCellRenderer {
26
27 private final ImageIcon iconDecided;
28 private final ImageIcon iconUndecided;
29 private final DefaultComboBoxModel<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 DefaultComboBoxModel<>();
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 Main.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 Main.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 */
112 protected void renderToolTipText(MultiValueResolutionDecision decision) {
113 String toolTipText = null;
114 switch (decision.getDecisionType()) {
115 case UNDECIDED:
116 toolTipText = tr("Please decide which values to keep");
117 break;
118 case KEEP_ONE:
119 toolTipText = tr("Value ''{0}'' is going to be applied for key ''{1}''",
120 decision.getChosenValue(), decision.getKey());
121 break;
122 case SUM_ALL_NUMERIC:
123 toolTipText = tr("All numeric values sumed as ''{0}'' are going to be applied for key ''{1}''",
124 decision.getChosenValue(), decision.getKey());
125 break;
126 case KEEP_NONE:
127 toolTipText = tr("The key ''{0}'' and all its values are going to be removed", decision.getKey());
128 break;
129 case KEEP_ALL:
130 toolTipText = tr("All values joined as ''{0}'' are going to be applied for key ''{1}''",
131 decision.getChosenValue(), decision.getKey());
132 break;
133 }
134 setToolTipText(toolTipText);
135 cbDecisionRenderer.setToolTipText(toolTipText);
136 }
137
138 protected void reset() {
139 setFont(UIManager.getFont("Table.font"));
140 setIcon(null);
141 setText("");
142 }
143
144 @Override
145 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
146
147 reset();
148 if (value == null)
149 return this;
150
151 MultiValueResolutionDecision decision = (MultiValueResolutionDecision) value;
152 TagConflictResolverModel tagModel = (TagConflictResolverModel) table.getModel();
153 boolean conflict = tagModel.getKeysWithConflicts().contains(tagModel.getKey(row));
154 renderColors(decision, isSelected, conflict);
155 renderToolTipText(decision);
156 switch(column) {
157 case 0:
158 if (decision.isDecided()) {
159 setIcon(iconDecided);
160 } else {
161 setIcon(iconUndecided);
162 }
163 return this;
164
165 case 1:
166 setText(decision.getKey());
167 return this;
168
169 case 2:
170 renderValue(decision);
171 return cbDecisionRenderer;
172 }
173 return this;
174 }
175}
Note: See TracBrowser for help on using the repository browser.