1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import java.awt.BorderLayout;
|
---|
5 | import java.beans.PropertyChangeEvent;
|
---|
6 | import java.beans.PropertyChangeListener;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import javax.swing.JPanel;
|
---|
10 | import javax.swing.event.TableModelEvent;
|
---|
11 | import javax.swing.event.TableModelListener;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
15 | import org.openstreetmap.josm.gui.tagging.TagEditorPanel;
|
---|
16 | import org.openstreetmap.josm.gui.tagging.TagModel;
|
---|
17 |
|
---|
18 | public class TagSettingsPanel extends JPanel implements PropertyChangeListener, TableModelListener {
|
---|
19 | static public final String UPLOAD_COMMENT_PROP = TagSettingsPanel.class.getName() + ".uploadComment";
|
---|
20 |
|
---|
21 | /** checkbox for selecting whether an atomic upload is to be used */
|
---|
22 | private TagEditorPanel pnlTagEditor;
|
---|
23 |
|
---|
24 | protected void build() {
|
---|
25 | setLayout(new BorderLayout());
|
---|
26 | add(pnlTagEditor = new TagEditorPanel(), BorderLayout.CENTER);
|
---|
27 | }
|
---|
28 |
|
---|
29 | public TagSettingsPanel() {
|
---|
30 | build();
|
---|
31 | pnlTagEditor.getModel().addTableModelListener(this);
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Replies the default value for "created_by"
|
---|
36 | *
|
---|
37 | * @return the default value for "created_by"
|
---|
38 | */
|
---|
39 | protected String getDefaultCreatedBy() {
|
---|
40 | Object ua = System.getProperties().get("http.agent");
|
---|
41 | return(ua == null) ? "JOSM" : ua.toString();
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void setUploadComment(String comment) {
|
---|
45 | if (comment == null) {
|
---|
46 | comment = "";
|
---|
47 | }
|
---|
48 | comment = comment.trim();
|
---|
49 | String commentInTag = getUploadComment();
|
---|
50 | if (comment.equals(commentInTag))
|
---|
51 | return;
|
---|
52 |
|
---|
53 | if (comment.equals("")) {
|
---|
54 | pnlTagEditor.getModel().delete("comment");
|
---|
55 | return;
|
---|
56 | }
|
---|
57 | TagModel tag = pnlTagEditor.getModel().get("comment");
|
---|
58 | if (tag == null) {
|
---|
59 | tag = new TagModel("comment", comment);
|
---|
60 | pnlTagEditor.getModel().add(tag);
|
---|
61 | } else {
|
---|
62 | pnlTagEditor.getModel().updateTagValue(tag, comment);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected String getUploadComment() {
|
---|
67 | TagModel tag = pnlTagEditor.getModel().get("comment");
|
---|
68 | if (tag == null) return null;
|
---|
69 | return tag.getValue();
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected void initNewChangeset() {
|
---|
73 | String currentComment = getUploadComment();
|
---|
74 | pnlTagEditor.getModel().clear();
|
---|
75 | if (currentComment != null) {
|
---|
76 | pnlTagEditor.getModel().add("comment", currentComment);
|
---|
77 | }
|
---|
78 | pnlTagEditor.getModel().add("created_by", getDefaultCreatedBy());
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected void initFromExistingChangeset(Changeset cs) {
|
---|
82 | String currentComment = getUploadComment();
|
---|
83 | Map<String,String> tags = cs.getKeys();
|
---|
84 | if (tags.get("comment") == null) {
|
---|
85 | tags.put("comment", currentComment);
|
---|
86 | }
|
---|
87 | tags.put("created_by", getDefaultCreatedBy());
|
---|
88 | pnlTagEditor.getModel().initFromTags(tags);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void initFromChangeset(Changeset cs) {
|
---|
92 | if (cs == null) {
|
---|
93 | initNewChangeset();
|
---|
94 | } else {
|
---|
95 | initFromExistingChangeset(cs);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Replies the map with the current tags in the tag editor model.
|
---|
101 | *
|
---|
102 | * @return the map with the current tags in the tag editor model.
|
---|
103 | */
|
---|
104 | public Map<String,String> getTags() {
|
---|
105 | return pnlTagEditor.getModel().getTags();
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void startUserInput() {
|
---|
109 | pnlTagEditor.initAutoCompletion(Main.main.getEditLayer());
|
---|
110 | }
|
---|
111 |
|
---|
112 | /* -------------------------------------------------------------------------- */
|
---|
113 | /* Interface PropertyChangeListener */
|
---|
114 | /* -------------------------------------------------------------------------- */
|
---|
115 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
116 | if (evt.getPropertyName().equals(ChangesetManagementPanel.SELECTED_CHANGESET_PROP)) {
|
---|
117 | Changeset cs = (Changeset)evt.getNewValue();
|
---|
118 | initFromChangeset(cs);
|
---|
119 | } else if (evt.getPropertyName().equals(BasicUploadSettingsPanel.UPLOAD_COMMENT_PROP)) {
|
---|
120 | String comment = (String)evt.getNewValue();
|
---|
121 | setUploadComment(comment);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* -------------------------------------------------------------------------- */
|
---|
126 | /* Interface TableChangeListener */
|
---|
127 | /* -------------------------------------------------------------------------- */
|
---|
128 | public void tableChanged(TableModelEvent e) {
|
---|
129 | String uploadComment = getUploadComment();
|
---|
130 | firePropertyChange(UPLOAD_COMMENT_PROP, null, uploadComment);
|
---|
131 | }
|
---|
132 | }
|
---|