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 package net.sf.echobinding.table;
29
30 import net.sf.echobinding.binding.BindingContext;
31 import net.sf.echobinding.persistence.DefaultTablePersistenceManager;
32 import net.sf.echobinding.persistence.PersistenceManager;
33 import net.sf.echobinding.util.Images;
34 import nextapp.echo2.app.*;
35 import nextapp.echo2.app.event.ActionEvent;
36 import nextapp.echo2.app.event.ActionListener;
37 import nextapp.echo2.app.table.TableCellRenderer;
38 import nextapp.echo2.app.table.TableModel;
39
40 /***
41 * A editable bound table.
42 *
43 */
44 public abstract class ListEditor<T> extends BoundTable<T> implements EditableTable<T>, EditableTableRendererSupport, ActionListener {
45
46 private PersistenceManager<T> _persistenceManager;
47
48 /***
49 * @param listAdapterId
50 * @param ctx
51 */
52 public ListEditor(String listAdapterId, BindingContext ctx) {
53 super( listAdapterId, ctx );
54
55 }
56
57
58
59
60 @Override
61 protected TableModel createTableModel() {
62 return new EditableTableModel(createTableHeaderData(), createTableData());
63 }
64
65
66
67
68
69 @Override
70 protected TableCellRenderer getTableCellRenderer() {
71 return new EditableTableCellRenderer();
72 }
73
74
75 @Override
76 protected BoundTableHeaderRenderer getTableHeaderRenderer() {
77 return new EditableTableHeaderRenderer();
78 }
79
80 @SuppressWarnings("serial")
81 public Component createHeaderButtonPanel() {
82 Row buttonRow = new Row();
83
84 Button addButton = new Button(Images.PAGE_ADD);
85 addButton.addActionListener(new ActionListener() {
86
87 public void actionPerformed(ActionEvent arg0) {
88 addRow();
89 }
90
91 });
92 buttonRow.add(addButton);
93
94 return buttonRow;
95 }
96
97
98 @SuppressWarnings("serial")
99 public Component createButtonPanel(int rowIndex) {
100
101 Row buttonRow = new Row();
102
103 final BindingContext context = super.getBindingContext(rowIndex);
104
105 Button editButton = new Button(Images.PAGE_EDIT);
106 editButton.addActionListener(new ActionListener() {
107 public void actionPerformed(ActionEvent arg0) {
108 editRow( context );
109 }
110 });
111
112 Button copyButton = new Button(Images.PAGE_COPY);
113 copyButton.addActionListener(new ActionListener() {
114
115 public void actionPerformed(ActionEvent arg0) {
116 copyRow( context );
117 }
118
119 });
120
121 Button deleteButton = new Button(Images.PAGE_DELETE);
122 deleteButton.addActionListener(new ActionListener() {
123
124 public void actionPerformed(ActionEvent arg0) {
125 deleteRow( context );
126 }
127
128 } );
129
130 buttonRow.add(editButton);
131 buttonRow.add(copyButton);
132 buttonRow.add(deleteButton);
133
134 buttonRow.setCellSpacing(new Extent(3));
135
136 return buttonRow;
137 }
138
139
140
141
142
143 public void editRow(BindingContext context) {
144 showForm(context);
145 }
146
147 /***
148 * Displays the form for editing a selected row.
149 *
150 * @param context
151 */
152 public abstract void showForm(BindingContext context);
153
154 @SuppressWarnings("unchecked")
155 public boolean isPersistent(int row) {
156
157 T bean = (T) getBindingContext(row).getModel();
158
159 return getPersistenceManager().isPersistent(bean);
160 }
161
162
163
164
165 public void addRow() {
166 T newRowObject = createNewRowObject();
167 getList().add(newRowObject);
168
169 initialize();
170
171 int index = getList().indexOf(newRowObject);
172 editRow(getBindingContext(index));
173 }
174
175 /***
176 * @return
177 */
178 @SuppressWarnings("unchecked")
179 private T createNewRowObject() {
180 return getPersistenceManager().createNewObject();
181 }
182
183
184
185
186 public void deleteRow(BindingContext context) {
187 T currentRowObject = (T) context.getModel();
188 getList().remove(currentRowObject);
189
190 deleteRowObject(currentRowObject);
191
192 initialize();
193 }
194
195 /***
196 * @param object
197 */
198 private void deleteRowObject(T object) {
199 if( getPersistenceManager().isPersistent(object))
200 getPersistenceManager().deleteObject(object);
201 }
202
203
204
205
206 public void copyRow(BindingContext context) {
207
208 T currentRowObject = (T) context.getModel();
209 T copy = copyRowObject(currentRowObject);
210
211 int currentIndex = getList().indexOf(currentRowObject);
212
213 int newIndex = currentIndex+1;
214 getList().add(newIndex, copy);
215
216 initialize();
217
218 newIndex = getList().indexOf(copy);
219 editRow(getBindingContext(newIndex));
220 }
221
222 /***
223 * @param currentRowObject
224 * @return
225 */
226 private T copyRowObject(T object) {
227 return getPersistenceManager().copyObject(object);
228 }
229
230
231
232 public void saveRow(BindingContext context) {
233
234 context.synchronize();
235
236 T bean = (T) context.getModel();
237
238 getPersistenceManager().saveObject(bean);
239
240 initialize();
241 }
242
243 /***
244 * Returns the persistence manager. If no persistence manager is set, the
245 * default persistence manager will be used.
246 *
247 * @return Returns the persistence manager.
248 */
249 public PersistenceManager<T> getPersistenceManager() {
250 if(_persistenceManager == null)
251 _persistenceManager = new DefaultTablePersistenceManager<T>(getList().get(0), this);
252 return _persistenceManager;
253 }
254
255 /***
256 * Sets the persistence manager.
257 *
258 * @param persistenceManager The persistence manager to set.
259 */
260 public void setPersistenceManager(PersistenceManager<T> persistenceManager) {
261 _persistenceManager = persistenceManager;
262 }
263
264 }