View Javadoc

1   /***
2    * Copyright (C) 2006 Philipp Mpalampanis
3    *
4    * License: MPL 1.1/GPL 2.0/LGPL 2.1
5    *
6    * The contents of this file are subject to the Mozilla Public License Version
7    * 1.1 (the "License"); you may not use this file except in compliance with
8    * the License. You may obtain a copy of the License at
9    * http://www.mozilla.org/MPL/
10   *
11   * Software distributed under the License is distributed on an "AS IS" basis,
12   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13   * for the specific language governing rights and limitations under the
14   * License.
15   *
16   * Alternatively, the contents of this file may be used under the terms of
17   * either the GNU General Public License Version 2 or later (the "GPL"), or
18   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19   * in which case the provisions of the GPL or the LGPL are applicable instead
20   * of those above. If you wish to allow use of your version of this file only
21   * under the terms of either the GPL or the LGPL, and not to allow others to
22   * use your version of this file under the terms of the MPL, indicate your
23   * decision by deleting the provisions above and replace them with the notice
24   * and other provisions required by the GPL or the LGPL. If you do not delete
25   * the provisions above, a recipient may use your version of this file under
26   * the terms of any one of the MPL, the GPL or the LGPL.
27   */
28  
29  package net.sf.echobinding.datacontrol;
30  
31  import java.util.List;
32  
33  import net.sf.echobinding.binding.BindingContext;
34  import net.sf.echobinding.model.PresentationModel;
35  import net.sf.echobinding.persistence.DefaultPersistenceManager;
36  import net.sf.echobinding.persistence.PersistenceManager;
37  import net.sf.echobinding.table.EditableTable;
38  import nextapp.echo2.app.Column;
39  import nextapp.echo2.app.Component;
40  import nextapp.echo2.app.event.ActionListener;
41  
42  /***
43   * A editable table that allows inline editing. A FormColumnTable
44   * organizes the table data within one <code>Column</code> component. Each
45   * item in the list will be added to the column as a <code>Row</code>. This
46   * row has to be defined by overwriting the abstract method
47   * <code>ceateRow(BindingContext ctx, int rowNummer)</code>.
48   * <p>
49   * If you supply a PersistenceMangager, you can add new rows to the table by
50   * calling the <code>addRow()</code> method. Again, new rows will be added
51   * to the table by the <code>ceateRow(BindingContext ctx, int rowNummer)</code>
52   * method.
53   * <p>
54   * 
55   */
56  public abstract class FormColumnTable<T> extends Column implements
57  		EditableTable<T> {
58  
59  	protected List<T> _list;
60  
61  	protected BindingContext _ctx;
62  
63  	protected ActionListener _actionListener;
64  
65  	private PersistenceManager<T> _persistenceManager;
66  	
67  	
68  	
69  	
70  	/***
71  	 * @param list
72  	 * @param ctx
73  	 */
74  	public FormColumnTable(List<T> list, BindingContext ctx) {
75  		super();
76  		
77  		_list = list;
78  		_ctx = ctx;
79  	}
80  
81  
82  	/* (non-Javadoc)
83  	 * @see nextapp.echo2.app.Component#init()
84  	 */
85  	@Override
86  	public void init() {
87  		super.init();
88  		initialize();
89  	}
90  
91  	/***
92  	 * 
93  	 */
94  	protected void initialize() {
95  		removeAll();
96  		int index = 0;
97  		for (T bean : getList()) {
98  			addRowObject( index++, bean );
99  		}
100 	}
101 
102 	/***
103 	 * 
104 	 * @param index
105 	 * @param bean
106 	 * @return
107 	 */
108 	private int addRowObject(int index, T bean) {
109 		BindingContext childContext = getContext().createChild().setModel(bean);
110 		add(createRow(childContext,	index++));
111 		
112 		initPresentationModel( childContext.getPresentationModel() );
113 		
114 		return index;
115 	}
116 
117 	/***
118 	 * @param presentationModel
119 	 */
120 	private void initPresentationModel(PresentationModel presentationModel) {
121 		if(presentationModel!=null)
122 			presentationModel.init();
123 	}
124 
125 	/***
126 	 * Defines how a table row should look like. 
127 	 * 
128 	 * @param ctx
129 	 * @param rowNumber
130 	 * @return
131 	 */
132 	public abstract Component createRow(BindingContext ctx, int rowNumber);
133 
134 
135 	public void addRow() {
136 		T newObject = createRowObject();
137 		addToTable( newObject );
138 	}
139 
140 	/***
141 	 * @param newObject
142 	 */
143 	private void addToTable(T newObject) {
144 		getList().add(newObject);
145 		int index = getList().indexOf(newObject);
146 		addRowObject(index, newObject);
147 	}
148 
149 	/***
150 	 * @return
151 	 */
152 	private T createRowObject() {
153 		return getPersistenceManager().createNewObject();
154 	}
155 
156 	public void deleteRow(BindingContext context) {
157 		T bean = (T) context.getModel();
158 		int index = getList().indexOf(bean);
159 		getList().remove(bean);
160 		
161 		if(getPersistenceManager().isPersistent(bean))
162 			getPersistenceManager().deleteObject(bean);
163 		
164 		remove(index);
165 	}
166 	
167 	
168 	/* (non-Javadoc)
169 	 * @see echobinding.table.EditableTable#copyRow(echobinding.binding.BindingContext)
170 	 */
171 	public void copyRow(BindingContext context) {
172 		T bean = (T) context.getModel();
173 		T copy = getPersistenceManager().copyObject(bean);
174 		addToTable(copy);		
175 	}
176 
177 	/* (non-Javadoc)
178 	 * @see echobinding.table.EditableTable#saveRow(echobinding.binding.BindingContext)
179 	 */
180 	public void saveRow(BindingContext context) {
181 		T bean = (T) context.getModel();
182 		getPersistenceManager().saveObject(bean);
183 	}
184 	
185 	/* (non-Javadoc)
186 	 * @see echobinding.table.EditableTable#editRow(echobinding.binding.BindingContext)
187 	 */
188 	public void editRow(BindingContext context) {
189 	}
190 
191 	protected ActionListener getActionListener() {
192 		return _actionListener;
193 	}
194 
195 	protected void setActionListener(ActionListener actionListener) {
196 		_actionListener = actionListener;
197 	}
198 	
199 	@SuppressWarnings("unchecked")
200 	public boolean isPersistent(int row) {
201 		T bean = getList().get(row);
202 		return getPersistenceManager().isPersistent(bean);
203 	}
204 	
205 	/***
206 	 * Saves each row object.
207 	 * 
208 	 * @see PersistenceManager
209 	 *
210 	 */
211 	public void save() {
212 		for(T obj : getList()) {
213 			getPersistenceManager().saveObject(obj);
214 		}
215 	}
216 	
217 	/***
218 	 * Returns the persistence manager. If no persistence manager is set, the
219 	 * default persistence manager will be used.
220 	 * 
221 	 * @return Returns the persistence manager.
222 	 */
223 	public PersistenceManager<T> getPersistenceManager() {
224 		if(_persistenceManager == null)
225 			_persistenceManager = new DefaultPersistenceManager<T>(getList().get(0));
226 		return _persistenceManager;
227 	}
228 
229 	/***
230 	 * Sets the persistence manager.
231 	 * 
232 	 * @param persistenceManager The persistence manager to set.
233 	 */
234 	public void setPersistenceManager(PersistenceManager<T> persistenceManager) {
235 		_persistenceManager = persistenceManager;
236 	}
237 
238 	/***
239 	 * Returns the binding context.
240 	 * 
241 	 * @return the binding context
242 	 */
243 	public BindingContext getContext() {
244 		return _ctx;
245 	}
246 	
247 	/***
248 	 * Sets the binding context.
249 	 * 
250 	 * @param ctx The context to set.
251 	 */
252 	public void setContext(BindingContext ctx) {
253 		_ctx = ctx;
254 	}
255 	
256 	/***
257 	 * Returns the list.
258 	 * 
259 	 * @return the list
260 	 */
261 	public List<T> getList() {
262 		return _list;
263 	}
264 	
265 	/***
266 	 * Sets the list.
267 	 * 
268 	 * @param list
269 	 *            the list to set.
270 	 */
271 	public void setList(List<T> list) {
272 		_list = list;
273 	}
274 }