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

Last change on this file since 9371 was 9246, checked in by Don-vip, 8 years ago

javadoc update

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import java.awt.BorderLayout;
5import java.util.HashMap;
6import java.util.Map;
7import java.util.Observable;
8import java.util.Observer;
9
10import javax.swing.JPanel;
11import javax.swing.event.TableModelEvent;
12import javax.swing.event.TableModelListener;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.Version;
16import org.openstreetmap.josm.data.osm.Changeset;
17import org.openstreetmap.josm.gui.tagging.TagEditorPanel;
18import org.openstreetmap.josm.gui.tagging.TagModel;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20
21public class TagSettingsPanel extends JPanel implements TableModelListener {
22
23 /** checkbox for selecting whether an atomic upload is to be used */
24 private final TagEditorPanel pnlTagEditor = new TagEditorPanel(null, null, Changeset.MAX_CHANGESET_TAG_LENGTH);
25 /** the model for the changeset comment */
26 private final transient ChangesetCommentModel changesetCommentModel;
27 private final transient ChangesetCommentModel changesetSourceModel;
28 /** tags that applied to uploaded changesets by default*/
29 private final transient Map<String, String> defaultTags = new HashMap<>();
30
31 protected void build() {
32 setLayout(new BorderLayout());
33 add(pnlTagEditor, BorderLayout.CENTER);
34 }
35
36 /**
37 * Creates a new panel
38 *
39 * @param changesetCommentModel the changeset comment model. Must not be null.
40 * @param changesetSourceModel the changeset source model. Must not be null.
41 * @throws IllegalArgumentException if {@code changesetCommentModel} is null
42 */
43 public TagSettingsPanel(ChangesetCommentModel changesetCommentModel, ChangesetCommentModel changesetSourceModel) {
44 CheckParameterUtil.ensureParameterNotNull(changesetCommentModel, "changesetCommentModel");
45 CheckParameterUtil.ensureParameterNotNull(changesetSourceModel, "changesetSourceModel");
46 this.changesetCommentModel = changesetCommentModel;
47 this.changesetSourceModel = changesetSourceModel;
48 this.changesetCommentModel.addObserver(new ChangesetCommentObserver("comment"));
49 this.changesetSourceModel.addObserver(new ChangesetCommentObserver("source"));
50 build();
51 pnlTagEditor.getModel().addTableModelListener(this);
52 }
53
54 protected void setProperty(String key, String value) {
55 if (value == null) {
56 value = "";
57 }
58 value = value.trim();
59 String commentInTag = getTagEditorValue(key);
60 if (value.equals(commentInTag))
61 return;
62
63 if (value.isEmpty()) {
64 pnlTagEditor.getModel().delete(key);
65 return;
66 }
67 TagModel tag = pnlTagEditor.getModel().get(key);
68 if (tag == null) {
69 tag = new TagModel(key, value);
70 pnlTagEditor.getModel().add(tag);
71 } else {
72 pnlTagEditor.getModel().updateTagValue(tag, value);
73 }
74 }
75
76 protected String getTagEditorValue(String key) {
77 TagModel tag = pnlTagEditor.getModel().get(key);
78 if (tag == null) return null;
79 return tag.getValue();
80 }
81
82 /**
83 * Initialize panel from changeset.
84 * @param cs changeset
85 */
86 public void initFromChangeset(Changeset cs) {
87 Map<String, String> tags = getDefaultTags();
88 if (cs != null) {
89 tags.putAll(cs.getKeys());
90 }
91 if (tags.get("comment") == null) {
92 tags.put("comment", getTagEditorValue("comment"));
93 }
94 if (tags.get("source") == null) {
95 tags.put("source", getTagEditorValue("source"));
96 }
97 String agent = Version.getInstance().getAgentString(false);
98 String created_by = tags.get("created_by");
99 if (created_by == null || created_by.isEmpty()) {
100 tags.put("created_by", agent);
101 } else if (!created_by.contains(agent)) {
102 tags.put("created_by", created_by + ';' + agent);
103 }
104 pnlTagEditor.getModel().initFromTags(tags);
105 }
106
107 /**
108 * Replies the map with the current tags in the tag editor model.
109 * @param keepEmpty {@code true} to keep empty tags
110 * @return the map with the current tags in the tag editor model.
111 */
112 public Map<String, String> getTags(boolean keepEmpty) {
113 return pnlTagEditor.getModel().getTags(keepEmpty);
114 }
115
116 /**
117 * Replies the map with the default tags.
118 * @return the map with the default tags
119 */
120 public Map<String, String> getDefaultTags() {
121 Map<String, String> tags = new HashMap<>();
122 tags.putAll(defaultTags);
123 return tags;
124 }
125
126 /**
127 * Sets the map with the default tags.
128 * @param tags the map with the default tags
129 */
130 public void setDefaultTags(Map<String, String> tags) {
131 defaultTags.clear();
132 defaultTags.putAll(tags);
133 tableChanged(null);
134 }
135
136 public void startUserInput() {
137 pnlTagEditor.initAutoCompletion(Main.main.getEditLayer());
138 }
139
140 /* -------------------------------------------------------------------------- */
141 /* Interface TableChangeListener */
142 /* -------------------------------------------------------------------------- */
143 @Override
144 public void tableChanged(TableModelEvent e) {
145 changesetCommentModel.setComment(getTagEditorValue("comment"));
146 changesetSourceModel.setComment(getTagEditorValue("source"));
147 }
148
149 /**
150 * Observes the changeset comment model and keeps the tag editor in sync
151 * with the current changeset comment
152 *
153 */
154 class ChangesetCommentObserver implements Observer {
155
156 private final String key;
157
158 ChangesetCommentObserver(String key) {
159 this.key = key;
160 }
161
162 @Override
163 public void update(Observable o, Object arg) {
164 if (!(o instanceof ChangesetCommentModel)) return;
165 String newValue = (String) arg;
166 String oldValue = getTagEditorValue(key);
167 if (oldValue == null) {
168 oldValue = "";
169 }
170 if (!oldValue.equals(newValue)) {
171 setProperty(key, (String) arg);
172 }
173 }
174 }
175}
Note: See TracBrowser for help on using the repository browser.