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.handlers;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.core.commands.IHandler;
015: import org.eclipse.core.expressions.Expression;
016: import org.eclipse.ui.handlers.IHandlerActivation;
017: import org.eclipse.ui.handlers.IHandlerService;
018: import org.eclipse.ui.internal.services.INestable;
019:
020: /**
021: * <p>
022: * A handler service which delegates almost all responsibility to the parent
023: * service. It is capable of being nested inside a component that is not
024: * recognizable by the "sources" event mechanism. This means that the handlers
025: * must be activated and deactivated manually.
026: * </p>
027: * <p>
028: * This class is not intended for use outside of the
029: * <code>org.eclipse.ui.workbench</code> plug-in.
030: * </p>
031: *
032: * @since 3.2
033: */
034: public final class NestableHandlerService extends SlaveHandlerService
035: implements INestable {
036:
037: /**
038: * Whether the component with which this service is associated is active.
039: */
040: private boolean active = false;
041:
042: /**
043: * Constructs a new instance.
044: *
045: * @param parentHandlerService
046: * The parent handler service for this slave; must not be
047: * <code>null</code>.
048: * @param defaultExpression
049: * The default expression to use if none is provided. This is
050: * primarily used for conflict resolution. This value may be
051: * <code>null</code>.
052: */
053: public NestableHandlerService(
054: final IHandlerService parentHandlerService,
055: final Expression defaultExpression) {
056: super (parentHandlerService, defaultExpression);
057: }
058:
059: public final void activate() {
060: if (active) {
061: return;
062: }
063:
064: final Iterator localActivationItr = localActivationsToParentActivations
065: .keySet().iterator();
066: while (localActivationItr.hasNext()) {
067: final Object object = localActivationItr.next();
068: if (object instanceof IHandlerActivation) {
069: final IHandlerActivation localActivation = (IHandlerActivation) object;
070: final String commandId = localActivation.getCommandId();
071: final IHandler handler = localActivation.getHandler();
072: final IHandlerActivation parentActivation = parent
073: .activateHandler(commandId, handler,
074: defaultExpression);
075: parentActivations.add(parentActivation);
076: localActivationsToParentActivations.put(
077: localActivation, parentActivation);
078: }
079: }
080:
081: active = true;
082: }
083:
084: protected final IHandlerActivation doActivation(
085: final IHandlerActivation localActivation) {
086: final IHandlerActivation parentActivation;
087: if (active) {
088: parentActivation = parent.activateHandler(localActivation);
089: parentActivations.add(parentActivation);
090: } else {
091: parentActivation = null;
092: }
093: localActivationsToParentActivations.put(localActivation,
094: parentActivation);
095: return localActivation;
096: }
097:
098: public final void deactivate() {
099: if (!active) {
100: return;
101: }
102:
103: deactivateHandlers(parentActivations);
104: parentActivations.clear();
105:
106: final Iterator localActivationItr = localActivationsToParentActivations
107: .keySet().iterator();
108: while (localActivationItr.hasNext()) {
109: final Object object = localActivationItr.next();
110: localActivationsToParentActivations.put(object, null);
111: }
112:
113: active = false;
114: }
115: }
|