Search in sources :

Example 1 with Active

use of org.eclipse.e4.core.contexts.Active in project ACS by ACS-Community.

the class PeriodicRefreshHandler method execute.

/**
	 * Starts a {@link NotifyServiceUpdateJob} with 10 seconds refresh period,
	 * which will notify the interested parts via the IEventBroker.
	 * <p>
	 * An alternative implementation could get parameter <code>@Active MPart part</code> injected
	 * and notify the part directly, without IEventBroker.
	 */
@Execute
public void execute(MHandledItem handledItem) {
    // sync menu item state
    sharedIsSelected = handledItem.isSelected();
    for (MItem menuItem : menuItemsToSync.values()) {
        menuItem.setSelected(sharedIsSelected);
    }
    // start or stop the periodic refresh job
    if (sharedIsSelected) {
        if (job == null) {
            // start periodic refreshes
            job = new NotifyServiceUpdateJob(eventModel, sync, statusLineWriter, eventBroker, refreshDelaySeconds * 1000);
            job.schedule();
            System.out.println("Scheduled refresh job.");
        } else {
            System.out.println("Error: refresh job is already running!");
        }
    } else {
        if (job != null) {
            job.cancel();
            job = null;
            System.out.println("Cancelled refresh job.");
        } else {
            System.out.println("Error: refresh job is already cancelled!");
        }
    }
}
Also used : MItem(org.eclipse.e4.ui.model.application.ui.menu.MItem) CanExecute(org.eclipse.e4.core.di.annotations.CanExecute) Execute(org.eclipse.e4.core.di.annotations.Execute)

Example 2 with Active

use of org.eclipse.e4.core.contexts.Active in project whole by wholeplatform.

the class AbstractE4DerivedGraphicalPart method createSelectionLinkable.

protected ILinkableSelectionListener createSelectionLinkable(IEntityPartViewer viewer) {
    IEclipseContext params = EclipseContextFactory.create();
    // we need to pass the viewer because it has not been set in the active context
    params.set(IEntityPartViewer.class, viewer);
    return ContextInjectionFactory.make(DerivedLinkableSelectionListener.class, context, configureSelectionLinkable(params));
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext)

Example 3 with Active

use of org.eclipse.e4.core.contexts.Active in project eclipse.platform.runtime by eclipse.

the class InvokeInRATTest method testStaticInvoke.

@Test
public void testStaticInvoke() {
    IEclipseContext context = EclipseContextFactory.create();
    final int[] count = new int[1];
    context.runAndTrack(new RunAndTrack() {

        @Override
        public boolean changed(IEclipseContext context) {
            TestHandler handler = (TestHandler) context.get("handlerA");
            if (handler != null) {
                ContextInjectionFactory.invoke(handler, CanExecute.class, context);
                count[0]++;
            }
            // continue to be notified
            return true;
        }
    });
    // check that updates are propagated
    context.set("active", Integer.valueOf(123));
    context.set("selected", "abc");
    TestHandler handler = new TestHandler();
    context.set("handlerA", handler);
    assertEquals(Integer.valueOf(123), handler.active);
    assertEquals("abc", handler.selected);
    // check handler replacement
    count[0] = 0;
    TestHandler newHandler = new TestHandler();
    context.set("handlerA", newHandler);
    assertEquals(1, count[0]);
    assertEquals(Integer.valueOf(123), newHandler.active);
    assertEquals("abc", newHandler.selected);
    // change values in the context; values should not be propagated to handlers
    context.set("active", Integer.valueOf(456));
    context.set("selected", "xyz");
    assertEquals(Integer.valueOf(123), handler.active);
    assertEquals("abc", handler.selected);
    assertEquals(Integer.valueOf(123), newHandler.active);
    assertEquals("abc", newHandler.selected);
}
Also used : RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) CanExecute(org.eclipse.e4.core.di.annotations.CanExecute) Test(org.junit.Test)

Example 4 with Active

use of org.eclipse.e4.core.contexts.Active in project eclipse.platform.runtime by eclipse.

the class ContextObjectSupplier method get.

@Override
public void get(IObjectDescriptor[] descriptors, Object[] actualArgs, final IRequestor requestor, boolean initial, boolean track, boolean group) {
    final String[] keys = new String[descriptors.length];
    final boolean[] active = new boolean[descriptors.length];
    for (int i = 0; i < descriptors.length; i++) {
        String key = getKey(descriptors[i]);
        if ((actualArgs[i] == IInjector.NOT_A_VALUE))
            keys[i] = key;
        else if (// allow provider to override IEclipseContext
        ECLIPSE_CONTEXT_NAME.equals(key))
            keys[i] = ECLIPSE_CONTEXT_NAME;
        else
            keys[i] = null;
        if (descriptors[i] == null)
            active[i] = false;
        else
            active[i] = (descriptors[i].hasQualifier(Active.class));
    }
    if (requestor != null && track) {
        // only track if requested
        if (initial) {
            RunAndTrack trackable = new ContextInjectionListener(context, actualArgs, keys, active, requestor, group);
            context.runAndTrack(trackable);
        } else {
            // we do track if this is done inside a computation, but don't create another runnable
            fillArgs(actualArgs, keys, active);
        }
    } else {
        if (descriptors.length > 0) {
            pauseRecording();
            try {
                fillArgs(actualArgs, keys, active);
            } finally {
                resumeRecording();
            }
        }
    }
}
Also used : Active(org.eclipse.e4.core.contexts.Active) RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack)

Example 5 with Active

use of org.eclipse.e4.core.contexts.Active in project eclipse.platform.runtime by eclipse.

the class ActivationTest method testGetActive.

@Test
public void testGetActive() {
    IEclipseContext root = EclipseContextFactory.create("root");
    IEclipseContext child1 = root.createChild("child1");
    IEclipseContext child11 = child1.createChild("child11");
    IEclipseContext child12 = child1.createChild("child12");
    IEclipseContext child2 = root.createChild("child2");
    IEclipseContext child21 = child2.createChild("child21");
    IEclipseContext child22 = child2.createChild("child22");
    child11.set("var", "1");
    child12.set("var", "2");
    child1.set("var", "3");
    child21.set("var", "4");
    child22.set("var", "5");
    child2.set("var", "6");
    root.set("var", "7");
    // nothing is active - we get value from the node
    assertEquals("3", child1.getActive("var"));
    child11.activateBranch();
    assertEquals("1", child1.getActive("var"));
    child12.activateBranch();
    assertEquals("2", child1.getActive("var"));
    child22.activateBranch();
    assertEquals("5", child2.getActive("var"));
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Aggregations

IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)11 Test (org.junit.Test)7 RunAndTrack (org.eclipse.e4.core.contexts.RunAndTrack)6 CanExecute (org.eclipse.e4.core.di.annotations.CanExecute)2 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Active (org.eclipse.e4.core.contexts.Active)1 InjectionException (org.eclipse.e4.core.di.InjectionException)1 Execute (org.eclipse.e4.core.di.annotations.Execute)1 MItem (org.eclipse.e4.ui.model.application.ui.menu.MItem)1 Bundle (org.osgi.framework.Bundle)1 BundleContext (org.osgi.framework.BundleContext)1 Event (org.osgi.service.event.Event)1 EventAdmin (org.osgi.service.event.EventAdmin)1