KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > table > EditableTableComponent


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.table;
14
15 import java.net.URL JavaDoc;
16 import java.util.Locale JavaDoc;
17 import java.util.ResourceBundle JavaDoc;
18
19 import javax.servlet.http.HttpSession JavaDoc;
20
21 import org.w3c.dom.Document JavaDoc;
22
23 import com.tonbeller.wcf.component.Component;
24 import com.tonbeller.wcf.component.ComponentSupport;
25 import com.tonbeller.wcf.controller.RequestContext;
26 import com.tonbeller.wcf.controller.RequestListener;
27 import com.tonbeller.wcf.form.FormDocument;
28 import com.tonbeller.wcf.selection.SelectionModel;
29 import com.tonbeller.wcf.utils.ResourceLocator;
30 import com.tonbeller.wcf.utils.SoftException;
31 import com.tonbeller.wcf.utils.XmlUtils;
32
33 /**
34  * a component that combines a table and its property form in a single
35  * component. The user may switch between them via the edit button.
36  *
37  * @author av
38  */

39 public class EditableTableComponent extends ComponentSupport implements ITableComponent {
40   TableComponent tableComp;
41   TablePropertiesFormComponent formComp;
42   String JavaDoc editButtonId;
43   boolean editable = true;
44
45   /**
46    * creates an editable table component.
47    * @param id
48    * @param tableComp
49    * @param formComp - form for editing the table properties
50    */

51   public EditableTableComponent(
52     String JavaDoc id,
53     Component parent,
54     TableComponent tableComp,
55     TablePropertiesFormComponent formComp) {
56     super(id, parent);
57     this.tableComp = tableComp;
58     this.formComp = formComp;
59     tableComp.setParent(this);
60     formComp.setParent(this);
61
62     // this is a little sloppy, because both components
63
// are validated although only one can be visible
64
addFormListener(tableComp);
65     addFormListener(formComp);
66
67     editButtonId = id + ".edit";
68     getDispatcher().addRequestListener(editButtonId, null, editButtonListener);
69   }
70
71   public static EditableTableComponent instance(RequestContext context, String JavaDoc id, TableComponent table) {
72     Locale JavaDoc locale = context.getLocale();
73     ResourceBundle JavaDoc resb = ResourceBundle.getBundle("com.tonbeller.wcf.table.resources", locale);
74     String JavaDoc path = resb.getString("wcf.table.editform");
75     URL JavaDoc url;
76     try {
77       url = ResourceLocator.getResource(context.getServletContext(), locale, path);
78     } catch (Exception JavaDoc e) {
79       throw new SoftException(e);
80     }
81     Document JavaDoc doc = XmlUtils.parse(url);
82     
83     //In replaceI18n(...) wird geprüft, ob "bundle"-Attribut vorhanden
84
FormDocument.replaceI18n(context, doc, null);
85     
86     TablePropertiesFormComponent formComp = new TablePropertiesFormComponent(id + ".form", null, doc, table);
87     formComp.setVisible(false);
88     formComp.setCloseable(true);
89     return new EditableTableComponent(id, null, table, formComp);
90   }
91   
92   RequestListener editButtonListener = new RequestListener() {
93     public void request(RequestContext context) throws Exception JavaDoc {
94       tableComp.validate(context);
95       formComp.setVisible(true);
96     }
97   };
98
99   public void initialize(RequestContext context) throws Exception JavaDoc {
100     super.initialize(context);
101     tableComp.initialize(context);
102     formComp.initialize(context);
103   }
104
105   public void destroy(HttpSession JavaDoc session) throws Exception JavaDoc {
106     formComp.destroy(session);
107     tableComp.destroy(session);
108     super.destroy(session);
109   }
110
111   public Document JavaDoc render(RequestContext context) throws Exception JavaDoc {
112     if (isEditFormVisible())
113       return formComp.render(context);
114
115     Document JavaDoc doc = tableComp.render(context);
116     if (editable)
117     doc.getDocumentElement().setAttribute("editId", editButtonId);
118     return doc;
119   }
120   
121   public boolean isVisible() {
122     return tableComp.isVisible();
123   }
124   
125   public void setVisible(boolean b) {
126     tableComp.setVisible(b);
127   }
128
129   public boolean isEditable() {
130     return editable;
131   }
132
133   public void setEditable(boolean b) {
134     editable = b;
135   }
136
137   public boolean isEditFormVisible() {
138     return formComp.isVisible();
139   }
140   
141     /**
142      * @return
143      */

144     public String JavaDoc getBorder() {
145         return tableComp.getBorder();
146     }
147     /**
148      * @return
149      */

150     public int getCurrentPage() {
151         return tableComp.getCurrentPage();
152     }
153     /**
154      * @return
155      */

156     public TableModel getModel() {
157         return tableComp.getModel();
158     }
159     /**
160      * @return
161      */

162     public int getPageCount() {
163         return tableComp.getPageCount();
164     }
165     /**
166      * @return
167      */

168     public int getPageSize() {
169         return tableComp.getPageSize();
170     }
171     /**
172      * @return
173      */

174     public String JavaDoc getRenderId() {
175         return tableComp.getRenderId();
176     }
177     /**
178      * @return
179      */

180     public RowComparator getRowComparator() {
181         return tableComp.getRowComparator();
182     }
183     /**
184      * @return
185      */

186     public SelectionModel getSelectionModel() {
187         return tableComp.getSelectionModel();
188     }
189     /**
190      * @return
191      */

192     public boolean isClosable() {
193         return tableComp.isClosable();
194     }
195     /**
196      * @return
197      */

198     public boolean isPageable() {
199         return tableComp.isPageable();
200     }
201     /**
202      * @return
203      */

204     public boolean isSortable() {
205         return tableComp.isSortable();
206     }
207     /**
208      * @param border
209      */

210     public void setBorder(String JavaDoc border) {
211         tableComp.setBorder(border);
212     }
213     /**
214      * @param b
215      */

216     public void setClosable(boolean b) {
217         tableComp.setClosable(b);
218     }
219     /**
220      * @param newCurrentPage
221      */

222     public void setCurrentPage(int newCurrentPage) {
223         tableComp.setCurrentPage(newCurrentPage);
224     }
225     /**
226      * @param message
227      */

228     public void setError(String JavaDoc message) {
229         tableComp.setError(message);
230     }
231     /**
232      * @param newModel
233      */

234     public void setModel(TableModel newModel) {
235         tableComp.setModel(newModel);
236         formComp.columnTreeModelChanged();
237     }
238     /**
239      * @param newPageable
240      */

241     public void setPageable(boolean newPageable) {
242         tableComp.setPageable(newPageable);
243     }
244     /**
245      * @param newPageSize
246      */

247     public void setPageSize(int newPageSize) {
248         tableComp.setPageSize(newPageSize);
249     }
250     /**
251      * @param renderId
252      */

253     public void setRenderId(String JavaDoc renderId) {
254         tableComp.setRenderId(renderId);
255     }
256     /**
257      * @param selectionModel
258      */

259     public void setSelectionModel(SelectionModel selectionModel) {
260         tableComp.setSelectionModel(selectionModel);
261     }
262     /**
263      * @param newSortable
264      */

265     public void setSortable(boolean newSortable) {
266         tableComp.setSortable(newSortable);
267     }
268     /**
269      * @param index
270      */

271     public void setSortColumnIndex(int index) {
272         tableComp.setSortColumnIndex(index);
273     }
274   /**
275    * @return Returns the tableComp.
276    */

277   public TableComponent getTableComp() {
278     return tableComp;
279   }
280
281   public boolean isReadOnly() {
282     return tableComp.isReadOnly();
283   }
284
285   public void setReadOnly(boolean readOnly) {
286     tableComp.setReadOnly(readOnly);
287   }
288 }
289
Popular Tags