1 13 package com.tonbeller.wcf.table; 14 15 import java.util.HashSet ; 16 import java.util.Set ; 17 18 import org.apache.log4j.Logger; 19 import org.jaxen.JaxenException; 20 import org.jaxen.dom.DOMXPath; 21 import org.w3c.dom.Document ; 22 import org.w3c.dom.Element ; 23 24 import com.tonbeller.tbutils.res.Resources; 25 import com.tonbeller.wcf.component.Component; 26 import com.tonbeller.wcf.controller.RequestContext; 27 import com.tonbeller.wcf.form.FormComponent; 28 import com.tonbeller.wcf.selection.AbstractSelectionModel; 29 import com.tonbeller.wcf.tree.AbstractTreeModel; 30 import com.tonbeller.wcf.tree.MutableTreeModelDecorator; 31 import com.tonbeller.wcf.tree.NodeRenderer; 32 import com.tonbeller.wcf.tree.TreeComponent; 33 import com.tonbeller.wcf.tree.TreeHandler; 34 import com.tonbeller.wcf.tree.TreeModel; 35 import com.tonbeller.wcf.utils.DomUtils; 36 37 42 public class TablePropertiesFormComponent extends FormComponent { 43 44 private Element closeElement; 45 private TableComponent tableComp; 46 private Element titleElement; 47 private Resources resources; 48 private TreeModel mutableTreeModel; 49 private TreeModel columnTreeModel; 50 private TreeComponent treeComp; 51 52 private static Logger logger = Logger.getLogger(TablePropertiesFormComponent.class); 53 54 public TablePropertiesFormComponent(String id, Component parent, Document document, TableComponent table) { 55 super(id, parent, document); 56 this.tableComp = table; 57 columnTreeModel = new TableColumnTreeModel(); 58 mutableTreeModel = new MutableTreeModelDecorator(columnTreeModel); 59 try { 60 DOMXPath dx = new DOMXPath("//title"); 61 titleElement = (Element ) dx.selectSingleNode(getDocument()); 62 dx = new DOMXPath("./imgButton"); 63 closeElement = (Element ) dx.selectSingleNode(titleElement); 64 } catch (JaxenException e) { 65 logger.error("?", e); 66 } 67 } 68 69 public void initialize(RequestContext context) throws Exception { 70 super.initialize(context); 71 this.resources = context.getResources(TablePropertiesFormComponent.class); 72 73 try { 74 DOMXPath dx = new DOMXPath("//skip[@id='" + getId() + ".tree']"); 75 Element te = (Element ) dx.selectSingleNode(getDocument()); 76 TreeHandler th = (TreeHandler) getHandler(te); 77 treeComp = th.getTree(); 78 treeComp.setNodeRenderer(new TableColumnNodeRenderer()); 79 treeComp.setSelectionModel(new TableColumnSelectionModel()); 80 } catch (JaxenException e) { 81 logger.error(null, e); 82 } 83 84 String title = tableComp.getModel().getTitle(); 85 if (title != null) 86 title = resources.getString("wcf.table.props.title.args", title); 87 else 88 title = resources.getString("wcf.table.props.title.noargs"); 89 setTitle(title); 90 } 91 92 95 public void onApply(RequestContext context) throws Exception { 96 if (isCloseable()) 97 setVisible(false); 98 } 99 100 103 public void onCancel(RequestContext context) throws Exception { 104 if (isCloseable()) 105 setVisible(false); 106 } 107 108 public TreeModel getColumnTreeModel() { 109 return mutableTreeModel; 110 } 111 112 private class TableColumnTreeModel extends AbstractTreeModel { 113 public Object [] getRoots() { 114 return tableComp.getTableColumns(); 115 } 116 public boolean hasChildren(Object node) { 117 return false; 118 } 119 public Object [] getChildren(Object node) { 120 return null; 121 } 122 public Object getParent(Object node) { 123 return null; 124 } 125 } 126 127 130 private class TableColumnNodeRenderer implements NodeRenderer { 131 public Element renderNode(RequestContext context, Document factory, Object node) { 132 int colIndex = ((TableColumn) node).getColumnIndex(); 133 String label = tableComp.getModel().getColumnTitle(colIndex); 134 Element nodeElem = factory.createElement(DEFAULT_NODE_ELEMENT_NAME); 135 nodeElem.setAttribute("label", label); 136 return nodeElem; 137 } 138 } 139 140 private class TableColumnSelectionModel extends AbstractSelectionModel { 141 public TableColumnSelectionModel() { 142 super(MULTIPLE_SELECTION); 143 } 144 145 public Set getSelection() { 146 Set set = new HashSet (); 147 TableColumn[] cols = tableComp.getTableColumns(); 148 for (int i = 0; i < cols.length; i++) { 149 if (!cols[i].isHidden()) 150 set.add(cols[i]); 151 } 152 return set; 153 } 154 155 public void add(Object obj) { 156 ((TableColumn) obj).setHidden(false); 157 } 158 159 public void remove(Object obj) { 160 ((TableColumn) obj).setHidden(true); 161 } 162 163 public void clear() { 164 TableColumn[] cols = tableComp.getTableColumns(); 165 for (int i = 0; i < cols.length; i++) 166 cols[i].setHidden(true); 167 } 168 } 169 170 173 public TableComponent getTable() { 174 return tableComp; 175 } 176 177 public String getTitle() { 178 return titleElement.getAttribute("value"); 179 } 180 181 public void setTitle(String title) { 182 titleElement.setAttribute("value", title); 183 } 184 187 public boolean isCloseable() { 188 return !closeElement.hasAttribute("hidden"); 189 } 190 191 public void setCloseable(boolean b) { 192 if (b) 193 DomUtils.removeAttribute(closeElement, "hidden"); 194 else 195 closeElement.setAttribute("hidden", "true"); 196 } 197 198 void columnTreeModelChanged() { 199 columnTreeModel.fireModelChanged(true); 200 } 201 } 202 | Popular Tags |