Search in sources :

Example 1 with Timer

use of com.haulmont.cuba.gui.components.Timer in project cuba by cuba-platform.

the class WindowLoader method addInitTimerMethodTask.

protected void addInitTimerMethodTask(Timer timer, String timerMethodName) {
    context.addPostInitTask((windowContext, window) -> {
        Method timerMethod;
        try {
            timerMethod = window.getClass().getMethod(timerMethodName, Timer.class);
        } catch (NoSuchMethodException e) {
            throw new GuiDevelopmentException("Unable to find invoke method for timer", windowContext.getFullFrameId(), ParamsMap.of("Timer Id", timer.getId(), "Method name", timerMethodName));
        }
        timer.addActionListener(t -> {
            try {
                timerMethod.invoke(window, t);
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new RuntimeException("Unable to invoke onTimer", e);
            }
        });
    });
}
Also used : Timer(com.haulmont.cuba.gui.components.Timer) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with Timer

use of com.haulmont.cuba.gui.components.Timer in project cuba by cuba-platform.

the class WebWindow method stopTimers.

/**
 * Completely stop and remove timers of the window.
 */
public void stopTimers() {
    AppUI appUI = AppUI.getCurrent();
    if (timers != null) {
        for (Timer timer : timers) {
            timer.stop();
            WebTimer webTimer = (WebTimer) timer;
            appUI.removeTimer(webTimer.getTimerImpl());
        }
        timers.clear();
    }
}
Also used : Timer(com.haulmont.cuba.gui.components.Timer) AppUI(com.haulmont.cuba.web.AppUI)

Example 3 with Timer

use of com.haulmont.cuba.gui.components.Timer in project cuba by cuba-platform.

the class WindowLoader method loadTimer.

protected void loadTimer(ComponentsFactory factory, final Window component, Element element) {
    Timer timer = factory.createTimer();
    timer.setXmlDescriptor(element);
    timer.setId(element.attributeValue("id"));
    String delay = element.attributeValue("delay");
    if (StringUtils.isEmpty(delay)) {
        throw new GuiDevelopmentException("Timer 'delay' can't be empty", context.getCurrentFrameId(), "Timer ID", timer.getId());
    }
    int value;
    try {
        value = Integer.parseInt(delay);
    } catch (NumberFormatException e) {
        throw new GuiDevelopmentException("Timer 'delay' must be numeric", context.getFullFrameId(), ParamsMap.of("Timer delay", delay, "Timer ID", timer.getId()));
    }
    if (value <= 0) {
        throw new GuiDevelopmentException("Timer 'delay' must be greater than 0", context.getFullFrameId(), "Timer ID", timer.getId());
    }
    timer.setDelay(value);
    timer.setRepeating(Boolean.parseBoolean(element.attributeValue("repeating")));
    String onTimer = element.attributeValue("onTimer");
    if (StringUtils.isNotEmpty(onTimer)) {
        String timerMethodName = onTimer;
        if (StringUtils.startsWith(onTimer, "invoke:")) {
            timerMethodName = StringUtils.substring(onTimer, "invoke:".length());
        }
        timerMethodName = StringUtils.trim(timerMethodName);
        addInitTimerMethodTask(timer, timerMethodName);
    }
    String autostart = element.attributeValue("autostart");
    if (StringUtils.isNotEmpty(autostart) && Boolean.parseBoolean(autostart)) {
        addAutoStartTimerTask(timer);
    }
    timer.setFrame(context.getFrame());
    component.addTimer(timer);
}
Also used : Timer(com.haulmont.cuba.gui.components.Timer) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException)

Example 4 with Timer

use of com.haulmont.cuba.gui.components.Timer in project cuba by cuba-platform.

the class TimerFacetProvider method addInitTimerMethodTask.

// for compatibility only
@Deprecated
protected void addInitTimerMethodTask(Timer timer, String timerMethodName, ComponentContext context) {
    FrameOwner controller = context.getFrame().getFrameOwner();
    if (controller instanceof LegacyFragmentAdapter) {
        controller = ((LegacyFragmentAdapter) controller).getRealScreen();
    }
    Class<? extends FrameOwner> windowClass = controller.getClass();
    Method timerMethod;
    try {
        timerMethod = windowClass.getMethod(timerMethodName, Timer.class);
    } catch (NoSuchMethodException e) {
        throw new GuiDevelopmentException("Unable to find invoke method for timer", context, ParamsMap.of("Timer Id", timer.getId(), "Method name", timerMethodName));
    }
    timer.addTimerActionListener(new DeclarativeTimerActionHandler(timerMethod, controller));
}
Also used : FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner) Timer(com.haulmont.cuba.gui.components.Timer) WebTimer(com.haulmont.cuba.web.gui.components.WebTimer) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) Method(java.lang.reflect.Method) LegacyFragmentAdapter(com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter)

Aggregations

Timer (com.haulmont.cuba.gui.components.Timer)4 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)3 Method (java.lang.reflect.Method)2 LegacyFragmentAdapter (com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter)1 FrameOwner (com.haulmont.cuba.gui.screen.FrameOwner)1 AppUI (com.haulmont.cuba.web.AppUI)1 WebTimer (com.haulmont.cuba.web.gui.components.WebTimer)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1