source: josm/trunk/src/org/openstreetmap/josm/gui/io/UploadDialog.java@ 9522

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

fix java warnings & sonar issues

  • Property svn:eol-style set to native
File size: 26.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.awt.BorderLayout;
9import java.awt.Component;
10import java.awt.Dimension;
11import java.awt.FlowLayout;
12import java.awt.GridBagLayout;
13import java.awt.event.ActionEvent;
14import java.awt.event.KeyEvent;
15import java.awt.event.WindowAdapter;
16import java.awt.event.WindowEvent;
17import java.beans.PropertyChangeEvent;
18import java.beans.PropertyChangeListener;
19import java.util.ArrayList;
20import java.util.Collection;
21import java.util.Collections;
22import java.util.HashMap;
23import java.util.Iterator;
24import java.util.List;
25import java.util.Map;
26import java.util.Map.Entry;
27
28import javax.swing.AbstractAction;
29import javax.swing.BorderFactory;
30import javax.swing.Icon;
31import javax.swing.JButton;
32import javax.swing.JComponent;
33import javax.swing.JOptionPane;
34import javax.swing.JPanel;
35import javax.swing.JTabbedPane;
36import javax.swing.KeyStroke;
37
38import org.openstreetmap.josm.Main;
39import org.openstreetmap.josm.data.APIDataSet;
40import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
41import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
42import org.openstreetmap.josm.data.Preferences.Setting;
43import org.openstreetmap.josm.data.Version;
44import org.openstreetmap.josm.data.osm.Changeset;
45import org.openstreetmap.josm.data.osm.DataSet;
46import org.openstreetmap.josm.data.osm.OsmPrimitive;
47import org.openstreetmap.josm.gui.ExtendedDialog;
48import org.openstreetmap.josm.gui.HelpAwareOptionPane;
49import org.openstreetmap.josm.gui.SideButton;
50import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
51import org.openstreetmap.josm.gui.help.HelpUtil;
52import org.openstreetmap.josm.io.OsmApi;
53import org.openstreetmap.josm.tools.GBC;
54import org.openstreetmap.josm.tools.ImageOverlay;
55import org.openstreetmap.josm.tools.ImageProvider;
56import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
57import org.openstreetmap.josm.tools.InputMapUtils;
58import org.openstreetmap.josm.tools.Utils;
59import org.openstreetmap.josm.tools.WindowGeometry;
60
61/**
62 * This is a dialog for entering upload options like the parameters for
63 * the upload changeset and the strategy for opening/closing a changeset.
64 * @since 2025
65 */
66public class UploadDialog extends AbstractUploadDialog implements PropertyChangeListener, PreferenceChangedListener {
67 /** the unique instance of the upload dialog */
68 private static UploadDialog uploadDialog;
69
70 /** list of custom components that can be added by plugins at JOSM startup */
71 private static final Collection<Component> customComponents = new ArrayList<>();
72
73 /** the "created_by" changeset OSM key */
74 private static final String CREATED_BY = "created_by";
75
76 /** the panel with the objects to upload */
77 private UploadedObjectsSummaryPanel pnlUploadedObjects;
78 /** the panel to select the changeset used */
79 private ChangesetManagementPanel pnlChangesetManagement;
80
81 private BasicUploadSettingsPanel pnlBasicUploadSettings;
82
83 private UploadStrategySelectionPanel pnlUploadStrategySelectionPanel;
84
85 /** checkbox for selecting whether an atomic upload is to be used */
86 private TagSettingsPanel pnlTagSettings;
87 /** the tabbed pane used below of the list of primitives */
88 private JTabbedPane tpConfigPanels;
89 /** the upload button */
90 private JButton btnUpload;
91
92 /** the changeset comment model keeping the state of the changeset comment */
93 private final transient ChangesetCommentModel changesetCommentModel = new ChangesetCommentModel();
94 private final transient ChangesetCommentModel changesetSourceModel = new ChangesetCommentModel();
95
96 private transient DataSet dataSet;
97
98 /**
99 * Constructs a new {@code UploadDialog}.
100 */
101 public UploadDialog() {
102 super(JOptionPane.getFrameForComponent(Main.parent), ModalityType.DOCUMENT_MODAL);
103 build();
104 }
105
106 /**
107 * Replies the unique instance of the upload dialog
108 *
109 * @return the unique instance of the upload dialog
110 */
111 public static synchronized UploadDialog getUploadDialog() {
112 if (uploadDialog == null) {
113 uploadDialog = new UploadDialog();
114 }
115 return uploadDialog;
116 }
117
118 /**
119 * builds the content panel for the upload dialog
120 *
121 * @return the content panel
122 */
123 protected JPanel buildContentPanel() {
124 JPanel pnl = new JPanel(new GridBagLayout());
125 pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
126
127 // the panel with the list of uploaded objects
128 pnlUploadedObjects = new UploadedObjectsSummaryPanel();
129 pnl.add(pnlUploadedObjects, GBC.eol().fill(GBC.BOTH));
130
131 // Custom components
132 for (Component c : customComponents) {
133 pnl.add(c, GBC.eol().fill(GBC.HORIZONTAL));
134 }
135
136 // a tabbed pane with configuration panels in the lower half
137 tpConfigPanels = new JTabbedPane() {
138 @Override
139 public Dimension getPreferredSize() {
140 // make sure the tabbed pane never grabs more space than necessary
141 return super.getMinimumSize();
142 }
143 };
144
145 pnlBasicUploadSettings = new BasicUploadSettingsPanel(changesetCommentModel, changesetSourceModel);
146 tpConfigPanels.add(pnlBasicUploadSettings);
147 tpConfigPanels.setTitleAt(0, tr("Settings"));
148 tpConfigPanels.setToolTipTextAt(0, tr("Decide how to upload the data and which changeset to use"));
149
150 pnlTagSettings = new TagSettingsPanel(changesetCommentModel, changesetSourceModel);
151 tpConfigPanels.add(pnlTagSettings);
152 tpConfigPanels.setTitleAt(1, tr("Tags of new changeset"));
153 tpConfigPanels.setToolTipTextAt(1, tr("Apply tags to the changeset data is uploaded to"));
154
155 pnlChangesetManagement = new ChangesetManagementPanel(changesetCommentModel);
156 tpConfigPanels.add(pnlChangesetManagement);
157 tpConfigPanels.setTitleAt(2, tr("Changesets"));
158 tpConfigPanels.setToolTipTextAt(2, tr("Manage open changesets and select a changeset to upload to"));
159
160 pnlUploadStrategySelectionPanel = new UploadStrategySelectionPanel();
161 tpConfigPanels.add(pnlUploadStrategySelectionPanel);
162 tpConfigPanels.setTitleAt(3, tr("Advanced"));
163 tpConfigPanels.setToolTipTextAt(3, tr("Configure advanced settings"));
164
165 pnl.add(tpConfigPanels, GBC.eol().fill(GBC.HORIZONTAL));
166 return pnl;
167 }
168
169 /**
170 * builds the panel with the OK and CANCEL buttons
171 *
172 * @return The panel with the OK and CANCEL buttons
173 */
174 protected JPanel buildActionPanel() {
175 JPanel pnl = new JPanel();
176 pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
177 pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
178
179 // -- upload button
180 btnUpload = new SideButton(new UploadAction());
181 pnl.add(btnUpload);
182 btnUpload.setFocusable(true);
183 InputMapUtils.enableEnter(btnUpload);
184
185 // -- cancel button
186 CancelAction cancelAction = new CancelAction();
187 pnl.add(new SideButton(cancelAction));
188 getRootPane().registerKeyboardAction(
189 cancelAction,
190 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
191 JComponent.WHEN_IN_FOCUSED_WINDOW
192 );
193 pnl.add(new SideButton(new ContextSensitiveHelpAction(ht("/Dialog/Upload"))));
194 HelpUtil.setHelpContext(getRootPane(), ht("/Dialog/Upload"));
195 return pnl;
196 }
197
198 /**
199 * builds the gui
200 */
201 protected void build() {
202 setTitle(tr("Upload to ''{0}''", OsmApi.getOsmApi().getBaseUrl()));
203 getContentPane().setLayout(new BorderLayout());
204 getContentPane().add(buildContentPanel(), BorderLayout.CENTER);
205 getContentPane().add(buildActionPanel(), BorderLayout.SOUTH);
206
207 addWindowListener(new WindowEventHandler());
208
209
210 // make sure the configuration panels listen to each other
211 // changes
212 //
213 pnlChangesetManagement.addPropertyChangeListener(this);
214 pnlChangesetManagement.addPropertyChangeListener(
215 pnlBasicUploadSettings.getUploadParameterSummaryPanel()
216 );
217 pnlChangesetManagement.addPropertyChangeListener(this);
218 pnlUploadedObjects.addPropertyChangeListener(
219 pnlBasicUploadSettings.getUploadParameterSummaryPanel()
220 );
221 pnlUploadedObjects.addPropertyChangeListener(pnlUploadStrategySelectionPanel);
222 pnlUploadStrategySelectionPanel.addPropertyChangeListener(
223 pnlBasicUploadSettings.getUploadParameterSummaryPanel()
224 );
225
226
227 // users can click on either of two links in the upload parameter
228 // summary handler. This installs the handler for these two events.
229 // We simply select the appropriate tab in the tabbed pane with the configuration dialogs.
230 //
231 pnlBasicUploadSettings.getUploadParameterSummaryPanel().setConfigurationParameterRequestListener(
232 new ConfigurationParameterRequestHandler() {
233 @Override
234 public void handleUploadStrategyConfigurationRequest() {
235 tpConfigPanels.setSelectedIndex(3);
236 }
237
238 @Override
239 public void handleChangesetConfigurationRequest() {
240 tpConfigPanels.setSelectedIndex(2);
241 }
242 }
243 );
244
245 pnlBasicUploadSettings.setUploadTagDownFocusTraversalHandlers(
246 new AbstractAction() {
247 @Override
248 public void actionPerformed(ActionEvent e) {
249 btnUpload.requestFocusInWindow();
250 }
251 }
252 );
253
254 setMinimumSize(new Dimension(300, 350));
255
256 Main.pref.addPreferenceChangeListener(this);
257 }
258
259 /**
260 * Sets the collection of primitives to upload
261 *
262 * @param toUpload the dataset with the objects to upload. If null, assumes the empty
263 * set of objects to upload
264 *
265 */
266 public void setUploadedPrimitives(APIDataSet toUpload) {
267 if (toUpload == null) {
268 List<OsmPrimitive> emptyList = Collections.emptyList();
269 pnlUploadedObjects.setUploadedPrimitives(emptyList, emptyList, emptyList);
270 return;
271 }
272 pnlUploadedObjects.setUploadedPrimitives(
273 toUpload.getPrimitivesToAdd(),
274 toUpload.getPrimitivesToUpdate(),
275 toUpload.getPrimitivesToDelete()
276 );
277 }
278
279 /**
280 * Sets the tags for this upload based on (later items overwrite earlier ones):
281 * <ul>
282 * <li>previous "source" and "comment" input</li>
283 * <li>the tags set in the dataset (see {@link DataSet#getChangeSetTags()})</li>
284 * <li>the tags from the selected open changeset</li>
285 * <li>the JOSM user agent (see {@link Version#getAgentString(boolean)})</li>
286 * </ul>
287 *
288 * @param dataSet to obtain the tags set in the dataset
289 */
290 public void setChangesetTags(DataSet dataSet) {
291 final Map<String, String> tags = new HashMap<>();
292
293 // obtain from previous input
294 tags.put("source", getLastChangesetSourceFromHistory());
295 tags.put("comment", getLastChangesetCommentFromHistory());
296
297 // obtain from dataset
298 if (dataSet != null) {
299 tags.putAll(dataSet.getChangeSetTags());
300 }
301 this.dataSet = dataSet;
302
303 // obtain from selected open changeset
304 if (pnlChangesetManagement.getSelectedChangeset() != null) {
305 tags.putAll(pnlChangesetManagement.getSelectedChangeset().getKeys());
306 }
307
308 // set/adapt created_by
309 final String agent = Version.getInstance().getAgentString(false);
310 final String createdBy = tags.get(CREATED_BY);
311 if (createdBy == null || createdBy.isEmpty()) {
312 tags.put(CREATED_BY, agent);
313 } else if (!createdBy.contains(agent)) {
314 tags.put(CREATED_BY, createdBy + ';' + agent);
315 }
316
317 // remove empty values
318 final Iterator<String> it = tags.keySet().iterator();
319 while (it.hasNext()) {
320 final String v = tags.get(it.next());
321 if (v == null || v.isEmpty()) {
322 it.remove();
323 }
324 }
325
326 pnlTagSettings.initFromTags(tags);
327 pnlTagSettings.tableChanged(null);
328 }
329
330 @Override
331 public void rememberUserInput() {
332 pnlBasicUploadSettings.rememberUserInput();
333 pnlUploadStrategySelectionPanel.rememberUserInput();
334 }
335
336 /**
337 * Initializes the panel for user input
338 */
339 public void startUserInput() {
340 tpConfigPanels.setSelectedIndex(0);
341 pnlBasicUploadSettings.startUserInput();
342 pnlTagSettings.startUserInput();
343 pnlUploadStrategySelectionPanel.initFromPreferences();
344 UploadParameterSummaryPanel pnl = pnlBasicUploadSettings.getUploadParameterSummaryPanel();
345 pnl.setUploadStrategySpecification(pnlUploadStrategySelectionPanel.getUploadStrategySpecification());
346 pnl.setCloseChangesetAfterNextUpload(pnlChangesetManagement.isCloseChangesetAfterUpload());
347 pnl.setNumObjects(pnlUploadedObjects.getNumObjectsToUpload());
348 }
349
350 /**
351 * Replies the current changeset
352 *
353 * @return the current changeset
354 */
355 public Changeset getChangeset() {
356 Changeset cs = pnlChangesetManagement.getSelectedChangeset();
357 if (cs == null) {
358 cs = new Changeset();
359 }
360 cs.setKeys(pnlTagSettings.getTags(false));
361 return cs;
362 }
363
364 /**
365 * Sets the changeset to be used in the next upload
366 *
367 * @param cs the changeset
368 */
369 public void setSelectedChangesetForNextUpload(Changeset cs) {
370 pnlChangesetManagement.setSelectedChangesetForNextUpload(cs);
371 }
372
373 /**
374 * @deprecated No longer supported, does nothing;
375 * @return empty map
376 */
377 @Deprecated
378 public Map<String, String> getDefaultChangesetTags() {
379 return pnlTagSettings.getDefaultTags();
380 }
381
382 /**
383 * @param tags ignored
384 * @deprecated No longer supported, does nothing; use {@link #setChangesetTags(DataSet)} instead!
385 */
386 @Deprecated
387 public void setDefaultChangesetTags(Map<String, String> tags) {
388 // Deprecated
389 }
390
391 /**
392 * Replies the {@link UploadStrategySpecification} the user entered in the dialog.
393 *
394 * @return the {@link UploadStrategySpecification} the user entered in the dialog.
395 */
396 public UploadStrategySpecification getUploadStrategySpecification() {
397 UploadStrategySpecification spec = pnlUploadStrategySelectionPanel.getUploadStrategySpecification();
398 spec.setCloseChangesetAfterUpload(pnlChangesetManagement.isCloseChangesetAfterUpload());
399 return spec;
400 }
401
402 /**
403 * Returns the current value for the upload comment
404 *
405 * @return the current value for the upload comment
406 */
407 protected String getUploadComment() {
408 return changesetCommentModel.getComment();
409 }
410
411 /**
412 * Returns the current value for the changeset source
413 *
414 * @return the current value for the changeset source
415 */
416 protected String getUploadSource() {
417 return changesetSourceModel.getComment();
418 }
419
420 @Override
421 public void setVisible(boolean visible) {
422 if (visible) {
423 new WindowGeometry(
424 getClass().getName() + ".geometry",
425 WindowGeometry.centerInWindow(
426 Main.parent,
427 new Dimension(400, 600)
428 )
429 ).applySafe(this);
430 startUserInput();
431 } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
432 new WindowGeometry(this).remember(getClass().getName() + ".geometry");
433 }
434 super.setVisible(visible);
435 }
436
437 /**
438 * Adds a custom component to this dialog.
439 * Custom components added at JOSM startup are displayed between the objects list and the properties tab pane.
440 * @param c The custom component to add. If {@code null}, this method does nothing.
441 * @return {@code true} if the collection of custom components changed as a result of the call
442 * @since 5842
443 */
444 public static boolean addCustomComponent(Component c) {
445 if (c != null) {
446 return customComponents.add(c);
447 }
448 return false;
449 }
450
451 /**
452 * Handles an upload
453 *
454 */
455 class UploadAction extends AbstractAction {
456 UploadAction() {
457 putValue(NAME, tr("Upload Changes"));
458 putValue(SMALL_ICON, ImageProvider.get("upload"));
459 putValue(SHORT_DESCRIPTION, tr("Upload the changed primitives"));
460 }
461
462 /**
463 * Displays a warning message indicating that the upload comment is empty/short.
464 * @return true if the user wants to revisit, false if they want to continue
465 */
466 protected boolean warnUploadComment() {
467 return warnUploadTag(
468 tr("Please revise upload comment"),
469 tr("Your upload comment is <i>empty</i>, or <i>very short</i>.<br /><br />" +
470 "This is technically allowed, but please consider that many users who are<br />" +
471 "watching changes in their area depend on meaningful changeset comments<br />" +
472 "to understand what is going on!<br /><br />" +
473 "If you spend a minute now to explain your change, you will make life<br />" +
474 "easier for many other mappers."),
475 "upload_comment_is_empty_or_very_short"
476 );
477 }
478
479 /**
480 * Displays a warning message indicating that no changeset source is given.
481 * @return true if the user wants to revisit, false if they want to continue
482 */
483 protected boolean warnUploadSource() {
484 return warnUploadTag(
485 tr("Please specify a changeset source"),
486 tr("You did not specify a source for your changes.<br />" +
487 "It is technically allowed, but this information helps<br />" +
488 "other users to understand the origins of the data.<br /><br />" +
489 "If you spend a minute now to explain your change, you will make life<br />" +
490 "easier for many other mappers."),
491 "upload_source_is_empty"
492 );
493 }
494
495 protected boolean warnUploadTag(final String title, final String message, final String togglePref) {
496 ExtendedDialog dlg = new ExtendedDialog(UploadDialog.this,
497 title,
498 new String[] {tr("Revise"), tr("Cancel"), tr("Continue as is")});
499 dlg.setContent("<html>" + message + "</html>");
500 dlg.setButtonIcons(new Icon[] {
501 new ImageProvider("ok").setMaxSize(ImageSizes.LARGEICON).get(),
502 new ImageProvider("cancel").setMaxSize(ImageSizes.LARGEICON).get(),
503 new ImageProvider("upload").setMaxSize(ImageSizes.LARGEICON).addOverlay(
504 new ImageOverlay(new ImageProvider("warning-small"), 0.5, 0.5, 1.0, 1.0)).get()});
505 dlg.setToolTipTexts(new String[] {
506 tr("Return to the previous dialog to enter a more descriptive comment"),
507 tr("Cancel and return to the previous dialog"),
508 tr("Ignore this hint and upload anyway")});
509 dlg.setIcon(JOptionPane.WARNING_MESSAGE);
510 dlg.toggleEnable(togglePref);
511 dlg.setCancelButton(1, 2);
512 return dlg.showDialog().getValue() != 3;
513 }
514
515 protected void warnIllegalChunkSize() {
516 HelpAwareOptionPane.showOptionDialog(
517 UploadDialog.this,
518 tr("Please enter a valid chunk size first"),
519 tr("Illegal chunk size"),
520 JOptionPane.ERROR_MESSAGE,
521 ht("/Dialog/Upload#IllegalChunkSize")
522 );
523 }
524
525 @Override
526 public void actionPerformed(ActionEvent e) {
527 if ((getUploadComment().trim().length() < 10 && warnUploadComment()) /* abort for missing comment */
528 || (getUploadSource().trim().isEmpty() && warnUploadSource()) /* abort for missing changeset source */
529 ) {
530 tpConfigPanels.setSelectedIndex(0);
531 pnlBasicUploadSettings.initEditingOfUploadComment();
532 return;
533 }
534
535 /* test for empty tags in the changeset metadata and proceed only after user's confirmation.
536 * though, accept if key and value are empty (cf. xor). */
537 List<String> emptyChangesetTags = new ArrayList<>();
538 for (final Entry<String, String> i : pnlTagSettings.getTags(true).entrySet()) {
539 final boolean isKeyEmpty = i.getKey() == null || i.getKey().trim().isEmpty();
540 final boolean isValueEmpty = i.getValue() == null || i.getValue().trim().isEmpty();
541 final boolean ignoreKey = "comment".equals(i.getKey()) || "source".equals(i.getKey());
542 if ((isKeyEmpty ^ isValueEmpty) && !ignoreKey) {
543 emptyChangesetTags.add(tr("{0}={1}", i.getKey(), i.getValue()));
544 }
545 }
546 if (!emptyChangesetTags.isEmpty() && JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(
547 Main.parent,
548 trn(
549 "<html>The following changeset tag contains an empty key/value:<br>{0}<br>Continue?</html>",
550 "<html>The following changeset tags contain an empty key/value:<br>{0}<br>Continue?</html>",
551 emptyChangesetTags.size(), Utils.joinAsHtmlUnorderedList(emptyChangesetTags)),
552 tr("Empty metadata"),
553 JOptionPane.OK_CANCEL_OPTION,
554 JOptionPane.WARNING_MESSAGE
555 )) {
556 tpConfigPanels.setSelectedIndex(0);
557 pnlBasicUploadSettings.initEditingOfUploadComment();
558 return;
559 }
560
561 UploadStrategySpecification strategy = getUploadStrategySpecification();
562 if (strategy.getStrategy().equals(UploadStrategy.CHUNKED_DATASET_STRATEGY)
563 && strategy.getChunkSize() == UploadStrategySpecification.UNSPECIFIED_CHUNK_SIZE) {
564 warnIllegalChunkSize();
565 tpConfigPanels.setSelectedIndex(0);
566 return;
567 }
568 setCanceled(false);
569 setVisible(false);
570 }
571 }
572
573 /**
574 * Action for canceling the dialog
575 *
576 */
577 class CancelAction extends AbstractAction {
578 CancelAction() {
579 putValue(NAME, tr("Cancel"));
580 putValue(SMALL_ICON, ImageProvider.get("cancel"));
581 putValue(SHORT_DESCRIPTION, tr("Cancel the upload and resume editing"));
582 }
583
584 @Override
585 public void actionPerformed(ActionEvent e) {
586 setCanceled(true);
587 setVisible(false);
588 }
589 }
590
591 /**
592 * Listens to window closing events and processes them as cancel events.
593 * Listens to window open events and initializes user input
594 *
595 */
596 class WindowEventHandler extends WindowAdapter {
597 @Override
598 public void windowClosing(WindowEvent e) {
599 setCanceled(true);
600 }
601
602 @Override
603 public void windowActivated(WindowEvent arg0) {
604 if (tpConfigPanels.getSelectedIndex() == 0) {
605 pnlBasicUploadSettings.initEditingOfUploadComment();
606 }
607 }
608 }
609
610 /* -------------------------------------------------------------------------- */
611 /* Interface PropertyChangeListener */
612 /* -------------------------------------------------------------------------- */
613 @Override
614 public void propertyChange(PropertyChangeEvent evt) {
615 if (evt.getPropertyName().equals(ChangesetManagementPanel.SELECTED_CHANGESET_PROP)) {
616 Changeset cs = (Changeset) evt.getNewValue();
617 setChangesetTags(dataSet);
618 if (cs == null) {
619 tpConfigPanels.setTitleAt(1, tr("Tags of new changeset"));
620 } else {
621 tpConfigPanels.setTitleAt(1, tr("Tags of changeset {0}", cs.getId()));
622 }
623 }
624 }
625
626 /* -------------------------------------------------------------------------- */
627 /* Interface PreferenceChangedListener */
628 /* -------------------------------------------------------------------------- */
629 @Override
630 public void preferenceChanged(PreferenceChangeEvent e) {
631 if (e.getKey() == null || !"osm-server.url".equals(e.getKey()))
632 return;
633 final Setting<?> newValue = e.getNewValue();
634 final String url;
635 if (newValue == null || newValue.getValue() == null) {
636 url = OsmApi.getOsmApi().getBaseUrl();
637 } else {
638 url = newValue.getValue().toString();
639 }
640 setTitle(tr("Upload to ''{0}''", url));
641 }
642
643 private static String getLastChangesetTagFromHistory(String historyKey, List<String> def) {
644 Collection<String> history = Main.pref.getCollection(historyKey, def);
645 int age = (int) (System.currentTimeMillis() / 1000 - Main.pref.getInteger(BasicUploadSettingsPanel.HISTORY_LAST_USED_KEY, 0));
646 if (age < Main.pref.getInteger(BasicUploadSettingsPanel.HISTORY_MAX_AGE_KEY, 4 * 3600 * 1000) && history != null && !history.isEmpty()) {
647 return history.iterator().next();
648 } else {
649 return null;
650 }
651 }
652
653 /**
654 * Returns the last changeset comment from history.
655 * @return the last changeset comment from history
656 */
657 public String getLastChangesetCommentFromHistory() {
658 return getLastChangesetTagFromHistory(BasicUploadSettingsPanel.HISTORY_KEY, new ArrayList<String>());
659 }
660
661 /**
662 * Returns the last changeset source from history.
663 * @return the last changeset source from history
664 */
665 public String getLastChangesetSourceFromHistory() {
666 return getLastChangesetTagFromHistory(BasicUploadSettingsPanel.SOURCE_HISTORY_KEY, BasicUploadSettingsPanel.getDefaultSources());
667 }
668}
Note: See TracBrowser for help on using the repository browser.