source: trunk/gli/src/org/greenstone/gatherer/gems/GValueModel.java@ 8270

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

Source files for the Greenstone Editor for Metadata Sets (GEMS). This is currently just the old MetadataEditorManager, modified to run stand-alone. It will be substantially improved by Attila Aros.

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