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

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

see #13036 - deprecate Command() default constructor, fix unit tests and java warnings

  • Property svn:eol-style set to native
File size: 6.7 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;
50 changesetCommentModel.addChangeListener(new ChangesetCommentChangeListener("comment"));
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;
137
[10210]138 ChangesetCommentChangeListener(String key) {
[6401]139 this.key = key;
140 }
141
[6084]142 @Override
[10210]143 public void stateChanged(ChangeEvent e) {
144 if (e.getSource() instanceof ChangesetCommentModel) {
145 String newValue = ((ChangesetCommentModel) e.getSource()).getComment();
[11553]146 String oldValue = Optional.ofNullable(getTagEditorValue(key)).orElse("");
[9522]147 if (!oldValue.equals(newValue)) {
[10210]148 setProperty(key, newValue);
[9522]149 }
[3133]150 }
151 }
152 }
[12719]153
154 /**
155 * Observes the changeset review model and keeps the tag editor in sync
156 * with the current changeset review request
157 */
158 class ChangesetReviewChangeListener implements ChangeListener {
159
[12726]160 private static final String KEY = "review_requested";
[12719]161
162 @Override
163 public void stateChanged(ChangeEvent e) {
164 if (e.getSource() instanceof ChangesetReviewModel) {
165 boolean newState = ((ChangesetReviewModel) e.getSource()).isReviewRequested();
[12726]166 boolean oldState = "yes".equals(Optional.ofNullable(getTagEditorValue(KEY)).orElse(""));
[12719]167 if (oldState != newState) {
[12726]168 setProperty(KEY, newState ? "yes" : null);
[12719]169 }
170 }
171 }
172 }
[2711]173}
Note: See TracBrowser for help on using the repository browser.