[4293] | 1 | package 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 | */
|
---|
| 38 | import java.util.*;
|
---|
| 39 | import javax.swing.tree.*;
|
---|
| 40 | import org.greenstone.gatherer.Gatherer;
|
---|
| 41 | import org.greenstone.gatherer.msm.ElementWrapper;
|
---|
| 42 | import org.greenstone.gatherer.msm.MetadataSetManager;
|
---|
| 43 | import org.greenstone.gatherer.msm.MSMUtils;
|
---|
| 44 | import org.greenstone.gatherer.util.SynchronizedTreeModelTools;
|
---|
| 45 | import org.greenstone.gatherer.util.Utility;
|
---|
| 46 | import org.w3c.dom.*;
|
---|
| 47 | /*
|
---|
| 48 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 49 | * @version 2.1
|
---|
| 50 | */
|
---|
| 51 | public class GValueModel
|
---|
[4364] | 52 | extends DefaultTreeModel {
|
---|
[4293] | 53 |
|
---|
[4364] | 54 | private ElementWrapper element;
|
---|
[4293] | 55 |
|
---|
[4364] | 56 | public GValueModel() {
|
---|
| 57 | super(new DefaultMutableTreeNode("Temp"));
|
---|
| 58 | }
|
---|
[4293] | 59 |
|
---|
[4364] | 60 | public GValueModel(String root) {
|
---|
| 61 | super(new DefaultMutableTreeNode(root));
|
---|
| 62 | }
|
---|
[4293] | 63 |
|
---|
[4364] | 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 | }
|
---|
[4293] | 73 |
|
---|
[4364] | 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 | }
|
---|
[4293] | 81 |
|
---|
[4364] | 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);
|
---|
[4293] | 90 | ///ystem.err.println("Found or created a node " + token);
|
---|
[4364] | 91 | }
|
---|
| 92 | return subject;
|
---|
| 93 | }
|
---|
[4293] | 94 |
|
---|
[4364] | 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) {
|
---|
[4293] | 99 | // Now add the new value.
|
---|
[4364] | 100 | Document document = subject.getElement().getOwnerDocument();
|
---|
[4293] | 101 | // Now we create a new subject and add it subject
|
---|
[4364] | 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);
|
---|
[4293] | 114 | // Figure out where this node will be inserted in subjects
|
---|
| 115 | // children.
|
---|
[4364] | 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 | }
|
---|
[4293] | 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);
|
---|
[4364] | 130 | insertNodeInto(value_node, subject, position);
|
---|
[4293] | 131 | // Inform listeners that we've changed.
|
---|
[4364] | 132 | Gatherer.c_man.getCollection().msm.fireValueChanged(element, null, this);
|
---|
| 133 | }
|
---|
| 134 | return value_node;
|
---|
| 135 | }
|
---|
[4293] | 136 |
|
---|
[4364] | 137 | public Document getDocument() {
|
---|
| 138 | return ((GValueNode)root).getElement().getOwnerDocument();
|
---|
| 139 | }
|
---|
[4293] | 140 |
|
---|
[4364] | 141 | public ElementWrapper getElement() {
|
---|
| 142 | return element;
|
---|
| 143 | }
|
---|
[4293] | 144 |
|
---|
[4364] | 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 | }
|
---|
[4293] | 153 |
|
---|
[4364] | 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)) {
|
---|
[4293] | 161 | // StringTokenize the index
|
---|
[4364] | 162 | StringTokenizer tokenizer = new StringTokenizer(index_str, ".");
|
---|
| 163 | result = (GValueNode) root;
|
---|
[4293] | 164 | // Using the index numbers retrieve the appropriate node.
|
---|
[4364] | 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 | }
|
---|
[4293] | 179 | // Most likely caused by parseInt throwing a wobbly.
|
---|
[4364] | 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) {
|
---|
[4293] | 189 | ///ystem.err.println("No existing value. Adding " + index_str);
|
---|
[4364] | 190 | result = addValue(index_str);
|
---|
| 191 | }
|
---|
| 192 | return result;
|
---|
| 193 | }
|
---|
[4293] | 194 |
|
---|
[4364] | 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 | }
|
---|
[4293] | 210 |
|
---|
[4364] | 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 | }
|
---|
[4293] | 216 |
|
---|
[4364] | 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 | }
|
---|
[4293] | 223 |
|
---|
[4364] | 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 | }
|
---|
[4293] | 233 |
|
---|
[4364] | 234 | public int size() {
|
---|
| 235 | return size(root);
|
---|
| 236 | }
|
---|
[4293] | 237 |
|
---|
[4364] | 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 | }
|
---|
[4293] | 245 |
|
---|
[4364] | 246 | public String toString() {
|
---|
| 247 | return element.toString();
|
---|
| 248 | }
|
---|
[4293] | 249 |
|
---|
[4364] | 250 | public Vector traverseTree() {
|
---|
| 251 | Vector contents = new Vector();
|
---|
| 252 | contents.addAll(traverseTree((GValueNode) root));
|
---|
| 253 | contents.remove((GValueNode) root);
|
---|
| 254 | return contents;
|
---|
| 255 | }
|
---|
[4293] | 256 |
|
---|
[4364] | 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 | }
|
---|
[4293] | 266 |
|
---|
[4364] | 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 | }
|
---|
[4293] | 279 |
|
---|
[4364] | 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 | }
|
---|
[4293] | 293 |
|
---|
[4364] | 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 | }
|
---|
[4293] | 315 |
|
---|
[4364] | 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 | }
|
---|
[4293] | 324 | }
|
---|