comparison dwtx/jface/action/ExternalActionManager.d @ 104:04b47443bb01

Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections. These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author Frank Benoit <benoit@tionex.de>
date Thu, 07 Aug 2008 15:01:33 +0200
parents 7ffeace6c47f
children
comparison
equal deleted inserted replaced
103:2d6540440fe6 104:04b47443bb01
13 13
14 module dwtx.jface.action.ExternalActionManager; 14 module dwtx.jface.action.ExternalActionManager;
15 15
16 import dwtx.jface.action.IAction; 16 import dwtx.jface.action.IAction;
17 17
18 import tango.util.collection.HashMap;
19 import tango.util.collection.HashSet;
20 import tango.util.collection.model.Map;
21 import tango.util.collection.model.Set;
22 18
23 import dwt.widgets.Event; 19 import dwt.widgets.Event;
24 import dwtx.core.commands.Command; 20 import dwtx.core.commands.Command;
25 import dwtx.core.commands.CommandEvent; 21 import dwtx.core.commands.CommandEvent;
26 import dwtx.core.commands.CommandManager; 22 import dwtx.core.commands.CommandManager;
45 import dwtx.jface.util.Policy; 41 import dwtx.jface.util.Policy;
46 import dwtx.jface.util.PropertyChangeEvent; 42 import dwtx.jface.util.PropertyChangeEvent;
47 import dwtx.jface.util.Util; 43 import dwtx.jface.util.Util;
48 44
49 import dwt.dwthelper.utils; 45 import dwt.dwthelper.utils;
46 import dwtx.dwtxhelper.Collection;
50 import dwt.dwthelper.ResourceBundle; 47 import dwt.dwthelper.ResourceBundle;
51 import tango.text.convert.Format; 48 import tango.text.convert.Format;
52 49
53 /** 50 /**
54 * <p> 51 * <p>
71 * 68 *
72 * @since 3.0 69 * @since 3.0
73 */ 70 */
74 public final class ExternalActionManager { 71 public final class ExternalActionManager {
75 72
76 private static HashMap!(String,String) EMPTY_MAP;
77 static this(){
78 EMPTY_MAP = new HashMap!(String,String);
79 }
80
81 /** 73 /**
82 * A simple implementation of the <code>ICallback</code> mechanism that 74 * A simple implementation of the <code>ICallback</code> mechanism that
83 * simply takes a <code>BindingManager</code> and a 75 * simply takes a <code>BindingManager</code> and a
84 * <code>CommandManager</code>. 76 * <code>CommandManager</code>.
85 * <p> 77 * <p>
128 * so far. For each of these, there will be a listener on the 120 * so far. For each of these, there will be a listener on the
129 * corresponding command. If the command ever becomes defined, the item 121 * corresponding command. If the command ever becomes defined, the item
130 * will be removed from this set and the listener removed. This value 122 * will be removed from this set and the listener removed. This value
131 * may be empty, but never <code>null</code>. 123 * may be empty, but never <code>null</code>.
132 */ 124 */
133 private const Set!(String) loggedCommandIds; 125 private const Set loggedCommandIds;
134 126
135 /** 127 /**
136 * The list of listeners that have registered for property change 128 * The list of listeners that have registered for property change
137 * notification. This is a map of command identifiers (<code>String</code>) 129 * notification. This is a map of command identifiers (<code>String</code>)
138 * to listeners (<code>IPropertyChangeListener</code> or 130 * to listeners (<code>IPropertyChangeListener</code> or
139 * <code>ListenerList</code> of <code>IPropertyChangeListener</code>). 131 * <code>ListenerList</code> of <code>IPropertyChangeListener</code>).
140 */ 132 */
141 private const Map!(String,IPropertyChangeListener) registeredListeners; 133 private const Map registeredListeners;
142 134
143 static this(){ 135 static this(){
144 RESOURCE_BUNDLE = ResourceBundle.getBundle( 136 RESOURCE_BUNDLE = ResourceBundle.getBundle(
145 getImportData!("dwtx.jface.action.ExternalActionManager.properties")); 137 getImportData!("dwtx.jface.action.ExternalActionManager.properties"));
146 } 138 }
217 */ 209 */
218 public this(BindingManager bindingManager, 210 public this(BindingManager bindingManager,
219 CommandManager commandManager, 211 CommandManager commandManager,
220 IActiveChecker activeChecker, 212 IActiveChecker activeChecker,
221 IExecuteApplicable checker) { 213 IExecuteApplicable checker) {
222 loggedCommandIds = new HashSet!(String); 214 loggedCommandIds = new HashSet();
223 registeredListeners = new HashMap!(String,IPropertyChangeListener); 215 registeredListeners = new HashMap();
224 if (bindingManager is null) { 216 if (bindingManager is null) {
225 throw new NullPointerException( 217 throw new NullPointerException(
226 "The callback needs a binding manager"); //$NON-NLS-1$ 218 "The callback needs a binding manager"); //$NON-NLS-1$
227 } 219 }
228 220
258 } else if (existing !is null) { 250 } else if (existing !is null) {
259 ListenerList listeners = new ListenerList(ListenerList.IDENTITY); 251 ListenerList listeners = new ListenerList(ListenerList.IDENTITY);
260 listeners.add(existing); 252 listeners.add(existing);
261 listeners.add(cast(Object)listener); 253 listeners.add(cast(Object)listener);
262 } else { 254 } else {
263 registeredListeners.add(commandId, listener); 255 registeredListeners.put(commandId, cast(Object)listener);
264 } 256 }
265 if (!bindingManagerListenerAttached) { 257 if (!bindingManagerListenerAttached) {
266 bindingManager.addBindingManagerListener(this); 258 bindingManager.addBindingManagerListener(this);
267 bindingManagerListenerAttached = true; 259 bindingManagerListenerAttached = true;
268 } 260 }
269 } 261 }
270 262
271 public final void bindingManagerChanged(BindingManagerEvent event) { 263 public final void bindingManagerChanged(BindingManagerEvent event) {
272 if (event.isActiveBindingsChanged()) { 264 if (event.isActiveBindingsChanged()) {
273 foreach( k,v; registeredListeners ){ 265 final Iterator listenerItr = registeredListeners.entrySet()
274 // Iterator listenerItr = registeredListeners.entrySet() 266 .iterator();
275 // .iterator(); 267 while (listenerItr.hasNext()) {
276 // while (listenerItr.hasNext()) { 268 Map.Entry entry = cast(Map.Entry) listenerItr.next();
277 // Map.Entry entry = cast(Map.Entry) listenerItr.next(); 269 String commandId = stringcast( entry.getKey());
278 String commandId = k;//stringcast(k);// entry.getKey(); 270 Command command = commandManager
279 Command command = commandManager.getCommand(commandId); 271 .getCommand(commandId);
280 ParameterizedCommand parameterizedCommand = new ParameterizedCommand( 272 ParameterizedCommand parameterizedCommand = new ParameterizedCommand(
281 command, null); 273 command, null);
282 if (event.isActiveBindingsChangedFor(parameterizedCommand)) { 274 if (event.isActiveBindingsChangedFor(parameterizedCommand)) {
283 Object value = cast(Object) v; 275 Object value = entry.getValue();
284 PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(event 276 PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(event
285 .getManager(), IAction.TEXT, null, null); 277 .getManager(), IAction.TEXT, null, null);
286 if (null !is cast(ListenerList)value ) { 278 if (null !is cast(ListenerList)value ) {
287 Object[] listeners= (cast(ListenerList) value).getListeners(); 279 Object[] listeners= (cast(ListenerList) value).getListeners();
288 for (int i = 0; i < listeners.length; i++) { 280 for (int i = 0; i < listeners.length; i++) {
299 } 291 }
300 292
301 /** 293 /**
302 * @see dwtx.jface.action.ExternalActionManager.ICallback#getAccelerator(String) 294 * @see dwtx.jface.action.ExternalActionManager.ICallback#getAccelerator(String)
303 */ 295 */
304 public ValueWrapperInt getAccelerator(String commandId) { 296 public Integer getAccelerator(String commandId) {
305 TriggerSequence triggerSequence = bindingManager 297 TriggerSequence triggerSequence = bindingManager
306 .getBestActiveBindingFor(commandId); 298 .getBestActiveBindingFor(commandId);
307 if (triggerSequence !is null) { 299 if (triggerSequence !is null) {
308 Trigger[] triggers = triggerSequence.getTriggers(); 300 Trigger[] triggers = triggerSequence.getTriggers();
309 if (triggers.length is 1) { 301 if (triggers.length is 1) {
310 Trigger trigger = triggers[0]; 302 Trigger trigger = triggers[0];
311 if ( auto keyStroke = cast(KeyStroke) trigger ) { 303 if ( auto keyStroke = cast(KeyStroke) trigger ) {
312 int accelerator = SWTKeySupport 304 int accelerator = SWTKeySupport
313 .convertKeyStrokeToAccelerator(keyStroke); 305 .convertKeyStrokeToAccelerator(keyStroke);
314 return new ValueWrapperInt(accelerator); 306 return new Integer(accelerator);
315 } 307 }
316 } 308 }
317 } 309 }
318 310
319 return null; 311 return null;
420 */ 412 */
421 public final void removePropertyChangeListener(String commandId, 413 public final void removePropertyChangeListener(String commandId,
422 IPropertyChangeListener listener) { 414 IPropertyChangeListener listener) {
423 Object existing = cast(Object) registeredListeners.get(commandId); 415 Object existing = cast(Object) registeredListeners.get(commandId);
424 if (existing is cast(Object)listener) { 416 if (existing is cast(Object)listener) {
425 registeredListeners.removeKey(commandId); 417 registeredListeners.remove(commandId);
426 if (registeredListeners.drained()) { 418 if (registeredListeners.isEmpty()) {
427 bindingManager.removeBindingManagerListener(this); 419 bindingManager.removeBindingManagerListener(this);
428 bindingManagerListenerAttached = false; 420 bindingManagerListenerAttached = false;
429 } 421 }
430 } else if (null !is cast(ListenerList)existing ) { 422 } else if (null !is cast(ListenerList)existing ) {
431 ListenerList existingList = cast(ListenerList) existing; 423 ListenerList existingList = cast(ListenerList) existing;
432 existingList.remove(cast(Object)listener); 424 existingList.remove(cast(Object)listener);
433 if (existingList.size() is 1) { 425 if (existingList.size() is 1) {
434 registeredListeners.add(commandId, cast(IPropertyChangeListener)existingList.getListeners()[0]); 426 registeredListeners.put(commandId, existingList.getListeners()[0]);
435 } 427 }
436 } 428 }
437 } 429 }
438 430
439 /** 431 /**
445 || !applicabilityChecker.isApplicable(action)) { 437 || !applicabilityChecker.isApplicable(action)) {
446 return; 438 return;
447 } 439 }
448 Command command = commandManager.getCommand(actionDefinitionId); 440 Command command = commandManager.getCommand(actionDefinitionId);
449 ExecutionEvent executionEvent = new ExecutionEvent(command, 441 ExecutionEvent executionEvent = new ExecutionEvent(command,
450 EMPTY_MAP, event, null); 442 Collections.EMPTY_MAP, event, null);
451 443
452 commandManager.firePreExecute(actionDefinitionId, executionEvent); 444 commandManager.firePreExecute(actionDefinitionId, executionEvent);
453 } 445 }
454 446
455 /** 447 /**