[2801] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.dialogs.changeset.query;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.Dimension;
|
---|
| 7 | import java.awt.GridBagConstraints;
|
---|
| 8 | import java.awt.GridBagLayout;
|
---|
| 9 | import java.awt.Insets;
|
---|
| 10 | import java.awt.event.FocusAdapter;
|
---|
| 11 | import java.awt.event.FocusEvent;
|
---|
| 12 | import java.net.MalformedURLException;
|
---|
| 13 | import java.net.URL;
|
---|
| 14 |
|
---|
| 15 | import javax.swing.BorderFactory;
|
---|
| 16 | import javax.swing.JLabel;
|
---|
| 17 | import javax.swing.JPanel;
|
---|
| 18 | import javax.swing.event.DocumentEvent;
|
---|
| 19 | import javax.swing.event.DocumentListener;
|
---|
| 20 | import javax.swing.event.HyperlinkEvent;
|
---|
| 21 | import javax.swing.event.HyperlinkListener;
|
---|
| 22 |
|
---|
[6453] | 23 | import org.openstreetmap.josm.Main;
|
---|
[2801] | 24 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
[6814] | 25 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
[2801] | 26 | import org.openstreetmap.josm.io.ChangesetQuery;
|
---|
[6814] | 27 | import org.openstreetmap.josm.io.ChangesetQuery.ChangesetQueryUrlException;
|
---|
[2801] | 28 | import org.openstreetmap.josm.io.OsmApi;
|
---|
| 29 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | public class UrlBasedQueryPanel extends JPanel {
|
---|
| 33 |
|
---|
[5886] | 34 | private JosmTextField tfUrl;
|
---|
[2801] | 35 | private JLabel lblValid;
|
---|
| 36 |
|
---|
| 37 | protected JPanel buildURLPanel() {
|
---|
| 38 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
| 39 | GridBagConstraints gc = new GridBagConstraints();
|
---|
| 40 | gc.weightx = 0.0;
|
---|
| 41 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
[8510] | 42 | gc.insets = new Insets(0, 0, 0, 5);
|
---|
[2801] | 43 | pnl.add(new JLabel(tr("URL: ")), gc);
|
---|
| 44 |
|
---|
| 45 | gc.gridx = 1;
|
---|
| 46 | gc.weightx = 1.0;
|
---|
| 47 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
[5886] | 48 | pnl.add(tfUrl = new JosmTextField(), gc);
|
---|
[2801] | 49 | tfUrl.getDocument().addDocumentListener(new ChangetQueryUrlValidator());
|
---|
| 50 | tfUrl.addFocusListener(
|
---|
| 51 | new FocusAdapter() {
|
---|
| 52 | @Override
|
---|
| 53 | public void focusGained(FocusEvent e) {
|
---|
| 54 | tfUrl.selectAll();
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | );
|
---|
| 58 |
|
---|
| 59 | gc.gridx = 2;
|
---|
| 60 | gc.weightx = 0.0;
|
---|
| 61 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
| 62 | pnl.add(lblValid = new JLabel(), gc);
|
---|
[8510] | 63 | lblValid.setPreferredSize(new Dimension(20, 20));
|
---|
[2801] | 64 | return pnl;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | protected JPanel buildHelpPanel() {
|
---|
[6920] | 68 | String apiUrl = OsmApi.getOsmApi().getBaseUrl();
|
---|
[2801] | 69 | HtmlPanel pnl = new HtmlPanel();
|
---|
| 70 | pnl.setText(
|
---|
| 71 | "<html><body>"
|
---|
| 72 | + tr("Please enter or paste an URL to retrieve changesets from the OSM API.")
|
---|
| 73 | + "<p><strong>" + tr("Examples") + "</strong></p>"
|
---|
| 74 | + "<ul>"
|
---|
[6920] | 75 | + "<li><a href=\""+Main.getOSMWebsite()+"/history?open=true\">"+Main.getOSMWebsite()+"/history?open=true</a></li>"
|
---|
| 76 | + "<li><a href=\""+apiUrl+"/changesets?open=true\">"+apiUrl+"/changesets?open=true</a></li>"
|
---|
[2801] | 77 | + "</ul>"
|
---|
| 78 | + tr("Note that changeset queries are currently always submitted to ''{0}'', regardless of the "
|
---|
[6920] | 79 | + "host, port and path of the URL entered below.", apiUrl)
|
---|
[2801] | 80 | + "</body></html>"
|
---|
| 81 | );
|
---|
| 82 | pnl.getEditorPane().addHyperlinkListener(
|
---|
| 83 | new HyperlinkListener() {
|
---|
[6084] | 84 | @Override
|
---|
[2801] | 85 | public void hyperlinkUpdate(HyperlinkEvent e) {
|
---|
| 86 | if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
|
---|
| 87 | tfUrl.setText(e.getDescription());
|
---|
| 88 | tfUrl.requestFocusInWindow();
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | );
|
---|
| 93 | return pnl;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[6890] | 96 | protected final void build() {
|
---|
[2801] | 97 | setLayout(new GridBagLayout());
|
---|
[8510] | 98 | setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
---|
[2801] | 99 |
|
---|
| 100 | GridBagConstraints gc = new GridBagConstraints();
|
---|
| 101 | gc.weightx = 1.0;
|
---|
| 102 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
[8510] | 103 | gc.insets = new Insets(0, 0, 10, 0);
|
---|
| 104 | add(buildHelpPanel(), gc);
|
---|
[2801] | 105 |
|
---|
| 106 | gc.gridy = 1;
|
---|
| 107 | gc.weightx = 1.0;
|
---|
| 108 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
[8510] | 109 | add(buildURLPanel(), gc);
|
---|
[2801] | 110 |
|
---|
| 111 | gc.gridy = 2;
|
---|
| 112 | gc.weightx = 1.0;
|
---|
| 113 | gc.weighty = 1.0;
|
---|
| 114 | gc.fill = GridBagConstraints.BOTH;
|
---|
[8510] | 115 | add(new JPanel(), gc);
|
---|
[6890] | 116 | }
|
---|
[2801] | 117 |
|
---|
[6890] | 118 | /**
|
---|
| 119 | * Constructs a new {@code UrlBasedQueryPanel}.
|
---|
| 120 | */
|
---|
[2801] | 121 | public UrlBasedQueryPanel() {
|
---|
| 122 | build();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | protected boolean isValidChangesetQueryUrl(String text) {
|
---|
| 126 | return buildChangesetQuery(text) != null;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | protected ChangesetQuery buildChangesetQuery(String text) {
|
---|
| 130 | URL url = null;
|
---|
| 131 | try {
|
---|
| 132 | url = new URL(text);
|
---|
[8510] | 133 | } catch (MalformedURLException e) {
|
---|
[2801] | 134 | return null;
|
---|
| 135 | }
|
---|
| 136 | String path = url.getPath();
|
---|
| 137 | String query = url.getQuery();
|
---|
[8444] | 138 | if (path == null || !path.endsWith("/changesets")) return null;
|
---|
[2801] | 139 |
|
---|
| 140 | try {
|
---|
| 141 | return ChangesetQuery.buildFromUrlQuery(query);
|
---|
[7299] | 142 | } catch (ChangesetQueryUrlException e) {
|
---|
| 143 | Main.warn(e.getMessage());
|
---|
[2801] | 144 | return null;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
[5266] | 149 | * Replies the {@link ChangesetQuery} specified in this panel. null, if no valid changeset query
|
---|
[2801] | 150 | * is specified.
|
---|
[3530] | 151 | *
|
---|
[2801] | 152 | * @return the changeset query
|
---|
| 153 | */
|
---|
| 154 | public ChangesetQuery buildChangesetQuery() {
|
---|
| 155 | String value = tfUrl.getText().trim();
|
---|
| 156 | return buildChangesetQuery(value);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | public void startUserInput() {
|
---|
| 160 | tfUrl.requestFocusInWindow();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * Validates text entered in the changeset query URL field on the fly
|
---|
| 165 | */
|
---|
| 166 | class ChangetQueryUrlValidator implements DocumentListener {
|
---|
| 167 | protected String getCurrentFeedback() {
|
---|
[8510] | 168 | String fb = (String) lblValid.getClientProperty("valid");
|
---|
[2801] | 169 | return fb == null ? "none" : fb;
|
---|
| 170 | }
|
---|
[8510] | 171 |
|
---|
[2801] | 172 | protected void feedbackValid() {
|
---|
[6990] | 173 | if ("valid".equals(getCurrentFeedback())) return;
|
---|
[6814] | 174 | lblValid.setIcon(ImageProvider.get("dialogs", "valid"));
|
---|
[7712] | 175 | lblValid.setToolTipText(null);
|
---|
[2801] | 176 | lblValid.putClientProperty("valid", "valid");
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | protected void feedbackInvalid() {
|
---|
[6990] | 180 | if ("invalid".equals(getCurrentFeedback())) return;
|
---|
[2801] | 181 | lblValid.setIcon(ImageProvider.get("warning-small"));
|
---|
| 182 | lblValid.setToolTipText(tr("This changeset query URL is invalid"));
|
---|
| 183 | lblValid.putClientProperty("valid", "invalid");
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | protected void feedbackNone() {
|
---|
| 187 | lblValid.setIcon(null);
|
---|
| 188 | lblValid.putClientProperty("valid", "none");
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | protected void validate() {
|
---|
| 192 | String value = tfUrl.getText();
|
---|
[6087] | 193 | if (value.trim().isEmpty()) {
|
---|
[2801] | 194 | feedbackNone();
|
---|
| 195 | return;
|
---|
| 196 | }
|
---|
| 197 | value = value.trim();
|
---|
| 198 | if (isValidChangesetQueryUrl(value)) {
|
---|
| 199 | feedbackValid();
|
---|
| 200 | } else {
|
---|
| 201 | feedbackInvalid();
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
[8510] | 204 |
|
---|
[6084] | 205 | @Override
|
---|
[2801] | 206 | public void changedUpdate(DocumentEvent e) {
|
---|
| 207 | validate();
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[6084] | 210 | @Override
|
---|
[2801] | 211 | public void insertUpdate(DocumentEvent e) {
|
---|
| 212 | validate();
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[6084] | 215 | @Override
|
---|
[2801] | 216 | public void removeUpdate(DocumentEvent e) {
|
---|
| 217 | validate();
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 | }
|
---|