Search in sources :

Example 1 with ViewEventListenerHook

use of com.qcadoo.view.internal.hooks.ViewEventListenerHook in project qcadoo by qcadoo.

the class DefaultEventHandlerTest method shouldCallCustomEventMethod.

@Test
public void shouldCallCustomEventMethod() throws Exception {
    // given
    ViewDefinitionState viewDefinitionState = mock(ViewDefinitionState.class);
    FormComponentPattern pattern = mock(FormComponentPattern.class);
    given(pattern.getExpressionNew()).willReturn(null);
    given(pattern.getExpressionEdit()).willReturn(null);
    setField(pattern, "applicationContext", applicationContext);
    FormComponentState component = new FormComponentState(pattern);
    ViewEventListenerHook eventListener = mock(ViewEventListenerHook.class);
    given(eventListener.getEventName()).willReturn("custom");
    component.registerCustomEvent(eventListener);
    // when
    component.performEvent(viewDefinitionState, "custom", "arg0", "arg1");
    // then
    Mockito.verify(eventListener).invokeEvent(viewDefinitionState, component, new String[] { "arg0", "arg1" });
}
Also used : ViewEventListenerHook(com.qcadoo.view.internal.hooks.ViewEventListenerHook) FormComponentPattern(com.qcadoo.view.internal.components.form.FormComponentPattern) ViewDefinitionState(com.qcadoo.view.api.ViewDefinitionState) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState) Test(org.junit.Test)

Example 2 with ViewEventListenerHook

use of com.qcadoo.view.internal.hooks.ViewEventListenerHook in project qcadoo by qcadoo.

the class ViewListenerModuleFactory method parseElement.

@Override
protected ViewListenerModule parseElement(final String pluginIdentifier, final Element element) {
    String plugin = getRequiredAttribute(element, "plugin");
    String view = getRequiredAttribute(element, "view");
    String component = getRequiredAttribute(element, "component");
    String eventName = getRequiredAttribute(element, "event");
    String className = getRequiredAttribute(element, "class");
    String method = getRequiredAttribute(element, "method");
    ViewEventListenerHook hook = hookFactory.buildViewEventListener(eventName, className, method, pluginIdentifier);
    return new ViewListenerModule(pluginIdentifier, viewDefinitionService, plugin, view, component, hook);
}
Also used : ViewEventListenerHook(com.qcadoo.view.internal.hooks.ViewEventListenerHook)

Example 3 with ViewEventListenerHook

use of com.qcadoo.view.internal.hooks.ViewEventListenerHook in project qcadoo by qcadoo.

the class AbstractComponentPattern method createComponentState.

@Override
public InternalComponentState createComponentState(final InternalViewDefinitionState viewDefinitionState) {
    AbstractComponentState state = (AbstractComponentState) getComponentStateInstance();
    state.setDataDefinition(dataDefinition);
    state.setName(name);
    state.setUuid(UUID.randomUUID().toString());
    state.setEnabled(isDefaultEnabled());
    state.setVisible(isDefaultVisible());
    state.setTranslationService(translationService);
    state.setTranslationPath(getTranslationPath());
    for (ViewEventListenerHook customEventListener : customEventListeners) {
        state.registerCustomEvent(customEventListener);
    }
    if (viewDefinitionState != null) {
        viewDefinitionState.registerComponent(getReference(), state);
    }
    return state;
}
Also used : ViewEventListenerHook(com.qcadoo.view.internal.hooks.ViewEventListenerHook) AbstractComponentState(com.qcadoo.view.internal.states.AbstractComponentState)

Example 4 with ViewEventListenerHook

use of com.qcadoo.view.internal.hooks.ViewEventListenerHook in project qcadoo by qcadoo.

the class DefaultEventHandlerTest method shouldCallMultipleEventMethods.

@Test
public void shouldCallMultipleEventMethods() throws Exception {
    // given
    ViewDefinitionState viewDefinitionState = mock(ViewDefinitionState.class);
    FormComponentPattern pattern = mock(FormComponentPattern.class);
    given(pattern.getExpressionNew()).willReturn(null);
    given(pattern.getExpressionEdit()).willReturn(null);
    setField(pattern, "applicationContext", applicationContext);
    FormComponentState component = new FormComponentState(pattern);
    component.setFieldValue(13L);
    ViewEventListenerHook eventListener1 = mock(ViewEventListenerHook.class);
    given(eventListener1.getEventName()).willReturn("clear");
    component.registerCustomEvent(eventListener1);
    ViewEventListenerHook eventListener2 = mock(ViewEventListenerHook.class);
    given(eventListener2.getEventName()).willReturn("clear");
    component.registerCustomEvent(eventListener2);
    // when
    component.performEvent(viewDefinitionState, "clear");
    // then
    assertNull("value is " + component.getFieldValue(), component.getFieldValue());
    Mockito.verify(eventListener1).invokeEvent(viewDefinitionState, component, new String[0]);
    Mockito.verify(eventListener2).invokeEvent(viewDefinitionState, component, new String[0]);
}
Also used : ViewEventListenerHook(com.qcadoo.view.internal.hooks.ViewEventListenerHook) FormComponentPattern(com.qcadoo.view.internal.components.form.FormComponentPattern) ViewDefinitionState(com.qcadoo.view.api.ViewDefinitionState) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState) Test(org.junit.Test)

Example 5 with ViewEventListenerHook

use of com.qcadoo.view.internal.hooks.ViewEventListenerHook in project qcadoo by qcadoo.

the class ComponentPatternTest method shouldHaveValidInstance.

@Test
@SuppressWarnings("unchecked")
public void shouldHaveValidInstance() throws Exception {
    // given
    InternalViewDefinitionState viewDefinitionState = mock(InternalViewDefinitionState.class);
    TranslationService translationService = mock(TranslationService.class);
    ApplicationContext applicationContext = mock(ApplicationContext.class);
    InternalViewDefinition viewDefinition = mock(InternalViewDefinition.class);
    ComponentDefinition componentDefinition = getComponentDefinition("testName", null);
    componentDefinition.setTranslationService(translationService);
    componentDefinition.setApplicationContext(applicationContext);
    componentDefinition.setViewDefinition(viewDefinition);
    AbstractComponentPattern pattern = new FormComponentPattern(componentDefinition);
    ViewEventListenerHook viewEventListenerHook = mock(ViewEventListenerHook.class);
    given(viewEventListenerHook.getEventName()).willReturn("save");
    pattern.addCustomEvent(viewEventListenerHook);
    // when
    ComponentState state = pattern.createComponentState(viewDefinitionState);
    // then
    assertTrue(state instanceof FormComponent);
    EventHandlerHolder eventHandlerHolder = (EventHandlerHolder) getField(state, "eventHandlerHolder");
    Map<String, List<EventHandlerHolder.EventHandler>> eventHandlers = (Map<String, List<EventHandlerHolder.EventHandler>>) getField(eventHandlerHolder, "eventHandlers");
    List<EventHandlerHolder.EventHandler> handlers = eventHandlers.get("save");
    assertNotNull(handlers);
    assertEquals(2, handlers.size());
    assertEquals(viewEventListenerHook, handlers.get(1));
}
Also used : FormComponent(com.qcadoo.view.api.components.FormComponent) InternalViewDefinitionState(com.qcadoo.view.internal.api.InternalViewDefinitionState) InternalViewDefinition(com.qcadoo.view.internal.api.InternalViewDefinition) EventHandlerHolder(com.qcadoo.view.internal.internal.EventHandlerHolder) ApplicationContext(org.springframework.context.ApplicationContext) ViewEventListenerHook(com.qcadoo.view.internal.hooks.ViewEventListenerHook) FormComponentPattern(com.qcadoo.view.internal.components.form.FormComponentPattern) TranslationService(com.qcadoo.localization.api.TranslationService) List(java.util.List) Map(java.util.Map) ComponentDefinition(com.qcadoo.view.internal.ComponentDefinition) ComponentState(com.qcadoo.view.api.ComponentState) Test(org.junit.Test)

Aggregations

ViewEventListenerHook (com.qcadoo.view.internal.hooks.ViewEventListenerHook)5 FormComponentPattern (com.qcadoo.view.internal.components.form.FormComponentPattern)3 Test (org.junit.Test)3 ViewDefinitionState (com.qcadoo.view.api.ViewDefinitionState)2 FormComponentState (com.qcadoo.view.internal.components.form.FormComponentState)2 TranslationService (com.qcadoo.localization.api.TranslationService)1 ComponentState (com.qcadoo.view.api.ComponentState)1 FormComponent (com.qcadoo.view.api.components.FormComponent)1 ComponentDefinition (com.qcadoo.view.internal.ComponentDefinition)1 InternalViewDefinition (com.qcadoo.view.internal.api.InternalViewDefinition)1 InternalViewDefinitionState (com.qcadoo.view.internal.api.InternalViewDefinitionState)1 EventHandlerHolder (com.qcadoo.view.internal.internal.EventHandlerHolder)1 AbstractComponentState (com.qcadoo.view.internal.states.AbstractComponentState)1 List (java.util.List)1 Map (java.util.Map)1 ApplicationContext (org.springframework.context.ApplicationContext)1