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.dom;
51
52 import javax.swing.AbstractListModel;
53 import javax.swing.event.ListDataEvent;
54 import javax.swing.event.ListDataListener;
55
56 import net.sourceforge.xwing.DiffingListModel;
57
58 import org.apache.commons.logging.LogFactory;
59 import org.jaxen.JaxenException;
60 import org.jaxen.dom.DOMXPath;
61 import org.w3c.dom.Node;
62 import org.w3c.dom.events.Event;
63
64 /***
65 * Represents a read-only connection to a list of Nodes that is interested
66 * in the text content of each node. Only the first child of each node is
67 * considered. The connection is specified via a Node and an DOMXPath expression
68 * that selects a list of nodes in the context of that Node.
69 */
70
71 public class NodeListModel extends AbstractListModel {
72
73 private NodeListener listener;
74 private DOMXPath select;
75 private DiffingListModel cache;
76
77 public NodeListModel(Node node, DOMXPath select) throws Exception {
78 listener = new NodeListener(node) {
79 protected void nodeChanged(Event evt) {
80 stateChanged();
81 }
82 };
83
84 this.select = select;
85 cache = new DiffingListModel(select.selectNodes(node));
86 cache.addListDataListener(new ListDataListener() {
87 public void contentsChanged(ListDataEvent e) {
88 fireContentsChanged(
89 NodeListModel.this,
90 e.getIndex0(),
91 e.getIndex1());
92 }
93
94 public void intervalAdded(ListDataEvent e) {
95 fireIntervalAdded(
96 NodeListModel.this,
97 e.getIndex0(),
98 e.getIndex1());
99 }
100
101 public void intervalRemoved(ListDataEvent e) {
102 fireIntervalRemoved(
103 NodeListModel.this,
104 e.getIndex0(),
105 e.getIndex1());
106 }
107 });
108 }
109
110 private void stateChanged() {
111 try {
112 cache.setList(select.selectNodes(listener.getNode()));
113 } catch (JaxenException exc) {
114 LogFactory.getLog(getClass()).error("Error in stateChanged", exc);
115 }
116 }
117
118 public void setNode(Node node) throws Exception {
119 listener.setNode(node);
120 }
121
122 public void setSelect(DOMXPath select) throws Exception {
123 this.select = select;
124 stateChanged();
125 }
126
127 public int getSize() {
128 return cache.getSize();
129 }
130
131 public Object getElementAt(int index) {
132 return cache.getElementAt(index);
133 }
134 }
This page was automatically generated by Maven