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

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

fix some Sonar issues (Constructor Calls Overridable Method)

  • 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.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 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 final 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 /**
118 * Constructs a new {@code UrlBasedQueryPanel}.
119 */
120 public UrlBasedQueryPanel() {
121 build();
122 }
123
124 protected boolean isValidChangesetQueryUrl(String text) {
125 return buildChangesetQuery(text) != null;
126 }
127
128 protected ChangesetQuery buildChangesetQuery(String text) {
129 URL url = null;
130 try {
131 url = new URL(text);
132 } catch(MalformedURLException e) {
133 return null;
134 }
135 String path = url.getPath();
136 String query = url.getQuery();
137 if (path == null || ! path.endsWith("/changesets")) return null;
138
139 try {
140 return ChangesetQuery.buildFromUrlQuery(query);
141 } catch(ChangesetQueryUrlException e) {
142 return null;
143 }
144 }
145
146 /**
147 * Replies the {@link ChangesetQuery} specified in this panel. null, if no valid changeset query
148 * is specified.
149 *
150 * @return the changeset query
151 */
152 public ChangesetQuery buildChangesetQuery() {
153 String value = tfUrl.getText().trim();
154 return buildChangesetQuery(value);
155 }
156
157 public void startUserInput() {
158 tfUrl.requestFocusInWindow();
159 }
160
161 /**
162 * Validates text entered in the changeset query URL field on the fly
163 */
164 class ChangetQueryUrlValidator implements DocumentListener {
165 protected String getCurrentFeedback() {
166 String fb = (String)lblValid.getClientProperty("valid");
167 return fb == null ? "none" : fb;
168 }
169 protected void feedbackValid() {
170 if (getCurrentFeedback().equals("valid")) return;
171 lblValid.setIcon(ImageProvider.get("dialogs", "valid"));
172 lblValid.setToolTipText("");
173 lblValid.putClientProperty("valid", "valid");
174 }
175
176 protected void feedbackInvalid() {
177 if (getCurrentFeedback().equals("invalid")) return;
178 lblValid.setIcon(ImageProvider.get("warning-small"));
179 lblValid.setToolTipText(tr("This changeset query URL is invalid"));
180 lblValid.putClientProperty("valid", "invalid");
181 }
182
183 protected void feedbackNone() {
184 lblValid.setIcon(null);
185 lblValid.putClientProperty("valid", "none");
186 }
187
188 protected void validate() {
189 String value = tfUrl.getText();
190 if (value.trim().isEmpty()) {
191 feedbackNone();
192 return;
193 }
194 value = value.trim();
195 if (isValidChangesetQueryUrl(value)) {
196 feedbackValid();
197 } else {
198 feedbackInvalid();
199 }
200 }
201 @Override
202 public void changedUpdate(DocumentEvent e) {
203 validate();
204 }
205
206 @Override
207 public void insertUpdate(DocumentEvent e) {
208 validate();
209 }
210
211 @Override
212 public void removeUpdate(DocumentEvent e) {
213 validate();
214 }
215 }
216}
Note: See TracBrowser for help on using the repository browser.