001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.conflict.tags;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005import static org.openstreetmap.josm.tools.I18n.trn;
006
007import java.awt.BorderLayout;
008import java.awt.Component;
009import java.awt.Dimension;
010import java.awt.FlowLayout;
011import java.awt.Font;
012import java.awt.GridBagConstraints;
013import java.awt.GridBagLayout;
014import java.awt.Insets;
015import java.awt.event.ActionEvent;
016import java.beans.PropertyChangeEvent;
017import java.beans.PropertyChangeListener;
018import java.util.ArrayList;
019import java.util.EnumMap;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023import java.util.Map.Entry;
024
025import javax.swing.AbstractAction;
026import javax.swing.Action;
027import javax.swing.ImageIcon;
028import javax.swing.JButton;
029import javax.swing.JDialog;
030import javax.swing.JLabel;
031import javax.swing.JPanel;
032import javax.swing.JTabbedPane;
033import javax.swing.JTable;
034import javax.swing.UIManager;
035import javax.swing.table.DefaultTableModel;
036import javax.swing.table.TableCellRenderer;
037
038import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
039import org.openstreetmap.josm.data.osm.TagCollection;
040import org.openstreetmap.josm.gui.tagging.TagTableColumnModelBuilder;
041import org.openstreetmap.josm.gui.util.GuiHelper;
042import org.openstreetmap.josm.gui.util.WindowGeometry;
043import org.openstreetmap.josm.tools.ImageProvider;
044import org.openstreetmap.josm.tools.InputMapUtils;
045
046/**
047 * This conflict resolution dialog is used when tags are pasted from the clipboard that conflict with the existing ones.
048 */
049public class PasteTagsConflictResolverDialog extends JDialog implements PropertyChangeListener {
050    static final Map<OsmPrimitiveType, String> PANE_TITLES;
051    static {
052        PANE_TITLES = new EnumMap<>(OsmPrimitiveType.class);
053        PANE_TITLES.put(OsmPrimitiveType.NODE, tr("Tags from nodes"));
054        PANE_TITLES.put(OsmPrimitiveType.WAY, tr("Tags from ways"));
055        PANE_TITLES.put(OsmPrimitiveType.RELATION, tr("Tags from relations"));
056    }
057
058    enum Mode {
059        RESOLVING_ONE_TAGCOLLECTION_ONLY,
060        RESOLVING_TYPED_TAGCOLLECTIONS
061    }
062
063    private final TagConflictResolverModel model = new TagConflictResolverModel();
064    private final transient Map<OsmPrimitiveType, TagConflictResolver> resolvers = new EnumMap<>(OsmPrimitiveType.class);
065    private final JTabbedPane tpResolvers = new JTabbedPane();
066    private Mode mode;
067    private boolean canceled;
068
069    private final ImageIcon iconResolved = ImageProvider.get("dialogs/conflict", "tagconflictresolved");
070    private final ImageIcon iconUnresolved = ImageProvider.get("dialogs/conflict", "tagconflictunresolved");
071    private final StatisticsTableModel statisticsModel = new StatisticsTableModel();
072    private final JPanel pnlTagResolver = new JPanel(new BorderLayout());
073
074    /**
075     * Constructs a new {@code PasteTagsConflictResolverDialog}.
076     * @param owner parent component
077     */
078    public PasteTagsConflictResolverDialog(Component owner) {
079        super(GuiHelper.getFrameForComponent(owner), ModalityType.DOCUMENT_MODAL);
080        build();
081    }
082
083    protected final void build() {
084        setTitle(tr("Conflicts in pasted tags"));
085        for (OsmPrimitiveType type: OsmPrimitiveType.dataValues()) {
086            TagConflictResolverModel tagModel = new TagConflictResolverModel();
087            resolvers.put(type, new TagConflictResolver(tagModel));
088            tagModel.addPropertyChangeListener(this);
089        }
090        getContentPane().setLayout(new GridBagLayout());
091        mode = null;
092        GridBagConstraints gc = new GridBagConstraints();
093        gc.gridx = 0;
094        gc.gridy = 0;
095        gc.fill = GridBagConstraints.HORIZONTAL;
096        gc.weightx = 1.0;
097        gc.weighty = 0.0;
098        getContentPane().add(buildSourceAndTargetInfoPanel(), gc);
099        gc.gridx = 0;
100        gc.gridy = 1;
101        gc.fill = GridBagConstraints.BOTH;
102        gc.weightx = 1.0;
103        gc.weighty = 1.0;
104        getContentPane().add(pnlTagResolver, gc);
105        gc.gridx = 0;
106        gc.gridy = 2;
107        gc.fill = GridBagConstraints.HORIZONTAL;
108        gc.weightx = 1.0;
109        gc.weighty = 0.0;
110        getContentPane().add(buildButtonPanel(), gc);
111        InputMapUtils.addEscapeAction(getRootPane(), new CancelAction());
112    }
113
114    protected JPanel buildButtonPanel() {
115        JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
116
117        // -- apply button
118        ApplyAction applyAction = new ApplyAction();
119        model.addPropertyChangeListener(applyAction);
120        for (TagConflictResolver r : resolvers.values()) {
121            r.getModel().addPropertyChangeListener(applyAction);
122        }
123        pnl.add(new JButton(applyAction));
124
125        // -- cancel button
126        CancelAction cancelAction = new CancelAction();
127        pnl.add(new JButton(cancelAction));
128
129        return pnl;
130    }
131
132    protected JPanel buildSourceAndTargetInfoPanel() {
133        JPanel pnl = new JPanel(new BorderLayout());
134        pnl.add(new StatisticsInfoTable(statisticsModel), BorderLayout.CENTER);
135        return pnl;
136    }
137
138    /**
139     * Initializes the conflict resolver for a specific type of primitives
140     *
141     * @param type the type of primitives
142     * @param tc the tags belonging to this type of primitives
143     * @param targetStatistics histogram of paste targets, number of primitives of each type in the paste target
144     */
145    protected void initResolver(OsmPrimitiveType type, TagCollection tc, Map<OsmPrimitiveType, Integer> targetStatistics) {
146        TagConflictResolver resolver = resolvers.get(type);
147        resolver.getModel().populate(tc, tc.getKeysWithMultipleValues());
148        resolver.getModel().prepareDefaultTagDecisions();
149        if (!tc.isEmpty() && targetStatistics.get(type) != null && targetStatistics.get(type) > 0) {
150            tpResolvers.add(PANE_TITLES.get(type), resolver);
151        }
152    }
153
154    /**
155     * Populates the conflict resolver with one tag collection
156     *
157     * @param tagsForAllPrimitives  the tag collection
158     * @param sourceStatistics histogram of tag source, number of primitives of each type in the source
159     * @param targetStatistics histogram of paste targets, number of primitives of each type in the paste target
160     */
161    public void populate(TagCollection tagsForAllPrimitives, Map<OsmPrimitiveType, Integer> sourceStatistics,
162            Map<OsmPrimitiveType, Integer> targetStatistics) {
163        mode = Mode.RESOLVING_ONE_TAGCOLLECTION_ONLY;
164        tagsForAllPrimitives = tagsForAllPrimitives == null ? new TagCollection() : tagsForAllPrimitives;
165        sourceStatistics = sourceStatistics == null ? new HashMap<>() : sourceStatistics;
166        targetStatistics = targetStatistics == null ? new HashMap<>() : targetStatistics;
167
168        // init the resolver
169        //
170        model.populate(tagsForAllPrimitives, tagsForAllPrimitives.getKeysWithMultipleValues());
171        model.prepareDefaultTagDecisions();
172
173        // prepare the dialog with one tag resolver
174        pnlTagResolver.removeAll();
175        pnlTagResolver.add(new TagConflictResolver(model), BorderLayout.CENTER);
176
177        statisticsModel.reset();
178        StatisticsInfo info = new StatisticsInfo();
179        info.numTags = tagsForAllPrimitives.getKeys().size();
180        info.sourceInfo.putAll(sourceStatistics);
181        info.targetInfo.putAll(targetStatistics);
182        statisticsModel.append(info);
183        validate();
184    }
185
186    protected int getNumResolverTabs() {
187        return tpResolvers.getTabCount();
188    }
189
190    protected TagConflictResolver getResolver(int idx) {
191        return (TagConflictResolver) tpResolvers.getComponentAt(idx);
192    }
193
194    /**
195     * Populate the tag conflict resolver with tags for each type of primitives
196     *
197     * @param tagsForNodes the tags belonging to nodes in the paste source
198     * @param tagsForWays the tags belonging to way in the paste source
199     * @param tagsForRelations the tags belonging to relations in the paste source
200     * @param sourceStatistics histogram of tag source, number of primitives of each type in the source
201     * @param targetStatistics histogram of paste targets, number of primitives of each type in the paste target
202     */
203    public void populate(TagCollection tagsForNodes, TagCollection tagsForWays, TagCollection tagsForRelations,
204            Map<OsmPrimitiveType, Integer> sourceStatistics, Map<OsmPrimitiveType, Integer> targetStatistics) {
205        tagsForNodes = (tagsForNodes == null) ? new TagCollection() : tagsForNodes;
206        tagsForWays = (tagsForWays == null) ? new TagCollection() : tagsForWays;
207        tagsForRelations = (tagsForRelations == null) ? new TagCollection() : tagsForRelations;
208        if (tagsForNodes.isEmpty() && tagsForWays.isEmpty() && tagsForRelations.isEmpty()) {
209            populate(null, null, null);
210            return;
211        }
212        tpResolvers.removeAll();
213        initResolver(OsmPrimitiveType.NODE, tagsForNodes, targetStatistics);
214        initResolver(OsmPrimitiveType.WAY, tagsForWays, targetStatistics);
215        initResolver(OsmPrimitiveType.RELATION, tagsForRelations, targetStatistics);
216
217        pnlTagResolver.removeAll();
218        pnlTagResolver.add(tpResolvers, BorderLayout.CENTER);
219        mode = Mode.RESOLVING_TYPED_TAGCOLLECTIONS;
220        validate();
221        statisticsModel.reset();
222        if (!tagsForNodes.isEmpty()) {
223            StatisticsInfo info = new StatisticsInfo();
224            info.numTags = tagsForNodes.getKeys().size();
225            int numTargets = targetStatistics.get(OsmPrimitiveType.NODE) == null ? 0 : targetStatistics.get(OsmPrimitiveType.NODE);
226            if (numTargets > 0) {
227                info.sourceInfo.put(OsmPrimitiveType.NODE, sourceStatistics.get(OsmPrimitiveType.NODE));
228                info.targetInfo.put(OsmPrimitiveType.NODE, numTargets);
229                statisticsModel.append(info);
230            }
231        }
232        if (!tagsForWays.isEmpty()) {
233            StatisticsInfo info = new StatisticsInfo();
234            info.numTags = tagsForWays.getKeys().size();
235            int numTargets = targetStatistics.get(OsmPrimitiveType.WAY) == null ? 0 : targetStatistics.get(OsmPrimitiveType.WAY);
236            if (numTargets > 0) {
237                info.sourceInfo.put(OsmPrimitiveType.WAY, sourceStatistics.get(OsmPrimitiveType.WAY));
238                info.targetInfo.put(OsmPrimitiveType.WAY, numTargets);
239                statisticsModel.append(info);
240            }
241        }
242        if (!tagsForRelations.isEmpty()) {
243            StatisticsInfo info = new StatisticsInfo();
244            info.numTags = tagsForRelations.getKeys().size();
245            int numTargets = targetStatistics.get(OsmPrimitiveType.RELATION) == null ? 0 : targetStatistics.get(OsmPrimitiveType.RELATION);
246            if (numTargets > 0) {
247                info.sourceInfo.put(OsmPrimitiveType.RELATION, sourceStatistics.get(OsmPrimitiveType.RELATION));
248                info.targetInfo.put(OsmPrimitiveType.RELATION, numTargets);
249                statisticsModel.append(info);
250            }
251        }
252
253        for (int i = 0; i < getNumResolverTabs(); i++) {
254            if (!getResolver(i).getModel().isResolvedCompletely()) {
255                tpResolvers.setSelectedIndex(i);
256                break;
257            }
258        }
259    }
260
261    protected void setCanceled(boolean canceled) {
262        this.canceled = canceled;
263    }
264
265    public boolean isCanceled() {
266        return this.canceled;
267    }
268
269    final class CancelAction extends AbstractAction {
270
271        private CancelAction() {
272            putValue(Action.SHORT_DESCRIPTION, tr("Cancel conflict resolution"));
273            putValue(Action.NAME, tr("Cancel"));
274            new ImageProvider("cancel").getResource().attachImageIcon(this);
275            setEnabled(true);
276        }
277
278        @Override
279        public void actionPerformed(ActionEvent arg0) {
280            setVisible(false);
281            setCanceled(true);
282        }
283    }
284
285    final class ApplyAction extends AbstractAction implements PropertyChangeListener {
286
287        private ApplyAction() {
288            putValue(Action.SHORT_DESCRIPTION, tr("Apply resolved conflicts"));
289            putValue(Action.NAME, tr("Apply"));
290            new ImageProvider("ok").getResource().attachImageIcon(this);
291            updateEnabledState();
292        }
293
294        @Override
295        public void actionPerformed(ActionEvent arg0) {
296            setVisible(false);
297        }
298
299        void updateEnabledState() {
300            if (mode == null) {
301                setEnabled(false);
302            } else if (mode == Mode.RESOLVING_ONE_TAGCOLLECTION_ONLY) {
303                setEnabled(model.isResolvedCompletely());
304            } else {
305                setEnabled(resolvers.values().stream().allMatch(val -> val.getModel().isResolvedCompletely()));
306            }
307        }
308
309        @Override
310        public void propertyChange(PropertyChangeEvent evt) {
311            if (evt.getPropertyName().equals(TagConflictResolverModel.NUM_CONFLICTS_PROP)) {
312                updateEnabledState();
313            }
314        }
315    }
316
317    @Override
318    public void setVisible(boolean visible) {
319        if (visible) {
320            new WindowGeometry(
321                    getClass().getName() + ".geometry",
322                    WindowGeometry.centerOnScreen(new Dimension(600, 400))
323            ).applySafe(this);
324        } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
325            new WindowGeometry(this).remember(getClass().getName() + ".geometry");
326        }
327        super.setVisible(visible);
328    }
329
330    /**
331     * Returns conflict resolution.
332     * @return conflict resolution
333     */
334    public TagCollection getResolution() {
335        return model.getResolution();
336    }
337
338    public TagCollection getResolution(OsmPrimitiveType type) {
339        if (type == null) return null;
340        return resolvers.get(type).getModel().getResolution();
341    }
342
343    @Override
344    public void propertyChange(PropertyChangeEvent evt) {
345        if (evt.getPropertyName().equals(TagConflictResolverModel.NUM_CONFLICTS_PROP)) {
346            TagConflictResolverModel tagModel = (TagConflictResolverModel) evt.getSource();
347            for (int i = 0; i < tpResolvers.getTabCount(); i++) {
348                TagConflictResolver resolver = (TagConflictResolver) tpResolvers.getComponentAt(i);
349                if (tagModel == resolver.getModel()) {
350                    tpResolvers.setIconAt(i,
351                            (Integer) evt.getNewValue() == 0 ? iconResolved : iconUnresolved
352                    );
353                }
354            }
355        }
356    }
357
358    static final class StatisticsInfo {
359        int numTags;
360        final Map<OsmPrimitiveType, Integer> sourceInfo;
361        final Map<OsmPrimitiveType, Integer> targetInfo;
362
363        StatisticsInfo() {
364            sourceInfo = new EnumMap<>(OsmPrimitiveType.class);
365            targetInfo = new EnumMap<>(OsmPrimitiveType.class);
366        }
367    }
368
369    static final class StatisticsTableModel extends DefaultTableModel {
370        private static final String[] HEADERS = {tr("Paste ..."), tr("From ..."), tr("To ...") };
371        private final transient List<StatisticsInfo> data = new ArrayList<>();
372
373        @Override
374        public Object getValueAt(int row, int column) {
375            if (row == 0)
376                return HEADERS[column];
377            else if (row -1 < data.size())
378                return data.get(row -1);
379            else
380                return null;
381        }
382
383        @Override
384        public boolean isCellEditable(int row, int column) {
385            return false;
386        }
387
388        @Override
389        public int getRowCount() {
390            return data == null ? 1 : data.size() + 1;
391        }
392
393        void reset() {
394            data.clear();
395        }
396
397        void append(StatisticsInfo info) {
398            data.add(info);
399            fireTableDataChanged();
400        }
401    }
402
403    static final class StatisticsInfoRenderer extends JLabel implements TableCellRenderer {
404        private void reset() {
405            setIcon(null);
406            setText("");
407            setFont(UIManager.getFont("Table.font"));
408        }
409
410        private void renderNumTags(StatisticsInfo info) {
411            if (info == null) return;
412            setText(trn("{0} tag", "{0} tags", info.numTags, info.numTags));
413        }
414
415        private void renderStatistics(Map<OsmPrimitiveType, Integer> stat) {
416            if (stat == null) return;
417            if (stat.isEmpty()) return;
418            if (stat.size() == 1) {
419                setIcon(ImageProvider.get(stat.keySet().iterator().next()));
420            } else {
421                setIcon(ImageProvider.get("data", "object"));
422            }
423            StringBuilder text = new StringBuilder();
424            for (Entry<OsmPrimitiveType, Integer> entry: stat.entrySet()) {
425                OsmPrimitiveType type = entry.getKey();
426                int numPrimitives = entry.getValue() == null ? 0 : entry.getValue();
427                if (numPrimitives == 0) {
428                    continue;
429                }
430                String msg;
431                switch(type) {
432                case NODE: msg = trn("{0} node", "{0} nodes", numPrimitives, numPrimitives); break;
433                case WAY: msg = trn("{0} way", "{0} ways", numPrimitives, numPrimitives); break;
434                case RELATION: msg = trn("{0} relation", "{0} relations", numPrimitives, numPrimitives); break;
435                default: throw new AssertionError();
436                }
437                if (text.length() > 0) {
438                    text.append(", ");
439                }
440                text.append(msg);
441            }
442            setText(text.toString());
443        }
444
445        private void renderFrom(StatisticsInfo info) {
446            renderStatistics(info.sourceInfo);
447        }
448
449        private void renderTo(StatisticsInfo info) {
450            renderStatistics(info.targetInfo);
451        }
452
453        @Override
454        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
455                boolean hasFocus, int row, int column) {
456            reset();
457            if (value == null)
458                return this;
459
460            if (row == 0) {
461                setFont(getFont().deriveFont(Font.BOLD));
462                setText((String) value);
463            } else {
464                StatisticsInfo info = (StatisticsInfo) value;
465
466                switch(column) {
467                case 0: renderNumTags(info); break;
468                case 1: renderFrom(info); break;
469                case 2: renderTo(info); break;
470                default: // Do nothing
471                }
472            }
473            return this;
474        }
475    }
476
477    static final class StatisticsInfoTable extends JPanel {
478
479        StatisticsInfoTable(StatisticsTableModel model) {
480            JTable infoTable = new JTable(model,
481                    new TagTableColumnModelBuilder(new StatisticsInfoRenderer(), tr("Paste ..."), tr("From ..."), tr("To ...")).build());
482            infoTable.setShowHorizontalLines(true);
483            infoTable.setShowVerticalLines(false);
484            infoTable.setEnabled(false);
485            setLayout(new BorderLayout());
486            add(infoTable, BorderLayout.CENTER);
487        }
488
489        @Override
490        public Insets getInsets() {
491            Insets insets = super.getInsets();
492            insets.bottom = 20;
493            return insets;
494        }
495    }
496}