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