Search in sources :

Example 1 with IGuiRefresher

use of org.apache.hop.core.gui.plugin.IGuiRefresher in project hop by apache.

the class GuiActionLambdaBuilder method createLambda.

/**
 * Create a copy of the given action and create an action lambda for it.
 *
 * @param guiAction
 * @param methodParameter
 * @return The action with the appropriate lambda capable of executing the provided method in the
 *     given parent object
 */
public GuiAction createLambda(GuiAction guiAction, T methodParameter, IGuiRefresher refresher) {
    if (guiAction.getGuiPluginMethodName() == null) {
        throw new RuntimeException("We need a method to execute this action");
    }
    // Create a copy to make sure we're not doing anything stupid
    // 
    GuiAction action = new GuiAction(guiAction);
    try {
        ClassLoader classLoader;
        if (guiAction.getClassLoader() == null) {
            classLoader = getClass().getClassLoader();
        } else {
            classLoader = guiAction.getClassLoader();
        }
        // We use the GUI Class defined in the GUI Action...
        // 
        Class<?> guiPluginClass;
        try {
            guiPluginClass = classLoader.loadClass(guiAction.getGuiPluginClassName());
        } catch (Exception e) {
            throw new HopException("Unable to find class '" + guiAction.getGuiPluginClassName() + "'", e);
        }
        Object guiPlugin;
        try {
            Method getInstanceMethod = guiPluginClass.getDeclaredMethod("getInstance");
            guiPlugin = getInstanceMethod.invoke(null, null);
        } catch (Exception nsme) {
            // 
            try {
                guiPlugin = guiPluginClass.newInstance();
            } catch (Exception e) {
                throw nsme;
            }
        }
        Method method = guiPluginClass.getMethod(action.getGuiPluginMethodName(), methodParameter.getClass());
        if (method == null) {
            throw new RuntimeException("Unable to find method " + action.getGuiPluginMethodName() + " with parameter " + methodParameter.getClass().getName() + " in class " + guiAction.getGuiPluginClassName());
        }
        final Object finalGuiPlugin = guiPlugin;
        IGuiActionLambda<T> actionLambda = (shiftClicked, controlClicked, objects) -> {
            try {
                method.invoke(finalGuiPlugin, methodParameter);
                if (refresher != null) {
                    refresher.updateGui();
                }
            } catch (Exception e) {
                throw new RuntimeException("Error executing method : " + action.getGuiPluginMethodName() + " in class " + guiAction.getGuiPluginClassName(), e);
            }
        };
        action.setActionLambda(actionLambda);
        return action;
    } catch (Exception e) {
        throw new RuntimeException("Error creating action function for action : " + toString() + ". Probably you need to provide a static getInstance() method in class " + guiAction.getGuiPluginClassName() + " to allow the GuiPlugin to be found and then method " + guiAction.getGuiPluginMethodName() + " can be called.", e);
    }
}
Also used : HopException(org.apache.hop.core.exception.HopException) IGuiActionLambda(org.apache.hop.core.gui.plugin.IGuiActionLambda) Method(java.lang.reflect.Method) IGuiRefresher(org.apache.hop.core.gui.plugin.IGuiRefresher) HopException(org.apache.hop.core.exception.HopException) Method(java.lang.reflect.Method) HopException(org.apache.hop.core.exception.HopException)

Aggregations

Method (java.lang.reflect.Method)1 HopException (org.apache.hop.core.exception.HopException)1 IGuiActionLambda (org.apache.hop.core.gui.plugin.IGuiActionLambda)1 IGuiRefresher (org.apache.hop.core.gui.plugin.IGuiRefresher)1