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.handlers;
011:
012: import org.eclipse.core.commands.ExecutionEvent;
013: import org.eclipse.core.commands.ExecutionException;
014: import org.eclipse.core.commands.IHandler;
015: import org.eclipse.core.commands.IHandlerListener;
016: import org.eclipse.core.commands.util.Tracing;
017: import org.eclipse.ui.internal.commands.ILegacyAttributeNames;
018: import org.eclipse.ui.internal.misc.Policy;
019:
020: /**
021: * A handler that wraps a legacy handler. This provide backward compatibility
022: * with the handlers release in Eclipse 3.0.
023: *
024: * @since 3.1
025: */
026: public final class LegacyHandlerWrapper implements IHandler {
027:
028: /**
029: * This flag can be set to <code>true</code> if commands should print
030: * information to <code>System.out</code> when changing handlers.
031: */
032: private static final boolean DEBUG_HANDLERS = Policy.DEBUG_HANDLERS
033: && Policy.DEBUG_HANDLERS_VERBOSE;
034:
035: /**
036: * The wrapped handler; never <code>null</code>.
037: */
038: private final org.eclipse.ui.commands.IHandler handler;
039:
040: /**
041: * Constructs a new instance of <code>HandlerWrapper</code>.
042: *
043: * @param handler
044: * The handler that should be wrapped; must not be
045: * <code>null</code>.
046: */
047: public LegacyHandlerWrapper(
048: final org.eclipse.ui.commands.IHandler handler) {
049: if (handler == null) {
050: throw new NullPointerException(
051: "A handler wrapper cannot be constructed on a null handler"); //$NON-NLS-1$
052: }
053:
054: this .handler = handler;
055: }
056:
057: /*
058: * (non-Javadoc)
059: *
060: * @see org.eclipse.core.commands.IHandler#addHandlerListener(org.eclipse.core.commands.IHandlerListener)
061: */
062: public final void addHandlerListener(
063: final IHandlerListener handlerListener) {
064: handler.addHandlerListener(new LegacyHandlerListenerWrapper(
065: this , handlerListener));
066: }
067:
068: /*
069: * (non-Javadoc)
070: *
071: * @see org.eclipse.core.commands.IHandler#dispose()
072: */
073: public final void dispose() {
074: handler.dispose();
075: }
076:
077: public final boolean equals(final Object object) {
078: if (object instanceof org.eclipse.ui.commands.IHandler) {
079: return this .handler == object;
080: }
081:
082: if (object instanceof LegacyHandlerWrapper) {
083: return this .handler == ((LegacyHandlerWrapper) object).handler;
084: }
085:
086: return false;
087: }
088:
089: /*
090: * (non-Javadoc)
091: *
092: * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
093: */
094: public final Object execute(final ExecutionEvent event)
095: throws ExecutionException {
096: // Debugging output
097: if (DEBUG_HANDLERS) {
098: final StringBuffer buffer = new StringBuffer(
099: "Executing LegacyHandlerWrapper for "); //$NON-NLS-1$
100: if (handler == null) {
101: buffer.append("no handler"); //$NON-NLS-1$
102: } else {
103: buffer.append('\'');
104: buffer.append(handler.getClass().getName());
105: buffer.append('\'');
106: }
107: Tracing.printTrace("HANDLERS", buffer.toString()); //$NON-NLS-1$
108: }
109:
110: try {
111: return handler.execute(event.getParameters());
112: } catch (final org.eclipse.ui.commands.ExecutionException e) {
113: throw new ExecutionException(e.getMessage(), e.getCause());
114: }
115: }
116:
117: public final int hashCode() {
118: return this .handler.hashCode();
119: }
120:
121: public final boolean isEnabled() {
122: final Object enabled = handler.getAttributeValuesByName().get(
123: ILegacyAttributeNames.ENABLED);
124: if (enabled instanceof Boolean) {
125: return ((Boolean) enabled).booleanValue();
126: }
127:
128: return true;
129: }
130:
131: public final boolean isHandled() {
132: final Object handled = handler.getAttributeValuesByName().get(
133: ILegacyAttributeNames.HANDLED);
134: if (handled instanceof Boolean) {
135: return ((Boolean) handled).booleanValue();
136: }
137:
138: return true;
139: }
140:
141: public final void removeHandlerListener(
142: final IHandlerListener handlerListener) {
143: handler.removeHandlerListener(new LegacyHandlerListenerWrapper(
144: this , handlerListener));
145: }
146:
147: public final String toString() {
148: final StringBuffer buffer = new StringBuffer();
149:
150: buffer.append("LegacyHandlerWrapper("); //$NON-NLS-1$
151: buffer.append(handler);
152: buffer.append(')');
153:
154: return buffer.toString();
155: }
156: }
|