ActionHandler.java

1: /*******************************************************************************
2: * Copyright (c) 2004, 2006 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.jface.commands;
12:
13: import org.eclipse.core.commands.AbstractHandler;
14: import org.eclipse.core.commands.ExecutionEvent;
15: import org.eclipse.core.commands.ExecutionException;
16: import org.eclipse.core.commands.HandlerEvent;
17: import org.eclipse.core.commands.IHandlerListener;
18: import org.eclipse.jface.action.IAction;
19: import org.eclipse.jface.util.IPropertyChangeListener;
20: import org.eclipse.jface.util.PropertyChangeEvent;
21: import org.eclipse.swt.widgets.Event;
22:
23: /**
24: * <p>
25: * This class adapts instances of <code>IAction</code> to
26: * <code>IHandler</code>.
27: * </p>
28: *
29: * @since 3.1
30: */
31: public final class ActionHandler extends AbstractHandler {
32:
33: /**
34: * The wrapped action. This value is never <code>null</code>.
35: */
36: private final IAction action;
37:
38: /**
39: * The property change listener hooked on to the action. This is initialized
40: * when the first listener is attached to this handler, and is removed when
41: * the handler is disposed or the last listener is removed.
42: */
43: private IPropertyChangeListener propertyChangeListener;
44:
45: /**
46: * Creates a new instance of this class given an instance of
47: * <code>IAction</code>.
48: *
49: * @param action
50: * the action. Must not be <code>null</code>.
51: */
52: public ActionHandler(final IAction action) {
53: if (action == null) {
54: throw new NullPointerException();
55: }
56:
57: this.action = action;
58: }
59:
60: public final void addHandlerListener(final IHandlerListener handlerListener) {
61: if (!hasListeners()) {
62: attachListener();
63: }
64:
65: super.addHandlerListener(handlerListener);
66: }
67:
68: /**
69: * When a listener is attached to this handler, then this registers a
70: * listener with the underlying action.
71: *
72: * @since 3.1
73: */
74: private final void attachListener() {
75: if (propertyChangeListener == null) {
76: propertyChangeListener = new IPropertyChangeListener() {
77: public final void propertyChange(
78: final PropertyChangeEvent propertyChangeEvent) {
79: final String property = propertyChangeEvent.getProperty();
80: fireHandlerChanged(new HandlerEvent(ActionHandler.this,
81: IAction.ENABLED.equals(property), IAction.HANDLED
82: .equals(property)));
83: }
84: };
85: }
86:
87: this.action.addPropertyChangeListener(propertyChangeListener);
88: }
89:
90: /**
91: * When no more listeners are registered, then this is used to removed the
92: * property change listener from the underlying action.
93: */
94: private final void detachListener() {
95: this.action.removePropertyChangeListener(propertyChangeListener);
96: propertyChangeListener = null;
97: }
98:
99: /**
100: * Removes the property change listener from the action.
101: *
102: * @see org.eclipse.core.commands.IHandler#dispose()
103: */
104: public final void dispose() {
105: if (hasListeners()) {
106: action.removePropertyChangeListener(propertyChangeListener);
107: }
108: }
109:
110: public final Object execute(final ExecutionEvent event)
111: throws ExecutionException {
112: if ((action.getStyle() == IAction.AS_CHECK_BOX)
113: || (action.getStyle() == IAction.AS_RADIO_BUTTON)) {
114: action.setChecked(!action.isChecked());
115: }
116: final Object trigger = event.getTrigger();
117: try {
118: if (trigger instanceof Event) {
119: action.runWithEvent((Event) trigger);
120: } else {
121: action.runWithEvent(new Event());
122: }
123: } catch (Exception e) {
124: throw new ExecutionException(
125: "While executing the action, an exception occurred", e); //$NON-NLS-1$
126: }
127: return null;
128: }
129:
130: /**
131: * Returns the action associated with this handler
132: *
133: * @return the action associated with this handler (not null)
134: * @since 3.1
135: */
136: public final IAction getAction() {
137: return action;
138: }
139:
140: public final boolean isEnabled() {
141: return action.isEnabled();
142: }
143:
144: public final boolean isHandled() {
145: return action.isHandled();
146: }
147:
148: public final void removeHandlerListener(
149: final IHandlerListener handlerListener) {
150: super.removeHandlerListener(handlerListener);
151:
152: if (!hasListeners()) {
153: detachListener();
154: }
155: }
156:
157: public final String toString() {
158: final StringBuffer buffer = new StringBuffer();
159:
160: buffer.append("ActionHandler("); //$NON-NLS-1$
161: buffer.append(action);
162: buffer.append(')');
163:
164: return buffer.toString();
165: }
166:}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值