source: josm/trunk/src/org/openstreetmap/josm/gui/io/TagSettingsPanel.java @ 13198

Last change on this file since 13198 was 13198, checked in by Don-vip, 15 months ago

fix #15651 - new property upload.changeset.hashtags to disable automatic changeset hashtags

  • Property svn:eol-style set to native
File size: 7.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import java.awt.BorderLayout;
5import java.util.Map;
6import java.util.Optional;
7
8import javax.swing.JPanel;
9import javax.swing.event.ChangeEvent;
10import javax.swing.event.ChangeListener;
11import javax.swing.event.TableModelEvent;
12import javax.swing.event.TableModelListener;
13
14import org.openstreetmap.josm.data.osm.Changeset;
15import org.openstreetmap.josm.gui.MainApplication;
16import org.openstreetmap.josm.gui.tagging.TagEditorPanel;
17import org.openstreetmap.josm.gui.tagging.TagModel;
18import org.openstreetmap.josm.spi.preferences.Config;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20
21/**
22 * Tag settings panel of upload dialog.
23 * @since 2599
24 */
25public class TagSettingsPanel extends JPanel implements TableModelListener {
26
27    /** checkbox for selecting whether an atomic upload is to be used  */
28    private final TagEditorPanel pnlTagEditor = new TagEditorPanel(null, null, Changeset.MAX_CHANGESET_TAG_LENGTH);
29    /** the model for the changeset comment */
30    private final transient ChangesetCommentModel changesetCommentModel;
31    private final transient ChangesetCommentModel changesetSourceModel;
32    private final transient ChangesetReviewModel changesetReviewModel;
33
34    /**
35     * Creates a new panel
36     *
37     * @param changesetCommentModel the changeset comment model. Must not be null.
38     * @param changesetSourceModel the changeset source model. Must not be null.
39     * @param changesetReviewModel the model for the changeset review. Must not be null.
40     * @throws IllegalArgumentException if {@code changesetCommentModel} is null
41     * @since 12719 (signature)
42     */
43    public TagSettingsPanel(ChangesetCommentModel changesetCommentModel, ChangesetCommentModel changesetSourceModel,
44            ChangesetReviewModel changesetReviewModel) {
45        CheckParameterUtil.ensureParameterNotNull(changesetCommentModel, "changesetCommentModel");
46        CheckParameterUtil.ensureParameterNotNull(changesetSourceModel, "changesetSourceModel");
47        CheckParameterUtil.ensureParameterNotNull(changesetReviewModel, "changesetReviewModel");
48        this.changesetCommentModel = changesetCommentModel;
49        this.changesetSourceModel = changesetSourceModel;
50        this.changesetReviewModel = changesetReviewModel;
51        changesetCommentModel.addChangeListener(new ChangesetCommentChangeListener("comment", "hashtags"));
52        changesetSourceModel.addChangeListener(new ChangesetCommentChangeListener("source"));
53        changesetReviewModel.addChangeListener(new ChangesetReviewChangeListener());
54        build();
55        pnlTagEditor.getModel().addTableModelListener(this);
56    }
57
58    protected void build() {
59        setLayout(new BorderLayout());
60        add(pnlTagEditor, BorderLayout.CENTER);
61    }
62
63    protected void setProperty(String key, String value) {
64        String val = (value == null ? "" : value).trim();
65        String commentInTag = getTagEditorValue(key);
66        if (val.equals(commentInTag))
67            return;
68
69        if (val.isEmpty()) {
70            pnlTagEditor.getModel().delete(key);
71            return;
72        }
73        TagModel tag = pnlTagEditor.getModel().get(key);
74        if (tag == null) {
75            tag = new TagModel(key, val);
76            pnlTagEditor.getModel().add(tag);
77        } else {
78            pnlTagEditor.getModel().updateTagValue(tag, val);
79        }
80    }
81
82    protected String getTagEditorValue(String key) {
83        TagModel tag = pnlTagEditor.getModel().get(key);
84        return tag == null ? null : tag.getValue();
85    }
86
87    /**
88     * Initialize panel from the given tags.
89     * @param tags the tags used to initialize the panel
90     */
91    public void initFromTags(Map<String, String> tags) {
92        pnlTagEditor.getModel().initFromTags(tags);
93    }
94
95    /**
96     * Replies the map with the current tags in the tag editor model.
97     * @param keepEmpty {@code true} to keep empty tags
98     * @return the map with the current tags in the tag editor model.
99     */
100    public Map<String, String> getTags(boolean keepEmpty) {
101        forceCommentFieldReload();
102        return pnlTagEditor.getModel().getTags(keepEmpty);
103    }
104
105    /**
106     * Initializes the panel for user input
107     */
108    public void startUserInput() {
109        pnlTagEditor.initAutoCompletion(MainApplication.getLayerManager().getEditLayer());
110    }
111
112    /* -------------------------------------------------------------------------- */
113    /* Interface TableChangeListener                                              */
114    /* -------------------------------------------------------------------------- */
115    @Override
116    public void tableChanged(TableModelEvent e) {
117        changesetCommentModel.setComment(getTagEditorValue("comment"));
118        changesetSourceModel.setComment(getTagEditorValue("source"));
119        changesetReviewModel.setReviewRequested("yes".equals(getTagEditorValue("review_requested")));
120    }
121
122    /**
123     * Force update the fields if the user is currently changing them. See #5676
124     */
125    private void forceCommentFieldReload() {
126        setProperty("comment", changesetCommentModel.getComment());
127        setProperty("source", changesetSourceModel.getComment());
128        setProperty("review_requested", changesetReviewModel.isReviewRequested() ? "yes" : null);
129    }
130
131    /**
132     * Observes the changeset comment model and keeps the tag editor in sync
133     * with the current changeset comment
134     */
135    class ChangesetCommentChangeListener implements ChangeListener {
136
137        private final String key;
138        private final String hashtagsKey;
139
140        ChangesetCommentChangeListener(String key) {
141            this(key, null);
142        }
143
144        ChangesetCommentChangeListener(String key, String hashtagsKey) {
145            this.key = key;
146            this.hashtagsKey = hashtagsKey;
147        }
148
149        @Override
150        public void stateChanged(ChangeEvent e) {
151            if (e.getSource() instanceof ChangesetCommentModel) {
152                ChangesetCommentModel model = ((ChangesetCommentModel) e.getSource());
153                String newValue = model.getComment();
154                String oldValue = Optional.ofNullable(getTagEditorValue(key)).orElse("");
155                if (!oldValue.equals(newValue)) {
156                    setProperty(key, newValue);
157                    if (hashtagsKey != null && Config.getPref().getBoolean("upload.changeset.hashtags", true)) {
158                        String newHashTags = String.join(";", model.findHashTags());
159                        String oldHashTags = Optional.ofNullable(getTagEditorValue(hashtagsKey)).orElse("");
160                        if (!oldHashTags.equals(newHashTags)) {
161                            setProperty(hashtagsKey, newHashTags);
162                        }
163                    }
164                }
165            }
166        }
167    }
168
169    /**
170     * Observes the changeset review model and keeps the tag editor in sync
171     * with the current changeset review request
172     */
173    class ChangesetReviewChangeListener implements ChangeListener {
174
175        private static final String KEY = "review_requested";
176
177        @Override
178        public void stateChanged(ChangeEvent e) {
179            if (e.getSource() instanceof ChangesetReviewModel) {
180                boolean newState = ((ChangesetReviewModel) e.getSource()).isReviewRequested();
181                boolean oldState = "yes".equals(Optional.ofNullable(getTagEditorValue(KEY)).orElse(""));
182                if (oldState != newState) {
183                    setProperty(KEY, newState ? "yes" : null);
184                }
185            }
186        }
187    }
188}
Note: See TracBrowser for help on using the repository browser.