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