source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/query/BasicChangesetQueryPanel.java@ 16075

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

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

  • Property svn:eol-style set to native
File size: 8.7 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.Insets;
10import java.awt.event.ItemEvent;
11import java.awt.event.ItemListener;
12import java.util.EnumMap;
13import java.util.Map;
14
15import javax.swing.BorderFactory;
16import javax.swing.ButtonGroup;
17import javax.swing.JCheckBox;
18import javax.swing.JPanel;
19import javax.swing.JRadioButton;
20
21import org.openstreetmap.josm.data.Bounds;
22import org.openstreetmap.josm.data.UserIdentityManager;
23import org.openstreetmap.josm.gui.MainApplication;
24import org.openstreetmap.josm.gui.MapView;
25import org.openstreetmap.josm.gui.widgets.HtmlPanel;
26import org.openstreetmap.josm.io.ChangesetQuery;
27import org.openstreetmap.josm.spi.preferences.Config;
28import org.openstreetmap.josm.tools.GBC;
29import org.openstreetmap.josm.tools.Logging;
30
31/**
32 * This panel presents a list of basic queries for changesets.
33 * @since 2689
34 */
35public class BasicChangesetQueryPanel extends JPanel {
36
37 /**
38 * Enumeration of basic, predefined queries
39 */
40 private enum BasicQuery {
41 MOST_RECENT_CHANGESETS,
42 MY_OPEN_CHANGESETS,
43 CHANGESETS_IN_MAP_VIEW;
44 }
45
46 private transient Map<BasicQuery, JRadioButton> rbQueries;
47 private JCheckBox cbMyChangesetsOnly;
48
49 protected JPanel buildQueriesPanel() {
50 JPanel pnl = new JPanel(new GridBagLayout());
51
52 ButtonGroup bgQueries = new ButtonGroup();
53 rbQueries = new EnumMap<>(BasicQuery.class);
54 SelectQueryHandler selectedQueryHandler = new SelectQueryHandler();
55 for (BasicQuery q: BasicQuery.values()) {
56 JRadioButton rb = new JRadioButton();
57 rb.addItemListener(selectedQueryHandler);
58 rbQueries.put(q, rb);
59 bgQueries.add(rb);
60 }
61
62 GridBagConstraints gc = GBC.eop().fill(GridBagConstraints.HORIZONTAL);
63 // -- most recent changes
64 pnl.add(rbQueries.get(BasicQuery.MOST_RECENT_CHANGESETS), gc);
65
66 // -- most recent changes
67 pnl.add(rbQueries.get(BasicQuery.MY_OPEN_CHANGESETS), gc);
68
69 // -- changesets in map view
70 pnl.add(rbQueries.get(BasicQuery.CHANGESETS_IN_MAP_VIEW), gc);
71
72 // -- checkbox my changesets only
73 gc.gridwidth = 2;
74 gc.insets = new Insets(5, 0, 3, 3);
75 cbMyChangesetsOnly = new JCheckBox(tr("Download my changesets only"));
76 pnl.add(cbMyChangesetsOnly, gc);
77 cbMyChangesetsOnly.setToolTipText(
78 tr("<html>Select to restrict the query to your changesets only.<br>Unselect to include all changesets in the query.</html>"));
79
80 // grab remaining space
81 pnl.add(new JPanel(), GBC.eol().insets(5, 0, 3, 3).fill());
82
83 return pnl;
84 }
85
86 protected JPanel buildInfoPanel() {
87 HtmlPanel pnlInfos = new HtmlPanel();
88 pnlInfos.setText(tr("<html>Please select one the following <strong>standard queries</strong>."
89 + "Select <strong>Download my changesets only</strong>"
90 + " if you only want to download changesets created by yourself.<br>"
91 + "Note that JOSM will download max. 100 changesets.</html>")
92 );
93 return pnlInfos;
94 }
95
96 protected final void build() {
97 setLayout(new BorderLayout(0, 5));
98 setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
99 add(buildInfoPanel(), BorderLayout.NORTH);
100 add(buildQueriesPanel(), BorderLayout.CENTER);
101 }
102
103 /**
104 * Constructs a new {@code BasicChangesetQueryPanel}.
105 */
106 public BasicChangesetQueryPanel() {
107 build();
108 }
109
110 /**
111 * Initializes the panel.
112 */
113 public void init() {
114 JRadioButton lbl = rbQueries.get(BasicQuery.MOST_RECENT_CHANGESETS);
115 lbl.setText(tr("<html>Download the latest changesets</html>"));
116
117 // query for open changesets only possible if we have a current user which is at least
118 // partially identified
119 lbl = rbQueries.get(BasicQuery.MY_OPEN_CHANGESETS);
120 if (UserIdentityManager.getInstance().isAnonymous()) {
121 rbQueries.get(BasicQuery.MY_OPEN_CHANGESETS).setEnabled(false);
122 lbl.setText(tr("<html>Download my open changesets<br><em>Disabled. " +
123 "Please enter your OSM user name in the preferences first.</em></html>"));
124 } else {
125 rbQueries.get(BasicQuery.MY_OPEN_CHANGESETS).setEnabled(true);
126 lbl.setText(tr("<html>Download my open changesets</html>"));
127 }
128
129 // query for changesets in the current map view only if there *is* a current map view
130 lbl = rbQueries.get(BasicQuery.CHANGESETS_IN_MAP_VIEW);
131 if (!MainApplication.isDisplayingMapView()) {
132 rbQueries.get(BasicQuery.CHANGESETS_IN_MAP_VIEW).setEnabled(false);
133 lbl.setText(tr("<html>Download changesets in the current map view.<br><em>Disabled. " +
134 "There is currently no map view active.</em></html>"));
135 } else {
136 rbQueries.get(BasicQuery.CHANGESETS_IN_MAP_VIEW).setEnabled(true);
137 lbl.setText(tr("<html>Download changesets in the current map view</html>"));
138 }
139
140 restoreFromPreferences();
141 }
142
143 /**
144 * Remember settings in preferences.
145 */
146 public void rememberInPreferences() {
147 BasicQuery q = getSelectedQuery();
148 if (q == null) {
149 Config.getPref().put("changeset-query.basic.query", null);
150 } else {
151 Config.getPref().put("changeset-query.basic.query", q.toString());
152 }
153 Config.getPref().putBoolean("changeset-query.basic.my-changesets-only", cbMyChangesetsOnly.isSelected());
154 }
155
156 /**
157 * Restore settings from preferences.
158 */
159 public void restoreFromPreferences() {
160 BasicQuery q;
161 String value = Config.getPref().get("changeset-query.basic.query", null);
162 if (value == null) {
163 q = BasicQuery.MOST_RECENT_CHANGESETS;
164 } else {
165 try {
166 q = BasicQuery.valueOf(BasicQuery.class, value);
167 } catch (IllegalArgumentException e) {
168 Logging.log(Logging.LEVEL_WARN, tr("Unexpected value for preference ''{0}'', got ''{1}''. Resetting to default query.",
169 "changeset-query.basic.query", value), e);
170 q = BasicQuery.MOST_RECENT_CHANGESETS;
171 }
172 }
173 rbQueries.get(q).setSelected(true);
174 boolean mineOnly = Config.getPref().getBoolean("changeset-query.basic.my-changesets-only", false);
175 mineOnly = mineOnly || q == BasicQuery.MY_OPEN_CHANGESETS;
176 cbMyChangesetsOnly.setSelected(mineOnly);
177 }
178
179 protected BasicQuery getSelectedQuery() {
180 for (BasicQuery q : BasicQuery.values()) {
181 if (rbQueries.get(q).isSelected())
182 return q;
183 }
184 return null;
185 }
186
187 /**
188 * Builds the changeset query.
189 * @return the changeset query
190 */
191 public ChangesetQuery buildChangesetQuery() {
192 BasicQuery q = getSelectedQuery();
193 ChangesetQuery query = new ChangesetQuery();
194 UserIdentityManager im = UserIdentityManager.getInstance();
195 if (q == null)
196 return query;
197 switch(q) {
198 case MOST_RECENT_CHANGESETS:
199 break;
200 case MY_OPEN_CHANGESETS:
201 query = query.beingOpen(true);
202 break;
203 case CHANGESETS_IN_MAP_VIEW:
204 MapView mapView = MainApplication.getMap().mapView;
205 Bounds b = mapView.getLatLonBounds(mapView.getBounds());
206 query = query.inBbox(b);
207 break;
208 }
209
210 if (cbMyChangesetsOnly.isSelected()) {
211 if (im.isPartiallyIdentified()) {
212 query = query.forUser(im.getUserName());
213 } else if (im.isFullyIdentified()) {
214 query = query.forUser(im.getUserId()).beingOpen(true);
215 } else
216 // anonymous -- can happen with a fresh config.
217 throw new IllegalStateException(tr("Cannot create changeset query for open changesets of anonymous user"));
218 }
219
220 return query;
221 }
222
223 /**
224 * Responds to changes in the selected query
225 */
226 class SelectQueryHandler implements ItemListener {
227 @Override
228 public void itemStateChanged(ItemEvent e) {
229 BasicQuery q = getSelectedQuery();
230 if (q == null) return;
231 if (q == BasicQuery.MY_OPEN_CHANGESETS) {
232 cbMyChangesetsOnly.setSelected(true);
233 cbMyChangesetsOnly.setEnabled(false);
234 } else {
235 if (!cbMyChangesetsOnly.isEnabled()) {
236 cbMyChangesetsOnly.setEnabled(true);
237 }
238 }
239 }
240 }
241}
Note: See TracBrowser for help on using the repository browser.