View Javadoc
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 java.util.ArrayList; 53 import java.util.List; 54 55 import javax.swing.AbstractListModel; 56 import javax.swing.ListModel; 57 import javax.swing.event.ListDataEvent; 58 import javax.swing.event.ListDataListener; 59 60 import org.apache.commons.jelly.JellyContext; 61 import org.apache.commons.jelly.JellyTagException; 62 import org.apache.commons.jelly.MissingAttributeException; 63 import org.apache.commons.jelly.TagSupport; 64 import org.apache.commons.jelly.XMLOutput; 65 import org.apache.commons.logging.LogFactory; 66 import org.w3c.dom.Node; 67 import org.w3c.dom.events.Event; 68 69 import net.sourceforge.xwing.AggregatingListModel; 70 import net.sourceforge.xwing.DiffingListModel; 71 import net.sourceforge.xwing.dom.NodeListener; 72 73 public class NodeSetTag extends TagSupport implements ListModel { 74 75 class NullListModel extends AbstractListModel { 76 public int getSize() { 77 return 0; 78 } 79 public Object getElementAt(int index) { 80 return null; 81 } 82 83 public void fireContentsChanged( 84 Object source, 85 int index0, 86 int index1) { 87 super.fireContentsChanged(source, index0, index1); 88 } 89 90 public void fireIntervalAdded(Object source, int index0, int index1) { 91 super.fireIntervalAdded(source, index0, index1); 92 } 93 94 public void fireIntervalRemoved( 95 Object source, 96 int index0, 97 int index1) { 98 super.fireIntervalRemoved(source, index0, index1); 99 } 100 }; 101 102 private String nodeSetStr; 103 private String nodeStr; 104 private Node tmpNode; 105 private NullListModel nlm; 106 107 private NodeListener listener; 108 109 private String var; 110 private String nodeVar; 111 private boolean active; 112 113 private DiffingListModel target; 114 115 public NodeSetTag(String nodeStr, String nodeSetStr) { 116 this.nodeStr = nodeStr; 117 this.nodeSetStr = nodeSetStr; 118 119 nlm = new NullListModel(); 120 121 target = new DiffingListModel(); 122 123 target.addListDataListener(new ListDataListener() { 124 public void contentsChanged(ListDataEvent e) { 125 nlm.fireContentsChanged( 126 NodeSetTag.this, 127 e.getIndex0(), 128 e.getIndex1()); 129 } 130 131 public void intervalAdded(ListDataEvent e) { 132 nlm.fireIntervalAdded( 133 NodeSetTag.this, 134 e.getIndex0(), 135 e.getIndex1()); 136 } 137 138 public void intervalRemoved(ListDataEvent e) { 139 nlm.fireIntervalRemoved( 140 NodeSetTag.this, 141 e.getIndex0(), 142 e.getIndex1()); 143 } 144 }); 145 active = true; 146 } 147 148 public void setNode(Node node) throws Exception { 149 if (listener != null) { 150 listener.setNode(node); 151 } else { 152 tmpNode = node; 153 } 154 } 155 156 public void setNodeVar(String nodeVar) { 157 this.nodeVar = nodeVar; 158 } 159 160 public void setVar(String var) { 161 this.var = var; 162 } 163 164 public void setActive(boolean active) { 165 this.active = active; 166 } 167 168 public void doTag(XMLOutput output) 169 throws MissingAttributeException, JellyTagException { 170 171 try { 172 173 if (tmpNode == null) { 174 tmpNode = (Node) context.getVariable(nodeStr); 175 } 176 177 if (active) { 178 listener = new NodeListener(tmpNode) { 179 protected void nodeChanged(Event evt) { 180 try { 181 runScript(); 182 } catch (JellyTagException exc) { 183 LogFactory.getLog(getClass()).error( 184 "Error in nodeChanged", 185 exc); 186 } 187 } 188 }; 189 } 190 191 AggregatingListModel nodes = 192 (AggregatingListModel) context.getVariable(nodeSetStr); 193 194 if (nodes == null) { 195 throw new JellyTagException( 196 "tag must be used " + "in the context of a table."); 197 } 198 199 nodes.addListModel(this); 200 201 if (var != null) { 202 context.setVariable(var, this); 203 } 204 205 runScript(); 206 207 } catch (JellyTagException exc) { 208 throw exc; 209 } catch (Exception exc) { 210 throw new JellyTagException(exc); 211 } 212 } 213 214 public void runScript() throws JellyTagException { 215 List results = null; 216 217 Node node = listener != null ? listener.getNode() : tmpNode; 218 219 AggregatingListModel rows = new AggregatingListModel(); 220 JellyContext child = context.newJellyContext(); 221 222 child.setVariable(nodeStr, node); 223 child.setVariable(nodeSetStr, rows); 224 225 if (nodeVar != null) { 226 child.setVariable(nodeVar, node); 227 } 228 229 getBody().run(child, XMLOutput.createDummyXMLOutput()); 230 231 int size = rows.getSize(); 232 233 results = new ArrayList(size); 234 for (int i = 0; i < size; ++i) { 235 results.add(rows.getElementAt(i)); 236 } 237 238 target.setList(results); 239 } 240 241 public int getSize() { 242 return target.getSize(); 243 } 244 245 public Object getElementAt(int index) { 246 return target.getElementAt(index); 247 } 248 249 public void addListDataListener(ListDataListener l) { 250 nlm.addListDataListener(l); 251 } 252 253 public void removeListDataListener(ListDataListener l) { 254 nlm.removeListDataListener(l); 255 } 256 }

This page was automatically generated by Maven