KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > model > FormBeanModel


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

18 package org.apache.beehive.netui.compiler.model;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.beehive.netui.compiler.model.schema.struts11.FormBeanDocument;
25 import org.apache.beehive.netui.compiler.model.schema.struts11.SetPropertyDocument.SetProperty;
26 import org.apache.beehive.netui.compiler.JpfLanguageConstants;
27
28
29 /**
30  * Represents a form bean in a Struts application.
31  */

32 public class FormBeanModel
33         extends StrutsElementSupport
34         implements JpfLanguageConstants
35 {
36     public static class Property
37     {
38         private String JavaDoc _name;
39         private String JavaDoc _type;
40         private boolean _required;
41         private boolean _multiValue;
42
43         public Property( String JavaDoc name, String JavaDoc type, boolean required, boolean multival )
44         {
45             _name = name;
46             _type = type;
47             _required = required;
48             _multiValue = multival;
49         }
50
51         public String JavaDoc getName()
52         {
53             return _name;
54         }
55
56         public void setName( String JavaDoc name )
57         {
58             _name = name;
59         }
60
61         public String JavaDoc getType()
62         {
63             return _type;
64         }
65
66         public void setType( String JavaDoc type )
67         {
68             _type = type;
69         }
70
71         public boolean isRequired()
72         {
73             return _required;
74         }
75
76         public void setRequired( boolean required )
77         {
78             _required = required;
79         }
80
81         public boolean isMultiValue ()
82         {
83             return _multiValue;
84         }
85
86         public void setMultiValue ( boolean multi )
87         {
88             _multiValue = multi;
89         }
90     }
91
92
93     private static final String JavaDoc JPF_ACTION_FORM_BEAN_CLASSNAME = PAGEFLOW_PACKAGE + ".config.PageFlowActionFormBean";
94
95     private String JavaDoc _id = ""; // NOI18N
96
private String JavaDoc _className = null;
97     private boolean _dynamic = false;
98     private String JavaDoc _name = null; // required to be set
99
private String JavaDoc _type = null; // required to be set
100

101     /** This is a NetUI-specific property. */
102     private String JavaDoc _actualType = null;
103     
104     /** This is a NetUI-specific property. */
105     private boolean _pageFlowScoped; // required to be set
106

107     private ArrayList JavaDoc _properties = new ArrayList JavaDoc();
108
109     public FormBeanModel( String JavaDoc name, String JavaDoc type, String JavaDoc actualType, boolean pageFlowScoped, StrutsApp parent )
110     {
111         super( parent );
112         _name = name;
113         _type = type;
114         _actualType = actualType;
115         _pageFlowScoped = pageFlowScoped;
116     }
117
118     public void writeToXMLBean( FormBeanDocument.FormBean xb )
119     {
120         xb.setName( _name );
121         
122         if ( xb.getType() == null ) xb.setType( _type );
123         if ( xb.getId() == null && _id != null && _id.length() > 0 ) xb.setId( _id );
124         if ( xb.getClassName() == null &&_className != null ) xb.setClassName( _className );
125         if ( xb.getDynamic() == null && _dynamic ) xb.setDynamic( FormBeanDocument.FormBean.Dynamic.TRUE );
126
127         if ( _actualType != null && ! _actualType.equals( xb.getType() ) )
128         {
129             SetProperty prop = xb.addNewSetProperty();
130             prop.setProperty( "actualType" );
131             prop.setValue( _actualType );
132             if ( xb.getClassName() == null ) xb.setClassName( JPF_ACTION_FORM_BEAN_CLASSNAME );
133         }
134         
135     }
136     
137     public String JavaDoc getId()
138     {
139         return _id;
140     }
141
142     public void setId( String JavaDoc id )
143     {
144         _id = id;
145     }
146
147     public String JavaDoc getClassName()
148     {
149         return _className;
150     }
151
152     public void setClassName( String JavaDoc className )
153     {
154         if ( className != null )
155         {
156             if ("org.apache.struts.action.DynaActionForm".equals( className ) ) // NOI18N
157
_dynamic = true;
158
159             _className = className;
160         }
161     }
162
163     public boolean isDynamic()
164     {
165         return _dynamic;
166     }
167
168     public void setDynamic( boolean dynamic )
169     {
170         _dynamic = dynamic;
171     }
172
173     public String JavaDoc getName()
174     {
175         return _name;
176     }
177
178     public void setName( String JavaDoc name )
179     {
180         _name = name;
181     }
182
183     public String JavaDoc getType()
184     {
185         return _type;
186     }
187
188     public void setType( String JavaDoc type )
189     {
190         _type = type;
191     }
192
193     public String JavaDoc getActualType()
194     {
195         return _actualType;
196     }
197
198     public void setActualType(String JavaDoc actualType)
199     {
200         _actualType = actualType;
201     }
202
203     public void addProperty( String JavaDoc name, String JavaDoc type, boolean required, boolean multival )
204     {
205         _properties.add( new Property( name, type, required, multival ) );
206     }
207
208     /**
209      * Sets the collection of properties for a form bean to a new collection.
210      */

211     public void updateProperties( Collection JavaDoc newProps )
212     {
213         _properties = new ArrayList JavaDoc();
214
215         if ( newProps != null )
216         {
217             _properties.addAll( newProps );
218         }
219     }
220
221     public Property[] getProperties()
222     {
223         return ( Property[] ) _properties.toArray( new Property[]{} );
224     }
225
226     public void deleteProperty( String JavaDoc name )
227     {
228         for ( int i = 0; i < _properties.size(); ++i )
229         {
230             Property prop = ( Property ) _properties.get( i );
231
232             if ( prop.getName().equals( name ) )
233             {
234                 _properties.remove( i-- );
235             }
236         }
237     }
238
239     public void deleteProperty( Property prop )
240     {
241         _properties.remove( prop );
242     }
243
244     public Property findProperty( String JavaDoc name )
245     {
246         int index = findPropertyIndex( name );
247         return index != -1 ? ( Property ) _properties.get( index ) : null;
248     }
249
250     protected int findPropertyIndex( String JavaDoc name )
251     {
252         for ( int i = 0; i < _properties.size(); ++i )
253         {
254             Property prop = ( Property ) _properties.get( i );
255
256             if ( prop.getName().equals( name ) )
257             {
258                 return i;
259             }
260         }
261
262         return -1;
263     }
264
265     /**
266      * Returns a clone (shallow copy) of the internal properties list.
267      */

268     protected final List JavaDoc getPropertyList()
269     {
270         return ( List JavaDoc ) _properties.clone();
271     }
272
273     public boolean isPageFlowScoped()
274     {
275         return _pageFlowScoped;
276     }
277 }
278
Popular Tags