1 /*
2 * Copyright (c) 2003 Scott Howlett & Paul Libbrecht.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. The end-user documentation included with the redistribution,
18 * if any, must include the following acknowledgment:
19 * "This product includes software developed by the Xwing
20 * Project ( http://xwing.sourceforge.net/ )."
21 * Alternately, this acknowledgment may appear in the software itself,
22 * if and wherever such third-party acknowledgments normally appear.
23 *
24 * 4. The name "Xwing" must not be used to endorse or promote products
25 * derived from this software without prior written permission. For
26 * written permission, please contact the project authors via
27 * the Xwing project site, http://xwing.sourceforge.net/ .
28 *
29 * 5. Products derived from this software may not be called "Xwing",
30 * nor may "Xwing" appear in their name, without prior written
31 * permission of the Xwing project.
32 *
33 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 * DISCLAIMED. IN NO EVENT SHALL THE XWING AUTHORS OR THE PROJECT
37 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 *
46 * For more information on Xwing, please see
47 * < http://xwing.sourceforge.net/ >.
48 */
49
50 package net.sourceforge.xwing.jelly.tags.xwing;
51
52 import javax.swing.DefaultListModel;
53 import javax.swing.ListModel;
54
55 import net.sourceforge.xwing.AggregatingListModel;
56 import net.sourceforge.xwing.BooleanToTextAdapter;
57 import net.sourceforge.xwing.SourceSink;
58 import net.sourceforge.xwing.SourceSinkFactory;
59 import net.sourceforge.xwing.dom.NodeListModel;
60 import net.sourceforge.xwing.dom.NodeSourceSink;
61 import net.sourceforge.xwing.dom.NodeTextListModel;
62 import net.sourceforge.xwing.dom.NodeTextSourceSink;
63 import net.sourceforge.xwing.swing.XTableColumn;
64
65 import org.apache.commons.jelly.JellyTagException;
66 import org.apache.commons.jelly.MissingAttributeException;
67 import org.apache.commons.jelly.TagSupport;
68 import org.apache.commons.jelly.XMLOutput;
69 import org.apache.commons.lang.StringUtils;
70 import org.jaxen.dom.DOMXPath;
71 import org.w3c.dom.Node;
72
73 public class ColTag extends TagSupport {
74
75 private String label;
76 private Node labelNode;
77 private String labelSelect;
78 private String valueSelect;
79 private String rowVar;
80 private String labelNodeVar;
81 private boolean editable;
82 private String cellClassname;
83
84 // If combobox
85
86 private String choices;
87 private Node choicesNode;
88 private String choicesPath;
89 private boolean comboEditable;
90
91 // If checkbox
92
93 private String selectedValue;
94 private String unselectedValue;
95
96 public void setLabel(String string) {
97 label = string;
98 }
99
100 public void setValueSelect(String string) {
101 valueSelect = string;
102 }
103
104 public void setRowVar(String string) {
105 rowVar = string;
106 }
107
108 public void setLabelNodeVar(String string) {
109 labelNodeVar = string;
110 }
111
112 public void setLabelNode(Node node) {
113 labelNode = node;
114 }
115
116 public void setLabelSelect(String string) {
117 labelSelect = string;
118 }
119
120 public void setEditable(boolean b) {
121 editable = b;
122 }
123
124 public void setChoices(String string) {
125 choices = string;
126 }
127
128 public void setChoicesNode(Node node) {
129 choicesNode = node;
130 }
131
132 public void setChoicesPath(String string) {
133 choicesPath = string;
134 }
135
136 public void setComboEditable(boolean comboEditable) {
137 this.comboEditable = comboEditable;
138 }
139
140 public void setSelectedValue(String string) {
141 selectedValue = string;
142 }
143
144 public void setUnselectedValue(String string) {
145 unselectedValue = string;
146 }
147
148 public void setCellClassname(String string) {
149 cellClassname = string;
150 }
151
152 public void doTag(XMLOutput output)
153 throws MissingAttributeException, JellyTagException {
154
155 if ((label != null) == (labelNode != null && labelSelect != null)) {
156 throw new JellyTagException(
157 "col tag must have either "
158 + "label or labelNode & labelSelect");
159 }
160
161 try {
162
163 AggregatingListModel cols =
164 (AggregatingListModel) context.getVariable("TableTag.Columns");
165
166 if (cols == null) {
167 throw new JellyTagException(
168 "col tag must be used " + "in the context of a table.");
169 }
170
171 final DOMXPath vSel =
172 valueSelect == null ? null : new DOMXPath(valueSelect);
173
174 final DOMXPath lSel =
175 labelSelect == null ? null : new DOMXPath(labelSelect);
176
177 SourceSink labelSS = null;
178
179 if (labelNode != null) {
180 labelSS =
181 new NodeTextSourceSink(new NodeSourceSink(labelNode, lSel));
182 }
183
184 SourceSinkFactory valueFactory = null;
185 ListModel choicesLM = null;
186
187 if (selectedValue != null && unselectedValue != null) {
188 valueFactory = new SourceSinkFactory() {
189
190 public SourceSink create(Object value) throws Exception {
191 return new BooleanToTextAdapter(
192 new NodeTextSourceSink(
193 new NodeSourceSink((Node) value, vSel)),
194 selectedValue,
195 unselectedValue);
196 }
197
198 public Class getValueClass() {
199 return Boolean.class;
200 }
201
202 };
203 } else {
204 valueFactory = new SourceSinkFactory() {
205
206 public SourceSink create(Object value) throws Exception {
207 return new NodeTextSourceSink(
208 new NodeSourceSink((Node) value, vSel));
209 }
210
211 public Class getValueClass() {
212 return String.class;
213 }
214 };
215
216 if (choices != null) {
217 DefaultListModel dlm = new DefaultListModel();
218 final String[] choicesArr = StringUtils.split(choices, ",");
219 for (int i = 0; i < choicesArr.length; ++i) {
220 dlm.addElement(choicesArr[i].trim());
221 }
222 choicesLM = dlm;
223
224 } else if (choicesNode != null && choicesPath != null) {
225 choicesLM =
226 new NodeTextListModel(
227 new NodeListModel(
228 choicesNode,
229 new DOMXPath(choicesPath)));
230 }
231
232 }
233
234 XTableColumn col =
235 labelSS != null
236 ? new XTableColumn(
237 labelSS,
238 valueFactory,
239 editable,
240 choicesLM,
241 !comboEditable)
242 : new XTableColumn(
243 label,
244 valueFactory,
245 editable,
246 choicesLM,
247 !comboEditable);
248
249 cols.addElement(col);
250
251 } catch (Exception exc) {
252 throw new JellyTagException(exc);
253 }
254 }
255
256 }
This page was automatically generated by Maven