KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.ui.internal.contexts;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.SortedSet JavaDoc;
20 import java.util.TreeSet JavaDoc;
21
22 import org.eclipse.core.commands.common.NotDefinedException;
23 import org.eclipse.core.commands.contexts.Context;
24 import org.eclipse.core.commands.contexts.ContextManager;
25 import org.eclipse.ui.contexts.ContextManagerEvent;
26 import org.eclipse.ui.contexts.IContext;
27 import org.eclipse.ui.contexts.IContextManager;
28 import org.eclipse.ui.contexts.IContextManagerListener;
29
30 /**
31  * A wrapper around the new API that supports the old API. This manager also
32  * adds support for reading from the registry.
33  *
34  * @since 3.1
35  */

36 public final class ContextManagerWrapper implements
37         org.eclipse.core.commands.contexts.IContextManagerListener,
38         IContextManager {
39
40     /**
41      * A comparator between context identifiers, that sorts them based on depth
42      * within the tree. Context identifiers representing deeper items (i.e.,
43      * items with more ancestors), have lesser values (i.e., would appear
44      * earlier in a set).
45      *
46      * @since 3.0
47      */

48     private class ContextIdDepthComparator implements Comparator JavaDoc {
49
50         /**
51          * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
52          */

53         public final int compare(final Object JavaDoc object1, final Object JavaDoc object2) {
54             final String JavaDoc contextId1 = (String JavaDoc) object1;
55             final String JavaDoc contextId2 = (String JavaDoc) object2;
56             Context context;
57             String JavaDoc parentId;
58
59             // Get the depth of the first context.
60
int depth1 = 0;
61             context = contextManager.getContext(contextId1);
62             try {
63                 parentId = context.getParentId();
64                 while (parentId != null) {
65                     depth1++;
66                     context = contextManager.getContext(parentId);
67                     parentId = context.getParentId();
68                 }
69             } catch (final NotDefinedException e) {
70                 // Do nothing. Stop ascending the ancestry.
71
}
72
73             // Get the depth of the second context.
74
int depth2 = 0;
75             context = contextManager.getContext(contextId2);
76             try {
77                 parentId = context.getParentId();
78                 while (parentId != null) {
79                     depth2++;
80                     context = contextManager.getContext(parentId);
81                     parentId = context.getParentId();
82                 }
83             } catch (final NotDefinedException e) {
84                 // Do nothing. Stop ascending the ancestry.
85
}
86
87             // If the contexts are equal depth, then use their identifier.
88
int compare = depth2 - depth1;
89             if (compare == 0) {
90                 compare = contextId1.compareTo(contextId2);
91             }
92
93             return compare;
94         }
95     }
96
97     /**
98      * A set that contains context identifiers (strings). The set is sorted
99      * based on how many ancestors the corresponding contexts have. Contexts
100      * with no parents appear last, while contexts with the most ancestors
101      * appear first.
102      *
103      * @since 3.0
104      */

105     private class DepthSortedContextIdSet extends TreeSet JavaDoc {
106
107         /**
108          * Generated serial version UID for this class.
109          *
110          * @since 3.1
111          */

112         private static final long serialVersionUID = 3257291326872892465L;
113
114         /**
115          * Constructs a new instance of <code>DepthSortedContextIdSet</code>
116          * with the set to be sorted.
117          *
118          * @param contextIds
119          * A set of context identifiers (strings); this may contain
120          * <code>null</code> values. The set may not be
121          * <code>null</code>, but may be empty.
122          */

123         private DepthSortedContextIdSet(final Set JavaDoc contextIds) {
124             super(new ContextIdDepthComparator());
125             addAll(contextIds);
126         }
127     }
128
129     private final ContextManager contextManager;
130
131     private List JavaDoc contextManagerListeners;
132
133     /**
134      * Constructs a new instance of <code>MutableContextManager</code>. The
135      * registry is created on the platform's extension registry.
136      *
137      * @param contextManager
138      * The manager which will provided the real support; must not be
139      * <code>null</code>.
140      */

141     public ContextManagerWrapper(ContextManager contextManager) {
142
143         if (contextManager == null) {
144             throw new NullPointerException JavaDoc("The context manager cannot be null"); //$NON-NLS-1$
145
}
146
147         this.contextManager = contextManager;
148         this.contextManager.addContextManagerListener(this);
149     }
150
151     public void addContextManagerListener(
152             IContextManagerListener contextManagerListener) {
153         if (contextManagerListener == null)
154             throw new NullPointerException JavaDoc();
155
156         if (contextManagerListeners == null)
157             contextManagerListeners = new ArrayList JavaDoc();
158
159         if (!contextManagerListeners.contains(contextManagerListener))
160             contextManagerListeners.add(contextManagerListener);
161     }
162
163     /*
164      * (non-Javadoc)
165      *
166      * @see org.eclipse.core.commands.contexts.IContextManagerListener#contextManagerChanged(org.eclipse.core.commands.contexts.ContextManagerEvent)
167      */

168     public void contextManagerChanged(
169             org.eclipse.core.commands.contexts.ContextManagerEvent contextManagerEvent) {
170         final String JavaDoc contextId = contextManagerEvent.getContextId();
171         final boolean definedContextsChanged;
172         final Set JavaDoc previouslyDefinedContextIds;
173         if (contextId == null) {
174             definedContextsChanged = false;
175             previouslyDefinedContextIds = null;
176         } else {
177             definedContextsChanged = true;
178             previouslyDefinedContextIds = new HashSet JavaDoc();
179             previouslyDefinedContextIds.addAll(contextManager
180                     .getDefinedContextIds());
181             if (contextManagerEvent.isContextDefined()) {
182                 previouslyDefinedContextIds.remove(contextId);
183             } else {
184                 previouslyDefinedContextIds.add(contextId);
185             }
186         }
187
188         fireContextManagerChanged(new ContextManagerEvent(this,
189                 definedContextsChanged, contextManagerEvent
190                         .isActiveContextsChanged(),
191                 previouslyDefinedContextIds, contextManagerEvent
192                         .getPreviouslyActiveContextIds()));
193
194     }
195
196     protected void fireContextManagerChanged(
197             ContextManagerEvent contextManagerEvent) {
198         if (contextManagerEvent == null)
199             throw new NullPointerException JavaDoc();
200
201         if (contextManagerListeners != null)
202             for (int i = 0; i < contextManagerListeners.size(); i++)
203                 ((IContextManagerListener) contextManagerListeners.get(i))
204                         .contextManagerChanged(contextManagerEvent);
205     }
206
207     public IContext getContext(String JavaDoc contextId) {
208         return new ContextWrapper(contextManager.getContext(contextId),
209                 contextManager);
210     }
211
212     public SortedSet JavaDoc getDefinedContextIds() {
213         return new DepthSortedContextIdSet(contextManager
214                 .getDefinedContextIds());
215     }
216
217     public SortedSet JavaDoc getEnabledContextIds() {
218         return new DepthSortedContextIdSet(contextManager.getActiveContextIds());
219     }
220
221     public void removeContextManagerListener(
222             IContextManagerListener contextManagerListener) {
223         if (contextManagerListener == null)
224             throw new NullPointerException JavaDoc();
225
226         if (contextManagerListeners != null)
227             contextManagerListeners.remove(contextManagerListener);
228     }
229
230     public void setEnabledContextIds(Set JavaDoc enabledContextIds) {
231         contextManager.setActiveContextIds(enabledContextIds);
232     }
233 }
234
Popular Tags