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

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

fix #12983 - replace calls to Main.main.get[Active|Edit]Layer() by Main.getLayerManager().get[Active|Edit]Layer() - gsoc-core

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import java.awt.BorderLayout;
5import java.util.Map;
6
7import javax.swing.JPanel;
8import javax.swing.event.ChangeEvent;
9import javax.swing.event.ChangeListener;
10import javax.swing.event.TableModelEvent;
11import javax.swing.event.TableModelListener;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.osm.Changeset;
15import org.openstreetmap.josm.gui.tagging.TagEditorPanel;
16import org.openstreetmap.josm.gui.tagging.TagModel;
17import org.openstreetmap.josm.tools.CheckParameterUtil;
18
19/**
20 * Tag settings panel of upload dialog.
21 * @since 2599
22 */
23public class TagSettingsPanel extends JPanel implements TableModelListener {
24
25 /** checkbox for selecting whether an atomic upload is to be used */
26 private final TagEditorPanel pnlTagEditor = new TagEditorPanel(null, null, Changeset.MAX_CHANGESET_TAG_LENGTH);
27 /** the model for the changeset comment */
28 private final transient ChangesetCommentModel changesetCommentModel;
29 private final transient ChangesetCommentModel changesetSourceModel;
30
31 /**
32 * Creates a new panel
33 *
34 * @param changesetCommentModel the changeset comment model. Must not be null.
35 * @param changesetSourceModel the changeset source model. Must not be null.
36 * @throws IllegalArgumentException if {@code changesetCommentModel} is null
37 */
38 public TagSettingsPanel(ChangesetCommentModel changesetCommentModel, ChangesetCommentModel changesetSourceModel) {
39 CheckParameterUtil.ensureParameterNotNull(changesetCommentModel, "changesetCommentModel");
40 CheckParameterUtil.ensureParameterNotNull(changesetSourceModel, "changesetSourceModel");
41 this.changesetCommentModel = changesetCommentModel;
42 this.changesetSourceModel = changesetSourceModel;
43 this.changesetCommentModel.addChangeListener(new ChangesetCommentChangeListener("comment"));
44 this.changesetSourceModel.addChangeListener(new ChangesetCommentChangeListener("source"));
45 build();
46 pnlTagEditor.getModel().addTableModelListener(this);
47 }
48
49 protected void build() {
50 setLayout(new BorderLayout());
51 add(pnlTagEditor, BorderLayout.CENTER);
52 }
53
54 protected void setProperty(String key, String value) {
55 String val = (value == null ? "" : value).trim();
56 String commentInTag = getTagEditorValue(key);
57 if (val.equals(commentInTag))
58 return;
59
60 if (val.isEmpty()) {
61 pnlTagEditor.getModel().delete(key);
62 return;
63 }
64 TagModel tag = pnlTagEditor.getModel().get(key);
65 if (tag == null) {
66 tag = new TagModel(key, val);
67 pnlTagEditor.getModel().add(tag);
68 } else {
69 pnlTagEditor.getModel().updateTagValue(tag, val);
70 }
71 }
72
73 protected String getTagEditorValue(String key) {
74 TagModel tag = pnlTagEditor.getModel().get(key);
75 return tag == null ? null : tag.getValue();
76 }
77
78 /**
79 * Initialize panel from the given tags.
80 * @param tags the tags used to initialize the panel
81 */
82 public void initFromTags(Map<String, String> tags) {
83 pnlTagEditor.getModel().initFromTags(tags);
84 }
85
86 /**
87 * Replies the map with the current tags in the tag editor model.
88 * @param keepEmpty {@code true} to keep empty tags
89 * @return the map with the current tags in the tag editor model.
90 */
91 public Map<String, String> getTags(boolean keepEmpty) {
92 return pnlTagEditor.getModel().getTags(keepEmpty);
93 }
94
95 /**
96 * Initializes the panel for user input
97 */
98 public void startUserInput() {
99 pnlTagEditor.initAutoCompletion(Main.getLayerManager().getEditLayer());
100 }
101
102 /* -------------------------------------------------------------------------- */
103 /* Interface TableChangeListener */
104 /* -------------------------------------------------------------------------- */
105 @Override
106 public void tableChanged(TableModelEvent e) {
107 changesetCommentModel.setComment(getTagEditorValue("comment"));
108 changesetSourceModel.setComment(getTagEditorValue("source"));
109 }
110
111 /**
112 * Observes the changeset comment model and keeps the tag editor in sync
113 * with the current changeset comment
114 */
115 class ChangesetCommentChangeListener implements ChangeListener {
116
117 private final String key;
118
119 ChangesetCommentChangeListener(String key) {
120 this.key = key;
121 }
122
123 @Override
124 public void stateChanged(ChangeEvent e) {
125 if (e.getSource() instanceof ChangesetCommentModel) {
126 String newValue = ((ChangesetCommentModel) e.getSource()).getComment();
127 String oldValue = getTagEditorValue(key);
128 if (oldValue == null) {
129 oldValue = "";
130 }
131 if (!oldValue.equals(newValue)) {
132 setProperty(key, newValue);
133 }
134 }
135 }
136 }
137}
Note: See TracBrowser for help on using the repository browser.