001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.contexts;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.core.expressions.Expression;
015: import org.eclipse.ui.contexts.IContextActivation;
016: import org.eclipse.ui.contexts.IContextService;
017: import org.eclipse.ui.internal.services.INestable;
018:
019: /**
020: * <p>
021: * A context service which delegates almost all responsibility to the parent
022: * service. This service is capable of being nested inside a component that is
023: * not recognized by the "source" event mechanism.
024: * </p>
025: * <p>
026: * This class is not intended for use outside of the
027: * <code>org.eclipse.ui.workbench</code> plug-in.
028: * </p>
029: *
030: * @since 3.2
031: */
032: public class NestableContextService extends SlaveContextService
033: implements INestable {
034: /**
035: * Maintain the state of the context service.
036: */
037: private boolean fActive;
038:
039: /**
040: * Construct the new nested slave context.
041: *
042: * @param parentService
043: * the parent context service; must not be <code>null</code>.
044: * @param defaultExpression
045: * A default expression to use to determine viability. It's
046: * mainly used for conflict resolution. It can be
047: * <code>null</code>.
048: */
049: public NestableContextService(IContextService parentService,
050: Expression defaultExpression) {
051: super (parentService, defaultExpression);
052: fActive = false;
053: }
054:
055: /*
056: * (non-Javadoc)
057: *
058: * @see org.eclipse.ui.internal.contexts.SlaveContextService#doActivateContext(org.eclipse.ui.contexts.IContextActivation)
059: */
060: protected IContextActivation doActivateContext(
061: IContextActivation activation) {
062: if (fActive) {
063: return super .doActivateContext(activation);
064: }
065: fLocalActivations.put(activation, null);
066: return activation;
067: }
068:
069: /*
070: * (non-Javadoc)
071: *
072: * @see org.eclipse.ui.internal.services.INestable#activate()
073: */
074: public void activate() {
075: if (fActive) {
076: return;
077: }
078:
079: Iterator c = fLocalActivations.keySet().iterator();
080: while (c.hasNext()) {
081: IContextActivation activation = (IContextActivation) c
082: .next();
083: super .doActivateContext(activation);
084: }
085: fActive = true;
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see org.eclipse.ui.internal.services.INestable#deactivate()
092: */
093: public void deactivate() {
094: if (!fActive) {
095: return;
096: }
097: deactivateContexts(fParentActivations);
098: fParentActivations.clear();
099:
100: Iterator c = fLocalActivations.keySet().iterator();
101: while (c.hasNext()) {
102: fLocalActivations.put(c.next(), null);
103: }
104: fActive = false;
105: }
106: }
|