View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.view;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.seasar.tuigwaa.model.common.EntityUtils;
24  import org.seasar.tuigwaa.model.core.TgwAttribute;
25  import org.seasar.tuigwaa.util.ajax.AbstractRowSetForm;
26  
27  import com.isenshi.util.HtmlBuffer;
28  
29  public class TableFormComponent extends AbstractFormComponent {
30  
31  	private EntityFormComponent formComponent;
32  
33  	public TableFormComponent(String siteName,
34  			EntityFormComponent formComponent, String actionName,
35  			String formName) {
36  		super(siteName, null);
37  		this.formComponent = formComponent;
38  		setActionName(actionName);
39  		setFormName(formName);
40  	}
41  
42  	public String getCustomFormName() {
43  		if (formComponent instanceof CustomFormComponent) {
44  			CustomFormComponent customFormComponent = (CustomFormComponent) formComponent;
45  			return customFormComponent.getCustomFormName();
46  		}
47  		return null;
48  	}
49  
50  	public String getOriginalFormName() {
51  		return formComponent.getFormName();
52  	}
53  
54  	public String[] getFieldElems(int index, Object valueObject,
55  			String removeButton) {
56  		List elems = toRowElements(index, valueObject);
57  		elems.add(removeButton);
58  		return (String[]) elems.toArray(new String[elems.size()]);
59  	}
60  
61  	public List toRowElements(int index, Object valueObject) {
62  		return toRowElements(index, valueObject, null);
63  	}
64  
65  	public void setBindingFkObjMap(Map bindingFkObjMap) {
66  		formComponent.setBindingFkObjMap(bindingFkObjMap);
67  	}
68  	
69  	public String getExtraHidden(){
70  		Iterator itr = formComponent.getEntity().getFieldIterator();
71  		String ret = "";
72  		while(itr.hasNext()){
73  			TgwAttribute attr = (TgwAttribute)itr.next();
74  			String hidden = formComponent.createExtraHidden(attr);
75  			if(hidden != null){
76  				ret = hidden;
77  			}
78  		}
79  		return ret;
80  	}
81  
82  	public List toRowElements(int index, Object valueObject, List headers) {
83  		String prefix = AbstractRowSetForm.getPrefix(index);
84  		List list = formComponent.getRowElements(prefix, valueObject);
85  
86  		List htmlList = new ArrayList();
87  		
88  		if (headers!= null && headers.contains("id")) {
89  			htmlList.add(toIdCell(prefix, valueObject));
90  		}
91  		for (Iterator i = list.iterator(); i.hasNext();) {
92  			FormElement elem = (FormElement) i.next();
93  			if (headers == null || headers.contains(elem.getAttrName())) {
94  				htmlList.add(elem.getElement());
95  			}
96  		}
97  		htmlList.set(0 , toHiddenId(prefix, valueObject)+ htmlList.get(0));
98  		return htmlList;
99  	}
100 
101 	private String toIdCell(String prefix, Object valueObject) {
102 		if (valueObject != null) {
103 			String id = String.valueOf(EntityUtils.getId(valueObject));
104 			return id;
105 		}
106 		return "";
107 	}
108 
109 	private String toHiddenId(String prefix, Object valueObject){
110 		String id  = "";
111 		if (valueObject != null) {
112 			id = String.valueOf(EntityUtils.getId(valueObject));
113 		}
114 		HtmlBuffer buf = new HtmlBuffer();
115 		buf.appendHidden(prefix + EntityUtils.ESCAPED_ID, id);
116 		return buf.toString();
117 	}
118 	
119 	public EntityFormComponent getFormComponent() {
120 		return formComponent;
121 	}
122 
123 	// * override method
124 
125 	protected String getOnSubmitFunction() {
126 		return null;
127 	}
128 
129 	protected boolean isMultiPart() {
130 		return formComponent.isMultiPart();
131 	}
132 
133 	protected Map getHiddenProperties() {
134 		return formComponent.getHiddenProperties();
135 	}
136 }