Example usage for com.vaadin.util CurrentInstance set

List of usage examples for com.vaadin.util CurrentInstance set

Introduction

In this page you can find the example usage for com.vaadin.util CurrentInstance set.

Prototype

public static <T> CurrentInstance set(Class<T> type, T instance) 

Source Link

Document

Sets the current instance of the given type.

Usage

From source file:com.google.code.vaadin.application.ui.ScopedUIProvider.java

License:Apache License

public UI createInstance(Class<? extends UI> uiClass) {
    Preconditions.checkArgument(ScopedUI.class.isAssignableFrom(uiClass),
            "ERROR: Invalid configuration - using ScopedUIProvider to instantiate no ScopedUI subclass");

    UIKey uiKey = uiKeyProvider.get();/* ww  w .j a v a2  s  .c o m*/
    // hold the key while UI is created
    CurrentInstance.set(UIKey.class, uiKey);
    // and set up the scope
    UIScope scope = UIScope.getCurrent();
    scope.startScope(uiKey);

    // create the UI
    ScopedUI ui = (ScopedUI) injector.getInstance(uiClass);
    ui.setInstanceKey(uiKey);
    ui.setScope(scope);

    ui.addDetachListener(createScopedUIDetachListener(uiClass, uiKey));

    logger.debug(String.format("Returning instance of [%s] with key [%s]", uiClass.getName(), uiKey));
    return ui;
}

From source file:com.google.code.vaadin.junit.AbstractMVPTestBase.java

License:Apache License

@Before
public void uiSetup() {
    // for the first test UI is initialized in the 'init' method above
    if (!isFirstTest) {
        logger.info("initialising test");

        ui = createTestUI(getTestUIClass());
        CurrentInstance.set(UI.class, ui);
        ui.doInit(mockedRequest, 1);//from  w  w w .ja  va 2  s . c o  m
        ui.setSession(mockedSession);
    }
}

From source file:de.appblocks.vaadin.cdi.TomcatCDIUIProvider.java

License:Apache License

@Override
public UI createInstance(UICreateEvent uiCreateEvent) {
    Class<? extends UI> type = uiCreateEvent.getUIClass();
    int uiId = uiCreateEvent.getUiId();
    VaadinRequest request = uiCreateEvent.getRequest();
    Bean<?> bean = scanForBeans(type);
    String uiMapping = "";
    if (bean == null) {
        if (type.isAnnotationPresent(CDIUI.class)) {
            uiMapping = parseUIMapping(request);
            bean = getUIBeanWithMapping(uiMapping);
        } else {/*from w w w .j  av  a2s. c o m*/
            throw new IllegalStateException("UI class: " + type.getName() + " with mapping: " + uiMapping
                    + " is not annotated with CDIUI!");
        }
    }
    UIBean uiBean = new UIBean(bean, uiId);
    try {
        // Make the UIBean available to UIScopedContext when creating nested
        // injected objects
        CurrentInstance.set(UIBean.class, uiBean);
        return (UI) getBeanManager().getReference(uiBean, type, getBeanManager().createCreationalContext(bean));
    } finally {
        CurrentInstance.set(UIBean.class, null);
    }
}

From source file:de.unioninvestment.eai.portal.support.vaadin.junit.LiferayContext.java

License:Apache License

private void initialize() {
    MockitoAnnotations.initMocks(this);

    UI.setCurrent(uiMock);//from  w  ww.  ja v a 2s.  co  m
    if (portletId != null) {
        when(uiMock.getPortletId()).thenReturn(portletId);
        when(uiMock.getCommunityId()).thenReturn(communityId);
    }

    when(uiMock.getPage()).thenReturn(pageMock);
    when(uiMock.getConnectorTracker()).thenReturn(connectorTrackerMock);

    when(pageMock.getJavaScript()).thenReturn(javascriptMock);

    CurrentInstance.set(VaadinResponse.class, vaadinPortletResponseMock);
    when(vaadinPortletResponseMock.getPortletResponse()).thenReturn(portletResponseMock);

    CurrentInstance.set(VaadinRequest.class, vaadinPortletRequestMock);
    when(vaadinPortletRequestMock.getPortletRequest()).thenReturn(portletRequestMock);
    when(portletRequestMock.getPortletSession()).thenReturn(portletSessionMock);
    when(portletSessionMock.getPortletContext()).thenReturn(portletContextMock);
    when(portletRequestMock.getPreferences()).thenReturn(portletPreferencesMock);
    when(vaadinPortletSessionMock.getConverterFactory()).thenReturn(new DefaultConverterFactory());
    when(vaadinPortletSessionMock.getPortletSession()).thenReturn(portletSessionMock);

    CurrentInstance.set(VaadinSession.class, vaadinPortletSessionMock);
    CurrentInstance.set(VaadinService.class, vaadinPortletServiceMock);
}

From source file:fixture.UITestBase.java

License:Apache License

@SuppressWarnings("deprecation")
protected ScopedUI createUI(final Class<? extends ScopedUI> clazz) {
    CurrentInstance.set(UI.class, null);
    CurrentInstance.set(UIKey.class, null);
    UICreateEvent event = mock(UICreateEvent.class);
    // when(event.getSource()).thenReturn(vaadinService);

    Answer<Class<? extends ScopedUI>> answer = new Answer<Class<? extends ScopedUI>>() {

        @Override/*w  ww .  j av a  2  s.c om*/
        public Class<? extends ScopedUI> answer(InvocationOnMock invocation) throws Throwable {
            return clazz;
        }
    };
    when(event.getUIClass()).thenAnswer(answer);
    ui = (ScopedUI) getUIProvider().createInstance(event);
    CurrentInstance.set(UI.class, ui);
    when(mockedRequest.getParameter("v-loc")).thenReturn(baseUri + "/");
    when(mockedSession.createConnectorId(Matchers.any(ClientConnector.class)))
            .thenAnswer(new ConnectorIdAnswer());
    ui.setSession(mockedSession);
    // ui.doInit(mockedRequest, 23);
    return ui;
}

From source file:org.simplewebstack.app.vaadin.guice.ui.ScopedUIProvider.java

License:Apache License

@Override
public UI createInstance(UICreateEvent event) {

    Class<? extends UI> uiClass = event.getUIClass();

    UIKey uiKey = uiKeyProvider.get();/*from   w w w  .j a v  a 2  s  . com*/

    // hold the key while UI is created
    CurrentInstance.set(UIKey.class, uiKey);
    // and set up the scope
    UIScope scope = UIScope.getCurrent();

    scope.startScope(uiKey);

    // create the UI
    ScopedUI ui = (ScopedUI) injector.getInstance(uiClass);
    ui.setInstanceKey(uiKey);
    ui.setScope(scope);

    logger.debug(String.format("Returning instance of [%s] with key [%s]", uiClass.getName(), uiKey));
    return ui;
}

From source file:org.vaadin.spring.servlet.internal.AbstractSpringAwareUIProvider.java

License:Apache License

@Override
public UI createInstance(UICreateEvent event) {
    final Class<UIID> key = UIID.class;
    final UIID identifier = new UIID(event);
    CurrentInstance.set(key, identifier);
    try {//from  w ww  . j a va 2s  .com
        logger.debug("Creating a new UI bean of class [{}] with identifier [{}]",
                event.getUIClass().getCanonicalName(), identifier);
        return webApplicationContext.getBean(event.getUIClass());
    } finally {
        CurrentInstance.set(key, null);
    }
}

From source file:org.vaadin.spring.test.MockUI.java

License:Apache License

/**
 * Sets up the Mock UI for the specified application context and current thread.
 *
 * @param applicationContext the application context. If the context is an instance of {@link org.springframework.context.ConfigurableApplicationContext},
 *                           the Mock UI instance will be registered with the real UI scope, which means that UI detachment
 *                           and {@code PreDestroy} methods will work as expected.
 *//*w w w .j  av  a  2 s .  c  o  m*/
public static synchronized void setUp(ApplicationContext applicationContext) {
    LOGGER.debug("Setting up MockUI for application context [{}]", applicationContext);
    final MockUI ui = new MockUI(++nextUIId);
    UI.setCurrent(ui);
    CurrentInstance.set(UIID.class, ui.getUiIdentifier());
    if (applicationContext instanceof ConfigurableApplicationContext) {
        final ConfigurableApplicationContext cac = (ConfigurableApplicationContext) applicationContext;
        final ConfigurableListableBeanFactory bf = cac.getBeanFactory();
        // Register the UI with the UI Scope (to get detach events, etc.)
        LOGGER.debug("Registering [{}] with the UI Scope", ui);
        bf.getRegisteredScope(UIScope.UI_SCOPE_NAME).get("mockUI", new ObjectFactory<MockUI>() {
            @Override
            public MockUI getObject() throws BeansException {
                return ui;
            }
        });
    } else {
        LOGGER.warn(
                "Application context was not configurable - the MockUI has not been registered with the UI Scope");
    }
}

From source file:org.vaadin.spring.test.MockUI.java

License:Apache License

/**
 * Detaches the Mock UI and clears it from the current thread. If the Mock UI was properly registered with the UI Scope
 * in {@link #setUp(org.springframework.context.ApplicationContext)}, the UI scope will also be destroyed.
 *//*from   w  ww.jav  a2s  . com*/
public static void tearDown() {
    if (UI.getCurrent() != null) {
        LOGGER.debug("Detaching MockUI [{}] after test", UI.getCurrent());
        UI.getCurrent().detach();
    }
    UI.setCurrent(null);
    CurrentInstance.set(UIID.class, null);
}

From source file:uk.q3c.krail.core.ui.ScopedUIProvider.java

License:Apache License

@Override
public UI createInstance(UICreateEvent event) {
    Class<? extends UI> uiClass = event.getUIClass();
    UIKey uiKey = uiKeyProvider.get();//w w  w .j av  a 2  s  . c om
    // hold the key while UI is created
    CurrentInstance.set(UIKey.class, uiKey);
    // and set up the scope
    UIScope scope = UIScope.getCurrent();
    scope.startScope(uiKey);
    // create the UI
    ScopedUI ui = (ScopedUI) injector.getInstance(uiClass);
    ui.setInstanceKey(uiKey);
    ui.setScope(scope);

    log.debug("Returning instance of {} with key {}", uiClass.getName(), uiKey);
    return ui;
}