1 13 package com.tonbeller.wcf.table; 14 15 import java.net.URL ; 16 import java.util.Locale ; 17 import java.util.ResourceBundle ; 18 19 import javax.servlet.http.HttpSession ; 20 21 import org.w3c.dom.Document ; 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 39 public class EditableTableComponent extends ComponentSupport implements ITableComponent { 40 TableComponent tableComp; 41 TablePropertiesFormComponent formComp; 42 String editButtonId; 43 boolean editable = true; 44 45 51 public EditableTableComponent( 52 String 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 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 id, TableComponent table) { 72 Locale locale = context.getLocale(); 73 ResourceBundle resb = ResourceBundle.getBundle("com.tonbeller.wcf.table.resources", locale); 74 String path = resb.getString("wcf.table.editform"); 75 URL url; 76 try { 77 url = ResourceLocator.getResource(context.getServletContext(), locale, path); 78 } catch (Exception e) { 79 throw new SoftException(e); 80 } 81 Document doc = XmlUtils.parse(url); 82 83 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 { 94 tableComp.validate(context); 95 formComp.setVisible(true); 96 } 97 }; 98 99 public void initialize(RequestContext context) throws Exception { 100 super.initialize(context); 101 tableComp.initialize(context); 102 formComp.initialize(context); 103 } 104 105 public void destroy(HttpSession session) throws Exception { 106 formComp.destroy(session); 107 tableComp.destroy(session); 108 super.destroy(session); 109 } 110 111 public Document render(RequestContext context) throws Exception { 112 if (isEditFormVisible()) 113 return formComp.render(context); 114 115 Document 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 144 public String getBorder() { 145 return tableComp.getBorder(); 146 } 147 150 public int getCurrentPage() { 151 return tableComp.getCurrentPage(); 152 } 153 156 public TableModel getModel() { 157 return tableComp.getModel(); 158 } 159 162 public int getPageCount() { 163 return tableComp.getPageCount(); 164 } 165 168 public int getPageSize() { 169 return tableComp.getPageSize(); 170 } 171 174 public String getRenderId() { 175 return tableComp.getRenderId(); 176 } 177 180 public RowComparator getRowComparator() { 181 return tableComp.getRowComparator(); 182 } 183 186 public SelectionModel getSelectionModel() { 187 return tableComp.getSelectionModel(); 188 } 189 192 public boolean isClosable() { 193 return tableComp.isClosable(); 194 } 195 198 public boolean isPageable() { 199 return tableComp.isPageable(); 200 } 201 204 public boolean isSortable() { 205 return tableComp.isSortable(); 206 } 207 210 public void setBorder(String border) { 211 tableComp.setBorder(border); 212 } 213 216 public void setClosable(boolean b) { 217 tableComp.setClosable(b); 218 } 219 222 public void setCurrentPage(int newCurrentPage) { 223 tableComp.setCurrentPage(newCurrentPage); 224 } 225 228 public void setError(String message) { 229 tableComp.setError(message); 230 } 231 234 public void setModel(TableModel newModel) { 235 tableComp.setModel(newModel); 236 formComp.columnTreeModelChanged(); 237 } 238 241 public void setPageable(boolean newPageable) { 242 tableComp.setPageable(newPageable); 243 } 244 247 public void setPageSize(int newPageSize) { 248 tableComp.setPageSize(newPageSize); 249 } 250 253 public void setRenderId(String renderId) { 254 tableComp.setRenderId(renderId); 255 } 256 259 public void setSelectionModel(SelectionModel selectionModel) { 260 tableComp.setSelectionModel(selectionModel); 261 } 262 265 public void setSortable(boolean newSortable) { 266 tableComp.setSortable(newSortable); 267 } 268 271 public void setSortColumnIndex(int index) { 272 tableComp.setSortColumnIndex(index); 273 } 274 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 |