KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > handlers > LegacyHandlerWrapper


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.handlers;
12
13 import org.eclipse.core.commands.ExecutionEvent;
14 import org.eclipse.core.commands.ExecutionException;
15 import org.eclipse.core.commands.IHandler;
16 import org.eclipse.core.commands.IHandlerListener;
17 import org.eclipse.core.commands.util.Tracing;
18 import org.eclipse.ui.internal.commands.ILegacyAttributeNames;
19 import org.eclipse.ui.internal.misc.Policy;
20
21 /**
22  * A handler that wraps a legacy handler. This provide backward compatibility
23  * with the handlers release in Eclipse 3.0.
24  *
25  * @since 3.1
26  */

27 public final class LegacyHandlerWrapper implements IHandler {
28
29     /**
30      * This flag can be set to <code>true</code> if commands should print
31      * information to <code>System.out</code> when changing handlers.
32      */

33     private static final boolean DEBUG_HANDLERS = Policy.DEBUG_HANDLERS
34             && Policy.DEBUG_HANDLERS_VERBOSE;
35
36     /**
37      * The wrapped handler; never <code>null</code>.
38      */

39     private final org.eclipse.ui.commands.IHandler handler;
40
41     /**
42      * Constructs a new instance of <code>HandlerWrapper</code>.
43      *
44      * @param handler
45      * The handler that should be wrapped; must not be
46      * <code>null</code>.
47      */

48     public LegacyHandlerWrapper(final org.eclipse.ui.commands.IHandler handler) {
49         if (handler == null) {
50             throw new NullPointerException JavaDoc(
51                     "A handler wrapper cannot be constructed on a null handler"); //$NON-NLS-1$
52
}
53
54         this.handler = handler;
55     }
56
57     /*
58      * (non-Javadoc)
59      *
60      * @see org.eclipse.core.commands.IHandler#addHandlerListener(org.eclipse.core.commands.IHandlerListener)
61      */

62     public final void addHandlerListener(final IHandlerListener handlerListener) {
63         handler.addHandlerListener(new LegacyHandlerListenerWrapper(this,
64                 handlerListener));
65     }
66
67     /*
68      * (non-Javadoc)
69      *
70      * @see org.eclipse.core.commands.IHandler#dispose()
71      */

72     public final void dispose() {
73         handler.dispose();
74     }
75
76     public final boolean equals(final Object JavaDoc object) {
77         if (object instanceof org.eclipse.ui.commands.IHandler) {
78             return this.handler == object;
79         }
80
81         if (object instanceof LegacyHandlerWrapper) {
82             return this.handler == ((LegacyHandlerWrapper) object).handler;
83         }
84
85         return false;
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
92      */

93     public final Object JavaDoc execute(final ExecutionEvent event)
94             throws ExecutionException {
95         // Debugging output
96
if (DEBUG_HANDLERS) {
97             final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("Executing LegacyHandlerWrapper for "); //$NON-NLS-1$
98
if (handler == null) {
99                 buffer.append("no handler"); //$NON-NLS-1$
100
} else {
101                 buffer.append('\'');
102                 buffer.append(handler.getClass().getName());
103                 buffer.append('\'');
104             }
105             Tracing.printTrace("HANDLERS", buffer.toString()); //$NON-NLS-1$
106
}
107
108         try {
109             return handler.execute(event.getParameters());
110         } catch (final org.eclipse.ui.commands.ExecutionException e) {
111             throw new ExecutionException(e.getMessage(), e.getCause());
112         }
113     }
114
115     public final int hashCode() {
116         return this.handler.hashCode();
117     }
118
119     public final boolean isEnabled() {
120         final Object JavaDoc enabled = handler.getAttributeValuesByName().get(
121                 ILegacyAttributeNames.ENABLED);
122         if (enabled instanceof Boolean JavaDoc) {
123             return ((Boolean JavaDoc) enabled).booleanValue();
124         }
125
126         return true;
127     }
128
129     public final boolean isHandled() {
130         final Object JavaDoc handled = handler.getAttributeValuesByName().get(
131                 ILegacyAttributeNames.HANDLED);
132         if (handled instanceof Boolean JavaDoc) {
133             return ((Boolean JavaDoc) handled).booleanValue();
134         }
135
136         return true;
137     }
138
139     public final void removeHandlerListener(
140             final IHandlerListener handlerListener) {
141         handler.removeHandlerListener(new LegacyHandlerListenerWrapper(this,
142                 handlerListener));
143     }
144
145     public final String JavaDoc toString() {
146         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
147
148         buffer.append("LegacyHandlerWrapper("); //$NON-NLS-1$
149
buffer.append(handler);
150         buffer.append(')');
151
152         return buffer.toString();
153     }
154 }
155
Popular Tags