View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  package org.caleigo.core.event;
19  
20  /*** The CELEventMulticaster uses a variation of the multi caster design used
21   * in java.awt package by the AWTEventMulticaster. 
22   *
23   * @author  Dennis Zikovic
24   * @version 1.00
25   *
26   *//* 
27   *
28   * WHEN        WHO               WHY & WHAT
29   * -----------------------------------------------------------------------------
30   * 2001-10-25  Dennis Zikovic    Creation
31   */
32  public final class CELEventMulticaster implements 
33          IEntityListener, 
34          IEntityChangeListener, 
35          ISelectionListener, 
36          IQualifierListener, 
37          IProxyListener
38  {
39      // Data members ------------------------------------------------------------
40      private final ICELEventListener first;
41      private final ICELEventListener second;
42      
43      // Static methods ----------------------------------------------------------
44      public static ICELEventListener add(ICELEventListener listener1, ICELEventListener listener2)
45      {
46  	if(listener1==null)  
47              return listener2;
48          else if(listener2==null)  
49              return listener1;
50          else
51              return new CELEventMulticaster(listener1, listener2);
52      }
53      
54      public static ICELEventListener remove(ICELEventListener existing, ICELEventListener toBeRemoved)
55      {
56          if(existing==toBeRemoved)
57              return null;
58          else if(existing instanceof CELEventMulticaster)
59              return ((CELEventMulticaster)existing).removeListener(toBeRemoved);
60          else
61              return existing;            
62      }
63      
64      // Constructors ------------------------------------------------------------
65      
66      /*** Creates new CELEventMulticaster. Note private scope.
67       */
68      private CELEventMulticaster(ICELEventListener listener1, ICELEventListener listener2) 
69      {
70          first = listener1;
71          second = listener2;
72      }
73      
74      // IEntityListener implementation ------------------------------------------
75      public void storePerformed(EntityEvent event)
76      {
77          ((IEntityListener)first).storePerformed(event);
78          ((IEntityListener)second).storePerformed(event);
79      }
80      
81      public void deletePerformed(EntityEvent event)
82      {
83          ((IEntityListener)first).deletePerformed(event);
84          ((IEntityListener)second).deletePerformed(event);
85      }
86      
87      public void refreshPerformed(EntityEvent event)  
88      {
89          ((IEntityListener)first).refreshPerformed(event);
90          ((IEntityListener)second).refreshPerformed(event);
91      }
92          
93      // IEntityChangeListener implementation ------------------------------------
94      public void dataChanged(EntityChangeEvent event)
95      {
96          ((IEntityChangeListener)first).dataChanged(event);
97          ((IEntityChangeListener)second).dataChanged(event);
98      }
99      
100     public void statusChanged(EntityChangeEvent event)
101     {
102         ((IEntityChangeListener)first).statusChanged(event);
103         ((IEntityChangeListener)second).statusChanged(event);
104     }
105     
106     // IEntityCollectionListener implementation --------------------------------
107     
108     /*** Called when an unspecified change of the content or order of the
109      * entities in the observed ISelection object has occured.
110      */
111     public void contentsChanged(SelectionEvent event)
112     {
113         ((ISelectionListener)first).contentsChanged(event);
114         ((ISelectionListener)second).contentsChanged(event);
115     }
116     
117     /*** Call when a single entity in the observed ISelection object has been
118      * added.
119      */
120     public void entityAdded(SelectionEvent event)
121     {
122         ((ISelectionListener)first).entityAdded(event);
123         ((ISelectionListener)second).entityAdded(event);
124     }
125     
126     /*** Call when a single entity in the observed ISelection object has been
127      * removed.
128      */
129     public void entityRemoved(SelectionEvent event)
130     {
131         ((ISelectionListener)first).entityRemoved(event);
132         ((ISelectionListener)second).entityRemoved(event);
133     }
134     
135     // IQualifierListener implemntation ----------------------------------------
136     
137     /*** Called whenever the structure of a qualifier has changed.
138      * Typically this is when a CompositeQualifier has had change in the
139      * contained Qualifier list.
140      */
141     public void structureChanged(QualifierEvent event)
142     {
143         ((IQualifierListener)first).structureChanged(event);
144         ((IQualifierListener)second).structureChanged(event);
145     }
146     
147     /*** Called whenever the content of a qualifier has changed.
148      * Typically this is when a RelationQualifier has had change in it's
149      * data content.
150      */
151     public void contentChanged(QualifierEvent event)
152     {
153         ((IQualifierListener)first).contentChanged(event);
154         ((IQualifierListener)second).contentChanged(event);
155     }
156     
157     // IProxyListener implemntation --------------------------------------------
158     
159     /*** Called when the remote object that have changed been replaced by
160      * another object or been set to null.
161      */
162     public void remoteChanged(ProxyEvent event)
163     {
164         ((IProxyListener)first).remoteChanged(event);
165         ((IProxyListener)second).remoteChanged(event);
166     }
167     
168     /*** Called when the remote object that have changes state from a
169      * placeholder to an actual object.
170      */
171     public void remoteExpanded(ProxyEvent event)
172     {
173         ((IProxyListener)first).remoteExpanded(event);
174         ((IProxyListener)second).remoteExpanded(event);
175     }
176     
177     // Action methods ----------------------------------------------------------
178     private ICELEventListener removeListener(ICELEventListener listener)
179     {
180         // Check for internal match.
181 	if(listener==first)  
182             return second;
183         else if(listener==second)  
184             return first;
185         
186         // Else recurse and check children.
187         ICELEventListener newFirst = CELEventMulticaster.remove(first, listener);
188         ICELEventListener newSecond = CELEventMulticaster.remove(second, listener);
189         if(first!=newFirst || second!=newSecond)
190             return CELEventMulticaster.add(newFirst, newSecond);
191         else
192             return this; 
193     }
194 }