KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > handlers > NestableHandlerService


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.internal.handlers;
13
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.commands.IHandler;
17 import org.eclipse.core.expressions.Expression;
18 import org.eclipse.ui.handlers.IHandlerActivation;
19 import org.eclipse.ui.handlers.IHandlerService;
20 import org.eclipse.ui.internal.services.INestable;
21
22 /**
23  * <p>
24  * A handler service which delegates almost all responsibility to the parent
25  * service. It is capable of being nested inside a component that is not
26  * recognizable by the "sources" event mechanism. This means that the handlers
27  * must be activated and deactivated manually.
28  * </p>
29  * <p>
30  * This class is not intended for use outside of the
31  * <code>org.eclipse.ui.workbench</code> plug-in.
32  * </p>
33  *
34  * @since 3.2
35  */

36 public final class NestableHandlerService extends SlaveHandlerService implements
37         INestable {
38
39     /**
40      * Whether the component with which this service is associated is active.
41      */

42     private boolean active = false;
43
44     /**
45      * Constructs a new instance.
46      *
47      * @param parentHandlerService
48      * The parent handler service for this slave; must not be
49      * <code>null</code>.
50      * @param defaultExpression
51      * The default expression to use if none is provided. This is
52      * primarily used for conflict resolution. This value may be
53      * <code>null</code>.
54      */

55     public NestableHandlerService(final IHandlerService parentHandlerService,
56             final Expression defaultExpression) {
57         super(parentHandlerService, defaultExpression);
58     }
59
60     public final void activate() {
61         if (active) {
62             return;
63         }
64
65         final Iterator JavaDoc localActivationItr = localActivationsToParentActivations
66                 .keySet().iterator();
67         while (localActivationItr.hasNext()) {
68             final Object JavaDoc object = localActivationItr.next();
69             if (object instanceof IHandlerActivation) {
70                 final IHandlerActivation localActivation = (IHandlerActivation) object;
71                 final String JavaDoc commandId = localActivation.getCommandId();
72                 final IHandler handler = localActivation.getHandler();
73                 final IHandlerActivation parentActivation = parent
74                         .activateHandler(commandId, handler, defaultExpression);
75                 parentActivations.add(parentActivation);
76                 localActivationsToParentActivations.put(localActivation,
77                         parentActivation);
78             }
79         }
80
81         active = true;
82     }
83
84     protected final IHandlerActivation doActivation(
85             final IHandlerActivation localActivation) {
86         final IHandlerActivation parentActivation;
87         if (active) {
88             parentActivation = parent.activateHandler(localActivation);
89             parentActivations.add(parentActivation);
90         } else {
91             parentActivation = null;
92         }
93         localActivationsToParentActivations.put(localActivation,
94                 parentActivation);
95         return localActivation;
96     }
97
98     public final void deactivate() {
99         if (!active) {
100             return;
101         }
102
103         deactivateHandlers(parentActivations);
104         parentActivations.clear();
105
106         final Iterator JavaDoc localActivationItr = localActivationsToParentActivations
107                 .keySet().iterator();
108         while (localActivationItr.hasNext()) {
109             final Object JavaDoc object = localActivationItr.next();
110             localActivationsToParentActivations.put(object, null);
111         }
112
113         active = false;
114     }
115 }
116
117
Popular Tags