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

Last change on this file since 6409 was 6409, checked in by simon04, 10 years ago

#fix 9348 - see #6381 - source= for changeset falsely reported as empty

  • Property svn:eol-style set to native
File size: 5.8 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);
25 /** the model for the changeset comment */
26 private final ChangesetCommentModel changesetCommentModel;
27 private final ChangesetCommentModel changesetSourceModel;
28 /** tags that applied to uploaded changesets by default*/
29 private final Map<String, String> defaultTags = new HashMap<String, String>();
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 thrown if {@code changesetCommentModel} is null
42 */
43 public TagSettingsPanel(ChangesetCommentModel changesetCommentModel, ChangesetCommentModel changesetSourceModel) throws IllegalArgumentException{
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 public void initFromChangeset(Changeset cs) {
83 Map<String,String> tags = getDefaultTags();
84 if (cs != null) {
85 tags.putAll(cs.getKeys());
86 }
87 if (tags.get("comment") == null) {
88 tags.put("comment", getTagEditorValue("comment"));
89 }
90 if (tags.get("source") == null) {
91 tags.put("source", getTagEditorValue("source"));
92 }
93 String agent = Version.getInstance().getAgentString(false);
94 String created_by = tags.get("created_by");
95 if (created_by == null || created_by.isEmpty()) {
96 tags.put("created_by", agent);
97 } else if (!created_by.contains(agent)) {
98 tags.put("created_by", created_by + ";" + agent);
99 }
100 pnlTagEditor.getModel().initFromTags(tags);
101 }
102
103 /**
104 * Replies the map with the current tags in the tag editor model.
105 *
106 * @return the map with the current tags in the tag editor model.
107 */
108 public Map<String,String> getTags(boolean keepEmpty) {
109 return pnlTagEditor.getModel().getTags(keepEmpty);
110 }
111
112 public Map<String,String> getDefaultTags() {
113 Map<String,String> tags = new HashMap<String, String>();
114 tags.putAll(defaultTags);
115 return tags;
116 }
117
118 public void setDefaultTags(Map<String, String> tags) {
119 defaultTags.clear();
120 defaultTags.putAll(tags);
121 }
122
123 public void startUserInput() {
124 pnlTagEditor.initAutoCompletion(Main.main.getEditLayer());
125 }
126
127 /* -------------------------------------------------------------------------- */
128 /* Interface TableChangeListener */
129 /* -------------------------------------------------------------------------- */
130 @Override
131 public void tableChanged(TableModelEvent e) {
132 changesetCommentModel.setComment(getTagEditorValue("comment"));
133 changesetSourceModel.setComment(getTagEditorValue("source"));
134 }
135
136 /**
137 * Observes the changeset comment model and keeps the tag editor in sync
138 * with the current changeset comment
139 *
140 */
141 class ChangesetCommentObserver implements Observer {
142
143 private final String key;
144
145 ChangesetCommentObserver(String key) {
146 this.key = key;
147 }
148
149 @Override
150 public void update(Observable o, Object arg) {
151 if (!(o instanceof ChangesetCommentModel)) return;
152 String newValue = (String)arg;
153 String oldValue = getTagEditorValue(key);
154 if (oldValue == null) {
155 oldValue = "";
156 }
157 if (!oldValue.equals(newValue)) {
158 setProperty(key, (String) arg);
159 }
160 }
161 }
162}
Note: See TracBrowser for help on using the repository browser.