1 11 package org.eclipse.ui.commands; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.core.commands.ExecutionEvent; 20 import org.eclipse.core.commands.ExecutionException; 21 import org.eclipse.core.commands.HandlerEvent; 22 import org.eclipse.core.commands.IHandlerAttributes; 23 import org.eclipse.core.commands.IHandlerListener; 24 25 39 public abstract class AbstractHandler extends 40 org.eclipse.core.commands.AbstractHandler implements IHandler { 41 42 48 private List handlerListeners; 49 50 53 public void addHandlerListener( 54 org.eclipse.ui.commands.IHandlerListener handlerListener) { 55 if (handlerListener == null) { 56 throw new NullPointerException (); 57 } 58 if (handlerListeners == null) { 59 handlerListeners = new ArrayList (); 60 } 61 if (!handlerListeners.contains(handlerListener)) { 62 handlerListeners.add(handlerListener); 63 } 64 } 65 66 72 public void dispose() { 73 } 75 76 public Object execute(final ExecutionEvent event) throws ExecutionException { 77 try { 78 return execute(event.getParameters()); 79 } catch (final org.eclipse.ui.commands.ExecutionException e) { 80 throw new ExecutionException(e.getMessage(), e.getCause()); 81 } 82 } 83 84 92 protected void fireHandlerChanged(HandlerEvent handlerEvent) { 93 super.fireHandlerChanged(handlerEvent); 94 95 if (handlerListeners != null) { 96 final boolean attributesChanged = handlerEvent.isEnabledChanged() 97 || handlerEvent.isHandledChanged(); 98 final Map previousAttributes; 99 if (attributesChanged) { 100 previousAttributes = new HashMap (); 101 previousAttributes.putAll(getAttributeValuesByName()); 102 if (handlerEvent.isEnabledChanged()) { 103 Boolean disabled = !isEnabled() ? Boolean.TRUE: Boolean.FALSE; 104 previousAttributes 105 .put("enabled", disabled); } 107 if (handlerEvent.isHandledChanged()) { 108 Boolean notHandled = !isHandled() ? Boolean.TRUE: Boolean.FALSE; 109 previousAttributes.put( 110 IHandlerAttributes.ATTRIBUTE_HANDLED, notHandled); 111 } 112 } else { 113 previousAttributes = null; 114 } 115 final org.eclipse.ui.commands.HandlerEvent legacyEvent = new org.eclipse.ui.commands.HandlerEvent( 116 this, attributesChanged, previousAttributes); 117 118 for (int i = 0; i < handlerListeners.size(); i++) { 119 ((org.eclipse.ui.commands.IHandlerListener) handlerListeners 120 .get(i)).handlerChanged(legacyEvent); 121 } 122 } 123 } 124 125 protected void fireHandlerChanged( 126 final org.eclipse.ui.commands.HandlerEvent handlerEvent) { 127 if (handlerEvent == null) { 128 throw new NullPointerException (); 129 } 130 131 if (handlerListeners != null) { 132 for (int i = 0; i < handlerListeners.size(); i++) { 133 ((org.eclipse.ui.commands.IHandlerListener) handlerListeners 134 .get(i)).handlerChanged(handlerEvent); 135 } 136 } 137 138 if (super.hasListeners()) { 139 final boolean enabledChanged; 140 final boolean handledChanged; 141 if (handlerEvent.haveAttributeValuesByNameChanged()) { 142 Map previousAttributes = handlerEvent 143 .getPreviousAttributeValuesByName(); 144 145 Object attribute = previousAttributes.get("enabled"); if (attribute instanceof Boolean ) { 147 enabledChanged = ((Boolean ) attribute).booleanValue(); 148 } else { 149 enabledChanged = false; 150 } 151 152 attribute = previousAttributes 153 .get(IHandlerAttributes.ATTRIBUTE_HANDLED); 154 if (attribute instanceof Boolean ) { 155 handledChanged = ((Boolean ) attribute).booleanValue(); 156 } else { 157 handledChanged = false; 158 } 159 } else { 160 enabledChanged = false; 161 handledChanged = true; 162 } 163 final HandlerEvent newEvent = new HandlerEvent(this, 164 enabledChanged, handledChanged); 165 super.fireHandlerChanged(newEvent); 166 } 167 } 168 169 175 public Map getAttributeValuesByName() { 176 return Collections.EMPTY_MAP; 177 } 178 179 187 protected final boolean hasListeners() { 188 return super.hasListeners() || handlerListeners != null; 189 } 190 191 public boolean isEnabled() { 192 final Object handled = getAttributeValuesByName().get("enabled"); if (handled instanceof Boolean ) { 194 return ((Boolean ) handled).booleanValue(); 195 } 196 197 return false; 198 } 199 200 public boolean isHandled() { 201 final Object handled = getAttributeValuesByName().get( 202 IHandlerAttributes.ATTRIBUTE_HANDLED); 203 if (handled instanceof Boolean ) { 204 return ((Boolean ) handled).booleanValue(); 205 } 206 207 return false; 208 } 209 210 213 public void removeHandlerListener( 214 org.eclipse.ui.commands.IHandlerListener handlerListener) { 215 if (handlerListener == null) { 216 throw new NullPointerException (); 217 } 218 if (handlerListeners == null) { 219 return; 220 } 221 222 if (handlerListeners != null) { 223 handlerListeners.remove(handlerListener); 224 } 225 if (handlerListeners.isEmpty()) { 226 handlerListeners = null; 227 } 228 } 229 } 230 | Popular Tags |