001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.conflict.tags;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.BorderLayout;
007import java.awt.GridBagConstraints;
008import java.awt.GridBagLayout;
009
010import javax.swing.BorderFactory;
011import javax.swing.JCheckBox;
012import javax.swing.JLabel;
013import javax.swing.JPanel;
014import javax.swing.JScrollPane;
015import javax.swing.event.ChangeEvent;
016import javax.swing.event.ChangeListener;
017
018import org.openstreetmap.josm.Main;
019
020/**
021 * This is a UI widget for resolving tag conflicts, i.e. differences of the tag values
022 * of multiple {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s.
023 *
024 *
025 */
026public class TagConflictResolver extends JPanel {
027
028    /** the model for the tag conflict resolver */
029    private TagConflictResolverModel model;
030    /** selects whether only tags with conflicts are displayed */
031    private JCheckBox cbShowTagsWithConflictsOnly;
032    private JCheckBox cbShowTagsWithMultiValuesOnly;
033
034    protected JPanel buildInfoPanel() {
035        JPanel pnl = new JPanel();
036        pnl.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
037        pnl.setLayout(new GridBagLayout());
038        GridBagConstraints gc = new GridBagConstraints();
039        gc.fill = GridBagConstraints.BOTH;
040        gc.weighty = 1.0;
041        gc.weightx = 1.0;
042        gc.anchor = GridBagConstraints.LINE_START;
043        gc.gridwidth = 2;
044        pnl.add(new JLabel(tr("<html>Please select the values to keep for the following tags.</html>")), gc);
045
046        gc.gridwidth = 1;
047        gc.gridy = 1;
048        gc.fill = GridBagConstraints.HORIZONTAL;
049        gc.weighty = 0.0;
050        pnl.add(cbShowTagsWithConflictsOnly = new JCheckBox(tr("Show tags with conflicts only")), gc);
051        pnl.add(cbShowTagsWithMultiValuesOnly = new JCheckBox(tr("Show tags with multiple values only")), gc);
052        cbShowTagsWithConflictsOnly.addChangeListener(
053                new ChangeListener() {
054                    @Override
055                    public void stateChanged(ChangeEvent e) {
056                        model.setShowTagsWithConflictsOnly(cbShowTagsWithConflictsOnly.isSelected());
057                        cbShowTagsWithMultiValuesOnly.setEnabled(cbShowTagsWithConflictsOnly.isSelected());
058                    }
059                }
060        );
061        cbShowTagsWithConflictsOnly.setSelected(
062                Main.pref.getBoolean(getClass().getName() + ".showTagsWithConflictsOnly", false)
063        );
064        cbShowTagsWithMultiValuesOnly.addChangeListener(
065                new ChangeListener() {
066                    @Override
067                    public void stateChanged(ChangeEvent e) {
068                        model.setShowTagsWithMultiValuesOnly(cbShowTagsWithMultiValuesOnly.isSelected());
069                    }
070                }
071        );
072        cbShowTagsWithMultiValuesOnly.setSelected(
073                Main.pref.getBoolean(getClass().getName() + ".showTagsWithMultiValuesOnly", false)
074        );
075        cbShowTagsWithMultiValuesOnly.setEnabled(cbShowTagsWithConflictsOnly.isSelected());
076        return pnl;
077    }
078
079    /**
080     * Remembers the current settings in the global preferences
081     *
082     */
083    public void rememberPreferences() {
084        Main.pref.put(getClass().getName() + ".showTagsWithConflictsOnly", cbShowTagsWithConflictsOnly.isSelected());
085        Main.pref.put(getClass().getName() + ".showTagsWithMultiValuesOnly", cbShowTagsWithMultiValuesOnly.isSelected());
086    }
087
088    protected final void build() {
089        setLayout(new BorderLayout());
090        add(buildInfoPanel(), BorderLayout.NORTH);
091        add(new JScrollPane(new TagConflictResolverTable(model)), BorderLayout.CENTER);
092    }
093
094    /**
095     * Constructs a new {@code TagConflictResolver}.
096     */
097    public TagConflictResolver() {
098        this.model = new TagConflictResolverModel();
099        build();
100    }
101
102    /**
103     * Replies the model used by this dialog
104     *
105     * @return the model
106     */
107    public TagConflictResolverModel getModel() {
108        return model;
109    }
110}