1 11 12 package org.eclipse.ui.internal.contexts; 13 14 import java.util.ArrayList ; 15 import java.util.Comparator ; 16 import java.util.HashSet ; 17 import java.util.List ; 18 import java.util.Set ; 19 import java.util.SortedSet ; 20 import java.util.TreeSet ; 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 36 public final class ContextManagerLegacyWrapper implements 37 org.eclipse.core.commands.contexts.IContextManagerListener, 38 IContextManager { 39 40 48 private class ContextIdDepthComparator implements Comparator { 49 50 53 public final int compare(final Object object1, final Object object2) { 54 final String contextId1 = (String ) object1; 55 final String contextId2 = (String ) object2; 56 Context context; 57 String parentId; 58 59 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 } 72 73 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 } 86 87 int compare = depth2 - depth1; 89 if (compare == 0) { 90 compare = contextId1.compareTo(contextId2); 91 } 92 93 return compare; 94 } 95 } 96 97 105 private class DepthSortedContextIdSet extends TreeSet { 106 107 112 private static final long serialVersionUID = 3257291326872892465L; 113 114 123 private DepthSortedContextIdSet(final Set contextIds) { 124 super(new ContextIdDepthComparator()); 125 addAll(contextIds); 126 } 127 } 128 129 private final ContextManager contextManager; 130 131 private List contextManagerListeners; 132 133 141 public ContextManagerLegacyWrapper(ContextManager contextManager) { 142 143 if (contextManager == null) { 144 throw new NullPointerException ("The context manager cannot be null"); } 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 (); 155 } 156 157 if (contextManagerListeners == null) { 158 contextManagerListeners = new ArrayList (); 159 } 160 161 if (!contextManagerListeners.contains(contextManagerListener)) { 162 contextManagerListeners.add(contextManagerListener); 163 } 164 } 165 166 171 public void contextManagerChanged( 172 org.eclipse.core.commands.contexts.ContextManagerEvent contextManagerEvent) { 173 final String contextId = contextManagerEvent.getContextId(); 174 final boolean definedContextsChanged; 175 final Set previouslyDefinedContextIds; 176 if (contextId == null) { 177 definedContextsChanged = false; 178 previouslyDefinedContextIds = null; 179 } else { 180 definedContextsChanged = true; 181 previouslyDefinedContextIds = new HashSet (); 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 (); 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 contextId) { 214 return new ContextLegacyWrapper(contextManager.getContext(contextId), 215 contextManager); 216 } 217 218 public SortedSet getDefinedContextIds() { 219 return new DepthSortedContextIdSet(contextManager 220 .getDefinedContextIds()); 221 } 222 223 public SortedSet getEnabledContextIds() { 224 return new DepthSortedContextIdSet(contextManager.getActiveContextIds()); 225 } 226 227 public void removeContextManagerListener( 228 IContextManagerListener contextManagerListener) { 229 if (contextManagerListener == null) { 230 throw new NullPointerException (); 231 } 232 233 if (contextManagerListeners != null) { 234 contextManagerListeners.remove(contextManagerListener); 235 } 236 } 237 238 public void setEnabledContextIds(Set enabledContextIds) { 239 contextManager.setActiveContextIds(enabledContextIds); 240 } 241 } 242 | Popular Tags |