1 11 package org.eclipse.ui.internal.contexts; 12 13 import java.util.Set ; 14 15 import org.eclipse.core.commands.contexts.ContextEvent; 16 import org.eclipse.core.commands.contexts.ContextManager; 17 import org.eclipse.core.commands.contexts.ContextManagerEvent; 18 import org.eclipse.core.commands.contexts.IContextListener; 19 import org.eclipse.core.commands.contexts.IContextManagerListener; 20 import org.eclipse.ui.contexts.IContext; 21 22 30 public class ContextListenerWrapper implements IContextListener, 31 IContextManagerListener { 32 33 37 private final IContext context; 38 39 43 private final ContextManager contextManager; 44 45 48 private final org.eclipse.ui.contexts.IContextListener wrappedListener; 49 50 62 public ContextListenerWrapper( 63 final org.eclipse.ui.contexts.IContextListener listener, 64 final ContextManager contextManager, final IContext context) { 65 if (listener == null) { 66 throw new NullPointerException ( 67 "Cannot create a listener wrapper on a null listener"); } 69 70 if (contextManager == null) { 71 throw new NullPointerException ( 72 "Cannot create a listener wrapper with a null context manager"); } 74 75 if (context == null) { 76 throw new NullPointerException ( 77 "Cannot create a listener wrapper with a null context"); } 79 80 wrappedListener = listener; 81 this.contextManager = contextManager; 82 this.context = context; 83 } 84 85 90 public final void contextChanged(final ContextEvent contextEvent) { 91 wrappedListener 92 .contextChanged(new org.eclipse.ui.contexts.ContextEvent( 93 new ContextWrapper(contextEvent.getContext(), 94 contextManager), contextEvent 95 .isDefinedChanged(), false, contextEvent 96 .isNameChanged(), contextEvent 97 .isParentIdChanged())); 98 } 99 100 public final void contextManagerChanged(final ContextManagerEvent event) { 101 final String contextId = context.getId(); 102 final boolean enabledChanged; 103 if (event.isActiveContextsChanged()) { 104 final Set previousContexts = event.getPreviouslyActiveContextIds(); 105 final Set currentContexts = contextManager.getActiveContextIds(); 106 if ((previousContexts != null) 107 && (previousContexts.contains(contextId)) 108 && ((currentContexts == null) || (currentContexts 109 .contains(contextId)))) { 110 enabledChanged = true; 111 } else if ((currentContexts != null) 112 && (currentContexts.contains(contextId)) 113 && ((previousContexts == null) || (previousContexts 114 .contains(contextId)))) { 115 enabledChanged = true; 116 } else { 117 enabledChanged = false; 118 } 119 } else { 120 enabledChanged = false; 121 } 122 123 wrappedListener 124 .contextChanged(new org.eclipse.ui.contexts.ContextEvent( 125 context, false, enabledChanged, false, false)); 126 } 127 128 public final boolean equals(final Object object) { 129 if (object instanceof ContextListenerWrapper) { 130 final ContextListenerWrapper other = (ContextListenerWrapper) object; 131 return wrappedListener.equals(other.wrappedListener); 132 } 133 134 if (object instanceof org.eclipse.ui.contexts.IContextListener) { 135 final org.eclipse.ui.contexts.IContextListener other = (org.eclipse.ui.contexts.IContextListener) object; 136 return wrappedListener.equals(other); 137 } 138 139 return false; 140 } 141 142 public final int hashCode() { 143 return wrappedListener.hashCode(); 144 } 145 } 146 | Popular Tags |