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  
19  package org.caleigo.core.event;
20  
21  import org.caleigo.core.*;
22  
23  /*** Event for relaying value and status changes in IEntity objects. 
24   *
25   * @author  Dennis Zikovic
26   * @version 1.00
27   * 
28   *//* 
29   *
30   * WHEN        WHO               WHY & WHAT
31   * ------------------------------------------------------------------------------
32   * 2001-07-12  Dennis Zikovic    Creation
33   * 2001-10-23  Dennis Zikovic    Changed superclass to EntityEvent.
34   */
35  public class EntityChangeEvent extends EntityEvent
36  {
37      // Data members ------------------------------------------------------------
38      private IFieldDescriptor mFieldDescriptor;
39      private int mStatusType;
40      private Object mOldValue;
41      private Object mNewValue;
42      
43      // Constructors ------------------------------------------------------------
44      
45      /*** Use this costructor for specified data content changes specified
46       * by an IFieldDescriptor.
47       */
48      public EntityChangeEvent(IEntity entity, IFieldDescriptor descriptor, Object oldValue, Object newValue) 
49      {
50          super(entity, DATA_CHANGED);
51          mFieldDescriptor = descriptor;
52          mStatusType = -1;
53          mOldValue = oldValue;
54          mNewValue = newValue;
55      }
56      
57      /*** Use this costructor for logic state changes.
58       */
59      public EntityChangeEvent(IEntity entity, int statusType, boolean newState) 
60      {
61          super(entity, STATUS_CHANGED);
62          mFieldDescriptor = null;
63          mStatusType = statusType;
64          mNewValue = new Boolean(newState);
65      }
66      
67      // Supercalss overrides ----------------------------------------------------
68      public String toString()
69      {
70          String params = null;
71          if(this.isDataChange())
72          {
73              params = "DATA_CHANGED, "+mFieldDescriptor;
74              params += ", "+mFieldDescriptor.getDataType().makeLogString(this.getOldValue())
75                      +" -> "+mFieldDescriptor.getDataType().makeLogString(this.getNewValue()); 
76          }
77          else
78          {
79              if((mStatusType & IEntity.DIRTY)!=0)
80                  params = "DIRTY"; 
81              if((mStatusType & IEntity.EMPTY)!=0)
82                  params = params==null ? "EMPTY" : params+"+EMPTY"; 
83              if((mStatusType & IEntity.PERSISTENT)!=0)
84                  params = params==null ? "PERSISTENT" : params+"+PERSISTENT"; 
85              if(params==null)
86                  params = "UNKNOWN_STATUS"; 
87              params = "STATUS_CHANGED, "+params;
88              params += ", "+this.getOldValue()+" -> "+this.getNewValue(); 
89          }
90  	return getClass().getName()+"["+params+"] on "+source.toString();
91      }
92  
93      // Access methods ----------------------------------------------------------
94      public boolean isDataChange()
95      {
96          return mFieldDescriptor!=null;
97      }
98      
99      public boolean isStatusChange()
100     {
101         return mFieldDescriptor==null;
102     }
103     
104     public int getStatusType() 
105     {
106         return mStatusType;
107     }
108     
109     public IFieldDescriptor getFieldDescriptor() 
110     {
111         return mFieldDescriptor;
112     }
113     
114     public Object getOldValue() 
115     {
116         if(isStatusChange() && mOldValue==null)
117             mOldValue = new Boolean(!((Boolean)mNewValue).booleanValue());
118         return mOldValue;
119     }
120     
121     public Object getNewValue() 
122     {
123         return mNewValue;
124     }
125 }