1 /*******************************************************************************
2  * Copyright (c) 2004, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.ui.internal.contexts;
15 
16 import java.util.Set;
17 
18 import org.eclipse.core.commands.contexts.ContextEvent;
19 import org.eclipse.core.commands.contexts.ContextManager;
20 import org.eclipse.core.commands.contexts.ContextManagerEvent;
21 import org.eclipse.core.commands.contexts.IContextListener;
22 import org.eclipse.core.commands.contexts.IContextManagerListener;
23 import org.eclipse.ui.contexts.IContext;
24 
25 /**
26  * <p>
27  * This wraps an old context listener so it supports the new API. This is used
28  * to support attaching old-style listens to the new context objects.
29  * </p>
30  *
31  * @since 3.1
32  */
33 public class LegacyContextListenerWrapper implements IContextListener, IContextManagerListener {
34 
35 	/**
36 	 * The legacy context that this listener would previously have been attached to.
37 	 * This value is never <code>null</code>.
38 	 */
39 	private final IContext context;
40 
41 	/**
42 	 * The context manager used for constructing the context wrapper when an event
43 	 * occurs; must not be <code>null</code>.
44 	 */
45 	private final ContextManager contextManager;
46 
47 	/**
48 	 * The listener to be wrapped. This value is never <code>null</code>.
49 	 */
50 	private final org.eclipse.ui.contexts.IContextListener wrappedListener;
51 
52 	/**
53 	 * Constructs a new instance of <code>ContextListenerWrapper</code>.
54 	 *
55 	 * @param listener       The listener to be wrapped. Must not be
56 	 *                       <code>null</code>.
57 	 * @param contextManager The context manager used for constructing the context
58 	 *                       wrapper when an event occurs; must not be
59 	 *                       <code>null</code>.
60 	 * @param context        The legacy context this listener will be listening to;
61 	 *                       must not be <code>null</code>.
62 	 */
LegacyContextListenerWrapper(final org.eclipse.ui.contexts.IContextListener listener, final ContextManager contextManager, final IContext context)63 	public LegacyContextListenerWrapper(final org.eclipse.ui.contexts.IContextListener listener,
64 			final ContextManager contextManager, final IContext context) {
65 		if (listener == null) {
66 			throw new NullPointerException("Cannot create a listener wrapper on a null listener"); //$NON-NLS-1$
67 		}
68 
69 		if (contextManager == null) {
70 			throw new NullPointerException("Cannot create a listener wrapper with a null context manager"); //$NON-NLS-1$
71 		}
72 
73 		if (context == null) {
74 			throw new NullPointerException("Cannot create a listener wrapper with a null context"); //$NON-NLS-1$
75 		}
76 
77 		wrappedListener = listener;
78 		this.contextManager = contextManager;
79 		this.context = context;
80 	}
81 
82 	@Override
contextChanged(final ContextEvent contextEvent)83 	public final void contextChanged(final ContextEvent contextEvent) {
84 		wrappedListener.contextChanged(new org.eclipse.ui.contexts.ContextEvent(
85 				new ContextLegacyWrapper(contextEvent.getContext(), contextManager), contextEvent.isDefinedChanged(),
86 				false, contextEvent.isNameChanged(), contextEvent.isParentIdChanged()));
87 	}
88 
89 	@Override
contextManagerChanged(final ContextManagerEvent event)90 	public final void contextManagerChanged(final ContextManagerEvent event) {
91 		final String contextId = context.getId();
92 		final boolean enabledChanged;
93 		if (event.isActiveContextsChanged()) {
94 			final Set previousContexts = event.getPreviouslyActiveContextIds();
95 			final Set currentContexts = contextManager.getActiveContextIds();
96 			if ((previousContexts != null) && (previousContexts.contains(contextId))
97 					&& ((currentContexts == null) || (currentContexts.contains(contextId)))) {
98 				enabledChanged = true;
99 			} else if ((currentContexts != null) && (currentContexts.contains(contextId))
100 					&& ((previousContexts == null) || (previousContexts.contains(contextId)))) {
101 				enabledChanged = true;
102 			} else {
103 				enabledChanged = false;
104 			}
105 		} else {
106 			enabledChanged = false;
107 		}
108 
109 		wrappedListener
110 				.contextChanged(new org.eclipse.ui.contexts.ContextEvent(context, false, enabledChanged, false, false));
111 	}
112 
113 	@Override
equals(final Object object)114 	public final boolean equals(final Object object) {
115 		if (object instanceof LegacyContextListenerWrapper) {
116 			final LegacyContextListenerWrapper other = (LegacyContextListenerWrapper) object;
117 			return wrappedListener.equals(other.wrappedListener);
118 		}
119 
120 		if (object instanceof org.eclipse.ui.contexts.IContextListener) {
121 			final org.eclipse.ui.contexts.IContextListener other = (org.eclipse.ui.contexts.IContextListener) object;
122 			return wrappedListener.equals(other);
123 		}
124 
125 		return false;
126 	}
127 
128 	@Override
hashCode()129 	public final int hashCode() {
130 		return wrappedListener.hashCode();
131 	}
132 }
133