KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.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 ContextManagerLegacyWrapper 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 ContextManagerLegacyWrapper(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
157         if (contextManagerListeners == null) {
158             contextManagerListeners = new ArrayList JavaDoc();
159         }
160
161         if (!contextManagerListeners.contains(contextManagerListener)) {
162             contextManagerListeners.add(contextManagerListener);
163         }
164     }
165
166     /*
167      * (non-Javadoc)
168      *
169      * @see org.eclipse.core.commands.contexts.IContextManagerListener#contextManagerChanged(org.eclipse.core.commands.contexts.ContextManagerEvent)
170      */

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