1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.awt.event.FocusAdapter;
|
---|
10 | import java.awt.event.FocusEvent;
|
---|
11 | import java.awt.event.ItemEvent;
|
---|
12 | import java.awt.event.KeyAdapter;
|
---|
13 | import java.awt.event.KeyEvent;
|
---|
14 | import java.util.Arrays;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Objects;
|
---|
18 | import java.util.concurrent.TimeUnit;
|
---|
19 |
|
---|
20 | import javax.swing.BorderFactory;
|
---|
21 | import javax.swing.JCheckBox;
|
---|
22 | import javax.swing.JEditorPane;
|
---|
23 | import javax.swing.JLabel;
|
---|
24 | import javax.swing.JPanel;
|
---|
25 | import javax.swing.event.AncestorEvent;
|
---|
26 | import javax.swing.event.AncestorListener;
|
---|
27 | import javax.swing.event.ChangeEvent;
|
---|
28 | import javax.swing.event.ChangeListener;
|
---|
29 | import javax.swing.event.HyperlinkEvent;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
32 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
33 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
34 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
35 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
36 | import org.openstreetmap.josm.tools.GBC;
|
---|
37 | import org.openstreetmap.josm.tools.Utils;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * BasicUploadSettingsPanel allows to enter the basic parameters required for uploading data.
|
---|
41 | * @since 2599
|
---|
42 | */
|
---|
43 | public class BasicUploadSettingsPanel extends JPanel {
|
---|
44 | /**
|
---|
45 | * Preference name for history collection
|
---|
46 | */
|
---|
47 | public static final String HISTORY_KEY = "upload.comment.history";
|
---|
48 | /**
|
---|
49 | * Preference name for last used upload comment
|
---|
50 | */
|
---|
51 | public static final String HISTORY_LAST_USED_KEY = "upload.comment.last-used";
|
---|
52 | /**
|
---|
53 | * Preference name for the max age search comments may have
|
---|
54 | */
|
---|
55 | public static final String HISTORY_MAX_AGE_KEY = "upload.comment.max-age";
|
---|
56 | /**
|
---|
57 | * Preference name for the history of source values
|
---|
58 | */
|
---|
59 | public static final String SOURCE_HISTORY_KEY = "upload.source.history";
|
---|
60 |
|
---|
61 | /** the history combo box for the upload comment */
|
---|
62 | private final HistoryComboBox hcbUploadComment = new HistoryComboBox();
|
---|
63 | private final HistoryComboBox hcbUploadSource = new HistoryComboBox();
|
---|
64 | private final transient JCheckBox obtainSourceAutomatically = new JCheckBox(
|
---|
65 | tr("Automatically obtain source from current layers"));
|
---|
66 | /** the panel with a summary of the upload parameters */
|
---|
67 | private final UploadParameterSummaryPanel pnlUploadParameterSummary = new UploadParameterSummaryPanel();
|
---|
68 | /** the checkbox to request feedback from other users */
|
---|
69 | private final JCheckBox cbRequestReview = new JCheckBox(tr("I would like someone to review my edits."));
|
---|
70 | /** the changeset comment model */
|
---|
71 | private final transient ChangesetCommentModel changesetCommentModel;
|
---|
72 | private final transient ChangesetCommentModel changesetSourceModel;
|
---|
73 | private final transient ChangesetReviewModel changesetReviewModel;
|
---|
74 |
|
---|
75 | protected JPanel buildUploadCommentPanel() {
|
---|
76 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
77 |
|
---|
78 | JEditorPane commentLabel = new JMultilineLabel("<html><b>" + tr("Provide a brief comment for the changes you are uploading:"));
|
---|
79 | pnl.add(commentLabel, GBC.eol().insets(0, 5, 10, 3).fill(GBC.HORIZONTAL));
|
---|
80 | hcbUploadComment.setToolTipText(tr("Enter an upload comment"));
|
---|
81 | hcbUploadComment.setMaxTextLength(Changeset.MAX_CHANGESET_TAG_LENGTH);
|
---|
82 | populateHistoryComboBox(hcbUploadComment, HISTORY_KEY, new LinkedList<String>());
|
---|
83 | CommentModelListener commentModelListener = new CommentModelListener(hcbUploadComment, changesetCommentModel);
|
---|
84 | hcbUploadComment.getEditor().addActionListener(commentModelListener);
|
---|
85 | hcbUploadComment.getEditorComponent().addFocusListener(commentModelListener);
|
---|
86 | pnl.add(hcbUploadComment, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
87 |
|
---|
88 | JEditorPane sourceLabel = new JMultilineLabel("<html><b>" + tr("Specify the data source for the changes") + ":</b>");
|
---|
89 | pnl.add(sourceLabel, GBC.eol().insets(0, 8, 10, 0).fill(GBC.HORIZONTAL));
|
---|
90 | JEditorPane obtainSourceOnce = new JMultilineLabel(
|
---|
91 | "<html>(<a href=\"urn:changeset-source\">" + tr("just once") + "</a>)</html>");
|
---|
92 | obtainSourceOnce.addHyperlinkListener(e -> {
|
---|
93 | if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
|
---|
94 | automaticallyAddSource();
|
---|
95 | }
|
---|
96 | });
|
---|
97 | obtainSourceAutomatically.setSelected(Config.getPref().getBoolean("upload.source.obtainautomatically", false));
|
---|
98 | obtainSourceAutomatically.addActionListener(e -> {
|
---|
99 | if (obtainSourceAutomatically.isSelected())
|
---|
100 | automaticallyAddSource();
|
---|
101 |
|
---|
102 | obtainSourceOnce.setVisible(!obtainSourceAutomatically.isSelected());
|
---|
103 | });
|
---|
104 | JPanel obtainSource = new JPanel(new GridBagLayout());
|
---|
105 | obtainSource.add(obtainSourceAutomatically, GBC.std().anchor(GBC.WEST));
|
---|
106 | obtainSource.add(obtainSourceOnce, GBC.std().anchor(GBC.WEST));
|
---|
107 | obtainSource.add(new JLabel(), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
108 | pnl.add(obtainSource, GBC.eol().insets(0, 0, 10, 3).fill(GBC.HORIZONTAL));
|
---|
109 |
|
---|
110 | hcbUploadSource.setToolTipText(tr("Enter a source"));
|
---|
111 | hcbUploadSource.setMaxTextLength(Changeset.MAX_CHANGESET_TAG_LENGTH);
|
---|
112 | populateHistoryComboBox(hcbUploadSource, SOURCE_HISTORY_KEY, getDefaultSources());
|
---|
113 | CommentModelListener sourceModelListener = new CommentModelListener(hcbUploadSource, changesetSourceModel);
|
---|
114 | hcbUploadSource.getEditor().addActionListener(sourceModelListener);
|
---|
115 | hcbUploadSource.getEditorComponent().addFocusListener(sourceModelListener);
|
---|
116 | pnl.add(hcbUploadSource, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
117 | if (obtainSourceAutomatically.isSelected()) {
|
---|
118 | automaticallyAddSource();
|
---|
119 | }
|
---|
120 | pnl.addAncestorListener(new AncestorListener() {
|
---|
121 | @Override
|
---|
122 | public void ancestorAdded(AncestorEvent event) {
|
---|
123 | if (obtainSourceAutomatically.isSelected())
|
---|
124 | automaticallyAddSource();
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override
|
---|
128 | public void ancestorRemoved(AncestorEvent event) {
|
---|
129 | // Do nothing
|
---|
130 | }
|
---|
131 |
|
---|
132 | @Override
|
---|
133 | public void ancestorMoved(AncestorEvent event) {
|
---|
134 | // Do nothing
|
---|
135 | }
|
---|
136 | });
|
---|
137 | return pnl;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Add the source tags
|
---|
142 | */
|
---|
143 | protected void automaticallyAddSource() {
|
---|
144 | final String source = MainApplication.getMap().mapView.getLayerInformationForSourceTag();
|
---|
145 | hcbUploadSource.setText(Utils.shortenString(source, Changeset.MAX_CHANGESET_TAG_LENGTH));
|
---|
146 | changesetSourceModel.setComment(hcbUploadSource.getText()); // Fix #9965
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Refreshes contents of upload history combo boxes from preferences.
|
---|
151 | */
|
---|
152 | protected void refreshHistoryComboBoxes() {
|
---|
153 | populateHistoryComboBox(hcbUploadComment, HISTORY_KEY, new LinkedList<String>());
|
---|
154 | populateHistoryComboBox(hcbUploadSource, SOURCE_HISTORY_KEY, getDefaultSources());
|
---|
155 | }
|
---|
156 |
|
---|
157 | private static void populateHistoryComboBox(HistoryComboBox hcb, String historyKey, List<String> defaultValues) {
|
---|
158 | hcb.setPossibleItemsTopDown(Config.getPref().getList(historyKey, defaultValues));
|
---|
159 | hcb.discardAllUndoableEdits();
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Discards undoable edits of upload history combo boxes.
|
---|
164 | */
|
---|
165 | protected void discardAllUndoableEdits() {
|
---|
166 | hcbUploadComment.discardAllUndoableEdits();
|
---|
167 | hcbUploadSource.discardAllUndoableEdits();
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Returns the default list of sources.
|
---|
172 | * @return the default list of sources
|
---|
173 | */
|
---|
174 | public static List<String> getDefaultSources() {
|
---|
175 | return Arrays.asList("knowledge", "survey", "Bing");
|
---|
176 | }
|
---|
177 |
|
---|
178 | protected void build() {
|
---|
179 | setLayout(new GridBagLayout());
|
---|
180 | setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
|
---|
181 | add(buildUploadCommentPanel(), GBC.eol().fill(GBC.BOTH));
|
---|
182 | add(pnlUploadParameterSummary, GBC.eol().fill(GBC.BOTH));
|
---|
183 | add(cbRequestReview, GBC.eol().fill(GBC.BOTH));
|
---|
184 | cbRequestReview.addItemListener(e -> changesetReviewModel.setReviewRequested(e.getStateChange() == ItemEvent.SELECTED));
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Creates the panel
|
---|
189 | *
|
---|
190 | * @param changesetCommentModel the model for the changeset comment. Must not be null
|
---|
191 | * @param changesetSourceModel the model for the changeset source. Must not be null.
|
---|
192 | * @param changesetReviewModel the model for the changeset review. Must not be null.
|
---|
193 | * @throws NullPointerException if a model is null
|
---|
194 | * @since 12719 (signature)
|
---|
195 | */
|
---|
196 | public BasicUploadSettingsPanel(ChangesetCommentModel changesetCommentModel, ChangesetCommentModel changesetSourceModel,
|
---|
197 | ChangesetReviewModel changesetReviewModel) {
|
---|
198 | this.changesetCommentModel = Objects.requireNonNull(changesetCommentModel, "changesetCommentModel");
|
---|
199 | this.changesetSourceModel = Objects.requireNonNull(changesetSourceModel, "changesetSourceModel");
|
---|
200 | this.changesetReviewModel = Objects.requireNonNull(changesetReviewModel, "changesetReviewModel");
|
---|
201 | changesetCommentModel.addChangeListener(new ChangesetCommentChangeListener(hcbUploadComment));
|
---|
202 | changesetSourceModel.addChangeListener(new ChangesetCommentChangeListener(hcbUploadSource));
|
---|
203 | changesetReviewModel.addChangeListener(new ChangesetReviewChangeListener());
|
---|
204 | build();
|
---|
205 | }
|
---|
206 |
|
---|
207 | void setUploadTagDownFocusTraversalHandlers(final ActionListener handler) {
|
---|
208 | setHistoryComboBoxDownFocusTraversalHandler(handler, hcbUploadComment);
|
---|
209 | setHistoryComboBoxDownFocusTraversalHandler(handler, hcbUploadSource);
|
---|
210 | }
|
---|
211 |
|
---|
212 | private static void setHistoryComboBoxDownFocusTraversalHandler(ActionListener handler, HistoryComboBox hcb) {
|
---|
213 | hcb.getEditor().addActionListener(handler);
|
---|
214 | hcb.getEditorComponent().addKeyListener(new HistoryComboBoxKeyAdapter(hcb, handler));
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Remembers the user input in the preference settings
|
---|
219 | */
|
---|
220 | public void rememberUserInput() {
|
---|
221 | // store the history of comments
|
---|
222 | if (getHistoryMaxAgeKey() > 0) {
|
---|
223 | hcbUploadComment.addCurrentItemToHistory();
|
---|
224 | Config.getPref().putList(HISTORY_KEY, hcbUploadComment.getHistory());
|
---|
225 | Config.getPref().putLong(HISTORY_LAST_USED_KEY, TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
|
---|
226 | }
|
---|
227 | // store the history of sources
|
---|
228 | hcbUploadSource.addCurrentItemToHistory();
|
---|
229 | Config.getPref().putList(SOURCE_HISTORY_KEY, hcbUploadSource.getHistory());
|
---|
230 |
|
---|
231 | // store current value of obtaining source automatically
|
---|
232 | Config.getPref().putBoolean("upload.source.obtainautomatically", obtainSourceAutomatically.isSelected());
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Initializes the panel for user input
|
---|
237 | */
|
---|
238 | public void startUserInput() {
|
---|
239 | hcbUploadComment.requestFocusInWindow();
|
---|
240 | hcbUploadComment.getEditorComponent().requestFocusInWindow();
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Initializes editing of upload comment.
|
---|
245 | */
|
---|
246 | public void initEditingOfUploadComment() {
|
---|
247 | hcbUploadComment.getEditor().selectAll();
|
---|
248 | hcbUploadComment.requestFocusInWindow();
|
---|
249 | }
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Initializes editing of upload source.
|
---|
253 | */
|
---|
254 | public void initEditingOfUploadSource() {
|
---|
255 | hcbUploadSource.getEditor().selectAll();
|
---|
256 | hcbUploadSource.requestFocusInWindow();
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * Returns the panel that displays a summary of data the user is about to upload.
|
---|
261 | * @return the upload parameter summary panel
|
---|
262 | */
|
---|
263 | public UploadParameterSummaryPanel getUploadParameterSummaryPanel() {
|
---|
264 | return pnlUploadParameterSummary;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Forces update of comment/source model if matching text field is focused.
|
---|
269 | * @since 14977
|
---|
270 | */
|
---|
271 | public void forceUpdateActiveField() {
|
---|
272 | updateModelIfFocused(hcbUploadComment, changesetCommentModel);
|
---|
273 | updateModelIfFocused(hcbUploadSource, changesetSourceModel);
|
---|
274 | }
|
---|
275 |
|
---|
276 | private static void updateModelIfFocused(HistoryComboBox hcb, ChangesetCommentModel changesetModel) {
|
---|
277 | if (hcb.getEditorComponent().hasFocus()) {
|
---|
278 | changesetModel.setComment(hcb.getText());
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | static long getHistoryMaxAgeKey() {
|
---|
283 | return Config.getPref().getLong(HISTORY_MAX_AGE_KEY, TimeUnit.HOURS.toSeconds(4));
|
---|
284 | }
|
---|
285 |
|
---|
286 | static long getHistoryLastUsedKey() {
|
---|
287 | return Config.getPref().getLong(BasicUploadSettingsPanel.HISTORY_LAST_USED_KEY, 0);
|
---|
288 | }
|
---|
289 |
|
---|
290 | static final class HistoryComboBoxKeyAdapter extends KeyAdapter {
|
---|
291 | private final HistoryComboBox hcb;
|
---|
292 | private final ActionListener handler;
|
---|
293 |
|
---|
294 | HistoryComboBoxKeyAdapter(HistoryComboBox hcb, ActionListener handler) {
|
---|
295 | this.hcb = hcb;
|
---|
296 | this.handler = handler;
|
---|
297 | }
|
---|
298 |
|
---|
299 | @Override
|
---|
300 | public void keyTyped(KeyEvent e) {
|
---|
301 | if (e.getKeyCode() == KeyEvent.VK_TAB) {
|
---|
302 | handler.actionPerformed(new ActionEvent(hcb, 0, "focusDown"));
|
---|
303 | }
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Updates the changeset comment model upon changes in the input field.
|
---|
309 | */
|
---|
310 | static class CommentModelListener extends FocusAdapter implements ActionListener {
|
---|
311 |
|
---|
312 | private final HistoryComboBox source;
|
---|
313 | private final ChangesetCommentModel destination;
|
---|
314 |
|
---|
315 | CommentModelListener(HistoryComboBox source, ChangesetCommentModel destination) {
|
---|
316 | this.source = source;
|
---|
317 | this.destination = destination;
|
---|
318 | }
|
---|
319 |
|
---|
320 | @Override
|
---|
321 | public void actionPerformed(ActionEvent e) {
|
---|
322 | destination.setComment(source.getText());
|
---|
323 | }
|
---|
324 |
|
---|
325 | @Override
|
---|
326 | public void focusLost(FocusEvent e) {
|
---|
327 | destination.setComment(source.getText());
|
---|
328 | }
|
---|
329 | }
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * Observes the changeset comment model and keeps the comment input field
|
---|
333 | * in sync with the current changeset comment
|
---|
334 | */
|
---|
335 | static class ChangesetCommentChangeListener implements ChangeListener {
|
---|
336 |
|
---|
337 | private final HistoryComboBox destination;
|
---|
338 |
|
---|
339 | ChangesetCommentChangeListener(HistoryComboBox destination) {
|
---|
340 | this.destination = destination;
|
---|
341 | }
|
---|
342 |
|
---|
343 | @Override
|
---|
344 | public void stateChanged(ChangeEvent e) {
|
---|
345 | if (!(e.getSource() instanceof ChangesetCommentModel)) return;
|
---|
346 | String newComment = ((ChangesetCommentModel) e.getSource()).getComment();
|
---|
347 | if (!destination.getText().equals(newComment)) {
|
---|
348 | destination.setText(newComment);
|
---|
349 | }
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Observes the changeset review model and keeps the review checkbox
|
---|
355 | * in sync with the current changeset review request
|
---|
356 | */
|
---|
357 | class ChangesetReviewChangeListener implements ChangeListener {
|
---|
358 | @Override
|
---|
359 | public void stateChanged(ChangeEvent e) {
|
---|
360 | if (!(e.getSource() instanceof ChangesetReviewModel)) return;
|
---|
361 | boolean newState = ((ChangesetReviewModel) e.getSource()).isReviewRequested();
|
---|
362 | if (cbRequestReview.isSelected() != newState) {
|
---|
363 | cbRequestReview.setSelected(newState);
|
---|
364 | }
|
---|
365 | }
|
---|
366 | }
|
---|
367 | }
|
---|