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

Last change on this file was 19050, checked in by taylor.smock, 15 hours ago

Revert most var changes from r19048, fix most new compile warnings and checkstyle issues

Also, document why various ErrorProne checks were originally disabled and fix
generic SonarLint issues.

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