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