001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.gui.io;
003
004 import java.awt.BorderLayout;
005 import java.util.HashMap;
006 import java.util.Map;
007 import java.util.Observable;
008 import java.util.Observer;
009
010 import javax.swing.JPanel;
011 import javax.swing.event.TableModelEvent;
012 import javax.swing.event.TableModelListener;
013
014 import org.openstreetmap.josm.Main;
015 import org.openstreetmap.josm.data.osm.Changeset;
016 import org.openstreetmap.josm.gui.tagging.TagEditorPanel;
017 import org.openstreetmap.josm.gui.tagging.TagModel;
018 import org.openstreetmap.josm.tools.CheckParameterUtil;
019
020 public class TagSettingsPanel extends JPanel implements TableModelListener {
021
022 /** checkbox for selecting whether an atomic upload is to be used */
023 private TagEditorPanel pnlTagEditor;
024 /** the model for the changeset comment */
025 private ChangesetCommentModel changesetCommentModel;
026 /** tags that applied to uploaded changesets by default*/
027 private Map<String, String> defaultTags = new HashMap<String, String>();
028
029 protected void build() {
030 setLayout(new BorderLayout());
031 add(pnlTagEditor = new TagEditorPanel(null), BorderLayout.CENTER);
032 }
033
034 /**
035 * Creates a new panel
036 *
037 * @param changesetCommentModel the changeset comment model. Must not be null.
038 * @throws IllegalArgumentException thrown if {@code changesetCommentModel} is null
039 */
040 public TagSettingsPanel(ChangesetCommentModel changesetCommentModel) throws IllegalArgumentException{
041 CheckParameterUtil.ensureParameterNotNull(changesetCommentModel, "changesetCommentModel");
042 this.changesetCommentModel = changesetCommentModel;
043 this.changesetCommentModel.addObserver(new ChangesetCommentObserver());
044 build();
045 pnlTagEditor.getModel().addTableModelListener(this);
046 }
047
048 /**
049 * Replies the default value for "created_by"
050 *
051 * @return the default value for "created_by"
052 */
053 public static String getDefaultCreatedBy() {
054 Object ua = System.getProperties().get("http.agent");
055 return(ua == null) ? "JOSM" : ua.toString();
056 }
057
058 protected void setUploadComment(String comment) {
059 if (comment == null) {
060 comment = "";
061 }
062 comment = comment.trim();
063 String commentInTag = getUploadComment();
064 if (comment.equals(commentInTag))
065 return;
066
067 if (comment.equals("")) {
068 pnlTagEditor.getModel().delete("comment");
069 return;
070 }
071 TagModel tag = pnlTagEditor.getModel().get("comment");
072 if (tag == null) {
073 tag = new TagModel("comment", comment);
074 pnlTagEditor.getModel().add(tag);
075 } else {
076 pnlTagEditor.getModel().updateTagValue(tag, comment);
077 }
078 }
079
080 protected String getUploadComment() {
081 TagModel tag = pnlTagEditor.getModel().get("comment");
082 if (tag == null) return null;
083 return tag.getValue();
084 }
085
086 public void initFromChangeset(Changeset cs) {
087 String currentComment = getUploadComment();
088 Map<String,String> tags = getDefaultTags();
089 if (cs != null) {
090 tags.putAll(cs.getKeys());
091 }
092 if (tags.get("comment") == null) {
093 tags.put("comment", currentComment);
094 }
095 String created_by = tags.get("created_by");
096 if (created_by == null || "".equals(created_by)) {
097 tags.put("created_by", getDefaultCreatedBy());
098 } else if (!created_by.contains(getDefaultCreatedBy())) {
099 tags.put("created_by", created_by + ";" + getDefaultCreatedBy());
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() {
110 return pnlTagEditor.getModel().getTags();
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 public void tableChanged(TableModelEvent e) {
132 String uploadComment = getUploadComment();
133 changesetCommentModel.setComment(uploadComment);
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 public void update(Observable o, Object arg) {
143 if (!(o instanceof ChangesetCommentModel)) return;
144 String newValue = (String)arg;
145 String oldValue = getUploadComment();
146 if (oldValue == null) {
147 oldValue = "";
148 }
149 if (!oldValue.equals(newValue)) {
150 setUploadComment((String)arg);
151 }
152 }
153 }
154 }