All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ird.observe.client.action.MenuActionSupport Maven / Gradle / Ivy

package fr.ird.observe.client.action;

/*-
 * #%L
 * ObServe Toolkit :: Common Client
 * %%
 * Copyright (C) 2008 - 2017 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import fr.ird.observe.client.MainUI;
import fr.ird.observe.client.ObserveKeyStrokes;
import java.awt.event.ActionEvent;
import java.util.Objects;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

/**
 * Created by tchemit on 16/08/17.
 *
 * @author Tony Chemit - [email protected]
 */
public abstract class MenuActionSupport extends UIActionSupport {

    protected MenuActionSupport(MainUI mainUI, String actionCommandKey, String label, String shortDescription, String actionIcon, KeyStroke acceleratorKey) {
        super(mainUI, actionCommandKey, label, shortDescription, actionIcon, acceleratorKey, false);
    }

    protected MenuActionSupport(MainUI mainUI, String actionCommandKey, String label, String shortDescription, String actionIcon, char mnemonic) {
        super(mainUI, actionCommandKey, label, shortDescription, actionIcon, KeyStroke.getKeyStroke((int) mnemonic, 0), false);
        putValue(MNEMONIC_KEY, (int) mnemonic);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (canExecuteAction(e)) {
            doActionPerformed(e);
        }
    }

    protected abstract void doActionPerformed(ActionEvent e);

    private boolean canExecuteAction(ActionEvent e) {
        JComponent editor = getEditor();
        Objects.requireNonNull(editor);
        if (!editor.isVisible() || !editor.isEnabled()) {
            return false;
        }
        if (editor instanceof JMenuItem) {
            return ((JMenuItem) editor).isArmed();
        }
        return editor.isShowing();
    }

    public void initForMainUi(AbstractButton editor) {

        super.initForMainUi(editor);

        if (editor instanceof JButton) {

            // this is a normal button using a menu action, let's add to text accelerator

            ObserveKeyStrokes.addKeyStoreToText(editor, this);
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy