1 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 31 public final class ActionHandler extends AbstractHandler { 32 33 36 private final IAction action; 37 38 43 private IPropertyChangeListener propertyChangeListener; 44 45 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 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 94 private final void detachListener() { 95 this.action.removePropertyChangeListener(propertyChangeListener); 96 propertyChangeListener = null; 97 } 98 99 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); } 127 return null; 128 } 129 130 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("); buffer.append(action); 162 buffer.append(')'); 163 164 return buffer.toString(); 165 } 166 } 167 | Popular Tags |