source: trunk/gli/src/org/greenstone/gatherer/valuetree/GValueModel.java@ 4364

Last change on this file since 4364 was 4364, checked in by mdewsnip, 21 years ago

Fixed tabbing.

  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 KB
Line 
1package org.greenstone.gatherer.valuetree;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import java.util.*;
39import javax.swing.tree.*;
40import org.greenstone.gatherer.Gatherer;
41import org.greenstone.gatherer.msm.ElementWrapper;
42import org.greenstone.gatherer.msm.MetadataSetManager;
43import org.greenstone.gatherer.msm.MSMUtils;
44import org.greenstone.gatherer.util.SynchronizedTreeModelTools;
45import org.greenstone.gatherer.util.Utility;
46import org.w3c.dom.*;
47/*
48 * @author John Thompson, Greenstone Digital Library, University of Waikato
49 * @version 2.1
50 */
51public class GValueModel
52 extends DefaultTreeModel {
53
54 private ElementWrapper element;
55
56 public GValueModel() {
57 super(new DefaultMutableTreeNode("Temp"));
58 }
59
60 public GValueModel(String root) {
61 super(new DefaultMutableTreeNode(root));
62 }
63
64 public GValueModel(ElementWrapper e) {
65 super(new DefaultMutableTreeNode("Temp"));
66 this.element = e;
67 // Load the template value tree document.
68 Document document = MSMUtils.getValueTreeTemplate();
69 Element new_root = document.getDocumentElement();
70 new_root.setAttribute("element", e.getName());
71 root = new GValueNode(new_root);
72 }
73
74 public GValueModel(ElementWrapper e, Document document) {
75 super(new DefaultMutableTreeNode("Temp"));
76 this.element = e;
77 Element new_root = document.getDocumentElement();
78 new_root.setAttribute("element", e.getName());
79 root = new GValueNode(new_root);
80 }
81
82 /** Value may include path ie news/newssw */
83 public GValueNode addValue(String value) {
84 ///ystem.err.println("Adding value to valuetree: " + value);
85 StringTokenizer tokenizer = new StringTokenizer(value, "\\");
86 GValueNode subject = (GValueNode) root;
87 while(tokenizer.hasMoreTokens() && subject != null) {
88 String token = tokenizer.nextToken();
89 subject = addValue(token, subject, null);
90 ///ystem.err.println("Found or created a node " + token);
91 }
92 return subject;
93 }
94
95 public GValueNode addValue(String value, GValueNode subject, String alias) {
96 // To add a value we must first ensure it isn't already present in -this- nodes children. The bummer is that the nice getElements functions search the whole tree so...
97 GValueNode value_node = subject.getValue(Utility.stripNL(value));
98 if(value_node == null) {
99 // Now add the new value.
100 Document document = subject.getElement().getOwnerDocument();
101 // Now we create a new subject and add it subject
102 Element new_subject = document.createElementNS("","Subject");
103 Element new_value = document.createElementNS("","Value");
104 new_subject.appendChild(new_value);
105 Text new_text = document.createTextNode(value);
106 new_value.appendChild(new_text);
107 if(alias != null && alias.length() > 0) {
108 Element new_alias = document.createElementNS("","Alias");
109 new_subject.appendChild(new_alias);
110 Text new_alias_text = document.createTextNode(alias);
111 new_alias.appendChild(new_alias_text);
112 }
113 value_node = new GValueNode(new_subject);
114 // Figure out where this node will be inserted in subjects
115 // children.
116 int position = -1;
117 for(int i = 0; position == -1 && i < subject.getChildCount(); i++) {
118 Object sibling = subject.getChildAt(i);
119 int rel_pos = value.compareTo(sibling.toString());
120 ///ystem.err.println("'"+value+"'.compareTo('"+sibling+"') = " + rel_pos);
121 if(rel_pos <= 0) {
122 position = i;
123 }
124 }
125 if(position == -1) {
126 position = subject.getChildCount();
127 }
128 // Insert it. If position is still -1, append it to the end of subjects children.
129 ///ystem.err.println("Inserting '" + value + "' at position " + position);
130 insertNodeInto(value_node, subject, position);
131 // Inform listeners that we've changed.
132 Gatherer.c_man.getCollection().msm.fireValueChanged(element, null, this);
133 }
134 return value_node;
135 }
136
137 public Document getDocument() {
138 return ((GValueNode)root).getElement().getOwnerDocument();
139 }
140
141 public ElementWrapper getElement() {
142 return element;
143 }
144
145 /** Retrieve the hindex for a certain value within the value tree.
146 * @param value The value whose index you wish to determine as a <strong>String</strong>.
147 * @return A <strong>String</strong> containing an index such as "1", "2.1" or "18.2.5".
148 */
149 public String getHIndex(String value) {
150 ///ystem.err.println("getHIndex(" + value + ")");
151 return getHIndex((GValueNode) root, value, null);
152 }
153
154 /** Retrieve a value node given its hierarchical reference or value.
155 * @param index The hierarchy index or value as a <strong>String</strong>.
156 */
157 public GValueNode getValue(String index_str) {
158 ///ystem.err.println("Retrieve the value for: " + index_str);
159 GValueNode result = null;
160 if(isHierarchy() && Utility.isIndex(index_str)) {
161 // StringTokenize the index
162 StringTokenizer tokenizer = new StringTokenizer(index_str, ".");
163 result = (GValueNode) root;
164 // Using the index numbers retrieve the appropriate node.
165 try {
166 while(result != null && tokenizer.hasMoreTokens()) {
167 int index = Integer.parseInt(tokenizer.nextToken()) - 1;
168 // Retrieve the index'th child of the current result node
169 if(0 <= index && index < result.getChildCount()) {
170 result = (GValueNode) result.getChildAt(index);
171 }
172 // Otherwise we're broken.
173 else {
174 ///ystem.err.println("There is no " + index + "th childnode of " + result);
175 result = null;
176 }
177 }
178 }
179 // Most likely caused by parseInt throwing a wobbly.
180 catch (Exception error) {
181 result = null;
182 }
183 if(result != null) {
184 // Ensure result is enabled
185 result.setEnabled(true);
186 }
187 }
188 if(result == null) {
189 ///ystem.err.println("No existing value. Adding " + index_str);
190 result = addValue(index_str);
191 }
192 return result;
193 }
194
195 public boolean isHierarchy() {
196 boolean result = false;
197 // We are a hierarchy if our element says so....
198 if(element.isHierarchy()) {
199 return true;
200 }
201 // Or if our children are actually a hierarchy.
202 for(int i = 0; i < root.getChildCount() && !result; i++) {
203 GValueNode node = (GValueNode) root.getChildAt(i);
204 if(node != null && node.getChildCount() > 0) {
205 result = true;
206 }
207 }
208 return result;
209 }
210
211 public void removeValue(GValueNode child) {
212 child.setEnabled(false); // Doesn't appear anymore.
213 SynchronizedTreeModelTools.removeNodeFromParent(this, child);
214 Gatherer.c_man.getCollection().msm.fireValueChanged(new ElementWrapper(child.getElement()), null, this);
215 }
216
217 public void removeValue(String value) {
218 // Retrieve the node to be removed.
219 GValueNode node = getValue(value);
220 // Now remove it
221 removeNodeFromParent(node);
222 }
223
224 /** Called to remove a certain value of metadata within a specific
225 * subject within a sbject hierarchy.
226 * Note that this method currently doesn't do anything.
227 * @param value A String representing the value to be removed.
228 * @param subject An Element representing the subject you wish to remove
229 * this value from.
230 */
231 public void removeValue(String value, GValueNode subject) {
232 }
233
234 public int size() {
235 return size(root);
236 }
237
238 private int size(TreeNode current) {
239 int size = 1;
240 for(int i = 0; i < current.getChildCount(); i++) {
241 size = size + size(current.getChildAt(i));
242 }
243 return size;
244 }
245
246 public String toString() {
247 return element.toString();
248 }
249
250 public Vector traverseTree() {
251 Vector contents = new Vector();
252 contents.addAll(traverseTree((GValueNode) root));
253 contents.remove((GValueNode) root);
254 return contents;
255 }
256
257 public void updateValue(String new_value, String old_value) {
258 ///ystem.err.println("Updating '" + old_value + "' to '" + new_value + "'");
259 StringTokenizer tokenizer = new StringTokenizer(new_value, "\\");
260 GValueNode subject = (GValueNode) root;
261 while(tokenizer.hasMoreTokens()) {
262 String token = tokenizer.nextToken();
263 subject = updateValue(token, old_value, subject);
264 }
265 }
266
267 /** Called to update a certain value of metadata within a specific
268 * subject within a sbject hierarchy.
269 * Note that this simply in turn calls removeValue() to deal with the
270 * old value, then addValue() to account for the new.
271 * @param new_value A String representing the updated value.
272 * @param old_value A String representing the old value.
273 * @param subject An Element representing the subject you wish to update
274 * this value in.
275 */
276 public GValueNode updateValue(String new_value, String old_value, GValueNode subject) {
277 return addValue(new_value, subject, null);
278 }
279
280 private GValueModel copy() {
281 Element document_element = ((GValueNode) root).getElement();
282 Document document_copy = MSMUtils.getValueTreeTemplate();
283 Element document_copy_element = document_copy.getDocumentElement();
284 document_copy_element.setAttribute("element", element.getName());
285 for(Node node = document_element.getFirstChild(); node != null; node = node.getNextSibling()) {
286 if(node.getNodeName().equals("Subject")) {
287 Node node_copy = document_copy.importNode(node, true);
288 document_copy_element.appendChild(node_copy);
289 }
290 }
291 return new GValueModel(element, document_copy);
292 }
293
294 private String getHIndex(GValueNode node, String value, String index) {
295 for(int i = node.size(); i != 0; i--) {
296 GValueNode next = (GValueNode)node.get(i - 1);
297 String next_str = next.toString();
298 if(value.startsWith(next_str)) {
299 if(index == null) {
300 index = String.valueOf(i);
301 }
302 else {
303 index = index + "." + i;
304 }
305 value = value.substring(next.toString().length());
306 if(value.startsWith("\\")) {
307 value = value.substring(1);
308 index = getHIndex(next, value, index);
309 }
310 return index;
311 }
312 }
313 return index;
314 }
315
316 private Vector traverseTree(GValueNode node) {
317 Vector contents = new Vector();
318 contents.add(node);
319 for(int i = 0; i < node.getChildCount(); i++) {
320 contents.addAll(traverseTree((GValueNode)node.getChildAt(i)));
321 }
322 return contents;
323 }
324}
Note: See TracBrowser for help on using the repository browser.