KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > contexts > ContextListenerWrapper


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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 package org.eclipse.ui.internal.contexts;
12
13 import java.util.Set JavaDoc;
14
15 import org.eclipse.core.commands.contexts.ContextEvent;
16 import org.eclipse.core.commands.contexts.ContextManager;
17 import org.eclipse.core.commands.contexts.ContextManagerEvent;
18 import org.eclipse.core.commands.contexts.IContextListener;
19 import org.eclipse.core.commands.contexts.IContextManagerListener;
20 import org.eclipse.ui.contexts.IContext;
21
22 /**
23  * <p>
24  * This wraps an old context listener so it supports the new API. This is used
25  * to support attaching old-style listens to the new context objects.
26  * </p>
27  *
28  * @since 3.1
29  */

30 public class ContextListenerWrapper implements IContextListener,
31         IContextManagerListener {
32
33     /**
34      * The legacy context that this listener would previously have been attached
35      * to. This value is never <code>null</code>.
36      */

37     private final IContext context;
38
39     /**
40      * The context manager used for constructing the context wrapper when an
41      * event occurs; must not be <code>null</code>.
42      */

43     private final ContextManager contextManager;
44
45     /**
46      * The listener to be wrapped. This value is never <code>null</code>.
47      */

48     private final org.eclipse.ui.contexts.IContextListener wrappedListener;
49
50     /**
51      * Constructs a new instance of <code>ContextListenerWrapper</code>.
52      *
53      * @param listener
54      * The listener to be wrapped. Must not be <code>null</code>.
55      * @param contextManager
56      * The context manager used for constructing the context wrapper
57      * when an event occurs; must not be <code>null</code>.
58      * @param context
59      * The legacy context this listener will be listening to; must
60      * not be <code>null</code>.
61      */

62     public ContextListenerWrapper(
63             final org.eclipse.ui.contexts.IContextListener listener,
64             final ContextManager contextManager, final IContext context) {
65         if (listener == null) {
66             throw new NullPointerException JavaDoc(
67                     "Cannot create a listener wrapper on a null listener"); //$NON-NLS-1$
68
}
69
70         if (contextManager == null) {
71             throw new NullPointerException JavaDoc(
72                     "Cannot create a listener wrapper with a null context manager"); //$NON-NLS-1$
73
}
74
75         if (context == null) {
76             throw new NullPointerException JavaDoc(
77                     "Cannot create a listener wrapper with a null context"); //$NON-NLS-1$
78
}
79
80         wrappedListener = listener;
81         this.contextManager = contextManager;
82         this.context = context;
83     }
84
85     /*
86      * (non-Javadoc)
87      *
88      * @see org.eclipse.core.commands.contexts.IContextListener#contextChanged(org.eclipse.core.commands.contexts.ContextEvent)
89      */

90     public final void contextChanged(final ContextEvent contextEvent) {
91         wrappedListener
92                 .contextChanged(new org.eclipse.ui.contexts.ContextEvent(
93                         new ContextWrapper(contextEvent.getContext(),
94                                 contextManager), contextEvent
95                                 .isDefinedChanged(), false, contextEvent
96                                 .isNameChanged(), contextEvent
97                                 .isParentIdChanged()));
98     }
99
100     public final void contextManagerChanged(final ContextManagerEvent event) {
101         final String JavaDoc contextId = context.getId();
102         final boolean enabledChanged;
103         if (event.isActiveContextsChanged()) {
104             final Set JavaDoc previousContexts = event.getPreviouslyActiveContextIds();
105             final Set JavaDoc currentContexts = contextManager.getActiveContextIds();
106             if ((previousContexts != null)
107                     && (previousContexts.contains(contextId))
108                     && ((currentContexts == null) || (currentContexts
109                             .contains(contextId)))) {
110                 enabledChanged = true;
111             } else if ((currentContexts != null)
112                     && (currentContexts.contains(contextId))
113                     && ((previousContexts == null) || (previousContexts
114                             .contains(contextId)))) {
115                 enabledChanged = true;
116             } else {
117                 enabledChanged = false;
118             }
119         } else {
120             enabledChanged = false;
121         }
122
123         wrappedListener
124                 .contextChanged(new org.eclipse.ui.contexts.ContextEvent(
125                         context, false, enabledChanged, false, false));
126     }
127
128     public final boolean equals(final Object JavaDoc object) {
129         if (object instanceof ContextListenerWrapper) {
130             final ContextListenerWrapper other = (ContextListenerWrapper) object;
131             return wrappedListener.equals(other.wrappedListener);
132         }
133
134         if (object instanceof org.eclipse.ui.contexts.IContextListener) {
135             final org.eclipse.ui.contexts.IContextListener other = (org.eclipse.ui.contexts.IContextListener) object;
136             return wrappedListener.equals(other);
137         }
138
139         return false;
140     }
141
142     public final int hashCode() {
143         return wrappedListener.hashCode();
144     }
145 }
146
Popular Tags