source: trunk/gli/src/org/greenstone/gatherer/cdm/DOMProxyListModel.java@ 8266

Last change on this file since 8266 was 8243, checked in by mdewsnip, 20 years ago

Removed all occurrences of classes explicitly importing other classes in the same package.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.cdm;
28
29import java.util.*;
30import javax.swing.*;
31import org.w3c.dom.*;
32
33/** This class provides ListModel like access to a list of nodes within a DOM model.
34 * @author John Thompson, Greenstone Digital Library, University of Waikato
35 * @version 2.3d
36 */
37public class DOMProxyListModel
38 extends AbstractListModel {
39 protected Element root;
40 private DOMProxyListEntry class_type;
41 private HashMap cache = new HashMap();
42 private NodeList children = null;
43 private String tag_name;
44
45 /** Constructor.
46 * @param root the Element at the root of the subtree to be searched for appropriate child elements
47 * @param tag_name the name of appropriate elements as a String
48 * @param class_type the type of object to wrap the elements returned in, as a DOMProxyListEntry
49 */
50 public DOMProxyListModel(Element root, String tag_name, DOMProxyListEntry class_type) {
51 this.class_type = class_type;
52 this.root = root;
53 this.tag_name = tag_name;
54 }
55
56 /** Used to add an element into the underlying dom, and fire the appropriate repaint events. This version always adds the new element at the very head of the DOM. */
57 public synchronized void add(DOMProxyListEntry entry) {
58 Element element = entry.getElement();
59 if(root.hasChildNodes()) {
60 Node sibling = root.getFirstChild();
61 root.insertBefore(element, sibling);
62 sibling = null;
63 }
64 else {
65 root.appendChild(element);
66 }
67 element = null;
68 // Regardless fire update event
69 cache.clear();
70 fireIntervalAdded(this, 0, 0);
71 }
72
73 /** Used to add an element into the underlying dom, and fire the appropriate repaint events.
74 * @param index the index where the element should be inserted (relative of the other elements in this proxy list)
75 * @param entry the <strong>DOMProxyListEntry</strong> to be inserted
76 */
77 public synchronized void add(int index, DOMProxyListEntry entry) {
78 ///atherer.println("Add entry at " + index + " where size = " + getSize());
79 Element element = entry.getElement();
80 // retrieve the node where we want to insert
81 if(index < children.getLength()) {
82 Node sibling = children.item(index);
83 // Find the parent node
84 Node parent_node = sibling.getParentNode();
85 parent_node.insertBefore(element, sibling);
86 sibling = null;
87 }
88 // If the index is too large, we are adding to the end of our list of entries. However you have to remember that this list is only a viewport on the entire DOM so there might be entries following this group that we actually want to insert before (not append at the very end!)
89 else {
90 // Retrieve the currently last entry
91 index = children.getLength() - 1;
92 Node sibling = null;
93 Node parent_node = null;
94 if(index >= 0) {
95 sibling = children.item(index);
96 parent_node = sibling.getParentNode();
97 sibling = sibling.getNextSibling();
98 }
99 if(sibling != null && parent_node != null) {
100 parent_node.insertBefore(element, sibling);
101 }
102 // Add to the root node
103 else {
104 index = 0;
105 root.appendChild(element);
106 }
107 }
108 // Regardless fire update event
109 cache.clear();
110 fireIntervalAdded(this, index, index);
111 }
112
113 /** Used to add an element into the underlying dom, and fire the appropriate repaint events. This version inserts the new entry immediately -after- the given entry in the DOM.
114 * @param entry the DOMProxyListEntry to be inserted
115 * @param proceeding_entry the DOMProxyListEntry immediately before where we want the new entry
116 */
117 public synchronized void addAfter(DOMProxyListEntry entry, DOMProxyListEntry proceeding_entry) {
118 Element element = entry.getElement();
119 Element proceeding_sibling = proceeding_entry.getElement();
120 Node parent_node = proceeding_sibling.getParentNode();
121 Node following_sibling = proceeding_sibling.getNextSibling();
122 if(following_sibling != null) {
123 parent_node.insertBefore(element, following_sibling);
124 }
125 else {
126 parent_node.appendChild(element);
127 }
128 // Regardless fire update event
129 cache.clear();
130 int index = indexOf(proceeding_entry) + 1;
131 fireIntervalAdded(this, index, index);
132 }
133
134 /** Used to add an element into the underlying dom, and fire the appropriate repaint events. This version inserts the new entry immediately -before- the given entry in the DOM.
135 * @param entry the DOMProxyListEntry to be inserted
136 * @param following_entry the DOMProxyListEntry immediately after where we want the new entry
137 */
138 public synchronized void addBefore(DOMProxyListEntry entry, DOMProxyListEntry following_entry) {
139 Element element = entry.getElement();
140 Element following_sibling = following_entry.getElement();
141 Node parent_node = following_sibling.getParentNode();
142 parent_node.insertBefore(element, following_sibling);
143 // Regardless fire update event
144 cache.clear();
145 int index = indexOf(entry);
146 fireIntervalAdded(this, index, index);
147 }
148
149 public synchronized void add(Node parent, DOMProxyListEntry entry, Node sibling) {
150 Element child = entry.getElement();
151 if(sibling != null) {
152 parent.insertBefore(child, sibling);
153 }
154 else {
155 parent.appendChild(child);
156 }
157 cache.clear();
158 int index = indexOf(entry);
159 fireIntervalAdded(this, index, index);
160 }
161
162 /** Used to add an element into the underlying dom, and fire the appropriate repaint events. This version always adds the new element at the end of the children. */
163 public synchronized void append(DOMProxyListEntry entry) {
164 Element element = entry.getElement();
165 root.appendChild(element);
166 element = null;
167 // Regardless fire update event
168 cache.clear();
169 fireIntervalAdded(this, 0, 0);
170 }
171
172 public synchronized ArrayList children() {
173 ArrayList child_list = new ArrayList();
174 int child_count = children.getLength();
175 for(int i = 0; i < child_count; i++) {
176 child_list.add(getElementAt(i));
177 }
178 return child_list;
179 }
180
181 public synchronized boolean contains(Object entry) {
182 boolean found = false;
183 int size = getSize();
184 for(int i = 0; !found && i < size; i++) {
185 DOMProxyListEntry sibling = (DOMProxyListEntry) getElementAt(i);
186 if(sibling.equals(entry)) {
187 found = true;
188 }
189 }
190 return found;
191 }
192
193 public synchronized Object getElementAt(int index) {
194 Object object = cache.get(new Integer(index));
195 if(object == null) {
196 // Retrieve the required element
197 Element element = (Element) children.item(index);
198 // Now wrap it in the object of the users choice
199 object = class_type.create(element);
200 cache.put(new Integer(index), object);
201 }
202 return object;
203 }
204
205 public synchronized int indexOf(DOMProxyListEntry entry) {
206 Element element = entry.getElement();
207 int children_length = children.getLength();
208 for(int i = 0; i < children_length; i++) {
209 Node node = children.item(i);
210 if(element == node) {
211 return i;
212 }
213 }
214 return -1;
215 }
216
217 public synchronized int getSize() {
218 if(children == null) {
219 children = root.getElementsByTagName(tag_name);
220 }
221 return children.getLength();
222 }
223
224 public synchronized void refresh() {
225 fireContentsChanged(this, 0, getSize());
226 }
227
228 public synchronized void refresh(DOMProxyListEntry entry) {
229 int index = indexOf(entry);
230 fireContentsChanged(this, index, index);
231 }
232
233 public synchronized void remove(DOMProxyListEntry entry) {
234 remove(indexOf(entry));
235 }
236
237 public synchronized void remove(int index) {
238 // Retrieve the required element
239 Node node = children.item(index);
240 // Find its parent
241 Node parent_node = node.getParentNode();
242 // Now remove it
243 parent_node.removeChild(node);
244 // Refresh model
245 cache.clear();
246 fireIntervalRemoved(this, index, index);
247 }
248
249 /** Changes the 'root' element that this list sources its information from.
250 * @param root the new root Element
251 */
252 public synchronized void setRoot(Element root) {
253 this.children = null;
254 cache.clear();
255 this.root = root;
256 }
257}
Note: See TracBrowser for help on using the repository browser.