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 ContextManagerWrapper 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 ContextManagerWrapper(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 if (contextManagerListeners == null) 157 contextManagerListeners = new ArrayList (); 158 159 if (!contextManagerListeners.contains(contextManagerListener)) 160 contextManagerListeners.add(contextManagerListener); 161 } 162 163 168 public void contextManagerChanged( 169 org.eclipse.core.commands.contexts.ContextManagerEvent contextManagerEvent) { 170 final String contextId = contextManagerEvent.getContextId(); 171 final boolean definedContextsChanged; 172 final Set previouslyDefinedContextIds; 173 if (contextId == null) { 174 definedContextsChanged = false; 175 previouslyDefinedContextIds = null; 176 } else { 177 definedContextsChanged = true; 178 previouslyDefinedContextIds = new HashSet (); 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 (); 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 contextId) { 208 return new ContextWrapper(contextManager.getContext(contextId), 209 contextManager); 210 } 211 212 public SortedSet getDefinedContextIds() { 213 return new DepthSortedContextIdSet(contextManager 214 .getDefinedContextIds()); 215 } 216 217 public SortedSet getEnabledContextIds() { 218 return new DepthSortedContextIdSet(contextManager.getActiveContextIds()); 219 } 220 221 public void removeContextManagerListener( 222 IContextManagerListener contextManagerListener) { 223 if (contextManagerListener == null) 224 throw new NullPointerException (); 225 226 if (contextManagerListeners != null) 227 contextManagerListeners.remove(contextManagerListener); 228 } 229 230 public void setEnabledContextIds(Set enabledContextIds) { 231 contextManager.setActiveContextIds(enabledContextIds); 232 } 233 } 234 | Popular Tags |