1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.changeset.query;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Insets;
|
---|
10 | import java.awt.event.ItemEvent;
|
---|
11 | import java.awt.event.ItemListener;
|
---|
12 |
|
---|
13 | import javax.swing.BorderFactory;
|
---|
14 | import javax.swing.ButtonGroup;
|
---|
15 | import javax.swing.JLabel;
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.JRadioButton;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
22 | import org.openstreetmap.josm.gui.JosmUserIdentityManager;
|
---|
23 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.server.UserNameValidator;
|
---|
25 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
26 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
27 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
28 | import org.openstreetmap.josm.io.ChangesetQuery;
|
---|
29 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * This is the panel for selecting whether the query should be restricted to a specific user.
|
---|
33 | * @since 11326 (extracted from AdvancedChangesetQueryPanel)
|
---|
34 | */
|
---|
35 | public class UserRestrictionPanel extends JPanel implements RestrictionPanel {
|
---|
36 | private final ButtonGroup bgUserRestrictions = new ButtonGroup();
|
---|
37 | private final JRadioButton rbRestrictToMyself = new JRadioButton();
|
---|
38 | private final JRadioButton rbRestrictToUid = new JRadioButton();
|
---|
39 | private final JRadioButton rbRestrictToUserName = new JRadioButton();
|
---|
40 | private final JosmTextField tfUid = new JosmTextField(10);
|
---|
41 | private transient UidInputFieldValidator valUid;
|
---|
42 | private final JosmTextField tfUserName = new JosmTextField(10);
|
---|
43 | private transient UserNameValidator valUserName;
|
---|
44 | private final JMultilineLabel lblRestrictedToMyself = new JMultilineLabel(tr("Only changesets owned by myself"));
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Constructs a new {@code UserRestrictionPanel}.
|
---|
48 | */
|
---|
49 | public UserRestrictionPanel() {
|
---|
50 | build();
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected JPanel buildUidInputPanel() {
|
---|
54 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
55 | GridBagConstraints gc = new GridBagConstraints();
|
---|
56 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
57 | gc.weightx = 0.0;
|
---|
58 | gc.insets = new Insets(0, 0, 0, 3);
|
---|
59 | pnl.add(new JLabel(tr("User ID:")), gc);
|
---|
60 |
|
---|
61 | gc.gridx = 1;
|
---|
62 | pnl.add(tfUid, gc);
|
---|
63 | SelectAllOnFocusGainedDecorator.decorate(tfUid);
|
---|
64 | valUid = UidInputFieldValidator.decorate(tfUid);
|
---|
65 |
|
---|
66 | // grab remaining space
|
---|
67 | gc.gridx = 2;
|
---|
68 | gc.weightx = 1.0;
|
---|
69 | pnl.add(new JPanel(), gc);
|
---|
70 | return pnl;
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected JPanel buildUserNameInputPanel() {
|
---|
74 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
75 | GridBagConstraints gc = new GridBagConstraints();
|
---|
76 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
77 | gc.weightx = 0.0;
|
---|
78 | gc.insets = new Insets(0, 0, 0, 3);
|
---|
79 | pnl.add(new JLabel(tr("User name:")), gc);
|
---|
80 |
|
---|
81 | gc.gridx = 1;
|
---|
82 | pnl.add(tfUserName, gc);
|
---|
83 | SelectAllOnFocusGainedDecorator.decorate(tfUserName);
|
---|
84 | valUserName = new UserNameValidator(tfUserName);
|
---|
85 |
|
---|
86 | // grab remaining space
|
---|
87 | gc.gridx = 2;
|
---|
88 | gc.weightx = 1.0;
|
---|
89 | pnl.add(new JPanel(), gc);
|
---|
90 | return pnl;
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected void build() {
|
---|
94 | setLayout(new GridBagLayout());
|
---|
95 | setBorder(BorderFactory.createCompoundBorder(
|
---|
96 | BorderFactory.createEmptyBorder(3, 3, 3, 3),
|
---|
97 | BorderFactory.createCompoundBorder(
|
---|
98 | BorderFactory.createLineBorder(Color.GRAY),
|
---|
99 | BorderFactory.createEmptyBorder(5, 5, 5, 5)
|
---|
100 | )
|
---|
101 | ));
|
---|
102 |
|
---|
103 | ItemListener userRestrictionChangeHandler = new UserRestrictionChangedHandler();
|
---|
104 | GridBagConstraints gc = new GridBagConstraints();
|
---|
105 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
106 | gc.gridx = 0;
|
---|
107 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
108 | gc.weightx = 0.0;
|
---|
109 | add(rbRestrictToMyself, gc);
|
---|
110 | rbRestrictToMyself.addItemListener(userRestrictionChangeHandler);
|
---|
111 |
|
---|
112 | gc.gridx = 1;
|
---|
113 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
114 | gc.weightx = 1.0;
|
---|
115 | add(lblRestrictedToMyself, gc);
|
---|
116 |
|
---|
117 | gc.gridx = 0;
|
---|
118 | gc.gridy = 1;
|
---|
119 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
120 | gc.weightx = 0.0;
|
---|
121 | add(rbRestrictToUid, gc);
|
---|
122 | rbRestrictToUid.addItemListener(userRestrictionChangeHandler);
|
---|
123 |
|
---|
124 | gc.gridx = 1;
|
---|
125 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
126 | gc.weightx = 1.0;
|
---|
127 | add(new JMultilineLabel(tr("Only changesets owned by the user with the following user ID")), gc);
|
---|
128 |
|
---|
129 | gc.gridx = 1;
|
---|
130 | gc.gridy = 2;
|
---|
131 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
132 | gc.weightx = 1.0;
|
---|
133 | add(buildUidInputPanel(), gc);
|
---|
134 |
|
---|
135 | gc.gridx = 0;
|
---|
136 | gc.gridy = 3;
|
---|
137 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
138 | gc.weightx = 0.0;
|
---|
139 | add(rbRestrictToUserName, gc);
|
---|
140 | rbRestrictToUserName.addItemListener(userRestrictionChangeHandler);
|
---|
141 |
|
---|
142 | gc.gridx = 1;
|
---|
143 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
144 | gc.weightx = 1.0;
|
---|
145 | add(new JMultilineLabel(tr("Only changesets owned by the user with the following user name")), gc);
|
---|
146 |
|
---|
147 | gc.gridx = 1;
|
---|
148 | gc.gridy = 4;
|
---|
149 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
150 | gc.weightx = 1.0;
|
---|
151 | add(buildUserNameInputPanel(), gc);
|
---|
152 |
|
---|
153 | bgUserRestrictions.add(rbRestrictToMyself);
|
---|
154 | bgUserRestrictions.add(rbRestrictToUid);
|
---|
155 | bgUserRestrictions.add(rbRestrictToUserName);
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Initializes HMI for user input.
|
---|
160 | */
|
---|
161 | public void startUserInput() {
|
---|
162 | if (JosmUserIdentityManager.getInstance().isAnonymous()) {
|
---|
163 | lblRestrictedToMyself.setText(tr("Only changesets owned by myself (disabled. JOSM is currently run by an anonymous user)"));
|
---|
164 | rbRestrictToMyself.setEnabled(false);
|
---|
165 | if (rbRestrictToMyself.isSelected()) {
|
---|
166 | rbRestrictToUid.setSelected(true);
|
---|
167 | }
|
---|
168 | } else {
|
---|
169 | lblRestrictedToMyself.setText(tr("Only changesets owned by myself"));
|
---|
170 | rbRestrictToMyself.setEnabled(true);
|
---|
171 | rbRestrictToMyself.setSelected(true);
|
---|
172 | }
|
---|
173 | restoreFromSettings();
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Sets the query restrictions on <code>query</code> for changeset owner based restrictions.
|
---|
178 | *
|
---|
179 | * @param query the query. Must not be null.
|
---|
180 | * @throws IllegalArgumentException if query is null
|
---|
181 | * @throws IllegalStateException if one of the available values for query parameters in this panel isn't valid
|
---|
182 | */
|
---|
183 | @Override
|
---|
184 | public void fillInQuery(ChangesetQuery query) {
|
---|
185 | CheckParameterUtil.ensureParameterNotNull(query, "query");
|
---|
186 | if (rbRestrictToMyself.isSelected()) {
|
---|
187 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
|
---|
188 | if (im.isPartiallyIdentified()) {
|
---|
189 | query.forUser(im.getUserName());
|
---|
190 | } else if (im.isFullyIdentified()) {
|
---|
191 | query.forUser(im.getUserId());
|
---|
192 | } else
|
---|
193 | throw new IllegalStateException(
|
---|
194 | tr("Cannot restrict changeset query to the current user because the current user is anonymous"));
|
---|
195 | } else if (rbRestrictToUid.isSelected()) {
|
---|
196 | int uid = valUid.getUid();
|
---|
197 | if (uid > 0) {
|
---|
198 | query.forUser(uid);
|
---|
199 | } else
|
---|
200 | throw new IllegalStateException(tr("Current value ''{0}'' for user ID is not valid", tfUid.getText()));
|
---|
201 | } else if (rbRestrictToUserName.isSelected()) {
|
---|
202 | if (!valUserName.isValid())
|
---|
203 | throw new IllegalStateException(
|
---|
204 | tr("Cannot restrict the changeset query to the user name ''{0}''", tfUserName.getText()));
|
---|
205 | query.forUser(tfUserName.getText());
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Determines if the changeset query time information is valid.
|
---|
211 | * @return {@code true} if the changeset query time information is valid.
|
---|
212 | */
|
---|
213 | @Override
|
---|
214 | public boolean isValidChangesetQuery() {
|
---|
215 | if (rbRestrictToUid.isSelected())
|
---|
216 | return valUid.isValid();
|
---|
217 | else if (rbRestrictToUserName.isSelected())
|
---|
218 | return valUserName.isValid();
|
---|
219 | return true;
|
---|
220 | }
|
---|
221 |
|
---|
222 | protected void alertInvalidUid() {
|
---|
223 | HelpAwareOptionPane.showOptionDialog(
|
---|
224 | this,
|
---|
225 | tr("Please enter a valid user ID"),
|
---|
226 | tr("Invalid user ID"),
|
---|
227 | JOptionPane.ERROR_MESSAGE,
|
---|
228 | HelpUtil.ht("/Dialog/ChangesetQueryDialog#InvalidUserId")
|
---|
229 | );
|
---|
230 | }
|
---|
231 |
|
---|
232 | protected void alertInvalidUserName() {
|
---|
233 | HelpAwareOptionPane.showOptionDialog(
|
---|
234 | this,
|
---|
235 | tr("Please enter a non-empty user name"),
|
---|
236 | tr("Invalid user name"),
|
---|
237 | JOptionPane.ERROR_MESSAGE,
|
---|
238 | HelpUtil.ht("/Dialog/ChangesetQueryDialog#InvalidUserName")
|
---|
239 | );
|
---|
240 | }
|
---|
241 |
|
---|
242 | @Override
|
---|
243 | public void displayMessageIfInvalid() {
|
---|
244 | if (rbRestrictToUid.isSelected()) {
|
---|
245 | if (!valUid.isValid()) {
|
---|
246 | alertInvalidUid();
|
---|
247 | }
|
---|
248 | } else if (rbRestrictToUserName.isSelected()) {
|
---|
249 | if (!valUserName.isValid()) {
|
---|
250 | alertInvalidUserName();
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Remember settings in preferences.
|
---|
257 | */
|
---|
258 | public void rememberSettings() {
|
---|
259 | String prefRoot = "changeset-query.advanced.user-restrictions";
|
---|
260 | if (rbRestrictToMyself.isSelected()) {
|
---|
261 | Main.pref.put(prefRoot + ".query-type", "mine");
|
---|
262 | } else if (rbRestrictToUid.isSelected()) {
|
---|
263 | Main.pref.put(prefRoot + ".query-type", "uid");
|
---|
264 | } else if (rbRestrictToUserName.isSelected()) {
|
---|
265 | Main.pref.put(prefRoot + ".query-type", "username");
|
---|
266 | }
|
---|
267 | Main.pref.put(prefRoot + ".uid", tfUid.getText());
|
---|
268 | Main.pref.put(prefRoot + ".username", tfUserName.getText());
|
---|
269 | }
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Restore settings from preferences.
|
---|
273 | */
|
---|
274 | public void restoreFromSettings() {
|
---|
275 | String prefRoot = "changeset-query.advanced.user-restrictions";
|
---|
276 | String v = Main.pref.get(prefRoot + ".query-type", "mine");
|
---|
277 | if ("mine".equals(v)) {
|
---|
278 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
|
---|
279 | if (im.isAnonymous()) {
|
---|
280 | rbRestrictToUid.setSelected(true);
|
---|
281 | } else {
|
---|
282 | rbRestrictToMyself.setSelected(true);
|
---|
283 | }
|
---|
284 | } else if ("uid".equals(v)) {
|
---|
285 | rbRestrictToUid.setSelected(true);
|
---|
286 | } else if ("username".equals(v)) {
|
---|
287 | rbRestrictToUserName.setSelected(true);
|
---|
288 | }
|
---|
289 | tfUid.setText(Main.pref.get(prefRoot + ".uid", ""));
|
---|
290 | if (!valUid.isValid()) {
|
---|
291 | tfUid.setText("");
|
---|
292 | }
|
---|
293 | tfUserName.setText(Main.pref.get(prefRoot + ".username", ""));
|
---|
294 | }
|
---|
295 |
|
---|
296 | class UserRestrictionChangedHandler implements ItemListener {
|
---|
297 | @Override
|
---|
298 | public void itemStateChanged(ItemEvent e) {
|
---|
299 | tfUid.setEnabled(rbRestrictToUid.isSelected());
|
---|
300 | tfUserName.setEnabled(rbRestrictToUserName.isSelected());
|
---|
301 | if (rbRestrictToUid.isSelected()) {
|
---|
302 | tfUid.requestFocusInWindow();
|
---|
303 | } else if (rbRestrictToUserName.isSelected()) {
|
---|
304 | tfUserName.requestFocusInWindow();
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|
308 | }
|
---|