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  /*
17   * Created on 2005/08/26
18   */
19  package org.seasar.tuigwaa.util.ajax;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.commons.collections.Factory;
29  import org.apache.commons.collections.ListUtils;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.struts.validator.ValidatorForm;
33  import org.json.JSONObject;
34  import org.seasar.tuigwaa.util.TgwResource;
35  
36  import com.isenshi.util.CharUtil;
37  import com.isenshi.util.HtmlBuffer;
38  
39  /***
40   * @author nishioka
41   */
42  public abstract class AbstractRowSetForm extends ValidatorForm implements
43  		Ajaxlizable, RowSetForm , Factory{
44  
45  	private Log log = LogFactory.getLog(getClass());
46  
47  	private static final String PREFIX = "row[${index}].";
48  
49  	private static final String ONCHANGE_EVENT = "onChangeType(this);";
50  
51  	private List rows = ListUtils.lazyList(new ArrayList(), (Factory)this);
52  
53  	private boolean changedRowElement = false;
54  
55  	private SharedObjects sharedObjs;
56  
57  	private List events;
58  
59  	// [Start] ----- Ajax Method ----
60  
61  	public final Object[] initializeByAjax() throws Exception {
62  		log.info("[Ajax:init]");
63  		List result = new ArrayList();
64  		try {
65  			Iterator itr = getRowSet().iterator();
66  			for (int i = 0; itr.hasNext(); i++) {
67  				String[] htmls = getHtmlData(i, itr.next());
68  				result.add(htmls);
69  			}
70  		} catch (Exception e) {
71  			e.printStackTrace();
72  			throw e;
73  		}
74  		return result.toArray();
75  	}
76  
77  	public final int removeRowByAjax(int index) {
78  		changedRowElement = true;
79  		log.info("[Ajax:remove]" + index);
80  		removeRow(index);
81  		return index;
82  	}
83  
84  	public final String[] addRowByAjax() throws Exception {
85  		changedRowElement = true;
86  		log.info("[Ajax:addRow]");
87  		String[] data = null;
88  		try {
89  			Object obj = createNewRow();
90  			addRow(obj);
91  			int index = getLastIndex();
92  			data = getHtmlData(index, obj);
93  		} catch (Exception e) {
94  			e.printStackTrace();
95  			throw e;
96  		}
97  		return data;
98  	}
99  
100 	public boolean isSharable() {
101 		return false;
102 	}
103 
104 	public String getSharedKey() {
105 		return null;
106 	}
107 
108 	public void setSharedObject(SharedObjects objs) {
109 		this.sharedObjs = objs;
110 	}
111 
112 	public void addEvent(AjaxEvent event) {
113 		if (events == null) {
114 			events = new ArrayList();
115 		}
116 		events.add(event);
117 	}
118 
119 	protected final List retrieveEvents() {
120 		List list = new ArrayList();
121 		if(events == null){
122 			return list;
123 		}
124 		synchronized (events) {
125 			if (events != null) {
126 				list.addAll(events);
127 				events.clear();
128 			}
129 		}
130 		return list;
131 	}
132 
133 	public final String replaceRowByAjax(int index, String value)
134 			throws Exception {
135 		changedRowElement = true;
136 		log.info("[Ajax:replaceRow]" + index + "," + value);
137 		String ret = null;
138 		try {
139 			Object row = createRow(index, value);
140 			replaceRow(index, row);
141 			String[] html = getHtmlData(index, row);
142 			JSONObject json = createJSONObject(index, html);
143 			ret = json.toString();
144 		} catch (Exception e) {
145 			e.printStackTrace();
146 			throw e;
147 		}
148 		return ret;
149 	}
150 
151 	public final String replaceCellByAjax(int rowIndex, int columnIndex,
152 			String value) {
153 		log.info("[Ajax:replaceCell]" + rowIndex + "," + columnIndex + ","
154 				+ value);
155 		String ret = null;
156 		try {
157 			String cell = getHtmlCell(rowIndex, columnIndex, value);
158 
159 			Map jsonMap = new HashMap();
160 			jsonMap.put("rowIndex", new Integer(rowIndex));
161 			jsonMap.put("columnIndex", new Integer(columnIndex));
162 			// jsonMap.put("data", JSONObject.quote(cell));
163 			jsonMap.put("data", cell);
164 
165 			ret = new JSONObject(jsonMap).toString();
166 
167 		} catch (Exception e) {
168 			e.printStackTrace();
169 			throw new RuntimeException(e);
170 		}
171 		return ret;
172 	}
173 
174 	// [End] ----- Ajax Method ----
175 
176 	public boolean isChangedRowElement() {
177 		return changedRowElement;
178 	}
179 
180 	public void setChangedRowElement(boolean changedRowElement) {
181 		this.changedRowElement = changedRowElement;
182 	}
183 
184 	protected String getMessage(String key) {
185 		return TgwResource.getMessage(key);
186 	}
187 
188 	protected abstract void removeRow(int index, Object row);
189 
190 	protected abstract Object createNewRow();
191 
192 	protected abstract Object createRow(int index, String value);
193 
194 	protected abstract String[] getHtmlData(int index, Object row);
195 
196 	public static final String getPrefix(int index) {
197 		return CharUtil.replace(PREFIX, "index", String.valueOf(index));
198 	}
199 
200 	protected static final void bindTypeEvent(HtmlBuffer buf) {
201 		bindTypeEvent(buf, ONCHANGE_EVENT);
202 	}
203 
204 	protected static final void bindTypeEvent(HtmlBuffer buf, String event) {
205 		buf.appendAttribute("onchange", event);
206 	}
207 
208 	protected final void addRowAll(Collection col) {
209 		rows.addAll(col);
210 	}
211 
212 	protected final synchronized void replaceRow(int index, Object replacement) {
213 		removeRow(index);
214 		rows.add(index, replacement);
215 	}
216 
217 	protected void addRow(Object row) {
218 		rows.add(row);
219 	}
220 
221 	protected Object removeRow(int index) {
222 		Object row = rows.remove(index);
223 		removeRow(index, row);
224 		return row;
225 	}
226 
227 	protected final void sendEvent(AjaxEvent event) {
228 		if (sharedObjs != null) {
229 			sharedObjs.addEvent(event, this);
230 		}
231 	}
232 
233 	// [Start] ----- For Struts Looped Form ----
234 
235 	public final List getRowSet() {
236 		return rows;
237 	}
238 
239 	public final void setRowSet(List columnList) {
240 		this.rows = columnList;
241 	}
242 
243 	public final Object getRow(int index) {
244 		return rows.get(index);
245 	}
246 
247 	// [End] ----- For Struts Looped Form ----
248 
249 	protected final int getLastIndex() {
250 		return rows.size() - 1;
251 	}
252 
253 	protected JSONObject createJSONObject(int index, String[] data) {
254 		Map jsonMap = new HashMap();
255 		jsonMap.put("index", new Integer(index));
256 		jsonMap.put("data", escapeHtml(data));
257 		return new JSONObject(jsonMap);
258 	}
259 
260 	protected static final String createRemoveButtonHtml(int index, String label) {
261 		HtmlBuffer buf = new HtmlBuffer();
262 		buf.appendButton(label, "clickRemove(this);");
263 		return buf.toString();
264 	}
265 
266 	protected static String createTextField(String name) {
267 		return createTextField(name, null);
268 	}
269 
270 	protected static String createTextField(String name, String value) {
271 		return createTextField(name, value, 10);
272 	}
273 	
274 	protected static String createTextField(String name, String value, int length){
275 		HtmlBuffer buf = new HtmlBuffer();
276 		buf.appendTextField(name, value, length);
277 		return buf.toString();		
278 	}
279 
280 	protected static String createTextArea(String name, String value) {
281 		HtmlBuffer buf = new HtmlBuffer();
282 		buf.appendTextArea(name, value);
283 		return buf.toString();
284 	}
285 
286 	private List escapeHtml(String[] html) {
287 		List jsonFieldList = new ArrayList();
288 		if (html == null) {
289 			return null;
290 		}
291 		for (int i = 0; i < html.length; i++) {
292 			String escapeString = JSONObject.quote(html[i]);
293 			jsonFieldList.add(escapeString);
294 		}
295 		return jsonFieldList;
296 	}
297 
298 	protected String getHtmlCell(int rowIndex, int columnIndex, String value) {
299 		return "";
300 	}
301 	
302 	/***
303 	 * create method for commons-collection.Factory.
304 	 */
305 	public Object create() {
306 		return createNewRow();
307 	}
308 }