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

Last change on this file since 13109 was 13109, checked in by Don-vip, 6 years ago

fix #15537 - Support changeset hashtags (hashtags changeset tag, extracted from comment at upload, or set by remote control through new changeset_hashtags parameter)

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