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

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

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

  • Property svn:eol-style set to native
File size: 6.8 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;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.gui.widgets.HtmlPanel;
24import org.openstreetmap.josm.gui.widgets.JosmTextField;
25import org.openstreetmap.josm.io.ChangesetQuery;
26import org.openstreetmap.josm.io.ChangesetQuery.ChangesetQueryUrlException;
27import org.openstreetmap.josm.io.OsmApi;
28import org.openstreetmap.josm.tools.ImageProvider;
29
30public class UrlBasedQueryPanel extends JPanel {
31
32 private final JosmTextField tfUrl = new JosmTextField();
33 private final JLabel lblValid = new JLabel();
34
35 /**
36 * Constructs a new {@code UrlBasedQueryPanel}.
37 */
38 public UrlBasedQueryPanel() {
39 build();
40 }
41
42 protected JPanel buildURLPanel() {
43 JPanel pnl = new JPanel(new GridBagLayout());
44 GridBagConstraints gc = new GridBagConstraints();
45 gc.weightx = 0.0;
46 gc.fill = GridBagConstraints.HORIZONTAL;
47 gc.insets = new Insets(0, 0, 0, 5);
48 pnl.add(new JLabel(tr("URL: ")), gc);
49
50 gc.gridx = 1;
51 gc.weightx = 1.0;
52 gc.fill = GridBagConstraints.HORIZONTAL;
53 pnl.add(tfUrl, gc);
54 tfUrl.getDocument().addDocumentListener(new ChangetQueryUrlValidator());
55 tfUrl.addFocusListener(
56 new FocusAdapter() {
57 @Override
58 public void focusGained(FocusEvent e) {
59 tfUrl.selectAll();
60 }
61 }
62 );
63
64 gc.gridx = 2;
65 gc.weightx = 0.0;
66 gc.fill = GridBagConstraints.HORIZONTAL;
67 pnl.add(lblValid, gc);
68 lblValid.setPreferredSize(new Dimension(20, 20));
69 return pnl;
70 }
71
72 protected JPanel buildHelpPanel() {
73 String apiUrl = OsmApi.getOsmApi().getBaseUrl();
74 HtmlPanel pnl = new HtmlPanel();
75 pnl.setText(
76 "<html><body>"
77 + tr("Please enter or paste an URL to retrieve changesets from the OSM API.")
78 + "<p><strong>" + tr("Examples") + "</strong></p>"
79 + "<ul>"
80 + "<li><a href=\""+Main.getOSMWebsite()+"/history?open=true\">"+Main.getOSMWebsite()+"/history?open=true</a></li>"
81 + "<li><a href=\""+apiUrl+"/changesets?open=true\">"+apiUrl+"/changesets?open=true</a></li>"
82 + "</ul>"
83 + tr("Note that changeset queries are currently always submitted to ''{0}'', regardless of the "
84 + "host, port and path of the URL entered below.", apiUrl)
85 + "</body></html>"
86 );
87 pnl.getEditorPane().addHyperlinkListener(e -> {
88 if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
89 tfUrl.setText(e.getDescription());
90 tfUrl.requestFocusInWindow();
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 protected boolean isValidChangesetQueryUrl(String text) {
119 return buildChangesetQuery(text) != null;
120 }
121
122 protected ChangesetQuery buildChangesetQuery(String text) {
123 URL url = null;
124 try {
125 url = new URL(text);
126 } catch (MalformedURLException e) {
127 return null;
128 }
129 String path = url.getPath();
130 if (path == null || !path.endsWith("/changesets"))
131 return null;
132
133 try {
134 return ChangesetQuery.buildFromUrlQuery(url.getQuery());
135 } catch (ChangesetQueryUrlException e) {
136 Main.warn(e.getMessage());
137 return null;
138 }
139 }
140
141 /**
142 * Replies the {@link ChangesetQuery} specified in this panel. null, if no valid changeset query
143 * is specified.
144 *
145 * @return the changeset query
146 */
147 public ChangesetQuery buildChangesetQuery() {
148 String value = tfUrl.getText().trim();
149 return buildChangesetQuery(value);
150 }
151
152 public void startUserInput() {
153 tfUrl.requestFocusInWindow();
154 }
155
156 /**
157 * Validates text entered in the changeset query URL field on the fly
158 */
159 class ChangetQueryUrlValidator implements DocumentListener {
160 protected String getCurrentFeedback() {
161 String fb = (String) lblValid.getClientProperty("valid");
162 return fb == null ? "none" : fb;
163 }
164
165 protected void feedbackValid() {
166 if ("valid".equals(getCurrentFeedback()))
167 return;
168 lblValid.setIcon(ImageProvider.get("dialogs", "valid"));
169 lblValid.setToolTipText(null);
170 lblValid.putClientProperty("valid", "valid");
171 }
172
173 protected void feedbackInvalid() {
174 if ("invalid".equals(getCurrentFeedback()))
175 return;
176 lblValid.setIcon(ImageProvider.get("warning-small"));
177 lblValid.setToolTipText(tr("This changeset query URL is invalid"));
178 lblValid.putClientProperty("valid", "invalid");
179 }
180
181 protected void feedbackNone() {
182 lblValid.setIcon(null);
183 lblValid.putClientProperty("valid", "none");
184 }
185
186 protected void validate() {
187 String value = tfUrl.getText();
188 if (value.trim().isEmpty()) {
189 feedbackNone();
190 return;
191 }
192 value = value.trim();
193 if (isValidChangesetQueryUrl(value)) {
194 feedbackValid();
195 } else {
196 feedbackInvalid();
197 }
198 }
199
200 @Override
201 public void changedUpdate(DocumentEvent e) {
202 validate();
203 }
204
205 @Override
206 public void insertUpdate(DocumentEvent e) {
207 validate();
208 }
209
210 @Override
211 public void removeUpdate(DocumentEvent e) {
212 validate();
213 }
214 }
215}
Note: See TracBrowser for help on using the repository browser.