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

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

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

  • Property svn:eol-style set to native
File size: 5.3 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.tools.CheckParameterUtil;
19
20/**
21 * Tag settings panel of upload dialog.
22 * @since 2599
23 */
24public class TagSettingsPanel extends JPanel implements TableModelListener {
25
26 /** checkbox for selecting whether an atomic upload is to be used */
27 private final TagEditorPanel pnlTagEditor = new TagEditorPanel(null, null, Changeset.MAX_CHANGESET_TAG_LENGTH);
28 /** the model for the changeset comment */
29 private final transient ChangesetCommentModel changesetCommentModel;
30 private final transient ChangesetCommentModel changesetSourceModel;
31
32 /**
33 * Creates a new panel
34 *
35 * @param changesetCommentModel the changeset comment model. Must not be null.
36 * @param changesetSourceModel the changeset source model. Must not be null.
37 * @throws IllegalArgumentException if {@code changesetCommentModel} is null
38 */
39 public TagSettingsPanel(ChangesetCommentModel changesetCommentModel, ChangesetCommentModel changesetSourceModel) {
40 CheckParameterUtil.ensureParameterNotNull(changesetCommentModel, "changesetCommentModel");
41 CheckParameterUtil.ensureParameterNotNull(changesetSourceModel, "changesetSourceModel");
42 this.changesetCommentModel = changesetCommentModel;
43 this.changesetSourceModel = changesetSourceModel;
44 this.changesetCommentModel.addChangeListener(new ChangesetCommentChangeListener("comment"));
45 this.changesetSourceModel.addChangeListener(new ChangesetCommentChangeListener("source"));
46 build();
47 pnlTagEditor.getModel().addTableModelListener(this);
48 }
49
50 protected void build() {
51 setLayout(new BorderLayout());
52 add(pnlTagEditor, BorderLayout.CENTER);
53 }
54
55 protected void setProperty(String key, String value) {
56 String val = (value == null ? "" : value).trim();
57 String commentInTag = getTagEditorValue(key);
58 if (val.equals(commentInTag))
59 return;
60
61 if (val.isEmpty()) {
62 pnlTagEditor.getModel().delete(key);
63 return;
64 }
65 TagModel tag = pnlTagEditor.getModel().get(key);
66 if (tag == null) {
67 tag = new TagModel(key, val);
68 pnlTagEditor.getModel().add(tag);
69 } else {
70 pnlTagEditor.getModel().updateTagValue(tag, val);
71 }
72 }
73
74 protected String getTagEditorValue(String key) {
75 TagModel tag = pnlTagEditor.getModel().get(key);
76 return tag == null ? null : tag.getValue();
77 }
78
79 /**
80 * Initialize panel from the given tags.
81 * @param tags the tags used to initialize the panel
82 */
83 public void initFromTags(Map<String, String> tags) {
84 pnlTagEditor.getModel().initFromTags(tags);
85 }
86
87 /**
88 * Replies the map with the current tags in the tag editor model.
89 * @param keepEmpty {@code true} to keep empty tags
90 * @return the map with the current tags in the tag editor model.
91 */
92 public Map<String, String> getTags(boolean keepEmpty) {
93 forceCommentFieldReload();
94 return pnlTagEditor.getModel().getTags(keepEmpty);
95 }
96
97 /**
98 * Initializes the panel for user input
99 */
100 public void startUserInput() {
101 pnlTagEditor.initAutoCompletion(MainApplication.getLayerManager().getEditLayer());
102 }
103
104 /* -------------------------------------------------------------------------- */
105 /* Interface TableChangeListener */
106 /* -------------------------------------------------------------------------- */
107 @Override
108 public void tableChanged(TableModelEvent e) {
109 changesetCommentModel.setComment(getTagEditorValue("comment"));
110 changesetSourceModel.setComment(getTagEditorValue("source"));
111 }
112
113 /**
114 * Force update the fields if the user is currently changing them. See #5676
115 */
116 private void forceCommentFieldReload() {
117 setProperty("comment", changesetCommentModel.getComment());
118 setProperty("source", changesetSourceModel.getComment());
119 }
120
121 /**
122 * Observes the changeset comment model and keeps the tag editor in sync
123 * with the current changeset comment
124 */
125 class ChangesetCommentChangeListener implements ChangeListener {
126
127 private final String key;
128
129 ChangesetCommentChangeListener(String key) {
130 this.key = key;
131 }
132
133 @Override
134 public void stateChanged(ChangeEvent e) {
135 if (e.getSource() instanceof ChangesetCommentModel) {
136 String newValue = ((ChangesetCommentModel) e.getSource()).getComment();
137 String oldValue = Optional.ofNullable(getTagEditorValue(key)).orElse("");
138 if (!oldValue.equals(newValue)) {
139 setProperty(key, newValue);
140 }
141 }
142 }
143 }
144}
Note: See TracBrowser for help on using the repository browser.