- Timestamp:
- 2003-05-27T15:40:47+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/valuetree/GValueModel.java
r4319 r4364 50 50 */ 51 51 public class GValueModel 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 90 ///ystem.err.println("Found or created a node " + token); 91 92 93 94 95 96 97 98 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 99 // Now add the new value. 100 100 Document document = subject.getElement().getOwnerDocument(); 101 101 // Now we create a new subject and add it subject 102 103 104 105 106 107 108 109 110 111 112 113 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 114 // Figure out where this node will be inserted in subjects 115 115 // children. 116 117 118 119 120 121 122 123 124 125 126 127 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 128 // Insert it. If position is still -1, append it to the end of subjects children. 129 129 ///ystem.err.println("Inserting '" + value + "' at position " + position); 130 130 insertNodeInto(value_node, subject, position); 131 131 // Inform listeners that we've changed. 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 161 // StringTokenize the index 162 163 162 StringTokenizer tokenizer = new StringTokenizer(index_str, "."); 163 result = (GValueNode) root; 164 164 // Using the index numbers retrieve the appropriate node. 165 166 167 168 169 170 171 172 173 174 175 176 177 178 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 179 // Most likely caused by parseInt throwing a wobbly. 180 181 182 183 184 185 186 187 188 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 189 ///ystem.err.println("No existing value. Adding " + index_str); 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 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 324 } 325 326
Note:
See TracChangeset
for help on using the changeset viewer.