KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > util > EventUtil


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

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 /**
29  * Contains access stack events related utility methods.
30  *
31  * @since 1.2
32  * @author Andrus Adamchik
33  */

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     /**
42      * Utility method that sets up a GraphChangeListener to be notified when DataChannel
43      * posts an event.
44      *
45      * @return false if an DataChannel doesn't have an EventManager and therefore does not
46      * support events.
47      */

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     /**
63      * Listen for events from all channels that use a given EventManager.
64      */

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     /**
78      * Registers GraphEventListener for multiple subjects at once.
79      */

80     static void listenForSubjects(
81             EventManager manager,
82             DataChannelListener listener,
83             Object JavaDoc sender,
84             EventSubject[] subjects) {
85
86         for (int i = 0; i < subjects.length; i++) {
87             // assume that subject name and listener method name match
88
String JavaDoc fqSubject = subjects[i].getSubjectName();
89             String JavaDoc method = fqSubject.substring(fqSubject.lastIndexOf('/') + 1);
90
91             manager.addListener(listener, method, GraphEvent.class, subjects[i], sender);
92         }
93     }
94
95     // not for instantiation
96
private EventUtil() {
97     }
98 }
99
Popular Tags