source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/query/AdvancedChangesetQueryPanel.java@ 8513

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

checkstyle: blocks

  • Property svn:eol-style set to native
File size: 46.5 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.BorderLayout;
7import java.awt.Color;
8import java.awt.GridBagConstraints;
9import java.awt.GridBagLayout;
10import java.awt.Insets;
11import java.awt.event.ItemEvent;
12import java.awt.event.ItemListener;
13import java.text.DateFormat;
14import java.text.ParseException;
15import java.util.Date;
16import java.util.GregorianCalendar;
17import java.util.Locale;
18
19import javax.swing.BorderFactory;
20import javax.swing.ButtonGroup;
21import javax.swing.JCheckBox;
22import javax.swing.JLabel;
23import javax.swing.JOptionPane;
24import javax.swing.JPanel;
25import javax.swing.JRadioButton;
26import javax.swing.JScrollPane;
27import javax.swing.text.JTextComponent;
28
29import org.openstreetmap.josm.Main;
30import org.openstreetmap.josm.gui.HelpAwareOptionPane;
31import org.openstreetmap.josm.gui.JosmUserIdentityManager;
32import org.openstreetmap.josm.gui.help.HelpUtil;
33import org.openstreetmap.josm.gui.util.GuiHelper;
34import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
35import org.openstreetmap.josm.gui.widgets.BoundingBoxSelectionPanel;
36import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
37import org.openstreetmap.josm.gui.widgets.JosmTextField;
38import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
39import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
40import org.openstreetmap.josm.io.ChangesetQuery;
41import org.openstreetmap.josm.tools.CheckParameterUtil;
42
43
44/**
45 * This panel allows to specify a changeset query
46 *
47 */
48public class AdvancedChangesetQueryPanel extends JPanel {
49
50 private JCheckBox cbUserRestriction;
51 private JCheckBox cbOpenAndCloseRestrictions;
52 private JCheckBox cbTimeRestrictions;
53 private JCheckBox cbBoundingBoxRestriction;
54 private UserRestrictionPanel pnlUserRestriction;
55 private OpenAndCloseStateRestrictionPanel pnlOpenAndCloseRestriction;
56 private TimeRestrictionPanel pnlTimeRestriction;
57 private BBoxRestrictionPanel pnlBoundingBoxRestriction;
58
59 protected JPanel buildQueryPanel() {
60 ItemListener stateChangeHandler = new RestrictionGroupStateChangeHandler();
61 JPanel pnl = new VerticallyScrollablePanel();
62 pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
63 pnl.setLayout(new GridBagLayout());
64 GridBagConstraints gc = new GridBagConstraints();
65
66 // -- select changesets by a specific user
67 //
68 gc.anchor = GridBagConstraints.NORTHWEST;
69 gc.weightx = 0.0;
70 gc.fill = GridBagConstraints.HORIZONTAL;
71 pnl.add(cbUserRestriction = new JCheckBox(), gc);
72 cbUserRestriction.addItemListener(stateChangeHandler);
73
74 gc.gridx = 1;
75 gc.weightx = 1.0;
76 pnl.add(new JMultilineLabel(tr("Select changesets owned by specific users")), gc);
77
78 gc.gridy = 1;
79 gc.gridx = 1;
80 gc.weightx = 1.0;
81 pnl.add(pnlUserRestriction = new UserRestrictionPanel(), gc);
82
83 // -- restricting the query to open and closed changesets
84 //
85 gc.gridy = 2;
86 gc.gridx = 0;
87 gc.anchor = GridBagConstraints.NORTHWEST;
88 gc.weightx = 0.0;
89 gc.fill = GridBagConstraints.HORIZONTAL;
90 pnl.add(cbOpenAndCloseRestrictions = new JCheckBox(), gc);
91 cbOpenAndCloseRestrictions.addItemListener(stateChangeHandler);
92
93 gc.gridx = 1;
94 gc.weightx = 1.0;
95 pnl.add(new JMultilineLabel(tr("Select changesets depending on whether they are open or closed")), gc);
96
97 gc.gridy = 3;
98 gc.gridx = 1;
99 gc.weightx = 1.0;
100 pnl.add(pnlOpenAndCloseRestriction = new OpenAndCloseStateRestrictionPanel(), gc);
101
102 // -- restricting the query to a specific time
103 //
104 gc.gridy = 4;
105 gc.gridx = 0;
106 gc.anchor = GridBagConstraints.NORTHWEST;
107 gc.weightx = 0.0;
108 gc.fill = GridBagConstraints.HORIZONTAL;
109 pnl.add(cbTimeRestrictions = new JCheckBox(), gc);
110 cbTimeRestrictions.addItemListener(stateChangeHandler);
111
112 gc.gridx = 1;
113 gc.weightx = 1.0;
114 pnl.add(new JMultilineLabel(tr("Select changesets based on the date/time they have been created or closed")), gc);
115
116 gc.gridy = 5;
117 gc.gridx = 1;
118 gc.weightx = 1.0;
119 pnl.add(pnlTimeRestriction = new TimeRestrictionPanel(), gc);
120
121
122 // -- restricting the query to a specific bounding box
123 //
124 gc.gridy = 6;
125 gc.gridx = 0;
126 gc.anchor = GridBagConstraints.NORTHWEST;
127 gc.weightx = 0.0;
128 gc.fill = GridBagConstraints.HORIZONTAL;
129 pnl.add(cbBoundingBoxRestriction = new JCheckBox(), gc);
130 cbBoundingBoxRestriction.addItemListener(stateChangeHandler);
131
132 gc.gridx = 1;
133 gc.weightx = 1.0;
134 pnl.add(new JMultilineLabel(tr("Select only changesets related to a specific bounding box")), gc);
135
136 gc.gridy = 7;
137 gc.gridx = 1;
138 gc.weightx = 1.0;
139 pnl.add(pnlBoundingBoxRestriction = new BBoxRestrictionPanel(), gc);
140
141
142 gc.gridy = 8;
143 gc.gridx = 0;
144 gc.gridwidth = 2;
145 gc.fill = GridBagConstraints.BOTH;
146 gc.weightx = 1.0;
147 gc.weighty = 1.0;
148 pnl.add(new JPanel(), gc);
149
150 return pnl;
151 }
152
153 protected final void build() {
154 setLayout(new BorderLayout());
155 JScrollPane spQueryPanel = GuiHelper.embedInVerticalScrollPane(buildQueryPanel());
156 add(spQueryPanel, BorderLayout.CENTER);
157 }
158
159 /**
160 * Constructs a new {@code AdvancedChangesetQueryPanel}.
161 */
162 public AdvancedChangesetQueryPanel() {
163 build();
164 }
165
166 public void startUserInput() {
167 restoreFromSettings();
168 pnlBoundingBoxRestriction.setVisible(cbBoundingBoxRestriction.isSelected());
169 pnlOpenAndCloseRestriction.setVisible(cbOpenAndCloseRestrictions.isSelected());
170 pnlTimeRestriction.setVisible(cbTimeRestrictions.isSelected());
171 pnlUserRestriction.setVisible(cbUserRestriction.isSelected());
172 pnlOpenAndCloseRestriction.startUserInput();
173 pnlUserRestriction.startUserInput();
174 pnlTimeRestriction.startUserInput();
175 }
176
177 public void displayMessageIfInvalid() {
178 if (cbUserRestriction.isSelected()) {
179 if (!pnlUserRestriction.isValidChangesetQuery()) {
180 pnlUserRestriction.displayMessageIfInvalid();
181 }
182 } else if (cbTimeRestrictions.isSelected()) {
183 if (!pnlTimeRestriction.isValidChangesetQuery()) {
184 pnlTimeRestriction.displayMessageIfInvalid();
185 }
186 } else if (cbBoundingBoxRestriction.isSelected()) {
187 if (!pnlBoundingBoxRestriction.isValidChangesetQuery()) {
188 pnlBoundingBoxRestriction.displayMessageIfInvalid();
189 }
190 }
191 }
192
193 /**
194 * Builds the changeset query based on the data entered in the form.
195 *
196 * @return the changeset query. null, if the data entered doesn't represent
197 * a valid changeset query.
198 */
199 public ChangesetQuery buildChangesetQuery() {
200 ChangesetQuery query = new ChangesetQuery();
201 if (cbUserRestriction.isSelected()) {
202 if (!pnlUserRestriction.isValidChangesetQuery())
203 return null;
204 pnlUserRestriction.fillInQuery(query);
205 }
206 if (cbOpenAndCloseRestrictions.isSelected()) {
207 // don't have to check whether it's valid. It always is.
208 pnlOpenAndCloseRestriction.fillInQuery(query);
209 }
210 if (cbBoundingBoxRestriction.isSelected()) {
211 if (!pnlBoundingBoxRestriction.isValidChangesetQuery())
212 return null;
213 pnlBoundingBoxRestriction.fillInQuery(query);
214 }
215 if (cbTimeRestrictions.isSelected()) {
216 if (!pnlTimeRestriction.isValidChangesetQuery())
217 return null;
218 pnlTimeRestriction.fillInQuery(query);
219 }
220 return query;
221 }
222
223 public void rememberSettings() {
224 Main.pref.put("changeset-query.advanced.user-restrictions", cbUserRestriction.isSelected());
225 Main.pref.put("changeset-query.advanced.open-restrictions", cbOpenAndCloseRestrictions.isSelected());
226 Main.pref.put("changeset-query.advanced.time-restrictions", cbTimeRestrictions.isSelected());
227 Main.pref.put("changeset-query.advanced.bbox-restrictions", cbBoundingBoxRestriction.isSelected());
228
229 pnlUserRestriction.rememberSettings();
230 pnlOpenAndCloseRestriction.rememberSettings();
231 pnlTimeRestriction.rememberSettings();
232 }
233
234 public void restoreFromSettings() {
235 cbUserRestriction.setSelected(Main.pref.getBoolean("changeset-query.advanced.user-restrictions", false));
236 cbOpenAndCloseRestrictions.setSelected(Main.pref.getBoolean("changeset-query.advanced.open-restrictions", false));
237 cbTimeRestrictions.setSelected(Main.pref.getBoolean("changeset-query.advanced.time-restrictions", false));
238 cbBoundingBoxRestriction.setSelected(Main.pref.getBoolean("changeset-query.advanced.bbox-restrictions", false));
239 }
240
241 class RestrictionGroupStateChangeHandler implements ItemListener {
242 protected void userRestrictionStateChanged() {
243 if (pnlUserRestriction == null) return;
244 pnlUserRestriction.setVisible(cbUserRestriction.isSelected());
245 }
246
247 protected void openCloseRestrictionStateChanged() {
248 if (pnlOpenAndCloseRestriction == null) return;
249 pnlOpenAndCloseRestriction.setVisible(cbOpenAndCloseRestrictions.isSelected());
250 }
251
252 protected void timeRestrictionsStateChanged() {
253 if (pnlTimeRestriction == null) return;
254 pnlTimeRestriction.setVisible(cbTimeRestrictions.isSelected());
255 }
256
257 protected void boundingBoxRestrictionChanged() {
258 if (pnlBoundingBoxRestriction == null) return;
259 pnlBoundingBoxRestriction.setVisible(cbBoundingBoxRestriction.isSelected());
260 }
261
262 @Override
263 public void itemStateChanged(ItemEvent e) {
264 if (e.getSource() == cbUserRestriction) {
265 userRestrictionStateChanged();
266 } else if (e.getSource() == cbOpenAndCloseRestrictions) {
267 openCloseRestrictionStateChanged();
268 } else if (e.getSource() == cbTimeRestrictions) {
269 timeRestrictionsStateChanged();
270 } else if (e.getSource() == cbBoundingBoxRestriction) {
271 boundingBoxRestrictionChanged();
272 }
273 validate();
274 repaint();
275 }
276 }
277
278 /**
279 * This is the panel for selecting whether the changeset query should be restricted to
280 * open or closed changesets
281 */
282 private static class OpenAndCloseStateRestrictionPanel extends JPanel {
283
284 private JRadioButton rbOpenOnly;
285 private JRadioButton rbClosedOnly;
286 private JRadioButton rbBoth;
287
288 protected void build() {
289 setLayout(new GridBagLayout());
290 setBorder(BorderFactory.createCompoundBorder(
291 BorderFactory.createEmptyBorder(3, 3, 3, 3),
292 BorderFactory.createCompoundBorder(
293 BorderFactory.createLineBorder(Color.GRAY),
294 BorderFactory.createEmptyBorder(5, 5, 5, 5)
295 )
296 ));
297 GridBagConstraints gc = new GridBagConstraints();
298 gc.anchor = GridBagConstraints.NORTHWEST;
299 gc.fill = GridBagConstraints.HORIZONTAL;
300 gc.weightx = 0.0;
301 add(rbOpenOnly = new JRadioButton(), gc);
302
303 gc.gridx = 1;
304 gc.weightx = 1.0;
305 add(new JMultilineLabel(tr("Query open changesets only")), gc);
306
307 gc.gridy = 1;
308 gc.gridx = 0;
309 gc.weightx = 0.0;
310 add(rbClosedOnly = new JRadioButton(), gc);
311
312 gc.gridx = 1;
313 gc.weightx = 1.0;
314 add(new JMultilineLabel(tr("Query closed changesets only")), gc);
315
316 gc.gridy = 2;
317 gc.gridx = 0;
318 gc.weightx = 0.0;
319 add(rbBoth = new JRadioButton(), gc);
320
321 gc.gridx = 1;
322 gc.weightx = 1.0;
323 add(new JMultilineLabel(tr("Query both open and closed changesets")), gc);
324
325 ButtonGroup bgRestrictions = new ButtonGroup();
326 bgRestrictions.add(rbBoth);
327 bgRestrictions.add(rbClosedOnly);
328 bgRestrictions.add(rbOpenOnly);
329 }
330
331 public OpenAndCloseStateRestrictionPanel() {
332 build();
333 }
334
335 public void startUserInput() {
336 restoreFromSettings();
337 }
338
339 public void fillInQuery(ChangesetQuery query) {
340 if (rbBoth.isSelected()) {
341 query.beingClosed(true);
342 query.beingOpen(true);
343 } else if (rbOpenOnly.isSelected()) {
344 query.beingOpen(true);
345 } else if (rbClosedOnly.isSelected()) {
346 query.beingClosed(true);
347 }
348 }
349
350 public void rememberSettings() {
351 String prefRoot = "changeset-query.advanced.open-restrictions";
352 if (rbBoth.isSelected()) {
353 Main.pref.put(prefRoot + ".query-type", "both");
354 } else if (rbOpenOnly.isSelected()) {
355 Main.pref.put(prefRoot + ".query-type", "open");
356 } else if (rbClosedOnly.isSelected()) {
357 Main.pref.put(prefRoot + ".query-type", "closed");
358 }
359 }
360
361 public void restoreFromSettings() {
362 String prefRoot = "changeset-query.advanced.open-restrictions";
363 String v = Main.pref.get(prefRoot + ".query-type", "open");
364 rbBoth.setSelected("both".equals(v));
365 rbOpenOnly.setSelected("open".equals(v));
366 rbClosedOnly.setSelected("closed".equals(v));
367 }
368 }
369
370 /**
371 * This is the panel for selecting whether the query should be restricted to a specific
372 * user
373 *
374 */
375 private static class UserRestrictionPanel extends JPanel {
376 private ButtonGroup bgUserRestrictions;
377 private JRadioButton rbRestrictToMyself;
378 private JRadioButton rbRestrictToUid;
379 private JRadioButton rbRestrictToUserName;
380 private JosmTextField tfUid;
381 private transient UidInputFieldValidator valUid;
382 private JosmTextField tfUserName;
383 private transient UserNameInputValidator valUserName;
384 private JMultilineLabel lblRestrictedToMyself;
385
386 protected JPanel buildUidInputPanel() {
387 JPanel pnl = new JPanel(new GridBagLayout());
388 GridBagConstraints gc = new GridBagConstraints();
389 gc.fill = GridBagConstraints.HORIZONTAL;
390 gc.weightx = 0.0;
391 gc.insets = new Insets(0, 0, 0, 3);
392 pnl.add(new JLabel(tr("User ID:")), gc);
393
394 gc.gridx = 1;
395 pnl.add(tfUid = new JosmTextField(10), gc);
396 SelectAllOnFocusGainedDecorator.decorate(tfUid);
397 valUid = UidInputFieldValidator.decorate(tfUid);
398
399 // grab remaining space
400 gc.gridx = 2;
401 gc.weightx = 1.0;
402 pnl.add(new JPanel(), gc);
403 return pnl;
404 }
405
406 protected JPanel buildUserNameInputPanel() {
407 JPanel pnl = new JPanel(new GridBagLayout());
408 GridBagConstraints gc = new GridBagConstraints();
409 gc.fill = GridBagConstraints.HORIZONTAL;
410 gc.weightx = 0.0;
411 gc.insets = new Insets(0, 0, 0, 3);
412 pnl.add(new JLabel(tr("User name:")), gc);
413
414 gc.gridx = 1;
415 pnl.add(tfUserName = new JosmTextField(10), gc);
416 SelectAllOnFocusGainedDecorator.decorate(tfUserName);
417 valUserName = UserNameInputValidator.decorate(tfUserName);
418
419 // grab remaining space
420 gc.gridx = 2;
421 gc.weightx = 1.0;
422 pnl.add(new JPanel(), gc);
423 return pnl;
424 }
425
426 protected void build() {
427 setLayout(new GridBagLayout());
428 setBorder(BorderFactory.createCompoundBorder(
429 BorderFactory.createEmptyBorder(3, 3, 3, 3),
430 BorderFactory.createCompoundBorder(
431 BorderFactory.createLineBorder(Color.GRAY),
432 BorderFactory.createEmptyBorder(5, 5, 5, 5)
433 )
434 ));
435
436 ItemListener userRestrictionChangeHandler = new UserRestrictionChangedHandler();
437 GridBagConstraints gc = new GridBagConstraints();
438 gc.anchor = GridBagConstraints.NORTHWEST;
439 gc.gridx = 0;
440 gc.fill = GridBagConstraints.HORIZONTAL;
441 gc.weightx = 0.0;
442 add(rbRestrictToMyself = new JRadioButton(), gc);
443 rbRestrictToMyself.addItemListener(userRestrictionChangeHandler);
444
445 gc.gridx = 1;
446 gc.fill = GridBagConstraints.HORIZONTAL;
447 gc.weightx = 1.0;
448 add(lblRestrictedToMyself = new JMultilineLabel(tr("Only changesets owned by myself")), gc);
449
450 gc.gridx = 0;
451 gc.gridy = 1;
452 gc.fill = GridBagConstraints.HORIZONTAL;
453 gc.weightx = 0.0;
454 add(rbRestrictToUid = new JRadioButton(), gc);
455 rbRestrictToUid.addItemListener(userRestrictionChangeHandler);
456
457 gc.gridx = 1;
458 gc.fill = GridBagConstraints.HORIZONTAL;
459 gc.weightx = 1.0;
460 add(new JMultilineLabel(tr("Only changesets owned by the user with the following user ID")), gc);
461
462 gc.gridx = 1;
463 gc.gridy = 2;
464 gc.fill = GridBagConstraints.HORIZONTAL;
465 gc.weightx = 1.0;
466 add(buildUidInputPanel(), gc);
467
468 gc.gridx = 0;
469 gc.gridy = 3;
470 gc.fill = GridBagConstraints.HORIZONTAL;
471 gc.weightx = 0.0;
472 add(rbRestrictToUserName = new JRadioButton(), gc);
473 rbRestrictToUserName.addItemListener(userRestrictionChangeHandler);
474
475 gc.gridx = 1;
476 gc.fill = GridBagConstraints.HORIZONTAL;
477 gc.weightx = 1.0;
478 add(new JMultilineLabel(tr("Only changesets owned by the user with the following user name")), gc);
479
480 gc.gridx = 1;
481 gc.gridy = 4;
482 gc.fill = GridBagConstraints.HORIZONTAL;
483 gc.weightx = 1.0;
484 add(buildUserNameInputPanel(), gc);
485
486 bgUserRestrictions = new ButtonGroup();
487 bgUserRestrictions.add(rbRestrictToMyself);
488 bgUserRestrictions.add(rbRestrictToUid);
489 bgUserRestrictions.add(rbRestrictToUserName);
490 }
491
492 public UserRestrictionPanel() {
493 build();
494 }
495
496 public void startUserInput() {
497 if (JosmUserIdentityManager.getInstance().isAnonymous()) {
498 lblRestrictedToMyself.setText(tr("Only changesets owned by myself (disabled. JOSM is currently run by an anonymous user)"));
499 rbRestrictToMyself.setEnabled(false);
500 if (rbRestrictToMyself.isSelected()) {
501 rbRestrictToUid.setSelected(true);
502 }
503 } else {
504 lblRestrictedToMyself.setText(tr("Only changesets owned by myself"));
505 rbRestrictToMyself.setEnabled(true);
506 rbRestrictToMyself.setSelected(true);
507 }
508 restoreFromSettings();
509 }
510
511 /**
512 * Sets the query restrictions on <code>query</code> for changeset owner based
513 * restrictions.
514 *
515 * @param query the query. Must not be null.
516 * @throws IllegalArgumentException if query is null
517 * @throws IllegalStateException if one of the available values for query parameters in
518 * this panel isn't valid
519 */
520 public void fillInQuery(ChangesetQuery query) {
521 CheckParameterUtil.ensureParameterNotNull(query, "query");
522 if (rbRestrictToMyself.isSelected()) {
523 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
524 if (im.isPartiallyIdentified()) {
525 query.forUser(im.getUserName());
526 } else if (im.isFullyIdentified()) {
527 query.forUser(im.getUserId());
528 } else
529 throw new IllegalStateException(tr("Cannot restrict changeset query to the current user because the current user is anonymous"));
530 } else if (rbRestrictToUid.isSelected()) {
531 int uid = valUid.getUid();
532 if (uid > 0) {
533 query.forUser(uid);
534 } else
535 throw new IllegalStateException(tr("Current value ''{0}'' for user ID is not valid", tfUid.getText()));
536 } else if (rbRestrictToUserName.isSelected()) {
537 if (!valUserName.isValid())
538 throw new IllegalStateException(tr("Cannot restrict the changeset query to the user name ''{0}''", tfUserName.getText()));
539 query.forUser(tfUserName.getText());
540 }
541 }
542
543 public boolean isValidChangesetQuery() {
544 if (rbRestrictToUid.isSelected())
545 return valUid.isValid();
546 else if (rbRestrictToUserName.isSelected())
547 return valUserName.isValid();
548 return true;
549 }
550
551 protected void alertInvalidUid() {
552 HelpAwareOptionPane.showOptionDialog(
553 this,
554 tr("Please enter a valid user ID"),
555 tr("Invalid user ID"),
556 JOptionPane.ERROR_MESSAGE,
557 HelpUtil.ht("/Dialog/ChangesetQueryDialog#InvalidUserId")
558 );
559 }
560
561 protected void alertInvalidUserName() {
562 HelpAwareOptionPane.showOptionDialog(
563 this,
564 tr("Please enter a non-empty user name"),
565 tr("Invalid user name"),
566 JOptionPane.ERROR_MESSAGE,
567 HelpUtil.ht("/Dialog/ChangesetQueryDialog#InvalidUserName")
568 );
569 }
570
571 public void displayMessageIfInvalid() {
572 if (rbRestrictToUid.isSelected()) {
573 if (!valUid.isValid()) {
574 alertInvalidUid();
575 }
576 } else if (rbRestrictToUserName.isSelected()) {
577 if (!valUserName.isValid()) {
578 alertInvalidUserName();
579 }
580 }
581 }
582
583 public void rememberSettings() {
584 String prefRoot = "changeset-query.advanced.user-restrictions";
585 if (rbRestrictToMyself.isSelected()) {
586 Main.pref.put(prefRoot + ".query-type", "mine");
587 } else if (rbRestrictToUid.isSelected()) {
588 Main.pref.put(prefRoot + ".query-type", "uid");
589 } else if (rbRestrictToUserName.isSelected()) {
590 Main.pref.put(prefRoot + ".query-type", "username");
591 }
592 Main.pref.put(prefRoot + ".uid", tfUid.getText());
593 Main.pref.put(prefRoot + ".username", tfUserName.getText());
594 }
595
596 public void restoreFromSettings() {
597 String prefRoot = "changeset-query.advanced.user-restrictions";
598 String v = Main.pref.get(prefRoot + ".query-type", "mine");
599 if ("mine".equals(v)) {
600 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
601 if (im.isAnonymous()) {
602 rbRestrictToUid.setSelected(true);
603 } else {
604 rbRestrictToMyself.setSelected(true);
605 }
606 } else if ("uid".equals(v)) {
607 rbRestrictToUid.setSelected(true);
608 } else if ("username".equals(v)) {
609 rbRestrictToUserName.setSelected(true);
610 }
611 tfUid.setText(Main.pref.get(prefRoot + ".uid", ""));
612 if (!valUid.isValid()) {
613 tfUid.setText("");
614 }
615 tfUserName.setText(Main.pref.get(prefRoot + ".username", ""));
616 }
617
618 class UserRestrictionChangedHandler implements ItemListener {
619 @Override
620 public void itemStateChanged(ItemEvent e) {
621 tfUid.setEnabled(rbRestrictToUid.isSelected());
622 tfUserName.setEnabled(rbRestrictToUserName.isSelected());
623 if (rbRestrictToUid.isSelected()) {
624 tfUid.requestFocusInWindow();
625 } else if (rbRestrictToUserName.isSelected()) {
626 tfUserName.requestFocusInWindow();
627 }
628 }
629 }
630 }
631
632 /**
633 * This is the panel to apply a time restriction to the changeset query
634 */
635 private static class TimeRestrictionPanel extends JPanel {
636
637 private JRadioButton rbClosedAfter;
638 private JRadioButton rbClosedAfterAndCreatedBefore;
639 private JosmTextField tfClosedAfterDate1;
640 private transient DateValidator valClosedAfterDate1;
641 private JosmTextField tfClosedAfterTime1;
642 private transient TimeValidator valClosedAfterTime1;
643 private JosmTextField tfClosedAfterDate2;
644 private transient DateValidator valClosedAfterDate2;
645 private JosmTextField tfClosedAfterTime2;
646 private transient TimeValidator valClosedAfterTime2;
647 private JosmTextField tfCreatedBeforeDate;
648 private transient DateValidator valCreatedBeforeDate;
649 private JosmTextField tfCreatedBeforeTime;
650 private transient TimeValidator valCreatedBeforeTime;
651
652 protected JPanel buildClosedAfterInputPanel() {
653 JPanel pnl = new JPanel(new GridBagLayout());
654 GridBagConstraints gc = new GridBagConstraints();
655 gc.fill = GridBagConstraints.HORIZONTAL;
656 gc.weightx = 0.0;
657 gc.insets = new Insets(0, 0, 0, 3);
658 pnl.add(new JLabel(tr("Date: ")), gc);
659
660 gc.gridx = 1;
661 gc.weightx = 0.7;
662 pnl.add(tfClosedAfterDate1 = new JosmTextField(), gc);
663 SelectAllOnFocusGainedDecorator.decorate(tfClosedAfterDate1);
664 valClosedAfterDate1 = DateValidator.decorate(tfClosedAfterDate1);
665 tfClosedAfterDate1.setToolTipText(valClosedAfterDate1.getStandardTooltipTextAsHtml());
666
667 gc.gridx = 2;
668 gc.weightx = 0.0;
669 pnl.add(new JLabel(tr("Time:")), gc);
670
671 gc.gridx = 3;
672 gc.weightx = 0.3;
673 pnl.add(tfClosedAfterTime1 = new JosmTextField(), gc);
674 SelectAllOnFocusGainedDecorator.decorate(tfClosedAfterTime1);
675 valClosedAfterTime1 = TimeValidator.decorate(tfClosedAfterTime1);
676 tfClosedAfterTime1.setToolTipText(valClosedAfterTime1.getStandardTooltipTextAsHtml());
677 return pnl;
678 }
679
680 protected JPanel buildClosedAfterAndCreatedBeforeInputPanel() {
681 JPanel pnl = new JPanel(new GridBagLayout());
682 GridBagConstraints gc = new GridBagConstraints();
683 gc.fill = GridBagConstraints.HORIZONTAL;
684 gc.weightx = 0.0;
685 gc.insets = new Insets(0, 0, 0, 3);
686 pnl.add(new JLabel(tr("Closed after - ")), gc);
687
688 gc.gridx = 1;
689 gc.fill = GridBagConstraints.HORIZONTAL;
690 gc.weightx = 0.0;
691 gc.insets = new Insets(0, 0, 0, 3);
692 pnl.add(new JLabel(tr("Date:")), gc);
693
694 gc.gridx = 2;
695 gc.weightx = 0.7;
696 pnl.add(tfClosedAfterDate2 = new JosmTextField(), gc);
697 SelectAllOnFocusGainedDecorator.decorate(tfClosedAfterDate2);
698 valClosedAfterDate2 = DateValidator.decorate(tfClosedAfterDate2);
699 tfClosedAfterDate2.setToolTipText(valClosedAfterDate2.getStandardTooltipTextAsHtml());
700 gc.gridx = 3;
701 gc.weightx = 0.0;
702 pnl.add(new JLabel(tr("Time:")), gc);
703
704 gc.gridx = 4;
705 gc.weightx = 0.3;
706 pnl.add(tfClosedAfterTime2 = new JosmTextField(), gc);
707 SelectAllOnFocusGainedDecorator.decorate(tfClosedAfterTime2);
708 valClosedAfterTime2 = TimeValidator.decorate(tfClosedAfterTime2);
709 tfClosedAfterTime2.setToolTipText(valClosedAfterTime2.getStandardTooltipTextAsHtml());
710
711 gc.gridy = 1;
712 gc.gridx = 0;
713 gc.fill = GridBagConstraints.HORIZONTAL;
714 gc.weightx = 0.0;
715 gc.insets = new Insets(0, 0, 0, 3);
716 pnl.add(new JLabel(tr("Created before - ")), gc);
717
718 gc.gridx = 1;
719 gc.fill = GridBagConstraints.HORIZONTAL;
720 gc.weightx = 0.0;
721 gc.insets = new Insets(0, 0, 0, 3);
722 pnl.add(new JLabel(tr("Date:")), gc);
723
724 gc.gridx = 2;
725 gc.weightx = 0.7;
726 pnl.add(tfCreatedBeforeDate = new JosmTextField(), gc);
727 SelectAllOnFocusGainedDecorator.decorate(tfCreatedBeforeDate);
728 valCreatedBeforeDate = DateValidator.decorate(tfCreatedBeforeDate);
729 tfCreatedBeforeDate.setToolTipText(valCreatedBeforeDate.getStandardTooltipTextAsHtml());
730
731 gc.gridx = 3;
732 gc.weightx = 0.0;
733 pnl.add(new JLabel(tr("Time:")), gc);
734
735 gc.gridx = 4;
736 gc.weightx = 0.3;
737 pnl.add(tfCreatedBeforeTime = new JosmTextField(), gc);
738 SelectAllOnFocusGainedDecorator.decorate(tfCreatedBeforeTime);
739 valCreatedBeforeTime = TimeValidator.decorate(tfCreatedBeforeTime);
740 tfCreatedBeforeTime.setToolTipText(valCreatedBeforeDate.getStandardTooltipTextAsHtml());
741
742 return pnl;
743 }
744
745 protected void build() {
746 setLayout(new GridBagLayout());
747 setBorder(BorderFactory.createCompoundBorder(
748 BorderFactory.createEmptyBorder(3, 3, 3, 3),
749 BorderFactory.createCompoundBorder(
750 BorderFactory.createLineBorder(Color.GRAY),
751 BorderFactory.createEmptyBorder(5, 5, 5, 5)
752 )
753 ));
754
755 // -- changesets closed after a specific date/time
756 //
757 GridBagConstraints gc = new GridBagConstraints();
758 gc.anchor = GridBagConstraints.NORTHWEST;
759 gc.gridx = 0;
760 gc.fill = GridBagConstraints.HORIZONTAL;
761 gc.weightx = 0.0;
762 add(rbClosedAfter = new JRadioButton(), gc);
763
764 gc.gridx = 1;
765 gc.fill = GridBagConstraints.HORIZONTAL;
766 gc.weightx = 1.0;
767 add(new JMultilineLabel(tr("Only changesets closed after the following date/time")), gc);
768
769 gc.gridx = 1;
770 gc.gridy = 1;
771 gc.fill = GridBagConstraints.HORIZONTAL;
772 gc.weightx = 1.0;
773 add(buildClosedAfterInputPanel(), gc);
774
775 // -- changesets closed after a specific date/time and created before a specific date time
776 //
777 gc = new GridBagConstraints();
778 gc.anchor = GridBagConstraints.NORTHWEST;
779 gc.gridy = 2;
780 gc.gridx = 0;
781 gc.fill = GridBagConstraints.HORIZONTAL;
782 gc.weightx = 0.0;
783 add(rbClosedAfterAndCreatedBefore = new JRadioButton(), gc);
784
785 gc.gridx = 1;
786 gc.fill = GridBagConstraints.HORIZONTAL;
787 gc.weightx = 1.0;
788 add(new JMultilineLabel(tr("Only changesets closed after and created before a specific date/time")), gc);
789
790 gc.gridx = 1;
791 gc.gridy = 3;
792 gc.fill = GridBagConstraints.HORIZONTAL;
793 gc.weightx = 1.0;
794 add(buildClosedAfterAndCreatedBeforeInputPanel(), gc);
795
796 ButtonGroup bg = new ButtonGroup();
797 bg.add(rbClosedAfter);
798 bg.add(rbClosedAfterAndCreatedBefore);
799
800 ItemListener restrictionChangeHandler = new TimeRestrictionChangedHandler();
801 rbClosedAfter.addItemListener(restrictionChangeHandler);
802 rbClosedAfterAndCreatedBefore.addItemListener(restrictionChangeHandler);
803
804 rbClosedAfter.setSelected(true);
805 }
806
807 public TimeRestrictionPanel() {
808 build();
809 }
810
811 public boolean isValidChangesetQuery() {
812 if (rbClosedAfter.isSelected())
813 return valClosedAfterDate1.isValid() && valClosedAfterTime1.isValid();
814 else if (rbClosedAfterAndCreatedBefore.isSelected())
815 return valClosedAfterDate2.isValid() && valClosedAfterTime2.isValid()
816 && valCreatedBeforeDate.isValid() && valCreatedBeforeTime.isValid();
817 // should not happen
818 return true;
819 }
820
821 class TimeRestrictionChangedHandler implements ItemListener {
822 @Override
823 public void itemStateChanged(ItemEvent e) {
824 tfClosedAfterDate1.setEnabled(rbClosedAfter.isSelected());
825 tfClosedAfterTime1.setEnabled(rbClosedAfter.isSelected());
826
827 tfClosedAfterDate2.setEnabled(rbClosedAfterAndCreatedBefore.isSelected());
828 tfClosedAfterTime2.setEnabled(rbClosedAfterAndCreatedBefore.isSelected());
829 tfCreatedBeforeDate.setEnabled(rbClosedAfterAndCreatedBefore.isSelected());
830 tfCreatedBeforeTime.setEnabled(rbClosedAfterAndCreatedBefore.isSelected());
831 }
832 }
833
834 public void startUserInput() {
835 restoreFromSettings();
836 }
837
838 public void fillInQuery(ChangesetQuery query) {
839 if (!isValidChangesetQuery())
840 throw new IllegalStateException(tr("Cannot build changeset query with time based restrictions. Input is not valid."));
841 if (rbClosedAfter.isSelected()) {
842 GregorianCalendar cal = new GregorianCalendar();
843 Date d1 = valClosedAfterDate1.getDate();
844 Date d2 = valClosedAfterTime1.getDate();
845 cal.setTimeInMillis(d1.getTime() + (d2 == null ? 0 : d2.getTime()));
846 query.closedAfter(cal.getTime());
847 } else if (rbClosedAfterAndCreatedBefore.isSelected()) {
848 GregorianCalendar cal = new GregorianCalendar();
849 Date d1 = valClosedAfterDate2.getDate();
850 Date d2 = valClosedAfterTime2.getDate();
851 cal.setTimeInMillis(d1.getTime() + (d2 == null ? 0 : d2.getTime()));
852 Date d3 = cal.getTime();
853
854 d1 = valCreatedBeforeDate.getDate();
855 d2 = valCreatedBeforeTime.getDate();
856 cal.setTimeInMillis(d1.getTime() + (d2 == null ? 0 : d2.getTime()));
857 Date d4 = cal.getTime();
858
859 query.closedAfterAndCreatedBefore(d3, d4);
860 }
861 }
862
863 public void displayMessageIfInvalid() {
864 if (isValidChangesetQuery()) return;
865 HelpAwareOptionPane.showOptionDialog(
866 this,
867 tr(
868 "<html>Please enter valid date/time values to restrict<br>"
869 + "the query to a specific time range.</html>"
870 ),
871 tr("Invalid date/time values"),
872 JOptionPane.ERROR_MESSAGE,
873 HelpUtil.ht("/Dialog/ChangesetQueryDialog#InvalidDateTimeValues")
874 );
875 }
876
877 public void rememberSettings() {
878 String prefRoot = "changeset-query.advanced.time-restrictions";
879 if (rbClosedAfter.isSelected()) {
880 Main.pref.put(prefRoot + ".query-type", "closed-after");
881 } else if (rbClosedAfterAndCreatedBefore.isSelected()) {
882 Main.pref.put(prefRoot + ".query-type", "closed-after-created-before");
883 }
884 Main.pref.put(prefRoot + ".closed-after.date", tfClosedAfterDate1.getText());
885 Main.pref.put(prefRoot + ".closed-after.time", tfClosedAfterTime1.getText());
886 Main.pref.put(prefRoot + ".closed-created.closed.date", tfClosedAfterDate2.getText());
887 Main.pref.put(prefRoot + ".closed-created.closed.time", tfClosedAfterTime2.getText());
888 Main.pref.put(prefRoot + ".closed-created.created.date", tfCreatedBeforeDate.getText());
889 Main.pref.put(prefRoot + ".closed-created.created.time", tfCreatedBeforeTime.getText());
890 }
891
892 public void restoreFromSettings() {
893 String prefRoot = "changeset-query.advanced.open-restrictions";
894 String v = Main.pref.get(prefRoot + ".query-type", "closed-after");
895 rbClosedAfter.setSelected("closed-after".equals(v));
896 rbClosedAfterAndCreatedBefore.setSelected("closed-after-created-before".equals(v));
897 if (!rbClosedAfter.isSelected() && !rbClosedAfterAndCreatedBefore.isSelected()) {
898 rbClosedAfter.setSelected(true);
899 }
900 tfClosedAfterDate1.setText(Main.pref.get(prefRoot + ".closed-after.date", ""));
901 tfClosedAfterTime1.setText(Main.pref.get(prefRoot + ".closed-after.time", ""));
902 tfClosedAfterDate2.setText(Main.pref.get(prefRoot + ".closed-created.closed.date", ""));
903 tfClosedAfterTime2.setText(Main.pref.get(prefRoot + ".closed-created.closed.time", ""));
904 tfCreatedBeforeDate.setText(Main.pref.get(prefRoot + ".closed-created.created.date", ""));
905 tfCreatedBeforeTime.setText(Main.pref.get(prefRoot + ".closed-created.created.time", ""));
906 if (!valClosedAfterDate1.isValid()) {
907 tfClosedAfterDate1.setText("");
908 }
909 if (!valClosedAfterTime1.isValid()) {
910 tfClosedAfterTime1.setText("");
911 }
912 if (!valClosedAfterDate2.isValid()) {
913 tfClosedAfterDate2.setText("");
914 }
915 if (!valClosedAfterTime2.isValid()) {
916 tfClosedAfterTime2.setText("");
917 }
918 if (!valCreatedBeforeDate.isValid()) {
919 tfCreatedBeforeDate.setText("");
920 }
921 if (!valCreatedBeforeTime.isValid()) {
922 tfCreatedBeforeTime.setText("");
923 }
924 }
925 }
926
927 private static class BBoxRestrictionPanel extends BoundingBoxSelectionPanel {
928 public BBoxRestrictionPanel() {
929 setBorder(BorderFactory.createCompoundBorder(
930 BorderFactory.createEmptyBorder(3, 3, 3, 3),
931 BorderFactory.createCompoundBorder(
932 BorderFactory.createLineBorder(Color.GRAY),
933 BorderFactory.createEmptyBorder(5, 5, 5, 5)
934 )
935 ));
936 }
937
938 public boolean isValidChangesetQuery() {
939 return getBoundingBox() != null;
940 }
941
942 public void fillInQuery(ChangesetQuery query) {
943 if (!isValidChangesetQuery())
944 throw new IllegalStateException(tr("Cannot restrict the changeset query to a specific bounding box. The input is invalid."));
945 query.inBbox(getBoundingBox());
946 }
947
948 public void displayMessageIfInvalid() {
949 if (isValidChangesetQuery()) return;
950 HelpAwareOptionPane.showOptionDialog(
951 this,
952 tr(
953 "<html>Please enter valid longitude/latitude values to restrict<br>" +
954 "the changeset query to a specific bounding box.</html>"
955 ),
956 tr("Invalid bounding box"),
957 JOptionPane.ERROR_MESSAGE,
958 HelpUtil.ht("/Dialog/ChangesetQueryDialog#InvalidBoundingBox")
959 );
960 }
961 }
962
963 /**
964 * Validator for user ids entered in a {@link JTextComponent}.
965 *
966 */
967 private static class UidInputFieldValidator extends AbstractTextComponentValidator {
968 public static UidInputFieldValidator decorate(JTextComponent tc) {
969 return new UidInputFieldValidator(tc);
970 }
971
972 public UidInputFieldValidator(JTextComponent tc) {
973 super(tc);
974 }
975
976 @Override
977 public boolean isValid() {
978 return getUid() > 0;
979 }
980
981 @Override
982 public void validate() {
983 String value = getComponent().getText();
984 if (value == null || value.trim().isEmpty()) {
985 feedbackInvalid("");
986 return;
987 }
988 try {
989 int uid = Integer.parseInt(value);
990 if (uid <= 0) {
991 feedbackInvalid(tr("The current value is not a valid user ID. Please enter an integer value > 0"));
992 return;
993 }
994 } catch (NumberFormatException e) {
995 feedbackInvalid(tr("The current value is not a valid user ID. Please enter an integer value > 0"));
996 return;
997 }
998 feedbackValid(tr("Please enter an integer value > 0"));
999 }
1000
1001 public int getUid() {
1002 String value = getComponent().getText();
1003 if (value == null || value.trim().isEmpty()) return 0;
1004 try {
1005 int uid = Integer.parseInt(value.trim());
1006 if (uid > 0) return uid;
1007 return 0;
1008 } catch (NumberFormatException e) {
1009 return 0;
1010 }
1011 }
1012 }
1013
1014 private static class UserNameInputValidator extends AbstractTextComponentValidator {
1015 public static UserNameInputValidator decorate(JTextComponent tc) {
1016 return new UserNameInputValidator(tc);
1017 }
1018
1019 public UserNameInputValidator(JTextComponent tc) {
1020 super(tc);
1021 }
1022
1023 @Override
1024 public boolean isValid() {
1025 return !getComponent().getText().trim().isEmpty();
1026 }
1027
1028 @Override
1029 public void validate() {
1030 String value = getComponent().getText();
1031 if (value.trim().isEmpty()) {
1032 feedbackInvalid(tr("<html>The current value is not a valid user name.<br>Please enter an non-empty user name.</html>"));
1033 return;
1034 }
1035 feedbackValid(tr("Please enter an non-empty user name"));
1036 }
1037 }
1038
1039 /**
1040 * Validates dates entered as text in a {@link JTextComponent}. Validates the input
1041 * on the fly and gives feedback about whether the date is valid or not.
1042 *
1043 * Dates can be entered in one of four standard formats defined for the current locale.
1044 */
1045 private static class DateValidator extends AbstractTextComponentValidator {
1046 public static DateValidator decorate(JTextComponent tc) {
1047 return new DateValidator(tc);
1048 }
1049
1050 public DateValidator(JTextComponent tc) {
1051 super(tc);
1052 }
1053
1054 @Override
1055 public boolean isValid() {
1056 return getDate() != null;
1057 }
1058
1059 public String getStandardTooltipTextAsHtml() {
1060 return "<html>" + getStandardTooltipText() + "</html>";
1061 }
1062
1063 public String getStandardTooltipText() {
1064 Date date = new Date();
1065 return tr(
1066 "Please enter a date in the usual format for your locale.<br>"
1067 + "Example: {0}<br>"
1068 + "Example: {1}<br>"
1069 + "Example: {2}<br>"
1070 + "Example: {3}<br>",
1071 DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()).format(date),
1072 DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault()).format(date),
1073 DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault()).format(date),
1074 DateFormat.getDateInstance(DateFormat.FULL, Locale.getDefault()).format(date)
1075 );
1076 }
1077
1078 @Override
1079 public void validate() {
1080 if (!isValid()) {
1081 String msg = "<html>The current value isn't a valid date.<br>" + getStandardTooltipText()+ "</html>";
1082 feedbackInvalid(msg);
1083 return;
1084 } else {
1085 String msg = "<html>" + getStandardTooltipText() + "</html>";
1086 feedbackValid(msg);
1087 }
1088 }
1089
1090 public Date getDate() {
1091 for (int format: new int[] {DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL}) {
1092 DateFormat df = DateFormat.getDateInstance(format);
1093 try {
1094 return df.parse(getComponent().getText());
1095 } catch (ParseException e) {
1096 // Try next format
1097 if (Main.isTraceEnabled()) {
1098 Main.trace(e.getMessage());
1099 }
1100 }
1101 }
1102 return null;
1103 }
1104 }
1105
1106 /**
1107 * Validates time values entered as text in a {@link JTextComponent}. Validates the input
1108 * on the fly and gives feedback about whether the time value is valid or not.
1109 *
1110 * Time values can be entered in one of four standard formats defined for the current locale.
1111 */
1112 private static class TimeValidator extends AbstractTextComponentValidator {
1113 public static TimeValidator decorate(JTextComponent tc) {
1114 return new TimeValidator(tc);
1115 }
1116
1117 public TimeValidator(JTextComponent tc) {
1118 super(tc);
1119 }
1120
1121 @Override
1122 public boolean isValid() {
1123 if (getComponent().getText().trim().isEmpty()) return true;
1124 return getDate() != null;
1125 }
1126
1127 public String getStandardTooltipTextAsHtml() {
1128 return "<html>" + getStandardTooltipText() + "</html>";
1129 }
1130
1131 public String getStandardTooltipText() {
1132 Date date = new Date();
1133 return tr(
1134 "Please enter a valid time in the usual format for your locale.<br>"
1135 + "Example: {0}<br>"
1136 + "Example: {1}<br>"
1137 + "Example: {2}<br>"
1138 + "Example: {3}<br>",
1139 DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault()).format(date),
1140 DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.getDefault()).format(date),
1141 DateFormat.getTimeInstance(DateFormat.LONG, Locale.getDefault()).format(date),
1142 DateFormat.getTimeInstance(DateFormat.FULL, Locale.getDefault()).format(date)
1143 );
1144 }
1145
1146 @Override
1147 public void validate() {
1148
1149 if (!isValid()) {
1150 String msg = "<html>The current value isn't a valid time.<br>" + getStandardTooltipText() + "</html>";
1151 feedbackInvalid(msg);
1152 return;
1153 } else {
1154 String msg = "<html>" + getStandardTooltipText() + "</html>";
1155 feedbackValid(msg);
1156 }
1157 }
1158
1159 public Date getDate() {
1160 if (getComponent().getText().trim().isEmpty())
1161 return null;
1162
1163 for (int style : new int[]{DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL}) {
1164 try {
1165 return DateFormat.getTimeInstance(style, Locale.getDefault()).parse(getComponent().getText());
1166 } catch (ParseException e) {
1167 continue;
1168 }
1169 }
1170 return null;
1171 }
1172 }
1173}
Note: See TracBrowser for help on using the repository browser.