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