KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashSet JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.apache.log4j.Logger;
19 import org.jaxen.JaxenException;
20 import org.jaxen.dom.DOMXPath;
21 import org.w3c.dom.Document JavaDoc;
22 import org.w3c.dom.Element JavaDoc;
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 /**
38  * A form that allows the user to change the properties of a table component.
39  *
40  * @author av
41  */

42 public class TablePropertiesFormComponent extends FormComponent {
43
44   private Element JavaDoc closeElement;
45   private TableComponent tableComp;
46   private Element JavaDoc 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 JavaDoc id, Component parent, Document JavaDoc 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 JavaDoc) dx.selectSingleNode(getDocument());
62       dx = new DOMXPath("./imgButton");
63       closeElement = (Element JavaDoc) dx.selectSingleNode(titleElement);
64     } catch (JaxenException e) {
65       logger.error("?", e);
66     }
67   }
68
69   public void initialize(RequestContext context) throws Exception JavaDoc {
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 JavaDoc te = (Element JavaDoc) 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 JavaDoc 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   /**
93    * called when the user presses OK
94    */

95   public void onApply(RequestContext context) throws Exception JavaDoc {
96     if (isCloseable())
97       setVisible(false);
98   }
99
100   /**
101    * called when the user presses Cancel
102    */

103   public void onCancel(RequestContext context) throws Exception JavaDoc {
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 JavaDoc[] getRoots() {
114       return tableComp.getTableColumns();
115     }
116     public boolean hasChildren(Object JavaDoc node) {
117       return false;
118     }
119     public Object JavaDoc[] getChildren(Object JavaDoc node) {
120       return null;
121     }
122     public Object JavaDoc getParent(Object JavaDoc node) {
123       return null;
124     }
125   }
126
127   /**
128    * renders a tree node (i.e. a TableColumn)
129    */

130   private class TableColumnNodeRenderer implements NodeRenderer {
131     public Element JavaDoc renderNode(RequestContext context, Document JavaDoc factory, Object JavaDoc node) {
132       int colIndex = ((TableColumn) node).getColumnIndex();
133       String JavaDoc label = tableComp.getModel().getColumnTitle(colIndex);
134       Element JavaDoc 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 JavaDoc getSelection() {
146       Set JavaDoc set = new HashSet JavaDoc();
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 JavaDoc obj) {
156       ((TableColumn) obj).setHidden(false);
157     }
158
159     public void remove(Object JavaDoc 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   /**
171    * used by xml form
172    */

173   public TableComponent getTable() {
174     return tableComp;
175   }
176
177   public String JavaDoc getTitle() {
178     return titleElement.getAttribute("value");
179   }
180
181   public void setTitle(String JavaDoc title) {
182     titleElement.setAttribute("value", title);
183   }
184   /**
185    * @return
186    */

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