source: josm/trunk/src/org/openstreetmap/josm/gui/io/BasicUploadSettingsPanel.java@ 17238

Last change on this file since 17238 was 17238, checked in by Don-vip, 3 years ago

see #17634 - enforce upload policy again (broken after #19381 changes)

  • Property svn:eol-style set to native
File size: 17.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.awt.event.ActionEvent;
8import java.awt.event.ActionListener;
9import java.awt.event.FocusAdapter;
10import java.awt.event.FocusEvent;
11import java.awt.event.ItemEvent;
12import java.awt.event.KeyAdapter;
13import java.awt.event.KeyEvent;
14import java.util.Arrays;
15import java.util.LinkedList;
16import java.util.List;
17import java.util.Objects;
18import java.util.concurrent.TimeUnit;
19
20import javax.swing.BorderFactory;
21import javax.swing.BoxLayout;
22import javax.swing.JCheckBox;
23import javax.swing.JEditorPane;
24import javax.swing.JLabel;
25import javax.swing.JPanel;
26import javax.swing.JTextField;
27import javax.swing.event.AncestorEvent;
28import javax.swing.event.AncestorListener;
29import javax.swing.event.ChangeEvent;
30import javax.swing.event.ChangeListener;
31import javax.swing.event.HyperlinkEvent;
32
33import org.openstreetmap.josm.data.osm.Changeset;
34import org.openstreetmap.josm.data.osm.OsmPrimitive;
35import org.openstreetmap.josm.gui.MainApplication;
36import org.openstreetmap.josm.gui.io.UploadTextComponentValidator.UploadAreaValidator;
37import org.openstreetmap.josm.gui.io.UploadTextComponentValidator.UploadCommentValidator;
38import org.openstreetmap.josm.gui.io.UploadTextComponentValidator.UploadSourceValidator;
39import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
40import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
41import org.openstreetmap.josm.spi.preferences.Config;
42import org.openstreetmap.josm.tools.GBC;
43import org.openstreetmap.josm.tools.Utils;
44
45/**
46 * BasicUploadSettingsPanel allows to enter the basic parameters required for uploading data.
47 * @since 2599
48 */
49public 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<>());
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<>());
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 /**
200 * Returns the list of {@link UploadTextComponentValidator} defined by this panel.
201 * @return the list of {@code UploadTextComponentValidator} defined by this panel.
202 * @since 17238
203 */
204 protected List<UploadTextComponentValidator> getUploadTextValidators() {
205 return Arrays.asList(areaValidator, uploadCommentValidator, uploadSourceValidator);
206 }
207
208 protected void build() {
209 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
210 setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
211 add(buildUploadCommentPanel());
212 add(buildUploadSourcePanel());
213 add(pnlUploadParameterSummary);
214 if (Config.getPref().getBoolean("upload.show.review.request", true)) {
215 JPanel pnl = new JPanel(new GridBagLayout());
216 pnl.add(cbRequestReview, GBC.eol().fill(GBC.HORIZONTAL));
217 add(pnl);
218 cbRequestReview.addItemListener(e -> changesetReviewModel.setReviewRequested(e.getStateChange() == ItemEvent.SELECTED));
219 }
220 JPanel pnl = new JPanel(new GridBagLayout());
221 pnl.add(areaValidatorFeedback, GBC.eol().fill(GBC.HORIZONTAL));
222 add(pnl);
223 }
224
225 /**
226 * Creates the panel
227 *
228 * @param changesetCommentModel the model for the changeset comment. Must not be null
229 * @param changesetSourceModel the model for the changeset source. Must not be null.
230 * @param changesetReviewModel the model for the changeset review. Must not be null.
231 * @throws NullPointerException if a model is null
232 * @since 12719 (signature)
233 */
234 public BasicUploadSettingsPanel(ChangesetCommentModel changesetCommentModel, ChangesetCommentModel changesetSourceModel,
235 ChangesetReviewModel changesetReviewModel) {
236 this.changesetCommentModel = Objects.requireNonNull(changesetCommentModel, "changesetCommentModel");
237 this.changesetSourceModel = Objects.requireNonNull(changesetSourceModel, "changesetSourceModel");
238 this.changesetReviewModel = Objects.requireNonNull(changesetReviewModel, "changesetReviewModel");
239 changesetCommentModel.addChangeListener(new ChangesetCommentChangeListener(hcbUploadComment));
240 changesetSourceModel.addChangeListener(new ChangesetCommentChangeListener(hcbUploadSource));
241 changesetReviewModel.addChangeListener(new ChangesetReviewChangeListener());
242 build();
243 }
244
245 void setUploadTagDownFocusTraversalHandlers(final ActionListener handler) {
246 setHistoryComboBoxDownFocusTraversalHandler(handler, hcbUploadComment);
247 setHistoryComboBoxDownFocusTraversalHandler(handler, hcbUploadSource);
248 }
249
250 private static void setHistoryComboBoxDownFocusTraversalHandler(ActionListener handler, HistoryComboBox hcb) {
251 hcb.getEditor().addActionListener(handler);
252 hcb.getEditorComponent().addKeyListener(new HistoryComboBoxKeyAdapter(hcb, handler));
253 }
254
255 /**
256 * Remembers the user input in the preference settings
257 */
258 public void rememberUserInput() {
259 // store the history of comments
260 if (getHistoryMaxAgeKey() > 0) {
261 hcbUploadComment.addCurrentItemToHistory();
262 Config.getPref().putList(HISTORY_KEY, hcbUploadComment.getHistory());
263 Config.getPref().putLong(HISTORY_LAST_USED_KEY, TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
264 }
265 // store the history of sources
266 hcbUploadSource.addCurrentItemToHistory();
267 Config.getPref().putList(SOURCE_HISTORY_KEY, hcbUploadSource.getHistory());
268
269 // store current value of obtaining source automatically
270 Config.getPref().putBoolean("upload.source.obtainautomatically", obtainSourceAutomatically.isSelected());
271 }
272
273 /**
274 * Initializes the panel for user input
275 */
276 public void startUserInput() {
277 hcbUploadComment.requestFocusInWindow();
278 hcbUploadComment.getEditorComponent().requestFocusInWindow();
279 uploadCommentValidator.validate();
280 uploadSourceValidator.validate();
281 }
282
283 /**
284 * Initializes editing of upload comment.
285 */
286 public void initEditingOfUploadComment() {
287 hcbUploadComment.getEditor().selectAll();
288 hcbUploadComment.requestFocusInWindow();
289 }
290
291 /**
292 * Initializes editing of upload source.
293 */
294 public void initEditingOfUploadSource() {
295 hcbUploadSource.getEditor().selectAll();
296 hcbUploadSource.requestFocusInWindow();
297 }
298
299 void setUploadedPrimitives(List<OsmPrimitive> primitives) {
300 areaValidator.computeArea(primitives);
301 }
302
303 /**
304 * Returns the panel that displays a summary of data the user is about to upload.
305 * @return the upload parameter summary panel
306 */
307 public UploadParameterSummaryPanel getUploadParameterSummaryPanel() {
308 return pnlUploadParameterSummary;
309 }
310
311 /**
312 * Forces update of comment/source model if matching text field is focused.
313 * @since 14977
314 */
315 public void forceUpdateActiveField() {
316 updateModelIfFocused(hcbUploadComment, changesetCommentModel);
317 updateModelIfFocused(hcbUploadSource, changesetSourceModel);
318 }
319
320 private static void updateModelIfFocused(HistoryComboBox hcb, ChangesetCommentModel changesetModel) {
321 if (hcb.getEditorComponent().hasFocus()) {
322 changesetModel.setComment(hcb.getText());
323 }
324 }
325
326 static long getHistoryMaxAgeKey() {
327 return Config.getPref().getLong(HISTORY_MAX_AGE_KEY, TimeUnit.HOURS.toSeconds(4));
328 }
329
330 static long getHistoryLastUsedKey() {
331 return Config.getPref().getLong(BasicUploadSettingsPanel.HISTORY_LAST_USED_KEY, 0);
332 }
333
334 static final class HistoryComboBoxKeyAdapter extends KeyAdapter {
335 private final HistoryComboBox hcb;
336 private final ActionListener handler;
337
338 HistoryComboBoxKeyAdapter(HistoryComboBox hcb, ActionListener handler) {
339 this.hcb = hcb;
340 this.handler = handler;
341 }
342
343 @Override
344 public void keyTyped(KeyEvent e) {
345 if (e.getKeyCode() == KeyEvent.VK_TAB) {
346 handler.actionPerformed(new ActionEvent(hcb, 0, "focusDown"));
347 }
348 }
349 }
350
351 /**
352 * Updates the changeset comment model upon changes in the input field.
353 */
354 static class CommentModelListener extends FocusAdapter implements ActionListener {
355
356 private final HistoryComboBox source;
357 private final ChangesetCommentModel destination;
358
359 CommentModelListener(HistoryComboBox source, ChangesetCommentModel destination) {
360 this.source = source;
361 this.destination = destination;
362 }
363
364 @Override
365 public void actionPerformed(ActionEvent e) {
366 destination.setComment(source.getText());
367 }
368
369 @Override
370 public void focusLost(FocusEvent e) {
371 destination.setComment(source.getText());
372 }
373 }
374
375 /**
376 * Observes the changeset comment model and keeps the comment input field
377 * in sync with the current changeset comment
378 */
379 static class ChangesetCommentChangeListener implements ChangeListener {
380
381 private final HistoryComboBox destination;
382
383 ChangesetCommentChangeListener(HistoryComboBox destination) {
384 this.destination = destination;
385 }
386
387 @Override
388 public void stateChanged(ChangeEvent e) {
389 if (!(e.getSource() instanceof ChangesetCommentModel)) return;
390 String newComment = ((ChangesetCommentModel) e.getSource()).getComment();
391 if (!destination.getText().equals(newComment)) {
392 destination.setText(newComment);
393 }
394 }
395 }
396
397 /**
398 * Observes the changeset review model and keeps the review checkbox
399 * in sync with the current changeset review request
400 */
401 class ChangesetReviewChangeListener implements ChangeListener {
402 @Override
403 public void stateChanged(ChangeEvent e) {
404 if (!(e.getSource() instanceof ChangesetReviewModel)) return;
405 boolean newState = ((ChangesetReviewModel) e.getSource()).isReviewRequested();
406 if (cbRequestReview.isSelected() != newState) {
407 cbRequestReview.setSelected(newState);
408 }
409 }
410 }
411}
Note: See TracBrowser for help on using the repository browser.