001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 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.commands;
011:
012: import java.util.ArrayList;
013: import java.util.Collections;
014: import java.util.HashMap;
015: import java.util.List;
016: import java.util.Map;
017:
018: import org.eclipse.core.commands.ExecutionEvent;
019: import org.eclipse.core.commands.ExecutionException;
020: import org.eclipse.core.commands.HandlerEvent;
021: import org.eclipse.core.commands.IHandlerAttributes;
022: import org.eclipse.core.commands.IHandlerListener;
023:
024: /**
025: * This class is a partial implementation of <code>IHandler</code>. This
026: * abstract implementation provides support for handler listeners. You should
027: * subclass from this method unless you want to implement your own listener
028: * support. Subclasses should call
029: * {@link AbstractHandler#fireHandlerChanged(HandlerEvent)}when the handler
030: * changes. Subclasses should also override
031: * {@link AbstractHandler#getAttributeValuesByName()}if they have any
032: * attributes.
033: *
034: * @since 3.0
035: * @deprecated Please use the "org.eclipse.core.commands" plug-in instead.
036: * @see org.eclipse.core.commands.AbstractHandler
037: */
038: public abstract class AbstractHandler extends
039: org.eclipse.core.commands.AbstractHandler implements IHandler {
040:
041: /**
042: * Those interested in hearing about changes to this instance of
043: * <code>IHandler</code>. This member is null iff there are
044: * no listeners attached to this handler. (Most handlers don't
045: * have any listeners, and this optimization saves some memory.)
046: */
047: private List handlerListeners;
048:
049: /**
050: * @see IHandler#addHandlerListener(IHandlerListener)
051: */
052: public void addHandlerListener(
053: org.eclipse.ui.commands.IHandlerListener handlerListener) {
054: if (handlerListener == null) {
055: throw new NullPointerException();
056: }
057: if (handlerListeners == null) {
058: handlerListeners = new ArrayList();
059: }
060: if (!handlerListeners.contains(handlerListener)) {
061: handlerListeners.add(handlerListener);
062: }
063: }
064:
065: /**
066: * The default implementation does nothing. Subclasses who attach listeners
067: * to other objects are encouraged to detach them in this method.
068: *
069: * @see org.eclipse.ui.commands.IHandler#dispose()
070: */
071: public void dispose() {
072: // Do nothing.
073: }
074:
075: public Object execute(final ExecutionEvent event)
076: throws ExecutionException {
077: try {
078: return execute(event.getParameters());
079: } catch (final org.eclipse.ui.commands.ExecutionException e) {
080: throw new ExecutionException(e.getMessage(), e.getCause());
081: }
082: }
083:
084: /**
085: * Fires an event to all registered listeners describing changes to this
086: * instance.
087: *
088: * @param handlerEvent
089: * the event describing changes to this instance. Must not be
090: * <code>null</code>.
091: */
092: protected void fireHandlerChanged(HandlerEvent handlerEvent) {
093: super .fireHandlerChanged(handlerEvent);
094:
095: if (handlerListeners != null) {
096: final boolean attributesChanged = handlerEvent
097: .isEnabledChanged()
098: || handlerEvent.isHandledChanged();
099: final Map previousAttributes;
100: if (attributesChanged) {
101: previousAttributes = new HashMap();
102: previousAttributes.putAll(getAttributeValuesByName());
103: if (handlerEvent.isEnabledChanged()) {
104: Boolean disabled = !isEnabled() ? Boolean.TRUE
105: : Boolean.FALSE;
106: previousAttributes.put("enabled", disabled); //$NON-NLS-1$
107: }
108: if (handlerEvent.isHandledChanged()) {
109: Boolean notHandled = !isHandled() ? Boolean.TRUE
110: : Boolean.FALSE;
111: previousAttributes.put(
112: IHandlerAttributes.ATTRIBUTE_HANDLED,
113: notHandled);
114: }
115: } else {
116: previousAttributes = null;
117: }
118: final org.eclipse.ui.commands.HandlerEvent legacyEvent = new org.eclipse.ui.commands.HandlerEvent(
119: this , attributesChanged, previousAttributes);
120:
121: for (int i = 0; i < handlerListeners.size(); i++) {
122: ((org.eclipse.ui.commands.IHandlerListener) handlerListeners
123: .get(i)).handlerChanged(legacyEvent);
124: }
125: }
126: }
127:
128: protected void fireHandlerChanged(
129: final org.eclipse.ui.commands.HandlerEvent handlerEvent) {
130: if (handlerEvent == null) {
131: throw new NullPointerException();
132: }
133:
134: if (handlerListeners != null) {
135: for (int i = 0; i < handlerListeners.size(); i++) {
136: ((org.eclipse.ui.commands.IHandlerListener) handlerListeners
137: .get(i)).handlerChanged(handlerEvent);
138: }
139: }
140:
141: if (super .hasListeners()) {
142: final boolean enabledChanged;
143: final boolean handledChanged;
144: if (handlerEvent.haveAttributeValuesByNameChanged()) {
145: Map previousAttributes = handlerEvent
146: .getPreviousAttributeValuesByName();
147:
148: Object attribute = previousAttributes.get("enabled"); //$NON-NLS-1$
149: if (attribute instanceof Boolean) {
150: enabledChanged = ((Boolean) attribute)
151: .booleanValue();
152: } else {
153: enabledChanged = false;
154: }
155:
156: attribute = previousAttributes
157: .get(IHandlerAttributes.ATTRIBUTE_HANDLED);
158: if (attribute instanceof Boolean) {
159: handledChanged = ((Boolean) attribute)
160: .booleanValue();
161: } else {
162: handledChanged = false;
163: }
164: } else {
165: enabledChanged = false;
166: handledChanged = true;
167: }
168: final HandlerEvent newEvent = new HandlerEvent(this ,
169: enabledChanged, handledChanged);
170: super .fireHandlerChanged(newEvent);
171: }
172: }
173:
174: /**
175: * This simply return an empty map. The default implementation has no
176: * attributes.
177: *
178: * @see IHandler#getAttributeValuesByName()
179: */
180: public Map getAttributeValuesByName() {
181: return Collections.EMPTY_MAP;
182: }
183:
184: /**
185: * Returns true iff there is one or more IHandlerListeners attached to this
186: * AbstractHandler.
187: *
188: * @return true iff there is one or more IHandlerListeners attached to this
189: * AbstractHandler
190: * @since 3.1
191: */
192: protected final boolean hasListeners() {
193: return super .hasListeners() || handlerListeners != null;
194: }
195:
196: public boolean isEnabled() {
197: final Object handled = getAttributeValuesByName()
198: .get("enabled"); //$NON-NLS-1$
199: if (handled instanceof Boolean) {
200: return ((Boolean) handled).booleanValue();
201: }
202:
203: return false;
204: }
205:
206: public boolean isHandled() {
207: final Object handled = getAttributeValuesByName().get(
208: IHandlerAttributes.ATTRIBUTE_HANDLED);
209: if (handled instanceof Boolean) {
210: return ((Boolean) handled).booleanValue();
211: }
212:
213: return false;
214: }
215:
216: /**
217: * @see IHandler#removeHandlerListener(IHandlerListener)
218: */
219: public void removeHandlerListener(
220: org.eclipse.ui.commands.IHandlerListener handlerListener) {
221: if (handlerListener == null) {
222: throw new NullPointerException();
223: }
224: if (handlerListeners == null) {
225: return;
226: }
227:
228: if (handlerListeners != null) {
229: handlerListeners.remove(handlerListener);
230: }
231: if (handlerListeners.isEmpty()) {
232: handlerListeners = null;
233: }
234: }
235: }
|