Search in sources :

Example 11 with Active

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

the class RunAndTrackTest method testRunAndTrackComplex.

/**
 * There was a failing scenario in the legacy workbench support. This captures the hierarchy and
 * function (without any workbench level references). It should be updated when we figure out
 * the failing scenario :-)
 *
 * @throws Exception
 */
@Test
public void testRunAndTrackComplex() throws Exception {
    final IEclipseContext workbenchContext = getGlobalContext();
    workbenchContext.set("activePart", new ActivePartLookupFunction());
    final IEclipseContext[] windows = createNextLevel(workbenchContext, "window", 1);
    windows[0].runAndTrack(new RunAndTrack() {

        @Override
        public boolean changed(IEclipseContext context) {
            final Object part = windows[0].get(ACTIVE_PART);
            windows[0].set(ACTIVE_PART_ID, part);
            return true;
        }

        @Override
        public String toString() {
            return ACTIVE_PART_ID;
        }
    });
    final IEclipseContext[] mainSashes = createNextLevel(windows[0], "mainSash", 2);
    createNextLevel(mainSashes[1], "editorArea", 1);
    final IEclipseContext[] viewSashes = createNextLevel(mainSashes[0], "viewSashes", 2);
    // create package explorer stack
    final IEclipseContext[] packageStack = createNextLevel(viewSashes[0], "packageStack", 1);
    final IEclipseContext[] packageViews = createNextLevel(packageStack[0], "packageViews", 3);
    assertNotNull(packageViews);
    assertEquals("packageViews0", windows[0].get(ACTIVE_PART));
    assertEquals("packageViews0", windows[0].get(ACTIVE_PART_ID));
    // create problems stack
    final IEclipseContext[] problemsStack = createNextLevel(viewSashes[1], "problemsStack", 1);
    final IEclipseContext[] problemsViews = createNextLevel(problemsStack[0], "problemViews", 5);
    assertNotNull(problemsViews);
    assertEquals("packageViews0", windows[0].get(ACTIVE_PART));
    assertEquals("packageViews0", windows[0].get(ACTIVE_PART_ID));
    assertEquals("problemViews0", problemsStack[0].get(ACTIVE_PART));
    // this won't change since it is a "runAndTrack" at the window context
    // level
    assertEquals("packageViews0", problemsStack[0].get(ACTIVE_PART_ID));
    // set the "problems view" active, propagating the information up
    // the active chain.
    problemsStack[0].set(ACTIVE_CHILD, problemsViews[0]);
    viewSashes[1].set(ACTIVE_CHILD, problemsStack[0]);
    mainSashes[0].set(ACTIVE_CHILD, viewSashes[1]);
    windows[0].set(ACTIVE_CHILD, mainSashes[0]);
    workbenchContext.set(ACTIVE_CHILD, windows[0]);
    assertEquals("problemViews0", windows[0].get(ACTIVE_PART));
    assertEquals("problemViews0", windows[0].get(ACTIVE_PART_ID));
    assertEquals("packageViews0", packageStack[0].get(ACTIVE_PART));
    assertEquals("problemViews0", packageStack[0].get(ACTIVE_PART_ID));
}
Also used : RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 12 with Active

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

the class EclipseContext method deactivate.

@Override
public void deactivate() {
    EclipseContext parent = getParent();
    if (parent == null)
        return;
    if (this != parent.getActiveChild())
        // this is not an active context; return
        return;
    parent.set(ACTIVE_CHILD, null);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext)

Example 13 with Active

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

the class EclipseContext method dispose.

@Override
public void dispose() {
    // dispose of child contexts first
    for (EclipseContext childContext : getChildren()) {
        childContext.dispose();
    }
    ContextChangeEvent event = new ContextChangeEvent(this, ContextChangeEvent.DISPOSE, null, null, null);
    Set<Computation> allComputations = new HashSet<>();
    allComputations.addAll(activeComputations.values());
    allComputations.addAll(activeRATs);
    activeComputations.clear();
    activeRATs.clear();
    Set<Scheduled> scheduled = new LinkedHashSet<>();
    allComputations.addAll(getListeners());
    weakListeners.clear();
    for (Computation computation : allComputations) {
        computation.handleInvalid(event, scheduled);
    }
    processScheduled(scheduled);
    synchronized (notifyOnDisposal) {
        for (IContextDisposalListener listener : notifyOnDisposal) {
            listener.disposed(this);
        }
        notifyOnDisposal.clear();
    }
    for (ValueComputation computation : localValueComputations.values()) {
        computation.dipose();
    }
    localValueComputations.clear();
    // if this was the parent's active child, deactivate it
    EclipseContext parent = getParent();
    EclipseContext rootContext = null;
    if (parent != null) {
        rootContext = getRoot();
        if (this == parent.getActiveChild())
            parent.set(ACTIVE_CHILD, null);
    }
    localValues.clear();
    if (parent != null) {
        parent.removeChild(this);
        if (rootContext != null) {
            rootContext.cleanup();
        }
        // inform the OSGi layer via EventAdmin about the context disposal
        // used for example to be able to cleanup cached requestors in
        // ExtendedObjectSupplier implementations
        EventAdmin admin = parent.get(EventAdmin.class);
        if (admin != null) {
            Event osgiEvent = new Event(IEclipseContext.TOPIC_DISPOSE, (Map<String, ?>) null);
            admin.postEvent(osgiEvent);
        }
    }
    if (debugAddOn != null)
        debugAddOn.notify(this, IEclipseContextDebugger.EventType.DISPOSED, null);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EventAdmin(org.osgi.service.event.EventAdmin) Event(org.osgi.service.event.Event) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 14 with Active

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

the class OSGiObjectSupplier method get.

@Override
public Object get(IObjectDescriptor descriptor, IRequestor requestor, boolean track, boolean group) {
    final Class<?> requestingObjectClass = requestor.getRequestingObjectClass();
    final Type desiredType = descriptor.getDesiredType();
    if (BundleContext.class.equals(desiredType)) {
        final Bundle bundle = FrameworkUtil.getBundle(requestingObjectClass);
        // Iff track is request and there is no listener yet, lets track the bundle
        if (track) {
            if (!requestor2listener.containsKey(requestor)) {
                track(bundle, requestor);
            }
        // Handlers only executed once and thus don't track the BC/Bundle.
        // Still guard to now de-register a non-existing listener.
        } else if (requestor2listener.containsKey(requestor)) {
            untrack(requestor);
        }
        final BundleContext bundleContext = bundle.getBundleContext();
        if (bundleContext != null) {
            return bundleContext;
        } else if (descriptor.getQualifier(Optional.class) != null) {
            // Do not have a bundle context but requestor has marked the parameter/field optional
            return null;
        }
        // $NON-NLS-1$  //$NON-NLS-2$
        throw new InjectionException("Unable to inject BundleContext: " + bundle.getSymbolicName() + " bundle is not active or starting/stopping");
    } else if (Bundle.class.equals(desiredType)) {
        // state. However, the requestor will go away along with its bundle anyway.
        return FrameworkUtil.getBundle(requestingObjectClass);
    }
    // Annotation used with unsupported type
    return null;
}
Also used : InjectionException(org.eclipse.e4.core.di.InjectionException) Type(java.lang.reflect.Type) Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext)

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