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

Last change on this file since 12641 was 12641, checked in by mdewsnip, 18 years ago

Changed all access to the static strings through CollectionConfiguration to directly use StaticStrings.

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