1 4 package com.nightlabs.rcp.notification; 5 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.Map ; 9 10 import org.eclipse.swt.widgets.Display; 11 12 import com.nightlabs.notification.NotificationEvent; 13 import com.nightlabs.notification.NotificationListener; 14 import com.nightlabs.notification.SubjectCarrier; 15 import com.nightlabs.util.RWLock; 16 17 20 public class SelectionManager extends NotificationManager 21 { 22 private static SelectionManager _sharedInstance = null; 23 24 public static SelectionManager sharedInstance() 25 { 26 if (_sharedInstance == null) 27 _sharedInstance = new SelectionManager(); 28 29 return _sharedInstance; 30 } 31 32 protected SelectionManager() 33 { 34 } 35 36 43 private Map eventsByZone = new HashMap (); 44 private RWLock eventsByZoneMutex = new RWLock("eventsByZoneMutex"); 45 46 protected void registerEvent(NotificationEvent event) 47 { 48 eventsByZoneMutex.acquireWriteLock(); 49 try { 50 51 Map eventsByClass = (Map ) eventsByZone.get(event.getZone()); 52 if (eventsByClass == null) { 53 eventsByClass = new HashMap (); 54 eventsByZone.put(event.getZone(), eventsByClass); 55 } 56 57 for (Iterator itSubjectCarriers = event.getSubjectCarriers().iterator(); itSubjectCarriers.hasNext(); ) { 58 SubjectCarrier carrier = (SubjectCarrier) itSubjectCarriers.next(); 59 for (Iterator itClasses = carrier.getSubjectClasses().iterator(); itClasses.hasNext(); ) { 60 Class clazz = (Class ) itClasses.next(); 61 62 if (carrier.isInheritanceIgnored()) 63 eventsByClass.put(clazz, event); 64 else { 65 do { 66 eventsByClass.put(clazz, event); 67 clazz = clazz.getSuperclass(); 68 } while (clazz != null); 69 } 70 } 71 } 73 89 } finally { 90 eventsByZoneMutex.releaseLock(); 91 } 92 } 93 94 97 public void addNotificationListener( 98 String zone, Class subjectClass, final NotificationListener listener) 99 { 100 super.addNotificationListener(zone, subjectClass, listener); 101 102 eventsByZoneMutex.acquireReadLock(); 103 try { 104 Map eventsByClass = (Map ) eventsByZone.get(zone); 105 if (eventsByClass == null) 106 return; 107 108 final NotificationEvent event = (NotificationEvent) eventsByClass.get(subjectClass); 109 if (event == null) 110 return; 111 112 Display.getDefault().asyncExec( 113 new Runnable () { 114 public void run() 115 { 116 SelectionManager.this.notify(event, listener); 117 } 118 }); 119 } finally { 120 eventsByZoneMutex.releaseLock(); 121 } 122 } 123 124 127 protected NotificationEvent intercept(NotificationEvent event) 128 { 129 event = super.intercept(event); 130 registerEvent(event); 131 return event; 132 } 133 134 } 135 | Popular Tags |