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

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

see #6381 - Ask for source tag in changeset/upload

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