source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/query/UrlBasedQueryPanel.java@ 6453

Last change on this file since 6453 was 6453, checked in by Don-vip, 10 years ago

global use of osm website url and new url scheme

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