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;
20  
21  import java.util.*;
22  
23  /*** <Description for DataBundleDescriptor>
24   *
25   * @author  Dennis Zikovic
26   * @version 1.00
27   *
28   *//*
29   *
30   * WHEN        WHO               WHY & WHAT
31   * -----------------------------------------------------------------------------
32   * 2002-09-23  Dennis Zikovic    Creation
33   */
34  public class DataBundleDescriptor implements IDataBundleDescriptor
35  {
36      // Data members --------------------------------------------------------
37      private List mDescriptorItemList;
38  
39      // Costructors ---------------------------------------------------------
40      public DataBundleDescriptor()
41      {
42          mDescriptorItemList = new ArrayList();
43      }
44  
45      // IActionDataDescriptor implementation --------------------------------
46  
47      /*** This method returns the number of data item objects that are
48       * defined by the IActionDataDescriptor object.
49       */
50      public int getItemCount()
51      {
52          return mDescriptorItemList.size();
53      }
54  
55      /*** This method returns the item type of the indexed data item
56       * defined by the IActionDataDescriptor object.
57       */
58      public int getItemType(int index)
59      {
60          return ((DescriptorItem)mDescriptorItemList.get(index)).mType;
61      }
62  
63      /*** Returns the DataType of the indexed data item if that item is of the 
64       * type DATA otherwise null is returned.
65       */
66      public DataType getDataType(int index)
67      {
68          if(this.getItemType(index)==DATA)
69              return (DataType)((DescriptorItem)mDescriptorItemList.get(index)).mDescriptionObject;
70          else
71              return null;
72      }
73  
74      /*** Returns the DataType of the indexed data item if that item is of the 
75       * type ENTITY or SELECTION otherwise null is returned.
76       */
77      public IEntityDescriptor getEntityDescriptor(int index)
78      {
79          if(this.getItemType(index)!=DATA)
80              return (IEntityDescriptor)((DescriptorItem)mDescriptorItemList.get(index)).mDescriptionObject;
81          else
82              return null;
83      }
84  
85      /*** Access method that returns the code name that can be used to identify
86       * the indexed data item.
87       */
88      public String getCodeName(int index)
89      {
90          return ((DescriptorItem)mDescriptorItemList.get(index)).mCodeName;
91      }
92  
93      /*** Access method that returns a end-user presentable name that can be used 
94       * as button and meny labels for the indexed data item.
95       */
96      public String getDisplayName(int index)
97      {
98          return ((DescriptorItem)mDescriptorItemList.get(index)).mDisplayName;
99      }
100 
101     /*** Access method that returns a end-user presentable hint text that can be 
102      * used as a help text or fly-by hint.
103      */
104     public String getDisplayHint(int index)
105     {
106         return ((DescriptorItem)mDescriptorItemList.get(index)).mDisplayHint;
107     }
108 
109     /*** Help method that returns the indexed of the named data item. If the
110      * code name can not be identified then a negative value is returned.
111      */
112     public int getItemIndex(String codeName)
113     {
114         int index = mDescriptorItemList.size()-1;
115         while(index>=0 && !codeName.equals(((DescriptorItem)mDescriptorItemList.get(index)).mCodeName))
116             index--;
117         return index;
118     }
119 
120     /*** Action method that creates IActionData object for the called ActionData-
121      * Descriptor. The created IActionData will be defined by the called
122      * IActionDataDescriptor and will only be complete if it contains all data
123      * specified by that descriptor.
124      */
125     public IDataBundle createActionData()
126     {
127         return new DataBundle(this);
128     }
129 
130     /*** Help method that validates the provided IActionData object. The data
131      * object must have the called descriptor as its defining descriptor or
132      * an IllegalArgumentException exception is thrown.
133      */
134     public ValidationResult validateData(IDataBundle actionData)
135     {
136         if(actionData==null || actionData.getDataBundleDescriptor()!=this)
137             throw new IllegalArgumentException("IActionDataDescriptor missmatch!");
138 
139         ValidationResult result = ValidationResult.VALID_RESULT;
140         for(int j=0; j<mDescriptorItemList.size() && result.isValid(); j++)
141             result = ((DescriptorItem)mDescriptorItemList.get(j)).validateData(actionData);
142         return result;
143     }
144 
145     // Action methods ------------------------------------------------------
146 
147     protected void addDataTypeDescriptor(DataType dataType, String codeName, String displayName, String displayHint)
148     {
149         mDescriptorItemList.add(new DescriptorItem(DATA, dataType, codeName, displayName, displayHint));
150     }
151 
152     protected void addEntityDescriptor(IEntityDescriptor entityDescriptor, String codeName, String displayName, String displayHint)
153     {
154         mDescriptorItemList.add(new DescriptorItem(ENTITY, entityDescriptor, codeName, displayName, displayHint));
155     }
156 
157     protected void addSelectionDescriptor(IEntityDescriptor entityDescriptor, String codeName, String displayName, String displayHint)
158     {
159         mDescriptorItemList.add(new DescriptorItem(SELECTION, entityDescriptor, codeName, displayName, displayHint));
160     }
161 
162     protected void addDataValidator(String dataCodeName, IDataValidator dataValidator)
163     {
164         ((DescriptorItem)mDescriptorItemList.get(this.getItemIndex(dataCodeName))).addDataValidator(dataValidator);
165     }
166 
167     // Internal classes ----------------------------------------------------
168     protected static class DescriptorItem
169     {
170         // Data members ----------------------------------------------------
171         public int mType;
172         public Object mDescriptionObject;
173         public String mCodeName;
174         public String mDisplayName;
175         public String mDisplayHint;
176 
177         protected List mValidatorList;
178 
179         // Constructors ----------------------------------------------------
180         public DescriptorItem(int type, Object descriptionObject, String codeName, String displayName, String hint)
181         {
182             mType = type;
183             mDescriptionObject = descriptionObject;
184             mCodeName = codeName;
185             mDisplayName = displayName;
186             mDisplayHint = hint;
187         }
188 
189         // Help methods ----------------------------------------------------
190         public ValidationResult validateData(IDataBundle actionData)
191         {
192             ValidationResult result = ValidationResult.VALID_RESULT;
193             for(int j=0; mValidatorList!=null && j<mValidatorList.size() && result.isValid(); j++)
194                 if(!((IDataValidator)mValidatorList.get(j)).isDataValid(actionData.getData(mCodeName), actionData))
195                     result = new ValidationResult(this, ((IDataValidator)mValidatorList.get(j)).getInvalidMessage(actionData.getData(mCodeName), actionData));
196             return result;
197         }
198 
199         protected void addDataValidator(IDataValidator dataValidator)
200         {
201             if(mValidatorList==null)
202                 mValidatorList = new ArrayList(3);
203             mValidatorList.add(dataValidator);
204         }
205     }
206 }