001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.compiler.model;
020:
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.List;
024:
025: import org.apache.beehive.netui.compiler.JpfLanguageConstants;
026: import org.w3c.dom.Element;
027:
028: /**
029: * Represents a form bean in a Struts application.
030: */
031: public class FormBeanModel extends StrutsElementSupport implements
032: JpfLanguageConstants {
033: public static class Property {
034: private String _name;
035: private String _type;
036: private boolean _required;
037: private boolean _multiValue;
038:
039: public Property(String name, String type, boolean required,
040: boolean multival) {
041: _name = name;
042: _type = type;
043: _required = required;
044: _multiValue = multival;
045: }
046:
047: public String getName() {
048: return _name;
049: }
050:
051: public void setName(String name) {
052: _name = name;
053: }
054:
055: public String getType() {
056: return _type;
057: }
058:
059: public void setType(String type) {
060: _type = type;
061: }
062:
063: public boolean isRequired() {
064: return _required;
065: }
066:
067: public void setRequired(boolean required) {
068: _required = required;
069: }
070:
071: public boolean isMultiValue() {
072: return _multiValue;
073: }
074:
075: public void setMultiValue(boolean multi) {
076: _multiValue = multi;
077: }
078: }
079:
080: private static final String CUSTOM_ACTION_FORM_BEAN_CLASSNAME = PAGEFLOW_PACKAGE
081: + ".config.PageFlowActionFormBean";
082:
083: private String _id = ""; // NOI18N
084: private boolean _dynamic = false;
085: private String _name = null; // required to be set
086: private String _type = null; // required to be set
087: private String _formBeanMessageResourcesKey = null;
088:
089: /** This is a NetUI-specific property. */
090: private String _actualType = null;
091:
092: /** This is a NetUI-specific property. */
093: private boolean _pageFlowScoped; // required to be set
094:
095: private ArrayList _properties = new ArrayList();
096:
097: public FormBeanModel(String name, String type, String actualType,
098: boolean pageFlowScoped, String formBeanMessageResorucesKey,
099: StrutsApp parent) {
100: super (parent);
101: _name = name;
102: _type = type;
103: _actualType = actualType;
104: _pageFlowScoped = pageFlowScoped;
105: _formBeanMessageResourcesKey = formBeanMessageResorucesKey;
106: }
107:
108: protected void writeToElement(XmlModelWriter xw, Element element) {
109: element.setAttribute("name", _name);
110:
111: setElementAttribute(element, "type", _type);
112: setElementAttribute(element, "id", _id);
113: setElementAttribute(element, "className", getClassName());
114: setElementAttribute(element, "dynamic", _dynamic);
115: setElementAttribute(element, "className",
116: CUSTOM_ACTION_FORM_BEAN_CLASSNAME);
117:
118: if (_actualType != null
119: && !_actualType.equals(element.getAttribute("type"))) {
120: setCustomProperty(xw, element, "actualType", _actualType,
121: CUSTOM_ACTION_FORM_BEAN_CLASSNAME);
122: }
123: }
124:
125: public String getId() {
126: return _id;
127: }
128:
129: public void setId(String id) {
130: _id = id;
131: }
132:
133: public void setClassName(String className) {
134: if (className != null) {
135: if ("org.apache.struts.action.DynaActionForm"
136: .equals(className)) // NOI18N
137: _dynamic = true;
138:
139: setClassName(className);
140: }
141: }
142:
143: public boolean isDynamic() {
144: return _dynamic;
145: }
146:
147: public void setDynamic(boolean dynamic) {
148: _dynamic = dynamic;
149: }
150:
151: public String getName() {
152: return _name;
153: }
154:
155: public void setName(String name) {
156: _name = name;
157: }
158:
159: public String getType() {
160: return _type;
161: }
162:
163: public void setType(String type) {
164: _type = type;
165: }
166:
167: public String getActualType() {
168: return _actualType;
169: }
170:
171: public void setActualType(String actualType) {
172: _actualType = actualType;
173: }
174:
175: public String getFormBeanMessageResourcesKey() {
176: return _formBeanMessageResourcesKey;
177: }
178:
179: public void addProperty(String name, String type, boolean required,
180: boolean multival) {
181: _properties.add(new Property(name, type, required, multival));
182: }
183:
184: /**
185: * Sets the collection of properties for a form bean to a new collection.
186: */
187: public void updateProperties(Collection newProps) {
188: _properties = new ArrayList();
189:
190: if (newProps != null) {
191: _properties.addAll(newProps);
192: }
193: }
194:
195: public Property[] getProperties() {
196: return (Property[]) _properties.toArray(new Property[] {});
197: }
198:
199: public void deleteProperty(String name) {
200: for (int i = 0; i < _properties.size(); ++i) {
201: Property prop = (Property) _properties.get(i);
202:
203: if (prop.getName().equals(name)) {
204: _properties.remove(i--);
205: }
206: }
207: }
208:
209: public void deleteProperty(Property prop) {
210: _properties.remove(prop);
211: }
212:
213: public Property findProperty(String name) {
214: int index = findPropertyIndex(name);
215: return index != -1 ? (Property) _properties.get(index) : null;
216: }
217:
218: protected int findPropertyIndex(String name) {
219: for (int i = 0; i < _properties.size(); ++i) {
220: Property prop = (Property) _properties.get(i);
221:
222: if (prop.getName().equals(name)) {
223: return i;
224: }
225: }
226:
227: return -1;
228: }
229:
230: /**
231: * Returns a clone (shallow copy) of the internal properties list.
232: */
233: protected final List getPropertyList() {
234: return (List) _properties.clone();
235: }
236:
237: public boolean isPageFlowScoped() {
238: return _pageFlowScoped;
239: }
240:
241: protected void addSetProperty(XmlModelWriter xw, Element element,
242: String propertyName, String propertyValue) {
243: throw new UnsupportedOperationException("not implemented for "
244: + getClass().getName());
245: }
246: }
|