001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.relation.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005import static org.openstreetmap.josm.tools.I18n.trn;
006
007import java.awt.event.ActionEvent;
008
009import javax.swing.JOptionPane;
010import javax.swing.event.DocumentEvent;
011import javax.swing.event.DocumentListener;
012
013import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
014import org.openstreetmap.josm.gui.MainApplication;
015import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
016import org.openstreetmap.josm.tools.ImageProvider;
017import org.openstreetmap.josm.tools.Utils;
018
019/**
020 * Sets a role for the selected members
021 * @since 9496
022 */
023public class SetRoleAction extends AbstractRelationEditorAction implements DocumentListener {
024    private static final long serialVersionUID = 1L;
025
026    private final transient AutoCompletingTextField tfRole;
027
028    /**
029     * Constructs a new {@code SetRoleAction}.
030     * @param editorAccess An interface to access the relation editor contents.
031     */
032    public SetRoleAction(IRelationEditorActionAccess editorAccess) {
033        super(editorAccess);
034        this.tfRole = editorAccess.getTextFieldRole();
035        putValue(SHORT_DESCRIPTION, tr("Sets a role for the selected members"));
036        new ImageProvider("apply").getResource().attachImageIcon(this);
037        putValue(NAME, tr("Apply Role"));
038        updateEnabledState();
039    }
040
041    @Override
042    protected void updateEnabledState() {
043        setEnabled(editorAccess.getMemberTable().getSelectedRowCount() > 0);
044    }
045
046    protected boolean isEmptyRole() {
047        return Utils.isBlank(tfRole.getText());
048    }
049
050    protected boolean confirmSettingEmptyRole(int onNumMembers) {
051        String message = "<html>"
052            + trn("You are setting an empty role on {0} object.",
053                    "You are setting an empty role on {0} objects.", onNumMembers, onNumMembers)
054                    + "<br>"
055                    + tr("This is equal to deleting the roles of these objects.") +
056                    "<br>"
057                    + tr("Do you really want to apply the new role?") + "</html>";
058        String[] options = {
059                tr("Yes, apply it"),
060                tr("No, do not apply")
061        };
062        int ret = ConditionalOptionPaneUtil.showOptionDialog(
063                "relation_editor.confirm_applying_empty_role",
064                MainApplication.getMainFrame(),
065                message,
066                tr("Confirm empty role"),
067                JOptionPane.YES_NO_OPTION,
068                JOptionPane.WARNING_MESSAGE,
069                options,
070                options[0]
071        );
072        switch(ret) {
073        case JOptionPane.YES_OPTION:
074        case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION:
075            return true;
076        default:
077            return false;
078        }
079    }
080
081    @Override
082    public void actionPerformed(ActionEvent e) {
083        if (isEmptyRole() && !confirmSettingEmptyRole(editorAccess.getMemberTable().getSelectedRowCount())) {
084            return;
085        }
086        editorAccess.getMemberTableModel().updateRole(editorAccess.getMemberTable().getSelectedRows(), Utils.strip(tfRole.getText()));
087    }
088
089    @Override
090    public void changedUpdate(DocumentEvent e) {
091        updateEnabledState();
092    }
093
094    @Override
095    public void insertUpdate(DocumentEvent e) {
096        updateEnabledState();
097    }
098
099    @Override
100    public void removeUpdate(DocumentEvent e) {
101        updateEnabledState();
102    }
103}