@Test
  public void testInjectWildCard() {
    IEclipseContext context = EclipseContextFactory.create();
    final Display d = Display.getDefault();

    context.set(Realm.class, DisplayRealm.getRealm(d));
    context.set(
        UISynchronize.class,
        new UISynchronize() {

          @Override
          public void syncExec(Runnable runnable) {
            d.syncExec(runnable);
          }

          @Override
          public void asyncExec(Runnable runnable) {
            d.asyncExec(runnable);
          }
        });

    InjectStarEvent target = ContextInjectionFactory.make(InjectStarEvent.class, context);

    // initial state
    assertEquals(0, target.counter1);
    assertNull(target.data);

    // send event
    helper.sendEvent("e4/test/eventInjection", "sample");

    assertEquals(1, target.counter1);
    assertEquals("sample", target.data);
  }
 @Override
 protected void createGUI(final MUIElement uiRoot) {
   Realm.runWithDefault(
       DisplayRealm.getRealm(display),
       new Runnable() {
         @Override
         public void run() {
           UIStartupTest.super.createGUI(uiRoot);
         }
       });
 }
 @Override
 public void testGetSecondPart_GetContext() {
   // need to wrap this since the renderer will try build the UI for the
   // part if it hasn't been built
   Realm.runWithDefault(
       DisplayRealm.getRealm(display),
       new Runnable() {
         @Override
         public void run() {
           UIStartupTest.super.testGetSecondPart_GetContext();
         }
       });
 }
  @Override
  protected void setUp() throws Exception {
    super.setUp();

    propertyName = "list";
    propertyDescriptor =
        ((IBeanProperty) BeanProperties.list(Bean.class, propertyName)).getPropertyDescriptor();
    bean = new Bean(new ArrayList());

    list =
        BeansObservables.observeList(
            DisplayRealm.getRealm(Display.getDefault()), bean, propertyName);
    beanObservable = (IBeanObservable) list;
  }
  @Override
  protected IEclipseContext createApplicationContext() {
    final IEclipseContext[] contexts = new IEclipseContext[1];
    Realm.runWithDefault(
        DisplayRealm.getRealm(display),
        new Runnable() {
          @Override
          public void run() {
            contexts[0] = UIStartupTest.super.createApplicationContext();
            contexts[0].set(IResourceUtilities.class.getName(), new ResourceUtility());
            contexts[0].set(
                IStylingEngine.class.getName(),
                new IStylingEngine() {
                  @Override
                  public void style(Object widget) {
                    // no-op
                  }

                  @Override
                  public void setId(Object widget, String id) {
                    // no-op
                  }

                  @Override
                  public void setClassname(Object widget, String classname) {
                    // no-op
                  }

                  @Override
                  public CSSStyleDeclaration getStyle(Object widget) {
                    // TODO Auto-generated method stub
                    return null;
                  }

                  @Override
                  public void setClassnameAndId(Object widget, String classname, String id) {
                    // no-op
                  }
                });
          }
        });
    return contexts[0];
  }
  public static void main(String[] args) {
    final Display display = new Display();

    // Set up data binding. In an RCP application, the threading Realm
    // will be set for you automatically by the Workbench. In an SWT
    // application, you can do this once, wrapping your binding
    // method call.
    Realm.runWithDefault(
        DisplayRealm.getRealm(display),
        new Runnable() {
          @Override
          public void run() {
            ViewModel viewModel = new ViewModel();
            Shell shell = new View(viewModel).createShell();

            // The SWT event loop
            while (!shell.isDisposed()) {
              if (!display.readAndDispatch()) {
                display.sleep();
              }
            }
          }
        });
  }
  @Test
  public void testEventInjection() {
    IInjector injector = InjectorFactory.getDefault();
    injector.addBinding(MyBinding.class);

    IEclipseContext context = EclipseContextFactory.create();
    final Display d = Display.getDefault();

    context.set(Realm.class, DisplayRealm.getRealm(d));
    context.set(
        UISynchronize.class,
        new UISynchronize() {

          @Override
          public void syncExec(Runnable runnable) {
            d.syncExec(runnable);
          }

          @Override
          public void asyncExec(Runnable runnable) {
            d.asyncExec(runnable);
          }
        });
    ContextInjectionFactory.setDefault(context);
    InjectTarget target = ContextInjectionFactory.make(InjectTarget.class, context);

    // initial state
    assertEquals(0, target.counter1);
    assertNull(target.string1);
    assertEquals(0, target.counter2);
    assertNull(target.string2);
    assertEquals(1, target.counter3);
    assertNull(target.string3);
    assertNotNull(target.myBinding);

    // send event1
    helper.sendEvent("e4/test/event1", "event1data");

    assertEquals(1, target.counter1);
    assertEquals("event1data", target.string1);
    assertEquals(0, target.counter2);
    assertNull(target.string2);
    assertEquals(1, target.counter3);
    assertNull(target.string3);
    assertNotNull(target.myBinding);

    // send event2
    helper.sendEvent("e4/test/event2", "event2data");

    assertEquals(1, target.counter1);
    assertEquals("event1data", target.string1);
    assertEquals(1, target.counter2);
    assertEquals("event2data", target.string2);
    assertEquals(1, target.counter3);
    assertNull(target.string3);
    assertNotNull(target.myBinding);

    // send event3
    helper.sendEvent("e4/test/event3", "event3data");

    assertEquals(1, target.counter1);
    assertEquals("event1data", target.string1);
    assertEquals(1, target.counter2);
    assertEquals("event2data", target.string2);
    assertEquals(2, target.counter3);
    assertEquals("event3data", target.string3);
    assertNotNull(target.myBinding);

    // send event1 again
    helper.sendEvent("e4/test/event1", "abc");

    assertEquals(2, target.counter1);
    assertEquals("abc", target.string1);
    assertEquals(1, target.counter2);
    assertEquals("event2data", target.string2);
    assertEquals(2, target.counter3);
    assertEquals("event3data", target.string3);
    assertNotNull(target.myBinding);
  }
Ejemplo n.º 8
0
  public E4Workbench createE4Workbench(
      IApplicationContext applicationContext, final Display display) {
    args = (String[]) applicationContext.getArguments().get(IApplicationContext.APPLICATION_ARGS);

    IEclipseContext appContext = createDefaultContext();
    appContext.set(Display.class, display);
    appContext.set(Realm.class, DisplayRealm.getRealm(display));
    appContext.set(
        UISynchronize.class,
        new UISynchronize() {

          @Override
          public void syncExec(Runnable runnable) {
            if (display != null && !display.isDisposed()) {
              display.syncExec(runnable);
            }
          }

          @Override
          public void asyncExec(Runnable runnable) {
            if (display != null && !display.isDisposed()) {
              display.asyncExec(runnable);
            }
          }
        });
    appContext.set(IApplicationContext.class, applicationContext);

    // This context will be used by the injector for its
    // extended data suppliers
    ContextInjectionFactory.setDefault(appContext);

    // Get the factory to create DI instances with
    IContributionFactory factory = appContext.get(IContributionFactory.class);

    // Install the life-cycle manager for this session if there's one
    // defined
    Optional<String> lifeCycleURI =
        getArgValue(IWorkbench.LIFE_CYCLE_URI_ARG, applicationContext, false);
    lifeCycleURI.ifPresent(
        lifeCycleURIValue -> {
          lcManager = factory.create(lifeCycleURIValue, appContext);
          if (lcManager != null) {
            // Let the manager manipulate the appContext if desired
            ContextInjectionFactory.invoke(lcManager, PostContextCreate.class, appContext, null);
          }
        });

    Optional<String> forcedPerspectiveId =
        getArgValue(PERSPECTIVE_ARG_NAME, applicationContext, false);
    forcedPerspectiveId.ifPresent(
        forcedPerspectiveIdValue ->
            appContext.set(E4Workbench.FORCED_PERSPECTIVE_ID, forcedPerspectiveIdValue));

    String showLocation = getLocationFromCommandLine();
    if (showLocation != null) {
      appContext.set(E4Workbench.FORCED_SHOW_LOCATION, showLocation);
    }

    // Create the app model and its context
    MApplication appModel = loadApplicationModel(applicationContext, appContext);
    appModel.setContext(appContext);

    boolean isRtl = ((Window.getDefaultOrientation() & SWT.RIGHT_TO_LEFT) != 0);
    appModel.getTransientData().put(E4Workbench.RTL_MODE, isRtl);

    // for compatibility layer: set the application in the OSGi service
    // context (see Workbench#getInstance())
    if (!E4Workbench.getServiceContext().containsKey(MApplication.class)) {
      // first one wins.
      E4Workbench.getServiceContext().set(MApplication.class, appModel);
    }

    // Set the app's context after adding itself
    appContext.set(MApplication.class, appModel);

    // adds basic services to the contexts
    initializeServices(appModel);

    // let the life cycle manager add to the model
    if (lcManager != null) {
      ContextInjectionFactory.invoke(lcManager, ProcessAdditions.class, appContext, null);
      ContextInjectionFactory.invoke(lcManager, ProcessRemovals.class, appContext, null);
    }

    // Create the addons
    IEclipseContext addonStaticContext = EclipseContextFactory.create();
    for (MAddon addon : appModel.getAddons()) {
      addonStaticContext.set(MAddon.class, addon);
      Object obj = factory.create(addon.getContributionURI(), appContext, addonStaticContext);
      addon.setObject(obj);
    }

    // Parse out parameters from both the command line and/or the product
    // definition (if any) and put them in the context
    Optional<String> xmiURI = getArgValue(IWorkbench.XMI_URI_ARG, applicationContext, false);
    xmiURI.ifPresent(
        xmiURIValue -> {
          appContext.set(IWorkbench.XMI_URI_ARG, xmiURIValue);
        });

    setCSSContextVariables(applicationContext, appContext);

    Optional<String> rendererFactoryURI =
        getArgValue(E4Workbench.RENDERER_FACTORY_URI, applicationContext, false);
    rendererFactoryURI.ifPresent(
        rendererFactoryURIValue -> {
          appContext.set(E4Workbench.RENDERER_FACTORY_URI, rendererFactoryURIValue);
        });

    // This is a default arg, if missing we use the default rendering engine
    Optional<String> presentationURI =
        getArgValue(IWorkbench.PRESENTATION_URI_ARG, applicationContext, false);
    appContext.set(
        IWorkbench.PRESENTATION_URI_ARG, presentationURI.orElse(PartRenderingEngine.engineURI));

    // Instantiate the Workbench (which is responsible for
    // 'running' the UI (if any)...
    return workbench = new E4Workbench(appModel, appContext);
  }
 @Override
 protected IObservable doCreateObservable() {
   return getObservableContractDelegate()
       .createObservable(DisplayRealm.getRealm(Display.getDefault()));
 }