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