source: josm/trunk/src/org/openstreetmap/josm/gui/conflict/tags/PasteTagsConflictResolverDialog.java@ 3385

Last change on this file since 3385 was 3385, checked in by jttt, 14 years ago

Fix warnings

  • Property svn:eol-style set to native
File size: 19.7 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;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.BorderLayout;
8import java.awt.Component;
9import java.awt.Dimension;
10import java.awt.FlowLayout;
11import java.awt.Font;
12import java.awt.GridBagConstraints;
13import java.awt.GridBagLayout;
14import java.awt.Insets;
15import java.awt.event.ActionEvent;
16import java.beans.PropertyChangeEvent;
17import java.beans.PropertyChangeListener;
18import java.util.ArrayList;
19import java.util.HashMap;
20import java.util.List;
21import java.util.Map;
22import java.util.logging.Logger;
23
24import javax.swing.AbstractAction;
25import javax.swing.Action;
26import javax.swing.ImageIcon;
27import javax.swing.JDialog;
28import javax.swing.JLabel;
29import javax.swing.JOptionPane;
30import javax.swing.JPanel;
31import javax.swing.JTabbedPane;
32import javax.swing.JTable;
33import javax.swing.UIManager;
34import javax.swing.table.DefaultTableColumnModel;
35import javax.swing.table.DefaultTableModel;
36import javax.swing.table.TableCellRenderer;
37import javax.swing.table.TableColumn;
38
39import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
40import org.openstreetmap.josm.data.osm.TagCollection;
41import org.openstreetmap.josm.gui.SideButton;
42import org.openstreetmap.josm.tools.ImageProvider;
43import org.openstreetmap.josm.tools.WindowGeometry;
44
45public class PasteTagsConflictResolverDialog extends JDialog implements PropertyChangeListener {
46 static private final Map<OsmPrimitiveType, String> PANE_TITLES;
47 static {
48 PANE_TITLES = new HashMap<OsmPrimitiveType, String>();
49 PANE_TITLES.put(OsmPrimitiveType.NODE, tr("Tags from nodes"));
50 PANE_TITLES.put(OsmPrimitiveType.WAY, tr("Tags from ways"));
51 PANE_TITLES.put(OsmPrimitiveType.RELATION, tr("Tags from relations"));
52 }
53
54 private enum Mode {
55 RESOLVING_ONE_TAGCOLLECTION_ONLY,
56 RESOLVING_TYPED_TAGCOLLECTIONS
57 }
58
59 private TagConflictResolver allPrimitivesResolver;
60 private Map<OsmPrimitiveType, TagConflictResolver> resolvers;
61 private JTabbedPane tpResolvers;
62 private Mode mode;
63 private boolean canceled = false;
64
65 private ImageIcon iconResolved;
66 private ImageIcon iconUnresolved;
67 private StatisticsTableModel statisticsModel;
68 private JPanel pnlTagResolver;
69
70 public PasteTagsConflictResolverDialog(Component owner) {
71 super(JOptionPane.getFrameForComponent(owner),true);
72 build();
73 iconResolved = ImageProvider.get("dialogs/conflict", "tagconflictresolved");
74 iconUnresolved = ImageProvider.get("dialogs/conflict", "tagconflictunresolved");
75 }
76
77 protected void build() {
78 setTitle(tr("Conflicts in pasted tags"));
79 allPrimitivesResolver = new TagConflictResolver();
80 resolvers = new HashMap<OsmPrimitiveType, TagConflictResolver>();
81 for (OsmPrimitiveType type: OsmPrimitiveType.values()) {
82 resolvers.put(type, new TagConflictResolver());
83 resolvers.get(type).getModel().addPropertyChangeListener(this);
84 }
85 tpResolvers = new JTabbedPane();
86 getContentPane().setLayout(new GridBagLayout());
87 mode = null;
88 GridBagConstraints gc = new GridBagConstraints();
89 gc.gridx = 0;
90 gc.gridy = 0;
91 gc.fill = GridBagConstraints.HORIZONTAL;
92 gc.weightx = 1.0;
93 gc.weighty = 0.0;
94 getContentPane().add(buildSourceAndTargetInfoPanel(), gc);
95 gc.gridx = 0;
96 gc.gridy = 1;
97 gc.fill = GridBagConstraints.BOTH;
98 gc.weightx = 1.0;
99 gc.weighty = 1.0;
100 getContentPane().add(pnlTagResolver = new JPanel(), gc);
101 gc.gridx = 0;
102 gc.gridy = 2;
103 gc.fill = GridBagConstraints.HORIZONTAL;
104 gc.weightx = 1.0;
105 gc.weighty = 0.0;
106 getContentPane().add(buildButtonPanel(), gc);
107 }
108
109 protected JPanel buildButtonPanel() {
110 JPanel pnl = new JPanel();
111 pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
112
113 // -- apply button
114 ApplyAction applyAction = new ApplyAction();
115 allPrimitivesResolver.getModel().addPropertyChangeListener(applyAction);
116 for (OsmPrimitiveType type: resolvers.keySet()) {
117 resolvers.get(type).getModel().addPropertyChangeListener(applyAction);
118 }
119 pnl.add(new SideButton(applyAction));
120
121 // -- cancel button
122 CancelAction cancelAction = new CancelAction();
123 pnl.add(new SideButton(cancelAction));
124
125 return pnl;
126 }
127
128 protected JPanel buildSourceAndTargetInfoPanel() {
129 JPanel pnl = new JPanel();
130 pnl.setLayout(new BorderLayout());
131 statisticsModel = new StatisticsTableModel();
132 pnl.add(new StatisticsInfoTable(statisticsModel), BorderLayout.CENTER);
133 return pnl;
134 }
135
136 /**
137 * Initializes the conflict resolver for a specific type of primitives
138 *
139 * @param type the type of primitives
140 * @param tc the tags belonging to this type of primitives
141 * @param targetStatistics histogram of paste targets, number of primitives of each type in the paste target
142 */
143 protected void initResolver(OsmPrimitiveType type, TagCollection tc, Map<OsmPrimitiveType,Integer> targetStatistics) {
144 resolvers.get(type).getModel().populate(tc,tc.getKeysWithMultipleValues());
145 resolvers.get(type).getModel().prepareDefaultTagDecisions();
146 if (!tc.isEmpty() && targetStatistics.get(type) != null && targetStatistics.get(type) > 0) {
147 tpResolvers.add(PANE_TITLES.get(type), resolvers.get(type));
148 }
149 }
150
151 /**
152 * Populates the conflict resolver with one tag collection
153 *
154 * @param tagsForAllPrimitives the tag collection
155 * @param sourceStatistics histogram of tag source, number of primitives of each type in the source
156 * @param targetStatistics histogram of paste targets, number of primitives of each type in the paste target
157 */
158 public void populate(TagCollection tagsForAllPrimitives, Map<OsmPrimitiveType, Integer> sourceStatistics, Map<OsmPrimitiveType,Integer> targetStatistics) {
159 mode = Mode.RESOLVING_ONE_TAGCOLLECTION_ONLY;
160 tagsForAllPrimitives = tagsForAllPrimitives == null? new TagCollection() : tagsForAllPrimitives;
161 sourceStatistics = sourceStatistics == null ? new HashMap<OsmPrimitiveType, Integer>() :sourceStatistics;
162 targetStatistics = targetStatistics == null ? new HashMap<OsmPrimitiveType, Integer>() : targetStatistics;
163
164 // init the resolver
165 //
166 allPrimitivesResolver.getModel().populate(tagsForAllPrimitives,tagsForAllPrimitives.getKeysWithMultipleValues());
167 allPrimitivesResolver.getModel().prepareDefaultTagDecisions();
168
169 // prepare the dialog with one tag resolver
170 pnlTagResolver.setLayout(new BorderLayout());
171 pnlTagResolver.removeAll();
172 pnlTagResolver.add(allPrimitivesResolver, BorderLayout.CENTER);
173
174 statisticsModel.reset();
175 StatisticsInfo info = new StatisticsInfo();
176 info.numTags = tagsForAllPrimitives.getKeys().size();
177 info.sourceInfo.putAll(sourceStatistics);
178 info.targetInfo.putAll(targetStatistics);
179 statisticsModel.append(info);
180 validate();
181 }
182
183 protected int getNumResolverTabs() {
184 return tpResolvers.getTabCount();
185 }
186
187 protected TagConflictResolver getResolver(int idx) {
188 return (TagConflictResolver)tpResolvers.getComponentAt(idx);
189 }
190
191 /**
192 * Populate the tag conflict resolver with tags for each type of primitives
193 *
194 * @param tagsForNodes the tags belonging to nodes in the paste source
195 * @param tagsForWays the tags belonging to way in the paste source
196 * @param tagsForRelations the tags belonging to relations in the paste source
197 * @param sourceStatistics histogram of tag source, number of primitives of each type in the source
198 * @param targetStatistics histogram of paste targets, number of primitives of each type in the paste target
199 */
200 public void populate(TagCollection tagsForNodes, TagCollection tagsForWays, TagCollection tagsForRelations, Map<OsmPrimitiveType,Integer> sourceStatistics, Map<OsmPrimitiveType, Integer> targetStatistics) {
201 tagsForNodes = (tagsForNodes == null) ? new TagCollection() : tagsForNodes;
202 tagsForWays = (tagsForWays == null) ? new TagCollection() : tagsForWays;
203 tagsForRelations = (tagsForRelations == null) ? new TagCollection() : tagsForRelations;
204 if (tagsForNodes.isEmpty() && tagsForWays.isEmpty() && tagsForRelations.isEmpty()) {
205 populate(null,null,null);
206 return;
207 }
208 tpResolvers.removeAll();
209 initResolver(OsmPrimitiveType.NODE,tagsForNodes, targetStatistics);
210 initResolver(OsmPrimitiveType.WAY,tagsForWays, targetStatistics);
211 initResolver(OsmPrimitiveType.RELATION,tagsForRelations, targetStatistics);
212
213 pnlTagResolver.setLayout(new BorderLayout());
214 pnlTagResolver.removeAll();
215 pnlTagResolver.add(tpResolvers, BorderLayout.CENTER);
216 mode = Mode.RESOLVING_TYPED_TAGCOLLECTIONS;
217 validate();
218 statisticsModel.reset();
219 if (!tagsForNodes.isEmpty()) {
220 StatisticsInfo info = new StatisticsInfo();
221 info.numTags = tagsForNodes.getKeys().size();
222 int numTargets = targetStatistics.get(OsmPrimitiveType.NODE) == null ? 0 : targetStatistics.get(OsmPrimitiveType.NODE);
223 if (numTargets > 0) {
224 info.sourceInfo.put(OsmPrimitiveType.NODE, sourceStatistics.get(OsmPrimitiveType.NODE));
225 info.targetInfo.put(OsmPrimitiveType.NODE, numTargets);
226 statisticsModel.append(info);
227 }
228 }
229 if (!tagsForWays.isEmpty()) {
230 StatisticsInfo info = new StatisticsInfo();
231 info.numTags = tagsForWays.getKeys().size();
232 int numTargets = targetStatistics.get(OsmPrimitiveType.WAY) == null ? 0 : targetStatistics.get(OsmPrimitiveType.WAY);
233 if (numTargets > 0) {
234 info.sourceInfo.put(OsmPrimitiveType.WAY, sourceStatistics.get(OsmPrimitiveType.WAY));
235 info.targetInfo.put(OsmPrimitiveType.WAY, numTargets);
236 statisticsModel.append(info);
237 }
238 }
239 if (!tagsForRelations.isEmpty()) {
240 StatisticsInfo info = new StatisticsInfo();
241 info.numTags = tagsForRelations.getKeys().size();
242 int numTargets = targetStatistics.get(OsmPrimitiveType.RELATION) == null ? 0 : targetStatistics.get(OsmPrimitiveType.RELATION);
243 if (numTargets > 0) {
244 info.sourceInfo.put(OsmPrimitiveType.RELATION, sourceStatistics.get(OsmPrimitiveType.RELATION));
245 info.targetInfo.put(OsmPrimitiveType.RELATION, numTargets);
246 statisticsModel.append(info);
247 }
248 }
249
250 for (int i =0; i < getNumResolverTabs(); i++) {
251 if (!getResolver(i).getModel().isResolvedCompletely()) {
252 tpResolvers.setSelectedIndex(i);
253 break;
254 }
255 }
256 }
257
258 protected void setCanceled(boolean canceled) {
259 this.canceled = canceled;
260 }
261
262 public boolean isCanceled() {
263 return this.canceled;
264 }
265
266 class CancelAction extends AbstractAction {
267
268 public CancelAction() {
269 putValue(Action.SHORT_DESCRIPTION, tr("Cancel conflict resolution"));
270 putValue(Action.NAME, tr("Cancel"));
271 putValue(Action.SMALL_ICON, ImageProvider.get("", "cancel"));
272 setEnabled(true);
273 }
274
275 public void actionPerformed(ActionEvent arg0) {
276 setVisible(false);
277 setCanceled(true);
278 }
279 }
280
281 class ApplyAction extends AbstractAction implements PropertyChangeListener {
282
283 public ApplyAction() {
284 putValue(Action.SHORT_DESCRIPTION, tr("Apply resolved conflicts"));
285 putValue(Action.NAME, tr("Apply"));
286 putValue(Action.SMALL_ICON, ImageProvider.get("ok"));
287 updateEnabledState();
288 }
289
290 public void actionPerformed(ActionEvent arg0) {
291 setVisible(false);
292 }
293
294 protected void updateEnabledState() {
295 if (mode == null) {
296 setEnabled(false);
297 } else if (mode.equals(Mode.RESOLVING_ONE_TAGCOLLECTION_ONLY)) {
298 setEnabled(allPrimitivesResolver.getModel().isResolvedCompletely());
299 } else {
300 boolean enabled = true;
301 for (OsmPrimitiveType type: resolvers.keySet()) {
302 enabled &= resolvers.get(type).getModel().isResolvedCompletely();
303 }
304 setEnabled(enabled);
305 }
306 }
307
308 public void propertyChange(PropertyChangeEvent evt) {
309 if (evt.getPropertyName().equals(TagConflictResolverModel.NUM_CONFLICTS_PROP)) {
310 updateEnabledState();
311 }
312 }
313 }
314
315 @Override
316 public void setVisible(boolean visible) {
317 if (visible) {
318 new WindowGeometry(
319 getClass().getName() + ".geometry",
320 WindowGeometry.centerOnScreen(new Dimension(400,300))
321 ).applySafe(this);
322 } else {
323 new WindowGeometry(this).remember(getClass().getName() + ".geometry");
324 }
325 super.setVisible(visible);
326 }
327
328 public TagCollection getResolution() {
329 return allPrimitivesResolver.getModel().getResolution();
330 }
331
332 public TagCollection getResolution(OsmPrimitiveType type) {
333 if (type == null) return null;
334 return resolvers.get(type).getModel().getResolution();
335 }
336
337 public void propertyChange(PropertyChangeEvent evt) {
338 if (evt.getPropertyName().equals(TagConflictResolverModel.NUM_CONFLICTS_PROP)) {
339 TagConflictResolverModel model = (TagConflictResolverModel)evt.getSource();
340 for (int i=0; i < tpResolvers.getTabCount();i++) {
341 TagConflictResolver resolver = (TagConflictResolver)tpResolvers.getComponentAt(i);
342 if (model == resolver.getModel()) {
343 tpResolvers.setIconAt(i,
344 (Boolean)evt.getNewValue() ? iconResolved : iconUnresolved
345
346 );
347 }
348 }
349 }
350 }
351
352 static public class StatisticsInfo {
353 public int numTags;
354 public Map<OsmPrimitiveType, Integer> sourceInfo;
355 public Map<OsmPrimitiveType, Integer> targetInfo;
356
357 public StatisticsInfo() {
358 sourceInfo = new HashMap<OsmPrimitiveType, Integer>();
359 targetInfo = new HashMap<OsmPrimitiveType, Integer>();
360 }
361 }
362
363 static private class StatisticsTableColumnModel extends DefaultTableColumnModel {
364 public StatisticsTableColumnModel() {
365 TableCellRenderer renderer = new StatisticsInfoRenderer();
366 TableColumn col = null;
367
368 // column 0 - Paste
369 col = new TableColumn(0);
370 col.setHeaderValue(tr("Paste ..."));
371 col.setResizable(true);
372 col.setCellRenderer(renderer);
373 addColumn(col);
374
375 // column 1 - From
376 col = new TableColumn(1);
377 col.setHeaderValue(tr("From ..."));
378 col.setResizable(true);
379 col.setCellRenderer(renderer);
380 addColumn(col);
381
382 // column 2 - To
383 col = new TableColumn(2);
384 col.setHeaderValue(tr("To ..."));
385 col.setResizable(true);
386 col.setCellRenderer(renderer);
387 addColumn(col);
388 }
389 }
390
391 static private class StatisticsTableModel extends DefaultTableModel {
392 private static final String[] HEADERS = new String[] {tr("Paste ..."), tr("From ..."), tr("To ...") };
393 private List<StatisticsInfo> data;
394
395 public StatisticsTableModel() {
396 data = new ArrayList<StatisticsInfo>();
397 }
398
399 @Override
400 public Object getValueAt(int row, int column) {
401 if (row == 0)
402 return HEADERS[column];
403 else if (row -1 < data.size())
404 return data.get(row -1);
405 else
406 return null;
407 }
408
409 @Override
410 public boolean isCellEditable(int row, int column) {
411 return false;
412 }
413
414 @Override
415 public int getRowCount() {
416 if (data == null) return 1;
417 return data.size() + 1;
418 }
419
420 public void reset() {
421 data.clear();
422 }
423
424 public void append(StatisticsInfo info) {
425 data.add(info);
426 fireTableDataChanged();
427 }
428 }
429
430 static private class StatisticsInfoRenderer extends JLabel implements TableCellRenderer {
431 @SuppressWarnings("unused")
432 static private final Logger logger = Logger.getLogger(StatisticsInfoRenderer.class.getName());
433
434 protected void reset() {
435 setIcon(null);
436 setText("");
437 setFont(UIManager.getFont("Table.font"));
438 }
439 protected void renderNumTags(StatisticsInfo info) {
440 if (info == null) return;
441 setText(trn("{0} tag", "{0} tags", info.numTags, info.numTags));
442 }
443
444 protected void renderStatistics(Map<OsmPrimitiveType, Integer> stat) {
445 if (stat == null) return;
446 if (stat.isEmpty()) return;
447 if (stat.size() == 1) {
448 setIcon(ImageProvider.get(stat.keySet().iterator().next()));
449 } else {
450 setIcon(ImageProvider.get("data", "object"));
451 }
452 String text = "";
453 for (OsmPrimitiveType type: stat.keySet()) {
454 int numPrimitives = stat.get(type) == null ? 0 : stat.get(type);
455 if (numPrimitives == 0) {
456 continue;
457 }
458 String msg = "";
459 switch(type) {
460 case NODE: msg = trn("{0} node", "{0} nodes", numPrimitives,numPrimitives); break;
461 case WAY: msg = trn("{0} way", "{0} ways", numPrimitives, numPrimitives); break;
462 case RELATION: msg = trn("{0} relation", "{0} relations", numPrimitives, numPrimitives); break;
463 }
464 text = text.equals("") ? msg : text + ", " + msg;
465 }
466 setText(text);
467 }
468
469 protected void renderFrom(StatisticsInfo info) {
470 renderStatistics(info.sourceInfo);
471 }
472
473 protected void renderTo(StatisticsInfo info) {
474 renderStatistics(info.targetInfo);
475 }
476
477 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
478 boolean hasFocus, int row, int column) {
479 reset();
480 if (row == 0) {
481 setFont(getFont().deriveFont(Font.BOLD));
482 setText((String)value);
483 } else {
484 StatisticsInfo info = (StatisticsInfo) value;
485
486 switch(column) {
487 case 0: renderNumTags(info); break;
488 case 1: renderFrom(info); break;
489 case 2: renderTo(info); break;
490 }
491 }
492 return this;
493 }
494 }
495
496 static private class StatisticsInfoTable extends JPanel {
497
498 private JTable infoTable;
499
500 protected void build(StatisticsTableModel model) {
501 infoTable = new JTable(model, new StatisticsTableColumnModel());
502 infoTable.setShowHorizontalLines(true);
503 infoTable.setShowVerticalLines(false);
504 infoTable.setEnabled(false);
505 setLayout(new BorderLayout());
506 add(infoTable, BorderLayout.CENTER);
507 }
508
509 public StatisticsInfoTable(StatisticsTableModel model) {
510 build(model);
511 }
512
513 @Override
514 public Insets getInsets() {
515 Insets insets = super.getInsets();
516 insets.bottom = 20;
517 return insets;
518 }
519 }
520}
Note: See TracBrowser for help on using the repository browser.