KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > core > event > TypedRelationEventListenerWrapper


1 /*
2  * Created on 9-sep-2005
3  * This software is OSI Certified Open Source Software.
4  * OSI Certified is a certification mark of the Open Source Initiative. The
5  * license (Mozilla version 1.0) can be read at the MMBase site. See
6  * http://www.MMBase.org/license
7  */

8 package org.mmbase.core.event;
9
10 import java.util.Properties JavaDoc;
11 import org.mmbase.module.core.*;
12 import org.mmbase.util.HashCodeUtil;
13 import org.mmbase.storage.search.RelationStep;
14 /**
15  * This class is a wrapper for relation event listeners that only want to listen
16  * to events concerning a specific builder - more specifically, events concerning changes
17  * in the relations from a specific builder's nodes.
18  *
19  * @author Ernst Bunders
20  * @since MMBase-1.8
21  *
22  */

23 public class TypedRelationEventListenerWrapper implements RelationEventListener {
24
25     private final MMObjectBuilder builder;
26     private final String JavaDoc nodeType;
27     private final RelationEventListener wrappedListener;
28     private final int direction;
29     private final boolean descendants;
30
31     /**
32      * @param builder The builder which must constrain the listener
33      * @param wrappedListener the relation event listener you want to wrap
34      * @param direction At which side of the relation nodes of this builders can be: {@link org.mmbase.storage.search.RelationStep#DIRECTIONS_SOURCE}, {@link org.mmbase.storage.search.RelationStep#DIRECTIONS_DESTINATION}, or {@link org.mmbase.storage.search.RelationStep#DIRECTIONS_BOTH}
35      * @param descendants Whether also descendants of the given builder must be listened to. ('true' would be the must logical value).
36      */

37     public TypedRelationEventListenerWrapper(MMObjectBuilder builder, RelationEventListener wrappedListener, int direction, boolean descendants) {
38         this.builder = builder;
39         this.nodeType = builder.getTableName();
40         this.wrappedListener = wrappedListener;
41         this.direction = direction;
42         this.descendants = descendants;
43     }
44
45
46     private boolean notify(RelationEvent event, String JavaDoc eventNodeType) {
47         if (nodeType.equals(eventNodeType)) {
48             wrappedListener.notify(event);
49             return true;
50         } else if (descendants) {
51             MMObjectBuilder eventBuilder = MMBase.getMMBase().getBuilder(eventNodeType);
52             if(nodeType.equals("object") || (eventBuilder != null && eventBuilder.isExtensionOf(builder))) {
53                 wrappedListener.notify(event);
54                 return true;
55             }
56         }
57         return false;
58     }
59
60     public void notify(RelationEvent event) {
61         switch(direction) {
62         case RelationStep.DIRECTIONS_SOURCE:
63             notify(event, event.getRelationSourceType());
64             break;
65         case RelationStep.DIRECTIONS_DESTINATION:
66             notify(event, event.getRelationDestinationType());
67             break;
68         case RelationStep.DIRECTIONS_BOTH:
69         default:
70             if (! notify(event, event.getRelationSourceType())) {
71                 notify(event, event.getRelationDestinationType());
72             }
73         }
74     }
75
76     public String JavaDoc toString() {
77         return "TypedRelationEventListenerWrapper(" + wrappedListener + ")";
78     }
79
80     public boolean equals(Object JavaDoc o) {
81         if (o instanceof TypedRelationEventListenerWrapper) {
82             TypedRelationEventListenerWrapper tw = (TypedRelationEventListenerWrapper) o;
83             return
84                 builder.equals(tw.builder) &&
85                 wrappedListener.equals(tw.wrappedListener) &&
86                 direction == tw.direction &&
87                 descendants == tw.descendants;
88         } else {
89             return false;
90         }
91     }
92     public int hashCode() {
93         int result = 0;
94         result = HashCodeUtil.hashCode(result, builder);
95         result = HashCodeUtil.hashCode(result, wrappedListener);
96         result = HashCodeUtil.hashCode(result, direction);
97         return result;
98
99     }
100
101
102 }
103
Popular Tags