[9665] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.dialogs.relation.actions;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
| 6 |
|
---|
| 7 | import java.awt.event.ActionEvent;
|
---|
| 8 |
|
---|
| 9 | import javax.swing.JOptionPane;
|
---|
| 10 | import javax.swing.event.DocumentEvent;
|
---|
| 11 | import javax.swing.event.DocumentListener;
|
---|
| 12 |
|
---|
| 13 | import org.openstreetmap.josm.Main;
|
---|
| 14 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
| 15 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
| 16 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * Sets a role for the selected members
|
---|
| 20 | * @since 9496
|
---|
| 21 | */
|
---|
| 22 | public class SetRoleAction extends AbstractRelationEditorAction implements DocumentListener {
|
---|
[14027] | 23 | private static final long serialVersionUID = 1L;
|
---|
[9665] | 24 |
|
---|
| 25 | private final transient AutoCompletingTextField tfRole;
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Constructs a new {@code SetRoleAction}.
|
---|
| 29 | * @param memberTable member table
|
---|
| 30 | * @param memberTableModel member table model
|
---|
| 31 | * @param tfRole role text field
|
---|
| 32 | */
|
---|
[14027] | 33 | public SetRoleAction(IRelationEditorActionAccess editorAccess) {
|
---|
| 34 | super(editorAccess);
|
---|
| 35 | this.tfRole = editorAccess.getTextFieldRole();
|
---|
[9665] | 36 | putValue(SHORT_DESCRIPTION, tr("Sets a role for the selected members"));
|
---|
[10424] | 37 | new ImageProvider("apply").getResource().attachImageIcon(this);
|
---|
[9665] | 38 | putValue(NAME, tr("Apply Role"));
|
---|
| 39 | updateEnabledState();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | @Override
|
---|
| 43 | protected void updateEnabledState() {
|
---|
[14027] | 44 | setEnabled(editorAccess.getMemberTable().getSelectedRowCount() > 0);
|
---|
[9665] | 45 | }
|
---|
| 46 |
|
---|
| 47 | protected boolean isEmptyRole() {
|
---|
| 48 | return tfRole.getText() == null || tfRole.getText().trim().isEmpty();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | protected boolean confirmSettingEmptyRole(int onNumMembers) {
|
---|
| 52 | String message = "<html>"
|
---|
| 53 | + trn("You are setting an empty role on {0} object.",
|
---|
| 54 | "You are setting an empty role on {0} objects.", onNumMembers, onNumMembers)
|
---|
| 55 | + "<br>"
|
---|
| 56 | + tr("This is equal to deleting the roles of these objects.") +
|
---|
| 57 | "<br>"
|
---|
| 58 | + tr("Do you really want to apply the new role?") + "</html>";
|
---|
| 59 | String[] options = new String[] {
|
---|
| 60 | tr("Yes, apply it"),
|
---|
| 61 | tr("No, do not apply")
|
---|
| 62 | };
|
---|
| 63 | int ret = ConditionalOptionPaneUtil.showOptionDialog(
|
---|
| 64 | "relation_editor.confirm_applying_empty_role",
|
---|
| 65 | Main.parent,
|
---|
| 66 | message,
|
---|
| 67 | tr("Confirm empty role"),
|
---|
| 68 | JOptionPane.YES_NO_OPTION,
|
---|
| 69 | JOptionPane.WARNING_MESSAGE,
|
---|
| 70 | options,
|
---|
| 71 | options[0]
|
---|
| 72 | );
|
---|
| 73 | switch(ret) {
|
---|
| 74 | case JOptionPane.YES_OPTION:
|
---|
| 75 | case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION:
|
---|
| 76 | return true;
|
---|
| 77 | default:
|
---|
| 78 | return false;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | @Override
|
---|
| 83 | public void actionPerformed(ActionEvent e) {
|
---|
[14027] | 84 | if (isEmptyRole() && !confirmSettingEmptyRole(editorAccess.getMemberTable().getSelectedRowCount())) {
|
---|
[9665] | 85 | return;
|
---|
| 86 | }
|
---|
[14027] | 87 | editorAccess.getMemberTableModel().updateRole(editorAccess.getMemberTable().getSelectedRows(), tfRole.getText());
|
---|
[9665] | 88 | }
|
---|
| 89 |
|
---|
| 90 | @Override
|
---|
| 91 | public void changedUpdate(DocumentEvent e) {
|
---|
| 92 | updateEnabledState();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | @Override
|
---|
| 96 | public void insertUpdate(DocumentEvent e) {
|
---|
| 97 | updateEnabledState();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | @Override
|
---|
| 101 | public void removeUpdate(DocumentEvent e) {
|
---|
| 102 | updateEnabledState();
|
---|
| 103 | }
|
---|
| 104 | }
|
---|