comparison org.eclipse.core.commands/src/org/eclipse/core/commands/AbstractHandler.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2004, 2008 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.core.commands.AbstractHandler;
14
15 import org.eclipse.core.commands.common.EventManager;
16 import org.eclipse.core.commands.IHandler2;
17 import org.eclipse.core.commands.IHandlerListener;
18 import org.eclipse.core.commands.HandlerEvent;
19
20 import java.lang.all;
21
22 /**
23 * <p>
24 * This class is a partial implementation of <code>IHandler</code>. This
25 * abstract implementation provides support for handler listeners. You should
26 * subclass from this method unless you want to implement your own listener
27 * support. Subclasses should call
28 * {@link AbstractHandler#fireHandlerChanged(HandlerEvent)}when the handler
29 * changes. Subclasses can also override {@link AbstractHandler#isEnabled()} and
30 * {@link AbstractHandler#isHandled()}.
31 * </p>
32 *
33 * @since 3.1
34 */
35 public abstract class AbstractHandler : EventManager, IHandler2 {
36
37 /**
38 * Track this base class enabled state.
39 *
40 * @since 3.4
41 */
42 private bool baseEnabled = true;
43
44 /**
45 * @see IHandler#addHandlerListener(IHandlerListener)
46 */
47 public void addHandlerListener(IHandlerListener handlerListener) {
48 addListenerObject(cast(Object)handlerListener);
49 }
50
51 /**
52 * The default implementation does nothing. Subclasses who attach listeners
53 * to other objects are encouraged to detach them in this method.
54 *
55 * @see org.eclipse.core.commands.IHandler#dispose()
56 */
57 public void dispose() {
58 // Do nothing.
59 }
60
61 /**
62 * Fires an event to all registered listeners describing changes to this
63 * instance.
64 * <p>
65 * Subclasses may extend the definition of this method (i.e., if a different
66 * type of listener can be attached to a subclass). This is used primarily
67 * for support of <code>AbstractHandler</code> in
68 * <code>org.eclipse.ui.workbench</code>, and clients should be wary of
69 * overriding this behaviour. If this method is overridden, then the first
70 * line of the method should be "<code>super.fireHandlerChanged(handlerEvent);</code>".
71 * </p>
72 *
73 * @param handlerEvent
74 * the event describing changes to this instance. Must not be
75 * <code>null</code>.
76 */
77 protected void fireHandlerChanged(HandlerEvent handlerEvent) {
78 if (handlerEvent is null) {
79 throw new NullPointerException();
80 }
81
82 Object[] listeners = getListeners();
83 for (int i = 0; i < listeners.length; i++) {
84 IHandlerListener listener = cast(IHandlerListener) listeners[i];
85 listener.handlerChanged(handlerEvent);
86 }
87 }
88
89 /**
90 * Whether this handler is capable of executing at this time. Subclasses may
91 * override this method. If clients override this method they should also
92 * consider overriding {@link #setEnabled(Object)} so they can be notified
93 * about framework execution contexts.
94 *
95 * @return <code>true</code>
96 * @see #setEnabled(Object)
97 * @see #setBaseEnabled(bool)
98 */
99 public bool isEnabled() {
100 return baseEnabled;
101 }
102
103 /**
104 * Allow the default {@link #isEnabled()} to answer our enabled state. It
105 * will fire a HandlerEvent if necessary. If clients use this method they
106 * should also consider overriding {@link #setEnabled(Object)} so they can
107 * be notified about framework execution contexts.
108 *
109 * @param state
110 * the enabled state
111 * @since 3.4
112 */
113 protected void setBaseEnabled(bool state) {
114 if (baseEnabled is state) {
115 return;
116 }
117 baseEnabled = state;
118 fireHandlerChanged(new HandlerEvent(this, true, false));
119 }
120
121 /**
122 * Called by the framework to allow the handler to update its enabled state
123 * by extracting the same information available at execution time. Clients
124 * may override if they need to extract information from the application
125 * context.
126 *
127 * @param evaluationContext
128 * the application context. May be <code>null</code>
129 * @since 3.4
130 * @see #setBaseEnabled(bool)
131 */
132 public void setEnabled(Object evaluationContext) {
133 }
134
135 /**
136 * Whether this handler is capable of handling delegated responsibilities at
137 * this time. Subclasses may override this method.
138 *
139 * @return <code>true</code>
140 */
141 public bool isHandled() {
142 return true;
143 }
144
145 /**
146 * <p>
147 * Returns true iff there is one or more IHandlerListeners attached to this
148 * AbstractHandler.
149 * </p>
150 * <p>
151 * Subclasses may extend the definition of this method (i.e., if a different
152 * type of listener can be attached to a subclass). This is used primarily
153 * for support of <code>AbstractHandler</code> in
154 * <code>org.eclipse.ui.workbench</code>, and clients should be wary of
155 * overriding this behaviour. If this method is overridden, then the return
156 * value should include "<code>super.hasListeners() ||</code>".
157 * </p>
158 *
159 * @return true iff there is one or more IHandlerListeners attached to this
160 * AbstractHandler
161 */
162 protected bool hasListeners() {
163 return isListenerAttached();
164 }
165
166 /**
167 * @see IHandler#removeHandlerListener(IHandlerListener)
168 */
169 public void removeHandlerListener(IHandlerListener handlerListener) {
170 removeListenerObject(cast(Object)handlerListener);
171 }
172 }