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.DefaultCellEditor;
53 import javax.swing.JComboBox;
54 import javax.swing.ListModel;
55 import javax.swing.event.ChangeEvent;
56 import javax.swing.event.ListSelectionEvent;
57 import javax.swing.event.TableColumnModelEvent;
58 import javax.swing.event.TableColumnModelListener;
59 import javax.swing.table.TableColumn;
60 import javax.swing.table.TableColumnModel;
61
62 import org.apache.commons.jelly.JellyTagException;
63 import org.apache.commons.jelly.XMLOutput;
64 import org.apache.commons.jelly.tags.swing.ComponentTag;
65 import org.apache.commons.jelly.tags.swing.Factory;
66 import org.apache.commons.logging.LogFactory;
67 import org.w3c.dom.Node;
68
69 import net.sourceforge.xwing.AggregatingListModel;
70 import net.sourceforge.xwing.swing.XComboBoxModel;
71 import net.sourceforge.xwing.swing.XTable;
72 import net.sourceforge.xwing.swing.XTableColumn;
73 import net.sourceforge.xwing.swing.XTableModel;
74
75 public class TableTag extends ComponentTag {
76
77 private AggregatingListModel rows;
78 private AggregatingListModel columns;
79
80 private Node node;
81
82 public TableTag() {
83 super(new Factory() {
84 public Object newInstance() throws InstantiationException {
85 try {
86 return new XTable();
87 } catch (Exception exc) {
88 throw new InstantiationException(exc.getMessage());
89 }
90 }
91 });
92
93 rows = new AggregatingListModel();
94 columns = new AggregatingListModel();
95 }
96
97 public void doTag(XMLOutput output) throws JellyTagException {
98 node = (Node) getAttributes().remove("node");
99
100 context.setVariable("TableTag.Node", node);
101 context.setVariable("TableTag.Rows", rows);
102 context.setVariable("TableTag.Columns", columns);
103
104 super.doTag(output);
105
106 context.removeVariable("TableTag.Node");
107 context.removeVariable("TableTag.Rows");
108 context.removeVariable("TableTag.Columns");
109 }
110
111 public XTable getTable() {
112 return (XTable) getBean();
113 }
114
115 protected void processBean(String var, Object bean)
116 throws JellyTagException {
117
118 try {
119 super.processBean(var, bean);
120
121 XTable table = (XTable) bean;
122 XTableModel model = new XTableModel(rows, columns);
123
124 table.getColumnModel().addColumnModelListener(
125 new XTableColumnModelListener(model));
126 table.setModel(model);
127 } catch (Exception exc) {
128 throw new JellyTagException(exc);
129 }
130
131 }
132
133 }
134
135 class XTableColumnModelListener implements TableColumnModelListener {
136
137 private XTableModel model;
138
139 public XTableColumnModelListener(XTableModel model) {
140 this.model = model;
141 }
142 public void columnMarginChanged(ChangeEvent e) {
143 }
144
145 public void columnSelectionChanged(ListSelectionEvent e) {
146 }
147
148 public void columnAdded(TableColumnModelEvent e) {
149 TableColumnModel tcm = (TableColumnModel) e.getSource();
150 TableColumn tc = tcm.getColumn(e.getToIndex());
151 XTableColumn col = model.getColumn(e.getToIndex());
152 ListModel choices = col.getChoices();
153 if (choices != null) {
154 try {
155 JComboBox cb = new JComboBox(new XComboBoxModel(null, choices));
156 cb.setEditable(!col.isConstrainedToChoices());
157 tc.setCellEditor(new DefaultCellEditor(cb));
158 } catch (Exception exc) {
159 LogFactory.getLog(getClass()).error(
160 "Error in columnAdded",
161 exc);
162 }
163 }
164 }
165
166 public void columnMoved(TableColumnModelEvent e) {
167 }
168
169 public void columnRemoved(TableColumnModelEvent e) {
170 }
171 }
This page was automatically generated by Maven