001: /*******************************************************************************
002: * Copyright (c) 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.contexts;
011:
012: import org.eclipse.core.commands.contexts.Context;
013: import org.eclipse.core.commands.contexts.ContextManager;
014: import org.eclipse.ui.contexts.IContext;
015: import org.eclipse.ui.contexts.IContextListener;
016: import org.eclipse.ui.contexts.NotDefinedException;
017: import org.eclipse.ui.internal.util.Util;
018:
019: /**
020: * This implements the old <code>IContext</code> interface based on the new
021: * context implementation in <code>org.eclipse.ui.contexts</code>. This is a
022: * wrapper.
023: *
024: * @since 3.1
025: */
026: public class ContextLegacyWrapper implements IContext {
027:
028: /**
029: * The context manager that maintains the set of active contexts; must not
030: * be <code>null</code>.
031: */
032: private final ContextManager contextManager;
033:
034: /**
035: * The wrapped instance of context. This value will never be
036: * <code>null</code>.
037: */
038: private final Context wrappedContext;
039:
040: /**
041: * Constructs a new instance of <code>ContextWrapper</code>.
042: *
043: * @param context
044: * The context to wrapper; must not be <code>null</code>.
045: * @param contextManager
046: * The context manager that maintains the set of active contexts;
047: * must not be <code>null</code>.
048: */
049: public ContextLegacyWrapper(final Context context,
050: final ContextManager contextManager) {
051: if (context == null) {
052: throw new NullPointerException(
053: "A wrapper cannot be created on a null context"); //$NON-NLS-1$
054: }
055:
056: if (contextManager == null) {
057: throw new NullPointerException(
058: "A wrapper cannot be created with a null manager"); //$NON-NLS-1$
059: }
060:
061: wrappedContext = context;
062: this .contextManager = contextManager;
063: }
064:
065: /*
066: * (non-Javadoc)
067: *
068: * @see org.eclipse.ui.contexts.IContext#addContextListener(org.eclipse.ui.contexts.IContextListener)
069: */
070: public void addContextListener(IContextListener contextListener) {
071: final LegacyContextListenerWrapper wrapper = new LegacyContextListenerWrapper(
072: contextListener, contextManager, this );
073: wrappedContext.addContextListener(wrapper);
074:
075: /*
076: * We need to add the listener to the context manager as well, as only
077: * the manager advertises changes to the enabled state.
078: */
079: contextManager.addContextManagerListener(wrapper);
080: }
081:
082: /*
083: * (non-Javadoc)
084: *
085: * @see java.lang.Comparable#compareTo(T)
086: */
087: public int compareTo(Object o) {
088: return Util.compare(wrappedContext,
089: ((ContextLegacyWrapper) o).wrappedContext);
090: }
091:
092: /*
093: * (non-Javadoc)
094: *
095: * @see org.eclipse.ui.contexts.IContext#getId()
096: */
097: public String getId() {
098: return wrappedContext.getId();
099: }
100:
101: /*
102: * (non-Javadoc)
103: *
104: * @see org.eclipse.ui.contexts.IContext#getName()
105: */
106: public String getName() throws NotDefinedException {
107: try {
108: return wrappedContext.getName();
109: } catch (final org.eclipse.core.commands.common.NotDefinedException e) {
110: throw new NotDefinedException(e);
111: }
112: }
113:
114: /*
115: * (non-Javadoc)
116: *
117: * @see org.eclipse.ui.contexts.IContext#getParentId()
118: */
119: public String getParentId() throws NotDefinedException {
120: try {
121: return wrappedContext.getParentId();
122: } catch (final org.eclipse.core.commands.common.NotDefinedException e) {
123: throw new NotDefinedException(e);
124: }
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see org.eclipse.ui.contexts.IContext#isDefined()
131: */
132: public boolean isDefined() {
133: return wrappedContext.isDefined();
134: }
135:
136: /*
137: * (non-Javadoc)
138: *
139: * @see org.eclipse.ui.contexts.IContext#isEnabled()
140: */
141: public boolean isEnabled() {
142: return contextManager.getActiveContextIds().contains(
143: wrappedContext.getId());
144: }
145:
146: /*
147: * (non-Javadoc)
148: *
149: * @see org.eclipse.ui.contexts.IContext#removeContextListener(org.eclipse.ui.contexts.IContextListener)
150: */
151: public void removeContextListener(IContextListener contextListener) {
152: final LegacyContextListenerWrapper wrapper = new LegacyContextListenerWrapper(
153: contextListener, contextManager, this);
154: wrappedContext.removeContextListener(wrapper);
155: contextManager.removeContextManagerListener(wrapper);
156: }
157:
158: }
|