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

Last change on this file since 5671 was 5671, checked in by jmt12, 21 years ago

Looks like I removed two spaces and nothing much else

  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 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.valuetree;
28
29import java.util.*;
30import javax.swing.tree.*;
31import org.greenstone.gatherer.Gatherer;
32import org.greenstone.gatherer.msm.ElementWrapper;
33import org.greenstone.gatherer.msm.MSMUtils;
34import org.greenstone.gatherer.util.PatternTokenizer;
35import org.greenstone.gatherer.util.Utility;
36import org.w3c.dom.*;
37/*
38 * @author John Thompson, Greenstone Digital Library, University of Waikato
39 * @version 2.1
40 */
41public class GValueModel
42 extends DefaultTreeModel {
43
44 static final public String PATH_SEP = "\\\\";
45 static final public String PATH_SEP_PATTERN = "\\\\\\\\";
46
47 private ElementWrapper element;
48
49 public GValueModel() {
50 super(new DefaultMutableTreeNode("Temp"));
51 }
52
53 public GValueModel(String root) {
54 super(new DefaultMutableTreeNode(root));
55 }
56
57 public GValueModel(ElementWrapper e) {
58 super(new DefaultMutableTreeNode("Temp"));
59 this.element = e;
60 // Load the template value tree document.
61 Document document = MSMUtils.getValueTreeTemplate();
62 Element new_root = document.getDocumentElement();
63 new_root.setAttribute("element", e.getName());
64 root = new GValueNode(new_root);
65 }
66
67 public GValueModel(ElementWrapper e, Document document) {
68 super(new DefaultMutableTreeNode("Temp"));
69 this.element = e;
70 Element new_root = document.getDocumentElement();
71 new_root.setAttribute("element", e.getName());
72 root = new GValueNode(new_root);
73 }
74
75 /** Value may include path ie news\newssw */
76 public GValueNode addValue(String value) {
77 try {
78 // Tokenize the string using the escaped character
79 PatternTokenizer tokenizer = new PatternTokenizer(value, PATH_SEP);
80 GValueNode subject = (GValueNode) root;
81 while(tokenizer.hasMoreTokens() && subject != null) {
82 String token = tokenizer.nextToken();
83 subject = addValue(token, subject, null);
84 }
85 return subject;
86 }
87 catch (Exception error) {
88 error.printStackTrace();
89 }
90 return null;
91 }
92
93 public GValueNode addValue(String value, GValueNode subject, String alias) {
94 // 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...
95 GValueNode value_node = subject.getValue(value);
96 if(value_node == null) {
97 // Now add the new value.
98 Document document = subject.getElement().getOwnerDocument();
99 // Now we create a new subject and add it subject
100 Element new_subject = document.createElementNS("","Subject");
101 Element new_value = document.createElementNS("","Value");
102 new_subject.appendChild(new_value);
103 Text new_text = document.createTextNode(value);
104 new_value.appendChild(new_text);
105 if(alias != null && alias.length() > 0) {
106 Element new_alias = document.createElementNS("","Alias");
107 new_subject.appendChild(new_alias);
108 Text new_alias_text = document.createTextNode(alias);
109 new_alias.appendChild(new_alias_text);
110 }
111 value_node = new GValueNode(new_subject);
112 ///ystem.err.println("(GValueModel) addValue()... " + value_node);
113
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 ///ystem.err.println("(GValueModel) Done insert node into...");
132 // SynchronizedTreeModelTools.insertNodeInto(this, subject, value_node);
133 // Inform listeners that we've changed.
134 Gatherer.c_man.getCollection().msm.fireValueChanged(element, null, this);
135 }
136 return value_node;
137 }
138
139 public Document getDocument() {
140 return ((GValueNode)root).getElement().getOwnerDocument();
141 }
142
143 public ElementWrapper getElement() {
144 return element;
145 }
146
147 /** Retrieve the hindex for a certain value within the value tree.
148 * @param value The value whose index you wish to determine as a <strong>String</strong>.
149 * @return A <strong>String</strong> containing an index such as "1", "2.1" or "18.2.5".
150 */
151 public String getHIndex(String value) {
152 ///ystem.err.println("getHIndex(" + value + ")");
153 return getHIndex((GValueNode) root, value, null);
154 }
155
156 /** Retrieve a value node given its hierarchical reference or value.
157 * @param index The hierarchy index or value as a <strong>String</strong>.
158 */
159 public GValueNode getValue(String index_str) {
160 ///ystem.err.println("Retrieve the value for: " + index_str);
161 GValueNode result = null;
162 if(isHierarchy() && Utility.isIndex(index_str)) {
163 // StringTokenize the index
164 StringTokenizer tokenizer = new StringTokenizer(index_str, ".");
165 result = (GValueNode) root;
166 // Using the index numbers retrieve the appropriate node.
167 try {
168 while(result != null && tokenizer.hasMoreTokens()) {
169 int index = Integer.parseInt(tokenizer.nextToken()) - 1;
170 // Retrieve the index'th child of the current result node
171 if(0 <= index && index < result.getChildCount()) {
172 result = (GValueNode) result.getChildAt(index);
173 }
174 // Otherwise we're broken.
175 else {
176 ///ystem.err.println("There is no " + index + "th childnode of " + result);
177 result = null;
178 }
179 }
180 }
181 // Most likely caused by parseInt throwing a wobbly.
182 catch (Exception error) {
183 result = null;
184 }
185 }
186 if(result == null) {
187 ///ystem.err.println("No existing value. Adding " + index_str);
188 result = addValue(index_str);
189 }
190 return result;
191 }
192
193 public boolean isHierarchy() {
194 boolean result = false;
195 // We are a hierarchy if our element says so....
196 if(element.isHierarchy()) {
197 return true;
198 }
199 // Or if our children are actually a hierarchy.
200 for(int i = 0; i < root.getChildCount() && !result; i++) {
201 GValueNode node = (GValueNode) root.getChildAt(i);
202 if(node != null && node.getChildCount() > 0) {
203 result = true;
204 }
205 }
206 return result;
207 }
208
209 public void removeValue(GValueNode child) {
210 //SynchronizedTreeModelTools.removeNodeFromParent(this, child);
211 removeNodeFromParent(child);
212 Gatherer.c_man.getCollection().msm.fireValueChanged(new ElementWrapper(child.getElement()), null, this);
213 }
214
215 public void removeValue(String value) {
216 // Retrieve the node to be removed.
217 GValueNode node = getValue(value);
218 if(node != null) {
219 removeValue(node);
220 }
221 }
222
223 public int size() {
224 return size(root);
225 }
226
227 private int size(TreeNode current) {
228 int size = 1;
229 for(int i = 0; i < current.getChildCount(); i++) {
230 size = size + size(current.getChildAt(i));
231 }
232 return size;
233 }
234
235 public String toString() {
236 return element.toString();
237 }
238
239 public Vector traverseTree() {
240 Vector contents = new Vector();
241 contents.addAll(traverseTree((GValueNode) root));
242 contents.remove((GValueNode) root);
243 return contents;
244 }
245
246 /** Called to update a certain value of metadata within a specific
247 * subject within a sbject hierarchy.
248 * Note that this simply in turn calls removeValue() to deal with the
249 * old value, then addValue() to account for the new.
250 * @param new_value A String representing the updated value.
251 * @param old_value A String representing the old value.
252 * @param subject An Element representing the subject you wish to update
253 * this value in.
254 */
255 public GValueNode updateValue(String new_value, String old_value, GValueNode subject) {
256 return addValue(new_value, subject, null);
257 }
258
259 private GValueModel copy() {
260 Element document_element = ((GValueNode) root).getElement();
261 Document document_copy = MSMUtils.getValueTreeTemplate();
262 Element document_copy_element = document_copy.getDocumentElement();
263 document_copy_element.setAttribute("element", element.getName());
264 for(Node node = document_element.getFirstChild(); node != null; node = node.getNextSibling()) {
265 if(node.getNodeName().equals("Subject")) {
266 Node node_copy = document_copy.importNode(node, true);
267 document_copy_element.appendChild(node_copy);
268 }
269 }
270 return new GValueModel(element, document_copy);
271 }
272
273 private String getHIndex(GValueNode node, String value, String index) {
274 for(int i = node.size(); i != 0; i--) {
275 GValueNode next = (GValueNode)node.get(i - 1);
276 String next_str = next.toString();
277 if(value.startsWith(next_str)) {
278 if(index == null) {
279 index = String.valueOf(i);
280 }
281 else {
282 index = index + "." + i;
283 }
284 value = value.substring(next.toString().length());
285 if(value.startsWith(PATH_SEP)) {
286 value = value.substring(2);
287 index = getHIndex(next, value, index);
288 }
289 return index;
290 }
291 }
292 return index;
293 }
294
295 private Vector traverseTree(GValueNode node) {
296 Vector contents = new Vector();
297 contents.add(node);
298 for(int i = 0; i < node.getChildCount(); i++) {
299 contents.addAll(traverseTree((GValueNode)node.getChildAt(i)));
300 }
301 return contents;
302 }
303}
Note: See TracBrowser for help on using the repository browser.