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 | */
|
---|
27 | package org.greenstone.gatherer.cdm;
|
---|
28 |
|
---|
29 | import java.util.*;
|
---|
30 |
|
---|
31 | import javax.swing.*;
|
---|
32 |
|
---|
33 | import org.greenstone.gatherer.DebugStream;
|
---|
34 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
35 | import org.w3c.dom.*;
|
---|
36 |
|
---|
37 | /** This class provides ListModel like access to a list of nodes within a DOM model.
|
---|
38 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
39 | * @version 2.3d
|
---|
40 | */
|
---|
41 | @SuppressWarnings({ "serial", "rawtypes" })
|
---|
42 | public class DOMProxyListModel
|
---|
43 | extends AbstractListModel {
|
---|
44 | protected Element root;
|
---|
45 | private DOMProxyListEntry class_type;
|
---|
46 | private HashMap cache = new HashMap ();
|
---|
47 | private NodeList children = null;
|
---|
48 | private String tag_name;
|
---|
49 |
|
---|
50 | /** Constructor.
|
---|
51 | * @param root the Element at the root of the subtree to be searched for appropriate child elements
|
---|
52 | * @param tag_name the name of appropriate elements as a String
|
---|
53 | * @param class_type the type of object to wrap the elements returned in, as a DOMProxyListEntry
|
---|
54 | */
|
---|
55 | public DOMProxyListModel (Element root, String tag_name, DOMProxyListEntry class_type) {
|
---|
56 | this.class_type = class_type;
|
---|
57 | this.root = root;
|
---|
58 | this.tag_name = tag_name;
|
---|
59 | this.children = this.root.getElementsByTagName (this.tag_name);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** 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. */
|
---|
63 | public synchronized void add (DOMProxyListEntry entry) {
|
---|
64 | Element element = entry.getElement ();
|
---|
65 | if(root.hasChildNodes ()) {
|
---|
66 | Node sibling = root.getFirstChild ();
|
---|
67 | root.insertBefore (element, sibling);
|
---|
68 | sibling = null;
|
---|
69 | }
|
---|
70 | else {
|
---|
71 | root.appendChild (element);
|
---|
72 | }
|
---|
73 | element = null;
|
---|
74 | // Regardless fire update event
|
---|
75 | cache.clear ();
|
---|
76 | fireIntervalAdded (this, 0, 0);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** Used to add an element into the underlying dom, and fire the appropriate repaint events.
|
---|
80 | * @param index the index where the element should be inserted (relative of the other elements in this proxy list)
|
---|
81 | * @param entry the <strong>DOMProxyListEntry</strong> to be inserted
|
---|
82 | */
|
---|
83 | public synchronized void add (int index, DOMProxyListEntry entry) {
|
---|
84 | ///atherer.println("Add entry at " + index + " where size = " + getSize());
|
---|
85 | Element element = entry.getElement ();
|
---|
86 | // retrieve the node where we want to insert
|
---|
87 | if(index < children.getLength ()) {
|
---|
88 | Node sibling = children.item (index);
|
---|
89 | // Find the parent node
|
---|
90 | Node parent_node = sibling.getParentNode ();
|
---|
91 | parent_node.insertBefore (element, sibling);
|
---|
92 | sibling = null;
|
---|
93 | }
|
---|
94 | // 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!)
|
---|
95 | else {
|
---|
96 | // Retrieve the currently last entry
|
---|
97 | index = children.getLength () - 1;
|
---|
98 | Node sibling = null;
|
---|
99 | Node parent_node = null;
|
---|
100 | if(index >= 0) {
|
---|
101 | sibling = children.item (index);
|
---|
102 | parent_node = sibling.getParentNode ();
|
---|
103 | sibling = sibling.getNextSibling ();
|
---|
104 | }
|
---|
105 | if(sibling != null && parent_node != null) {
|
---|
106 | parent_node.insertBefore (element, sibling);
|
---|
107 | }
|
---|
108 | // Add to the root node
|
---|
109 | else {
|
---|
110 | index = 0;
|
---|
111 | root.appendChild (element);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | // Regardless fire update event
|
---|
115 | cache.clear ();
|
---|
116 | fireIntervalAdded (this, index, index);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** 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.
|
---|
120 | * @param entry the DOMProxyListEntry to be inserted
|
---|
121 | * @param preceeding_entry the DOMProxyListEntry immediately before where we want the new entry
|
---|
122 | */
|
---|
123 | public synchronized void addAfter (DOMProxyListEntry entry, DOMProxyListEntry preceeding_entry) {
|
---|
124 | Element element = entry.getElement ();
|
---|
125 | Element preceeding_sibling = preceeding_entry.getElement ();
|
---|
126 | Node parent_node = preceeding_sibling.getParentNode ();
|
---|
127 | Node following_sibling = preceeding_sibling.getNextSibling ();
|
---|
128 | if(following_sibling != null) {
|
---|
129 | parent_node.insertBefore (element, following_sibling);
|
---|
130 | }
|
---|
131 | else {
|
---|
132 | parent_node.appendChild (element);
|
---|
133 | }
|
---|
134 | // Regardless fire update event
|
---|
135 | cache.clear ();
|
---|
136 | int index = indexOf (entry);
|
---|
137 | fireIntervalAdded (this, index, index);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** 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.
|
---|
141 | * @param entry the DOMProxyListEntry to be inserted
|
---|
142 | * @param following_entry the DOMProxyListEntry immediately after where we want the new entry
|
---|
143 | */
|
---|
144 | public synchronized void addBefore (DOMProxyListEntry entry, DOMProxyListEntry following_entry) {
|
---|
145 | Element element = entry.getElement ();
|
---|
146 | Element following_sibling = following_entry.getElement ();
|
---|
147 | Node parent_node = following_sibling.getParentNode ();
|
---|
148 | parent_node.insertBefore (element, following_sibling);
|
---|
149 | // Regardless fire update event
|
---|
150 | cache.clear ();
|
---|
151 | int index = indexOf (entry);
|
---|
152 | fireIntervalAdded (this, index, index);
|
---|
153 | }
|
---|
154 |
|
---|
155 | public synchronized void add (Node parent, DOMProxyListEntry entry, Node sibling) {
|
---|
156 | Element child = entry.getElement ();
|
---|
157 | if(sibling != null) {
|
---|
158 | parent.insertBefore (child, sibling);
|
---|
159 | }
|
---|
160 | else {
|
---|
161 | parent.appendChild (child);
|
---|
162 | }
|
---|
163 | cache.clear ();
|
---|
164 | int index = indexOf (entry);
|
---|
165 |
|
---|
166 |
|
---|
167 | fireIntervalAdded (this, index, index);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /** 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. */
|
---|
171 | public synchronized void append (DOMProxyListEntry entry) {
|
---|
172 | Element element = entry.getElement ();
|
---|
173 | root.appendChild (element);
|
---|
174 | element = null;
|
---|
175 | // Regardless fire update event
|
---|
176 | cache.clear ();
|
---|
177 | fireIntervalAdded (this, 0, 0);
|
---|
178 | }
|
---|
179 |
|
---|
180 | @SuppressWarnings("unchecked")
|
---|
181 | public synchronized ArrayList children () {
|
---|
182 | ArrayList child_list = new ArrayList ();
|
---|
183 | int child_count = children.getLength ();
|
---|
184 | for(int i = 0; i < child_count; i++) {
|
---|
185 | child_list.add (getElementAt (i));
|
---|
186 | }
|
---|
187 | return child_list;
|
---|
188 | }
|
---|
189 |
|
---|
190 | public synchronized boolean contains (Object entry) {
|
---|
191 | boolean found = false;
|
---|
192 | int size = getSize ();
|
---|
193 |
|
---|
194 | for(int i = 0; !found && i < size; i++) {
|
---|
195 | DOMProxyListEntry sibling = (DOMProxyListEntry) getElementAt (i);
|
---|
196 | if(sibling.equals (entry)) {
|
---|
197 |
|
---|
198 | found = true;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | return found;
|
---|
202 | }
|
---|
203 |
|
---|
204 | @SuppressWarnings("unchecked")
|
---|
205 | public synchronized Object getElementAt (int index) {
|
---|
206 | /** There are times when the length of the 'cache' is not as same as the length of the 'children', etc. not up to date.
|
---|
207 | eg, when a classifier has been deleted. So we rather not have this efficiency for Format4gs3.java.*/
|
---|
208 | if (class_type instanceof Format4gs3) {
|
---|
209 | Element element = (Element) children.item (index);
|
---|
210 | // Now wrap it in the object of the users choice
|
---|
211 | Object object = class_type.create (element);
|
---|
212 | return object;
|
---|
213 | }
|
---|
214 |
|
---|
215 | Object object = cache.get (new Integer (index));
|
---|
216 | if (object != null) {
|
---|
217 | return object;
|
---|
218 | }
|
---|
219 |
|
---|
220 | // Retrieve the required element
|
---|
221 | Element element = (Element) children.item (index);
|
---|
222 | if (element == null) {
|
---|
223 | return null;
|
---|
224 | }
|
---|
225 | DebugStream.println ("Element at index " + index + " not in cache: " + element);
|
---|
226 |
|
---|
227 | // Now wrap it in the object of the users choice
|
---|
228 | object = class_type.create (element);
|
---|
229 | cache.put (new Integer (index), object);
|
---|
230 | return object;
|
---|
231 | }
|
---|
232 |
|
---|
233 | public synchronized int indexOf (DOMProxyListEntry entry) {
|
---|
234 | Element element = entry.getElement ();
|
---|
235 | int children_length = children.getLength ();
|
---|
236 | for(int i = 0; i < children_length; i++) {
|
---|
237 | Node node = children.item (i);
|
---|
238 | if(element == node) {
|
---|
239 | return i;
|
---|
240 | }
|
---|
241 | }
|
---|
242 | return -1;
|
---|
243 | }
|
---|
244 |
|
---|
245 | public synchronized int getSize () {
|
---|
246 |
|
---|
247 | if(children == null) {
|
---|
248 | children = root.getElementsByTagName (tag_name);
|
---|
249 | }
|
---|
250 | return children.getLength ();
|
---|
251 | }
|
---|
252 |
|
---|
253 | public synchronized void refresh () {
|
---|
254 | fireContentsChanged (this, 0, getSize ());
|
---|
255 | }
|
---|
256 |
|
---|
257 | public synchronized void refresh (DOMProxyListEntry entry) {
|
---|
258 | int index = indexOf (entry);
|
---|
259 | fireContentsChanged (this, index, index);
|
---|
260 | }
|
---|
261 |
|
---|
262 | public synchronized void remove (DOMProxyListEntry entry) {
|
---|
263 | remove (indexOf (entry));
|
---|
264 | }
|
---|
265 |
|
---|
266 | public synchronized void remove (int index) {
|
---|
267 | // Retrieve the required element
|
---|
268 | Node node = children.item (index);
|
---|
269 | // Find its parent
|
---|
270 | Node parent_node = node.getParentNode ();
|
---|
271 | // Now remove it
|
---|
272 | parent_node.removeChild (node);
|
---|
273 | // Refresh model
|
---|
274 | cache.clear ();
|
---|
275 |
|
---|
276 | fireIntervalRemoved (this, index, index);
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | /** Changes the 'root' element that this list sources its information from.
|
---|
281 | * @param root the new root Element
|
---|
282 | */
|
---|
283 | public synchronized void setRoot (Element root) {
|
---|
284 | this.children = null;
|
---|
285 | cache.clear ();
|
---|
286 | this.root = root;
|
---|
287 | this.children = this.root.getElementsByTagName (this.tag_name);
|
---|
288 | fireContentsChanged (this, 0, getSize ());
|
---|
289 | }
|
---|
290 |
|
---|
291 | public synchronized void setAssigned (boolean assigned) {
|
---|
292 | if (assigned) {
|
---|
293 | root.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
|
---|
294 | }
|
---|
295 | else {
|
---|
296 | root.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | public boolean isAssigned () {
|
---|
301 | return (root.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals ("") || root.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.TRUE_STR));
|
---|
302 | }
|
---|
303 |
|
---|
304 | }
|
---|