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

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

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 7.1 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.gui.widgets.JosmTextField;
26import org.openstreetmap.josm.io.ChangesetQuery;
27import org.openstreetmap.josm.io.ChangesetQuery.ChangesetQueryUrlException;
28import org.openstreetmap.josm.io.OsmApi;
29import org.openstreetmap.josm.tools.ImageProvider;
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 String apiUrl = OsmApi.getOsmApi().getBaseUrl();
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>"
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>"
77 + "</ul>"
78 + tr("Note that changeset queries are currently always submitted to ''{0}'', regardless of the "
79 + "host, port and path of the URL entered below.", apiUrl)
80 + "</body></html>"
81 );
82 pnl.getEditorPane().addHyperlinkListener(
83 new HyperlinkListener() {
84 @Override
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
96 protected final void build() {
97 setLayout(new GridBagLayout());
98 setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
99
100 GridBagConstraints gc = new GridBagConstraints();
101 gc.weightx = 1.0;
102 gc.fill = GridBagConstraints.HORIZONTAL;
103 gc.insets = new Insets(0, 0, 10, 0);
104 add(buildHelpPanel(), gc);
105
106 gc.gridy = 1;
107 gc.weightx = 1.0;
108 gc.fill = GridBagConstraints.HORIZONTAL;
109 add(buildURLPanel(), gc);
110
111 gc.gridy = 2;
112 gc.weightx = 1.0;
113 gc.weighty = 1.0;
114 gc.fill = GridBagConstraints.BOTH;
115 add(new JPanel(), gc);
116 }
117
118 /**
119 * Constructs a new {@code UrlBasedQueryPanel}.
120 */
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);
133 } catch (MalformedURLException e) {
134 return null;
135 }
136 String path = url.getPath();
137 String query = url.getQuery();
138 if (path == null || !path.endsWith("/changesets")) return null;
139
140 try {
141 return ChangesetQuery.buildFromUrlQuery(query);
142 } catch (ChangesetQueryUrlException e) {
143 Main.warn(e.getMessage());
144 return null;
145 }
146 }
147
148 /**
149 * Replies the {@link ChangesetQuery} specified in this panel. null, if no valid changeset query
150 * is specified.
151 *
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() {
168 String fb = (String) lblValid.getClientProperty("valid");
169 return fb == null ? "none" : fb;
170 }
171
172 protected void feedbackValid() {
173 if ("valid".equals(getCurrentFeedback())) return;
174 lblValid.setIcon(ImageProvider.get("dialogs", "valid"));
175 lblValid.setToolTipText(null);
176 lblValid.putClientProperty("valid", "valid");
177 }
178
179 protected void feedbackInvalid() {
180 if ("invalid".equals(getCurrentFeedback())) return;
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();
193 if (value.trim().isEmpty()) {
194 feedbackNone();
195 return;
196 }
197 value = value.trim();
198 if (isValidChangesetQueryUrl(value)) {
199 feedbackValid();
200 } else {
201 feedbackInvalid();
202 }
203 }
204
205 @Override
206 public void changedUpdate(DocumentEvent e) {
207 validate();
208 }
209
210 @Override
211 public void insertUpdate(DocumentEvent e) {
212 validate();
213 }
214
215 @Override
216 public void removeUpdate(DocumentEvent e) {
217 validate();
218 }
219 }
220}
Note: See TracBrowser for help on using the repository browser.