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;
015
016import org.openstreetmap.josm.Main;
017
018/**
019 * This is a UI widget for resolving tag conflicts, i.e. differences of the tag values
020 * of multiple {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s.
021 * @since 2008
022 */
023public class TagConflictResolver extends JPanel {
024
025    /** the model for the tag conflict resolver */
026    private final TagConflictResolverModel model;
027    /** selects whether only tags with conflicts are displayed */
028    private final JCheckBox cbShowTagsWithConflictsOnly = new JCheckBox(tr("Show tags with conflicts only"));
029    private final JCheckBox cbShowTagsWithMultiValuesOnly = new JCheckBox(tr("Show tags with multiple values only"));
030
031    /**
032     * Constructs a new {@code TagConflictResolver}.
033     */
034    public TagConflictResolver() {
035        this.model = new TagConflictResolverModel();
036        build();
037    }
038
039    protected JPanel buildInfoPanel() {
040        JPanel pnl = new JPanel(new GridBagLayout());
041        pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
042        GridBagConstraints gc = new GridBagConstraints();
043        gc.fill = GridBagConstraints.BOTH;
044        gc.weighty = 1.0;
045        gc.weightx = 1.0;
046        gc.anchor = GridBagConstraints.LINE_START;
047        gc.gridwidth = 2;
048        pnl.add(new JLabel(tr("<html>Please select the values to keep for the following tags.</html>")), gc);
049
050        gc.gridwidth = 1;
051        gc.gridy = 1;
052        gc.fill = GridBagConstraints.HORIZONTAL;
053        gc.weighty = 0.0;
054        pnl.add(cbShowTagsWithConflictsOnly, gc);
055        pnl.add(cbShowTagsWithMultiValuesOnly, gc);
056        cbShowTagsWithConflictsOnly.addChangeListener(e -> {
057                model.setShowTagsWithConflictsOnly(cbShowTagsWithConflictsOnly.isSelected());
058                cbShowTagsWithMultiValuesOnly.setEnabled(cbShowTagsWithConflictsOnly.isSelected());
059        });
060        cbShowTagsWithConflictsOnly.setSelected(
061                Main.pref.getBoolean(getClass().getName() + ".showTagsWithConflictsOnly", false)
062        );
063        cbShowTagsWithMultiValuesOnly.addChangeListener(
064                e -> model.setShowTagsWithMultiValuesOnly(cbShowTagsWithMultiValuesOnly.isSelected())
065        );
066        cbShowTagsWithMultiValuesOnly.setSelected(
067                Main.pref.getBoolean(getClass().getName() + ".showTagsWithMultiValuesOnly", false)
068        );
069        cbShowTagsWithMultiValuesOnly.setEnabled(cbShowTagsWithConflictsOnly.isSelected());
070        return pnl;
071    }
072
073    /**
074     * Remembers the current settings in the global preferences
075     *
076     */
077    public void rememberPreferences() {
078        Main.pref.put(getClass().getName() + ".showTagsWithConflictsOnly", cbShowTagsWithConflictsOnly.isSelected());
079        Main.pref.put(getClass().getName() + ".showTagsWithMultiValuesOnly", cbShowTagsWithMultiValuesOnly.isSelected());
080    }
081
082    protected final void build() {
083        setLayout(new BorderLayout());
084        add(buildInfoPanel(), BorderLayout.NORTH);
085        add(new JScrollPane(new TagConflictResolverTable(model)), BorderLayout.CENTER);
086    }
087
088    /**
089     * Replies the model used by this dialog
090     *
091     * @return the model
092     */
093    public TagConflictResolverModel getModel() {
094        return model;
095    }
096}