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;
005
006import java.awt.event.ActionEvent;
007import java.awt.event.KeyEvent;
008
009import org.openstreetmap.josm.tools.ImageProvider;
010import org.openstreetmap.josm.tools.Shortcut;
011
012/**
013 * Sort the relation members
014 * @since 9496
015 */
016public class SortAction extends AbstractRelationEditorAction {
017    private static final long serialVersionUID = 1L;
018
019    /**
020     * Constructs a new {@code SortAction}.
021     * @param editorAccess An interface to access the relation editor contents.
022     */
023    public SortAction(IRelationEditorActionAccess editorAccess) {
024        super(editorAccess, IRelationEditorUpdateOn.MEMBER_TABLE_CHANGE);
025        new ImageProvider("dialogs", "sort").getResource().attachImageIcon(this, true);
026        putValue(NAME, tr("Sort"));
027        Shortcut sc = Shortcut.registerShortcut("relationeditor:sort", tr("Relation Editor: Sort"), KeyEvent.VK_END, Shortcut.ALT);
028        sc.setAccelerator(this);
029        sc.setTooltip(this, tr("Sort the relation members"));
030        updateEnabledState();
031    }
032
033    @Override
034    public void actionPerformed(ActionEvent e) {
035        editorAccess.getMemberTableModel().sort();
036        editorAccess.stopMemberCellEditing();
037    }
038
039    @Override
040    protected void updateEnabledState() {
041        setEnabled(editorAccess.getMemberTableModel().getRowCount() > 0);
042    }
043}