1 19 20 package org.apache.cayenne.util; 21 22 import org.apache.cayenne.DataChannel; 23 import org.apache.cayenne.DataChannelListener; 24 import org.apache.cayenne.event.EventManager; 25 import org.apache.cayenne.event.EventSubject; 26 import org.apache.cayenne.graph.GraphEvent; 27 28 34 public class EventUtil { 35 36 static final EventSubject[] CHANNEL_SUBJECTS = new EventSubject[] { 37 DataChannel.GRAPH_CHANGED_SUBJECT, DataChannel.GRAPH_FLUSHED_SUBJECT, 38 DataChannel.GRAPH_ROLLEDBACK_SUBJECT 39 }; 40 41 48 public static boolean listenForChannelEvents( 49 DataChannel channel, 50 DataChannelListener listener) { 51 52 EventManager manager = channel.getEventManager(); 53 54 if (manager == null) { 55 return false; 56 } 57 58 listenForSubjects(manager, listener, channel, CHANNEL_SUBJECTS); 59 return true; 60 } 61 62 65 public static boolean listenForChannelEvents( 66 EventManager manager, 67 DataChannelListener listener) { 68 69 if (manager == null) { 70 return false; 71 } 72 73 listenForSubjects(manager, listener, null, CHANNEL_SUBJECTS); 74 return true; 75 } 76 77 80 static void listenForSubjects( 81 EventManager manager, 82 DataChannelListener listener, 83 Object sender, 84 EventSubject[] subjects) { 85 86 for (int i = 0; i < subjects.length; i++) { 87 String fqSubject = subjects[i].getSubjectName(); 89 String method = fqSubject.substring(fqSubject.lastIndexOf('/') + 1); 90 91 manager.addListener(listener, method, GraphEvent.class, subjects[i], sender); 92 } 93 } 94 95 private EventUtil() { 97 } 98 } 99 | Popular Tags |