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.JTextField;
|
---|
16 | import javax.swing.event.DocumentEvent;
|
---|
17 | import javax.swing.event.DocumentListener;
|
---|
18 | import javax.swing.text.JTextComponent;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.gui.SideButton;
|
---|
21 | import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
|
---|
22 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * This panel allows to enter the ID of single changeset and to download
|
---|
27 | * the respective changeset.
|
---|
28 | *
|
---|
29 | */
|
---|
30 | public class SingleChangesetDownloadPanel extends JPanel {
|
---|
31 |
|
---|
32 | private JTextField tfChangsetId;
|
---|
33 | private DownloadAction actDownload;
|
---|
34 | private ChangesetIdValidator valChangesetId;
|
---|
35 |
|
---|
36 | protected void build() {
|
---|
37 | setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
|
---|
38 | setBorder(
|
---|
39 | BorderFactory.createCompoundBorder(
|
---|
40 | BorderFactory.createLineBorder(Color.GRAY),
|
---|
41 | BorderFactory.createEmptyBorder(0,3,0,3)
|
---|
42 | )
|
---|
43 | );
|
---|
44 |
|
---|
45 | add(new JLabel(tr("Changeset ID: ")));
|
---|
46 | add(tfChangsetId = new JTextField(10));
|
---|
47 | tfChangsetId.setToolTipText(tr("Enter a changset id"));
|
---|
48 | valChangesetId =ChangesetIdValidator.decorate(tfChangsetId);
|
---|
49 | SelectAllOnFocusGainedDecorator.decorate(tfChangsetId);
|
---|
50 |
|
---|
51 | actDownload = new DownloadAction();
|
---|
52 | SideButton btn = new SideButton(actDownload);
|
---|
53 | tfChangsetId.addActionListener(actDownload);
|
---|
54 | tfChangsetId.getDocument().addDocumentListener(actDownload);
|
---|
55 | add(btn);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public SingleChangesetDownloadPanel() {
|
---|
59 | build();
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Replies the changeset id entered in this panel. 0 if no changeset id
|
---|
64 | * or an invalid changeset id is currently entered.
|
---|
65 | *
|
---|
66 | * @return the changeset id entered in this panel
|
---|
67 | */
|
---|
68 | public int getChangsetId() {
|
---|
69 | return valChangesetId.getChangesetId();
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Downloads the single changeset from the OSM API
|
---|
74 | */
|
---|
75 | class DownloadAction extends AbstractAction implements DocumentListener{
|
---|
76 |
|
---|
77 | public DownloadAction() {
|
---|
78 | putValue(SMALL_ICON, ImageProvider.get("dialogs/changeset", "downloadchangesetcontent"));
|
---|
79 | putValue(SHORT_DESCRIPTION, tr("Download the changeset with the specified id, including the changeset content"));
|
---|
80 | updateEnabledState();
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void actionPerformed(ActionEvent arg0) {
|
---|
84 | if (!isEnabled())
|
---|
85 | return;
|
---|
86 | int id = getChangsetId();
|
---|
87 | if (id == 0) return;
|
---|
88 | ChangesetContentDownloadTask task = new ChangesetContentDownloadTask(
|
---|
89 | SingleChangesetDownloadPanel.this,
|
---|
90 | Collections.singleton(id)
|
---|
91 | );
|
---|
92 | ChangesetCacheManager.getInstance().runDownloadTask(task);
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected void updateEnabledState() {
|
---|
96 | String v = tfChangsetId.getText();
|
---|
97 | if (v == null || v.trim().length() == 0) {
|
---|
98 | setEnabled(false);
|
---|
99 | return;
|
---|
100 | }
|
---|
101 | setEnabled(valChangesetId.isValid());
|
---|
102 | }
|
---|
103 |
|
---|
104 | public void changedUpdate(DocumentEvent arg0) {
|
---|
105 | updateEnabledState();
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void insertUpdate(DocumentEvent arg0) {
|
---|
109 | updateEnabledState();
|
---|
110 | }
|
---|
111 |
|
---|
112 | public void removeUpdate(DocumentEvent arg0) {
|
---|
113 | updateEnabledState();
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Validator for a changeset ID entered in a {@see JTextComponent}.
|
---|
119 | *
|
---|
120 | */
|
---|
121 | static private class ChangesetIdValidator extends AbstractTextComponentValidator {
|
---|
122 | static public ChangesetIdValidator decorate(JTextComponent tc) {
|
---|
123 | return new ChangesetIdValidator(tc);
|
---|
124 | }
|
---|
125 |
|
---|
126 | public ChangesetIdValidator(JTextComponent tc) {
|
---|
127 | super(tc);
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Override
|
---|
131 | public boolean isValid() {
|
---|
132 | String value = getComponent().getText();
|
---|
133 | if (value == null || value.trim().length() == 0)
|
---|
134 | return true;
|
---|
135 | return getChangesetId() > 0;
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | public void validate() {
|
---|
140 | if (!isValid()) {
|
---|
141 | feedbackInvalid(tr("The current value isn't a valid changeset ID. Please enter an integer value > 0"));
|
---|
142 | } else {
|
---|
143 | feedbackValid(tr("Please enter an integer value > 0"));
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | public int getChangesetId() {
|
---|
148 | String value = getComponent().getText();
|
---|
149 | if (value == null || value.trim().length() == 0) return 0;
|
---|
150 | try {
|
---|
151 | int changesetId = Integer.parseInt(value.trim());
|
---|
152 | if (changesetId > 0) return changesetId;
|
---|
153 | return 0;
|
---|
154 | } catch(NumberFormatException e) {
|
---|
155 | return 0;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|