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

fr.ird.observe.client.action.ObserveActionMap 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 com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import fr.ird.observe.client.MainUI;
import fr.ird.observe.client.ObserveClientTechnicalException;
import java.awt.Component;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import org.apache.commons.beanutils.ConstructorUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.reflections.Reflections;

/**
 * Created on 10/11/16.
 *
 * @author Tony Chemit - [email protected]
 * @since 6.0
 */
public class ObserveActionMap extends ActionMap {

    /** Logger */
    private static final Log log = LogFactory.getLog(ObserveActionMap.class);
    public static final Class[] PARAMETER_TYPES = {MainUI.class};
    public static final String ACTION_NAME = "ACTION_NAME";
    private static final long serialVersionUID = 156817733140470120L;

    private final Component ui;
    private final ImmutableMap> simpleActionsMapping;
    private final Multimap simpleActionsCache;
    private final ImmutableMap globalActions;

    public ObserveActionMap(MainUI ui) {
        this((JComponent) ui, ui.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW));
    }

    public ObserveActionMap(JComponent ui) {
        this(ui, ui.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW));
    }

    public ObserveActionMap(Component ui, InputMap inputMap) {
        this.ui = ui;
        this.simpleActionsCache = ArrayListMultimap.create();
        Set> actionTypes = new Reflections("fr.ird.observe.client.ui.actions").getSubTypesOf(UIActionSupport.class);

        ImmutableMap.Builder> mappingBuilder = ImmutableMap.builder();
        ImmutableMap.Builder globalActionsBuilder = ImmutableMap.builder();
        for (Class actionType : actionTypes) {

            if (Modifier.isAbstract(actionType.getModifiers())) {
                continue;
            }

            String actionId;
            try {
                actionId = (String) actionType.getDeclaredField(ACTION_NAME).get(null);
            } catch (IllegalAccessException | NoSuchFieldException e) {
                throw new ObserveClientTechnicalException("Can't find ACTION_NAME field on action: " + actionType.getName());
            }

            if (GlobalUIActionSupport.class.isAssignableFrom(actionType)) {

                //noinspection unchecked
                Class globalActionType = (Class) actionType;
                if (log.isInfoEnabled()) {
                    log.debug("Register global action: " + actionId + " - type: " + globalActionType.getName());
                }
                Optional globalUIAction = newAction(actionId, globalActionType);
                if (globalUIAction.isPresent()) {

                    globalActionsBuilder.put(actionId, globalUIAction.get());
                    inputMap.put(globalUIAction.get().getAcceleratorKey(), actionId);
                }

            } else {

                // action simple
                if (log.isInfoEnabled()) {
                    log.debug("Register simple action: " + actionId + " - type: " + actionType.getName());
                }
                mappingBuilder.put(actionId, actionType);

            }

        }

        simpleActionsMapping = mappingBuilder.build();
        globalActions = globalActionsBuilder.build();

        log.info(String.format("Load %d simple action(s).", simpleActionsMapping.size()));
        log.info(String.format("Load %d global action(s).", globalActions.size()));
    }

    public void registerAction(AbstractButton editor) {
        simpleActionsCache.put(editor.getActionCommand(), (UIActionSupport) editor.getAction());
    }

    @Override
    public Action get(Object key) {

        String actionCommand = key.toString();

        GlobalUIActionSupport globalUIAction = globalActions.get(actionCommand);
        if (globalUIAction != null) {
            return globalUIAction;
        }

        Class actionType = simpleActionsMapping.get(actionCommand);
        if (actionType == null) {

            return super.get(key);
        }
        Collection candidates = simpleActionsCache.get(actionCommand);
        if (candidates.isEmpty()) {

            Optional optional = newAction((String) key, actionType);
            return optional.orElse(null);
        } else {
            return candidates.iterator().next();
        }

    }

    public ImmutableMap getGlobalActions() {
        return globalActions;
    }

    private  Optional newAction(String key, Class actionType) {

        try {
            if (log.isDebugEnabled()) {
                log.debug("Create action: " + key + ", type: " + actionType.getName());
            }
            return Optional.of(ConstructorUtils.invokeConstructor(actionType, new Object[]{ui}, PARAMETER_TYPES));
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            throw new ObserveClientTechnicalException("Hum, can't creation action for type: " + actionType.getName(), e);
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy