[2711] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.dialogs.changeset;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.Color;
|
---|
| 7 | import java.awt.FlowLayout;
|
---|
| 8 | import java.awt.event.ActionEvent;
|
---|
| 9 | import java.util.Collections;
|
---|
| 10 |
|
---|
| 11 | import javax.swing.AbstractAction;
|
---|
| 12 | import javax.swing.BorderFactory;
|
---|
| 13 | import javax.swing.JLabel;
|
---|
| 14 | import javax.swing.JPanel;
|
---|
| 15 | import javax.swing.event.DocumentEvent;
|
---|
| 16 | import javax.swing.event.DocumentListener;
|
---|
| 17 |
|
---|
[5765] | 18 | import org.openstreetmap.josm.Main;
|
---|
[10124] | 19 | import org.openstreetmap.josm.actions.downloadtasks.ChangesetContentDownloadTask;
|
---|
[2711] | 20 | import org.openstreetmap.josm.gui.SideButton;
|
---|
[5765] | 21 | import org.openstreetmap.josm.gui.widgets.ChangesetIdTextField;
|
---|
[2711] | 22 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
[7434] | 23 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
[10438] | 24 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[2711] | 25 |
|
---|
| 26 | /**
|
---|
| 27 | * This panel allows to enter the ID of single changeset and to download
|
---|
| 28 | * the respective changeset.
|
---|
[10179] | 29 | * @since 2689
|
---|
[2711] | 30 | */
|
---|
| 31 | public class SingleChangesetDownloadPanel extends JPanel {
|
---|
| 32 |
|
---|
[10179] | 33 | private final ChangesetIdTextField tfChangesetId = new ChangesetIdTextField();
|
---|
[2711] | 34 |
|
---|
[10179] | 35 | /**
|
---|
| 36 | * Constructs a new {@link SingleChangesetDownloadPanel}
|
---|
| 37 | */
|
---|
| 38 | public SingleChangesetDownloadPanel() {
|
---|
| 39 | build();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[2711] | 42 | protected void build() {
|
---|
[8510] | 43 | setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
|
---|
[2711] | 44 | setBorder(
|
---|
| 45 | BorderFactory.createCompoundBorder(
|
---|
| 46 | BorderFactory.createLineBorder(Color.GRAY),
|
---|
[8510] | 47 | BorderFactory.createEmptyBorder(0, 3, 0, 3)
|
---|
[2711] | 48 | )
|
---|
| 49 | );
|
---|
| 50 |
|
---|
| 51 | add(new JLabel(tr("Changeset ID: ")));
|
---|
[10179] | 52 | add(tfChangesetId);
|
---|
[2915] | 53 | tfChangesetId.setToolTipText(tr("Enter a changeset id"));
|
---|
| 54 | SelectAllOnFocusGainedDecorator.decorate(tfChangesetId);
|
---|
[2711] | 55 |
|
---|
[8464] | 56 | DownloadAction actDownload = new DownloadAction();
|
---|
[2711] | 57 | SideButton btn = new SideButton(actDownload);
|
---|
[2915] | 58 | tfChangesetId.addActionListener(actDownload);
|
---|
| 59 | tfChangesetId.getDocument().addDocumentListener(actDownload);
|
---|
[2711] | 60 | add(btn);
|
---|
[6070] | 61 |
|
---|
| 62 | if (Main.pref.getBoolean("downloadchangeset.autopaste", true)) {
|
---|
[5765] | 63 | tfChangesetId.tryToPasteFromClipboard();
|
---|
| 64 | }
|
---|
[2711] | 65 | }
|
---|
| 66 |
|
---|
[5765] | 67 | /**
|
---|
[2711] | 68 | * Replies the changeset id entered in this panel. 0 if no changeset id
|
---|
| 69 | * or an invalid changeset id is currently entered.
|
---|
| 70 | *
|
---|
| 71 | * @return the changeset id entered in this panel
|
---|
| 72 | */
|
---|
[2915] | 73 | public int getChangesetId() {
|
---|
[5765] | 74 | return tfChangesetId.getChangesetId();
|
---|
[2711] | 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Downloads the single changeset from the OSM API
|
---|
| 79 | */
|
---|
[9059] | 80 | class DownloadAction extends AbstractAction implements DocumentListener {
|
---|
[2711] | 81 |
|
---|
[8836] | 82 | DownloadAction() {
|
---|
[10438] | 83 | new ImageProvider("dialogs/changeset", "downloadchangesetcontent").getResource().attachImageIcon(this);
|
---|
[2711] | 84 | putValue(SHORT_DESCRIPTION, tr("Download the changeset with the specified id, including the changeset content"));
|
---|
| 85 | updateEnabledState();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[6084] | 88 | @Override
|
---|
[2711] | 89 | public void actionPerformed(ActionEvent arg0) {
|
---|
| 90 | if (!isEnabled())
|
---|
| 91 | return;
|
---|
[2915] | 92 | int id = getChangesetId();
|
---|
[10179] | 93 | if (id == 0)
|
---|
| 94 | return;
|
---|
[10378] | 95 | ChangesetContentDownloadTask task = new ChangesetContentDownloadTask(
|
---|
[2711] | 96 | SingleChangesetDownloadPanel.this,
|
---|
| 97 | Collections.singleton(id)
|
---|
| 98 | );
|
---|
| 99 | ChangesetCacheManager.getInstance().runDownloadTask(task);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | protected void updateEnabledState() {
|
---|
[7434] | 103 | setEnabled(tfChangesetId.readIds() && !Main.isOffline(OnlineResource.OSM_API));
|
---|
[2711] | 104 | }
|
---|
| 105 |
|
---|
[6084] | 106 | @Override
|
---|
[2711] | 107 | public void changedUpdate(DocumentEvent arg0) {
|
---|
| 108 | updateEnabledState();
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[6084] | 111 | @Override
|
---|
[2711] | 112 | public void insertUpdate(DocumentEvent arg0) {
|
---|
| 113 | updateEnabledState();
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[6084] | 116 | @Override
|
---|
[2711] | 117 | public void removeUpdate(DocumentEvent arg0) {
|
---|
| 118 | updateEnabledState();
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|