KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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 org.eclipse.core.commands.contexts.Context;
14 import org.eclipse.core.commands.contexts.ContextManager;
15 import org.eclipse.ui.contexts.IContext;
16 import org.eclipse.ui.contexts.IContextListener;
17 import org.eclipse.ui.contexts.NotDefinedException;
18 import org.eclipse.ui.internal.util.Util;
19
20 /**
21  * This implements the old <code>IContext</code> interface based on the new
22  * context implementation in <code>org.eclipse.ui.contexts</code>. This is a
23  * wrapper.
24  *
25  * @since 3.1
26  */

27 public class ContextWrapper implements IContext {
28
29     /**
30      * The context manager that maintains the set of active contexts; must not
31      * be <code>null</code>.
32      */

33     private final ContextManager contextManager;
34
35     /**
36      * The wrapped instance of context. This value will never be
37      * <code>null</code>.
38      */

39     private final Context wrappedContext;
40
41     /**
42      * Constructs a new instance of <code>ContextWrapper</code>.
43      *
44      * @param context
45      * The context to wrapper; must not be <code>null</code>.
46      * @param contextManager
47      * The context manager that maintains the set of active contexts;
48      * must not be <code>null</code>.
49      */

50     public ContextWrapper(final Context context,
51             final ContextManager contextManager) {
52         if (context == null) {
53             throw new NullPointerException JavaDoc(
54                     "A wrapper cannot be created on a null context"); //$NON-NLS-1$
55
}
56
57         if (contextManager == null) {
58             throw new NullPointerException JavaDoc(
59                     "A wrapper cannot be created with a null manager"); //$NON-NLS-1$
60
}
61
62         wrappedContext = context;
63         this.contextManager = contextManager;
64     }
65
66     /*
67      * (non-Javadoc)
68      *
69      * @see org.eclipse.ui.contexts.IContext#addContextListener(org.eclipse.ui.contexts.IContextListener)
70      */

71     public void addContextListener(IContextListener contextListener) {
72         final ContextListenerWrapper wrapper = new ContextListenerWrapper(
73                 contextListener, contextManager, this);
74         wrappedContext.addContextListener(wrapper);
75
76         /*
77          * We need to add the listener to the context manager as well, as only
78          * the manager advertises changes to the enabled state.
79          */

80         contextManager.addContextManagerListener(wrapper);
81     }
82
83     /*
84      * (non-Javadoc)
85      *
86      * @see java.lang.Comparable#compareTo(T)
87      */

88     public int compareTo(Object JavaDoc o) {
89         return Util
90                 .compare(wrappedContext, ((ContextWrapper) o).wrappedContext);
91     }
92
93     /*
94      * (non-Javadoc)
95      *
96      * @see org.eclipse.ui.contexts.IContext#getId()
97      */

98     public String JavaDoc getId() {
99         return wrappedContext.getId();
100     }
101
102     /*
103      * (non-Javadoc)
104      *
105      * @see org.eclipse.ui.contexts.IContext#getName()
106      */

107     public String JavaDoc getName() throws NotDefinedException {
108         try {
109             return wrappedContext.getName();
110         } catch (final org.eclipse.core.commands.common.NotDefinedException e) {
111             throw new NotDefinedException(e);
112         }
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see org.eclipse.ui.contexts.IContext#getParentId()
119      */

120     public String JavaDoc getParentId() throws NotDefinedException {
121         try {
122             return wrappedContext.getParentId();
123         } catch (final org.eclipse.core.commands.common.NotDefinedException e) {
124             throw new NotDefinedException(e);
125         }
126     }
127
128     /*
129      * (non-Javadoc)
130      *
131      * @see org.eclipse.ui.contexts.IContext#isDefined()
132      */

133     public boolean isDefined() {
134         return wrappedContext.isDefined();
135     }
136
137     /*
138      * (non-Javadoc)
139      *
140      * @see org.eclipse.ui.contexts.IContext#isEnabled()
141      */

142     public boolean isEnabled() {
143         return contextManager.getActiveContextIds().contains(
144                 wrappedContext.getId());
145     }
146
147     /*
148      * (non-Javadoc)
149      *
150      * @see org.eclipse.ui.contexts.IContext#removeContextListener(org.eclipse.ui.contexts.IContextListener)
151      */

152     public void removeContextListener(IContextListener contextListener) {
153         final ContextListenerWrapper wrapper = new ContextListenerWrapper(
154                 contextListener, contextManager, this);
155         wrappedContext.removeContextListener(wrapper);
156         contextManager.removeContextManagerListener(wrapper);
157     }
158
159 }
160
Popular Tags