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

Last change on this file was 16075, checked in by simon04, 6 years ago

fix #18885 - AdvancedChangesetQueryPanel: select checkbox/radio-button when clicking its label

  • Property svn:eol-style set to native
File size: 8.3 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.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.event.ItemEvent;
10import java.awt.event.ItemListener;
11
12import javax.swing.BorderFactory;
13import javax.swing.JCheckBox;
14import javax.swing.JPanel;
15import javax.swing.JScrollPane;
16
17import org.openstreetmap.josm.gui.util.GuiHelper;
18import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
19import org.openstreetmap.josm.io.ChangesetQuery;
20import org.openstreetmap.josm.spi.preferences.Config;
21import org.openstreetmap.josm.tools.GBC;
22
23/**
24 * This panel allows to specify a changeset query
25 * @since 2689
26 */
27public class AdvancedChangesetQueryPanel extends JPanel {
28
29 private final JCheckBox cbUserRestriction = new JCheckBox(tr("Select changesets owned by specific users"));
30 private final JCheckBox cbOpenAndCloseRestrictions = new JCheckBox(tr("Select changesets depending on whether they are open or closed"));
31 private final JCheckBox cbTimeRestrictions = new JCheckBox(tr("Select changesets based on the date/time they have been created or closed"));
32 private final JCheckBox cbBoundingBoxRestriction = new JCheckBox(tr("Select only changesets related to a specific bounding box"));
33 private final UserRestrictionPanel pnlUserRestriction = new UserRestrictionPanel();
34 private final OpenAndCloseStateRestrictionPanel pnlOpenAndCloseRestriction = new OpenAndCloseStateRestrictionPanel();
35 private final TimeRestrictionPanel pnlTimeRestriction = new TimeRestrictionPanel();
36 private final BBoxRestrictionPanel pnlBoundingBoxRestriction = new BBoxRestrictionPanel();
37
38 /**
39 * Constructs a new {@code AdvancedChangesetQueryPanel}.
40 */
41 public AdvancedChangesetQueryPanel() {
42 build();
43 }
44
45 protected JPanel buildQueryPanel() {
46 ItemListener stateChangeHandler = new RestrictionGroupStateChangeHandler();
47 JPanel pnl = new VerticallyScrollablePanel(new GridBagLayout());
48 pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
49 GridBagConstraints gc = GBC.eol().fill(GridBagConstraints.HORIZONTAL);
50
51 // -- select changesets by a specific user
52 //
53 pnl.add(cbUserRestriction, gc);
54 cbUserRestriction.addItemListener(stateChangeHandler);
55 pnl.add(pnlUserRestriction, gc);
56
57 // -- restricting the query to open and closed changesets
58 //
59 pnl.add(cbOpenAndCloseRestrictions, gc);
60 cbOpenAndCloseRestrictions.addItemListener(stateChangeHandler);
61 pnl.add(pnlOpenAndCloseRestriction, gc);
62
63 // -- restricting the query to a specific time
64 //
65 pnl.add(cbTimeRestrictions, gc);
66 cbTimeRestrictions.addItemListener(stateChangeHandler);
67 pnl.add(pnlTimeRestriction, gc);
68
69
70 // -- restricting the query to a specific bounding box
71 //
72 pnl.add(cbBoundingBoxRestriction, gc);
73 cbBoundingBoxRestriction.addItemListener(stateChangeHandler);
74 pnl.add(pnlBoundingBoxRestriction, gc);
75
76 pnl.add(new JPanel(), gc);
77
78 return pnl;
79 }
80
81 protected final void build() {
82 setLayout(new BorderLayout());
83 JScrollPane spQueryPanel = GuiHelper.embedInVerticalScrollPane(buildQueryPanel());
84 add(spQueryPanel, BorderLayout.CENTER);
85 }
86
87 /**
88 * Initializes HMI for user input.
89 */
90 public void startUserInput() {
91 restoreFromSettings();
92 pnlBoundingBoxRestriction.setVisible(cbBoundingBoxRestriction.isSelected());
93 pnlOpenAndCloseRestriction.setVisible(cbOpenAndCloseRestrictions.isSelected());
94 pnlTimeRestriction.setVisible(cbTimeRestrictions.isSelected());
95 pnlUserRestriction.setVisible(cbUserRestriction.isSelected());
96 pnlOpenAndCloseRestriction.startUserInput();
97 pnlUserRestriction.startUserInput();
98 pnlTimeRestriction.startUserInput();
99 }
100
101 /**
102 * Display error message if a field is invalid.
103 */
104 public void displayMessageIfInvalid() {
105 if (cbUserRestriction.isSelected()) {
106 if (!pnlUserRestriction.isValidChangesetQuery()) {
107 pnlUserRestriction.displayMessageIfInvalid();
108 }
109 } else if (cbTimeRestrictions.isSelected()) {
110 if (!pnlTimeRestriction.isValidChangesetQuery()) {
111 pnlTimeRestriction.displayMessageIfInvalid();
112 }
113 } else if (cbBoundingBoxRestriction.isSelected()) {
114 if (!pnlBoundingBoxRestriction.isValidChangesetQuery()) {
115 pnlBoundingBoxRestriction.displayMessageIfInvalid();
116 }
117 }
118 }
119
120 /**
121 * Builds the changeset query based on the data entered in the form.
122 *
123 * @return the changeset query. null, if the data entered doesn't represent
124 * a valid changeset query.
125 */
126 public ChangesetQuery buildChangesetQuery() {
127 ChangesetQuery query = new ChangesetQuery();
128 if (cbUserRestriction.isSelected()) {
129 if (!pnlUserRestriction.isValidChangesetQuery())
130 return null;
131 pnlUserRestriction.fillInQuery(query);
132 }
133 if (cbOpenAndCloseRestrictions.isSelected()) {
134 // don't have to check whether it's valid. It always is.
135 pnlOpenAndCloseRestriction.fillInQuery(query);
136 }
137 if (cbBoundingBoxRestriction.isSelected()) {
138 if (!pnlBoundingBoxRestriction.isValidChangesetQuery())
139 return null;
140 pnlBoundingBoxRestriction.fillInQuery(query);
141 }
142 if (cbTimeRestrictions.isSelected()) {
143 if (!pnlTimeRestriction.isValidChangesetQuery())
144 return null;
145 pnlTimeRestriction.fillInQuery(query);
146 }
147 return query;
148 }
149
150 /**
151 * Remember settings in preferences.
152 */
153 public void rememberSettings() {
154 Config.getPref().putBoolean("changeset-query.advanced.user-restrictions", cbUserRestriction.isSelected());
155 Config.getPref().putBoolean("changeset-query.advanced.open-restrictions", cbOpenAndCloseRestrictions.isSelected());
156 Config.getPref().putBoolean("changeset-query.advanced.time-restrictions", cbTimeRestrictions.isSelected());
157 Config.getPref().putBoolean("changeset-query.advanced.bbox-restrictions", cbBoundingBoxRestriction.isSelected());
158
159 pnlUserRestriction.rememberSettings();
160 pnlOpenAndCloseRestriction.rememberSettings();
161 pnlTimeRestriction.rememberSettings();
162 }
163
164 /**
165 * Restore settings from preferences.
166 */
167 public void restoreFromSettings() {
168 cbUserRestriction.setSelected(Config.getPref().getBoolean("changeset-query.advanced.user-restrictions", false));
169 cbOpenAndCloseRestrictions.setSelected(Config.getPref().getBoolean("changeset-query.advanced.open-restrictions", false));
170 cbTimeRestrictions.setSelected(Config.getPref().getBoolean("changeset-query.advanced.time-restrictions", false));
171 cbBoundingBoxRestriction.setSelected(Config.getPref().getBoolean("changeset-query.advanced.bbox-restrictions", false));
172 }
173
174 class RestrictionGroupStateChangeHandler implements ItemListener {
175 protected void userRestrictionStateChanged() {
176 pnlUserRestriction.setVisible(cbUserRestriction.isSelected());
177 }
178
179 protected void openCloseRestrictionStateChanged() {
180 pnlOpenAndCloseRestriction.setVisible(cbOpenAndCloseRestrictions.isSelected());
181 }
182
183 protected void timeRestrictionsStateChanged() {
184 pnlTimeRestriction.setVisible(cbTimeRestrictions.isSelected());
185 }
186
187 protected void boundingBoxRestrictionChanged() {
188 pnlBoundingBoxRestriction.setVisible(cbBoundingBoxRestriction.isSelected());
189 }
190
191 @Override
192 public void itemStateChanged(ItemEvent e) {
193 if (e.getSource() == cbUserRestriction) {
194 userRestrictionStateChanged();
195 } else if (e.getSource() == cbOpenAndCloseRestrictions) {
196 openCloseRestrictionStateChanged();
197 } else if (e.getSource() == cbTimeRestrictions) {
198 timeRestrictionsStateChanged();
199 } else if (e.getSource() == cbBoundingBoxRestriction) {
200 boundingBoxRestrictionChanged();
201 }
202 validate();
203 repaint();
204 }
205 }
206}
Note: See TracBrowser for help on using the repository browser.