Changeset 4364 for trunk/gli/src/org/greenstone/gatherer/valuetree
- Timestamp:
- 2003-05-27T15:40:47+12:00 (21 years ago)
- Location:
- trunk/gli/src/org/greenstone/gatherer/valuetree
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/valuetree/GNode.java
r4293 r4364 48 48 49 49 public class GNode 50 50 extends DefaultMutableTreeNode { 51 51 52 52 private String name = null; 53 53 54 55 56 54 public GNode(String name) { 55 this.name = name; 56 } 57 57 58 59 60 58 public String toString() { 59 return name; 60 } 61 61 } -
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 -
trunk/gli/src/org/greenstone/gatherer/valuetree/GValueNode.java
r4293 r4364 72 72 */ 73 73 public class GValueNode 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 74 extends DefaultMutableTreeNode { 75 private String element_name = null; 76 private String default_value = null; 77 public GValueNode(Element element) { 78 this.children = null; 79 this.userObject = element; 80 this.default_value = null; 81 } 82 83 public GValueNode(String element_name, String default_value) { 84 this.element_name = element_name; 85 this.default_value = default_value; 86 } 87 /** Compares two GValueNodes for ordering by using the String.compareTo method. 88 * @param sibling The <strong>Object</strong> representing the GValueNode we are comparing ourselves to. 89 * @return An <i>int</i> indicating the ordering. The value is less than zero if we are before the parameter object, zero if we are equal and greater than zero if we preceed sibling. 90 */ 91 public int compareTo(Object sibling) { 92 return toString().compareTo(sibling.toString()); 93 } 94 /** Determine if this tree node contains a child with a matching value. 95 * @param value The value we are attempting to match, as a <strong>String</strong>. 96 * @return <i>true</i> if there is a matching child node, <i>false</i> otherwise. 97 */ 98 public boolean containsValue(String value) { 99 if(default_value != null) { 100 return false; 101 } 102 return getValue(value) != null; 103 } 104 /** Returns an enumeration of the child nodes. 105 * @return An <strong>Enumeration</strong> containing the child nodes. 106 */ 107 public Enumeration children() { 108 if(default_value != null) { 109 return null; 110 } 111 if(children == null) { 112 map(); 113 } 114 return children.elements(); 115 } 116 public boolean equals(Object other) { 117 return compareTo(other) == 0; 118 } 119 120 public GValueNode get(int index) { 121 GValueNode result = null; 122 if(children != null) { 123 result = (GValueNode) children.get(index); 124 } 125 return result; 126 } 127 128 public String getAlias() { 129 // Attempt to retrieve a child node named Alias 130 Element element = (Element) userObject; 131 for(Node pos_node = element.getFirstChild(); pos_node != null; pos_node = pos_node.getNextSibling()) { 132 132 // And if we find such a node 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 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 133 if(pos_node.getNodeName().equals("Alias")) { 134 // Retrieve its text node 135 for(Node pos_text = pos_node.getFirstChild(); pos_text != null; pos_text = pos_text.getNextSibling()) { 136 // When we find the node return its value 137 if(pos_text.getNodeName().equals("#text")) { 138 return pos_text.getNodeValue(); 139 } 140 } 141 } 142 } 143 return ""; 144 } 145 146 public String getAlias(String index) { 147 return index; 148 } 149 150 /** Returns <I>true</I> if the reciever allows children. */ 151 public boolean getAllowsChildren() { 152 return true; 153 } 154 155 /** Returns the child <I>TreeNode</I> at index <I>childIndex</I>.*/ 156 public TreeNode getChildAt(int index) { 157 GValueNode result = null; 158 if(default_value != null) { 159 return this; 160 } 161 else { 162 if(children == null) { 163 map(); 164 } 165 for(int i = 0; index >= 0 && i < children.size(); i++) { 166 GValueNode child = (GValueNode) children.get(i); 167 if(child.getEnabled()) { 168 result = child; 169 index--; 170 } 171 } 172 } 173 return result; 174 } 175 /** Returns the number of children <I>TreeNode</I>s the reciever contains. */ 176 public int getChildCount() { 177 int size = 0; 178 if(default_value != null) { 179 size = 0; 180 } 181 else { 182 if(children == null) { 183 map(); 184 } 185 for(int i = 0; i < children.size(); i++) { 186 GValueNode child = (GValueNode) children.get(i); 187 if(child.getEnabled()) { 188 size++; 189 } 190 } 191 } 192 return size; 193 } 194 public Element getElement() { 195 return (Element) userObject; 196 } 197 public boolean getEnabled() { 198 return !(((Element)userObject).getAttribute("enabled").equals("false")); 199 } 200 201 /** Return the path to this node within the value tree model. 202 * @return A <strong>String</strong> representing the path, ie "Titles\Modern\The Green Mile" 203 */ 204 public String getFullPath() { 205 if(default_value != null) { 206 return default_value; 207 } 208 StringBuffer path = new StringBuffer(toString()); 209 GValueNode node = (GValueNode) getParent(); 210 while(node != null && !node.getElement().getNodeName().equalsIgnoreCase("AssignedValues")) { 211 path.insert(0, "\\"); 212 path.insert(0, node.toString()); 213 node = (GValueNode) node.getParent(); 214 } 215 return path.toString(); 216 } 217 218 /** Returns the index of <I>node</I> in the recievers children. If the 219 * reciever does not contain <I>node</I>, <I>-1</I> will be returned. */ 220 public int getIndex(TreeNode node) { 221 if(default_value != null) { 222 return 0; 223 } 224 if(children == null) { 225 map(); 226 } 227 return children.indexOf(node); 228 } 229 230 public String getMetadataElementName() { 231 if(default_value != null) { 232 return element_name; 233 } 234 GValueNode node = this; 235 while(node != null && !node.getElement().getNodeName().equalsIgnoreCase("AssignedValues")) { 236 node = (GValueNode)node.getParent(); 237 } 238 if(node != null) { 239 return node.getElement().getAttribute("element"); 240 } 241 return null; 242 } 243 244 public GValueNode getValue(String value) { 245 if(default_value != null) { 246 return this; 247 } 248 if(children == null) { 249 map(); 250 } 251 GValueNode result = null; 252 for(int i = 0; result == null && i < children.size(); i++) { 253 Object child = children.get(i); 254 if(child.toString().equals(value)) { 255 result = (GValueNode) child; 256 } 257 } 258 return result; 259 } 260 261 /** Adds <I>child</I> to the receiever at <I>index</I>. <I>child</I> will 262 * be messaged with setParent(). 263 */ 264 public void insert(MutableTreeNode child, int index) { 265 if(default_value != null) { 266 return; 267 } 268 if(index >= children.size()) { 269 269 // Append to children. 270 270 children.add(child); 271 271 // And to document 272 273 274 275 272 getElement().appendChild(((GValueNode)child).getElement()); 273 } 274 else { 275 GValueNode sibling = (GValueNode) children.get(index); 276 276 // Insert in children 277 277 children.add(index, child); 278 278 // And in document 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 279 getElement().insertBefore(((GValueNode)child).getElement(), sibling.getElement()); 280 } 281 child.setParent(this); 282 } 283 /** Returns <I>true</I> if the reciever is a leaf. */ 284 public boolean isLeaf() { 285 if(default_value != null || getChildCount() > 0) { 286 return false; 287 } 288 return true; 289 } 290 291 /** Ensure that the children nodes of this tree are instantiated, and record the initial mappings for legacy sake. 292 * @param mapping A <strong>HashMap</strong> into which to write the initial mappings. 293 * @param prefix The prefix to use for indexes, as a <strong>String</strong>. 294 */ 295 public void map(HashMap mapping, String prefix) { 296 if(default_value != null) { 297 return; 298 } 299 children = new Vector(); 300 if(prefix.length() > 0) { 301 prefix = prefix + "."; 302 } 303 int i = 1; 304 for(Node node = getElement().getFirstChild(); node != null; node = node.getNextSibling()) { 305 if(node.getNodeName().equals("Subject")) { 306 GValueNode child = new GValueNode((Element)node); 307 child.setParent(this); 308 children.add(child); 309 String index = prefix + i; 310 mapping.put(index, child); 311 child.map(mapping, index); 312 i++; 313 } 314 } 315 } 316 317 /** Removes the child at <I>index</I> from the reciever. */ 318 public void remove(int index) { 319 // Can't ever remove Elements from here. Have to use editor. */ 320 } 321 322 /** Removes <I>node</I> from the reciever. <I>setParent</I> will be 323 * messaged on node. 324 */ 325 public void remove(MutableTreeNode node) { 326 // Can't ever remove Elements from here. Have to use editor. */ 327 } 328 329 /** Removes the reciever from its parent. */ 330 public void removeFromParent() { 331 // Can't ever remove Elements from here. Have to use editor. */ 332 } 333 334 public void setAlias(String alias) { 335 setNodeValue("Alias", alias); 336 } 337 338 public void setEnabled(boolean state) { 339 Element element = (Element) userObject; 340 if(state) { 341 element.setAttribute("enabled", "true"); 342 } 343 else { 344 element.setAttribute("enabled", "false"); 345 } 346 } 347 348 /** Resets the user object of the reciever to <I>object</I>. */ 349 public void setUserObject(Object object) { 350 // Can't ever change Elements from here. Have to use editor. */ 351 } 352 353 public void setValue(String value) { 354 setNodeValue("Value", value); 355 } 356 357 public int size() { 358 int size = 0; 359 if(children != null) { 360 size = children.size(); 361 } 362 return size; 363 } 364 365 /** Returns a <I>String</I> representation of the reciever. If this 366 * happens to be the AssignedValues 'root' then we do something slightly 367 * different, otherwise we return the value of the <I>#Text</I> child of 368 * the <I>Value</I> of this <I>Subject</I> node! 369 */ 370 public String toString() { 371 if(default_value != null) { 372 return default_value; 373 } 374 Element element = getElement(); 375 String name = element.getNodeName(); 376 String result = null; 377 if(name.equals("Subject")) { 378 result = MSMUtils.getValue(element); 379 } 380 else if(name.equals("AssignedValues")) { 381 result = getMetadataElementName(); 382 } 383 return Utility.stripNL(result); 384 } 385 386 public void unmap() { 387 children = null; 388 } 389 390 private void map() { 391 children = new Vector(); 392 for(Node node = getElement().getFirstChild(); node != null; node = node.getNextSibling()) { 393 if(node.getNodeName().equals("Subject")) { 394 GValueNode child = new GValueNode((Element)node); 395 child.setParent(this); 396 children.add(child); 397 } 398 } 399 } 400 401 private void setNodeValue(String name, String value) { 402 boolean found = false; 403 // Attempt to retrieve a child node named name 404 Element element = (Element) userObject; 405 for(Node pos_node = element.getFirstChild(); pos_node != null; pos_node = pos_node.getNextSibling()) { 406 406 // And if we find such a node 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 407 if(pos_node.getNodeName().equals(name)) { 408 // And the new value is non-null, retrieve its text node and change it. 409 if(value != null) { 410 for(Node pos_text = pos_node.getFirstChild(); pos_text != null; pos_text = pos_text.getNextSibling()) { 411 // When we find the node... 412 if(pos_text.getNodeName().equals("#text")) { 413 pos_text.setNodeValue(value); 414 } 415 } 416 } 417 // Otherwise remove the node altogether 418 else { 419 element.removeChild(pos_node); 420 } 421 found = true; 422 } 423 } 424 // Otherwise if no such node exists add it. 425 if(!found && value != null) { 426 Document document = element.getOwnerDocument(); 427 Node new_node = document.createElementNS("", name); 428 element.appendChild(new_node); 429 Node new_text = document.createTextNode(value); 430 new_node.appendChild(new_text); 431 new_text = null; 432 new_node = null; 433 } 434 // Done. 435 element = null; 436 return; 437 } 438 438 } 439 440 441 442 -
trunk/gli/src/org/greenstone/gatherer/valuetree/GValueTree.java
r4354 r4364 61 61 */ 62 62 public class GValueTree 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 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 63 extends JPanel 64 implements ActionListener, FocusListener, MSMListener, TreeSelectionListener { 65 /** Is the metadata currently shown in the edit controls common to all records selected. */ 66 private boolean common = false; 67 /** Certain actions are disabled if the metadata selected isn't at file level. */ 68 private boolean file_level = false; 69 private boolean ignore = false; 70 private boolean ignore_item_change = false; 71 private CardLayout card_layout; 72 private DefaultListModel list_model; 73 /** The metadata element that is currently selected. */ 74 private ElementWrapper selected_metadata_element = null; 75 //private GComboBox elements; 76 private MetadataComboBoxModel em; 77 private GValueModel vm; 78 private Hashtable previous_values = new Hashtable(); 79 private JButton add; 80 /** This button creates an editor dialog to allow for an expanded area to type in text. */ 81 private JButton expand = null; 82 private JButton remove; 83 private JButton restore; 84 private JButton search; 85 private JButton update; 86 //private JLabel element_label; 87 private JLabel value_label; 88 private JList list; 89 private JPanel auto_pane; 90 private JPanel button_pane; 91 private JPanel center_pane; 92 //private JPanel list_pane; 93 private JPanel north_pane; 94 //private JPanel south_pane; 95 private JPanel tree_pane; 96 private JPanel value_pane; 97 private JTextArea auto_message; 98 private JTextField value; 99 private JTree tree; 100 private MetaEditPane metaedit; 101 private String args[] = new String[1]; 102 private TreePath previous; 103 private ValueListener value_listener; 104 /** Stock standard size for labels. */ 105 static final private Dimension LABEL_SIZE = new Dimension(132, 26); 106 107 //static final private String LIST = "List"; 108 static final private String NONE = "None"; 109 static final private String TREE = "Tree"; 110 111 112 public GValueTree(MetaEditPane metaedit, int width, int height, JButton add, JButton update, JButton remove) { 113 this(metaedit, new Dimension(width, height), add, update, remove); 114 } 115 116 public GValueTree(MetaEditPane metaedit, Dimension size, JButton add, JButton update, JButton remove) { 117 super(); 118 119 this.add = add; 120 this.metaedit = metaedit; 121 this.remove = remove; 122 this.update = update; 123 124 ///ystem.err.println("Created new ValueListener!"); 125 value_listener = new ValueListener(); 126 127 int height = size.height; 128 int width = size.width; 129 130 setFont(Gatherer.config.getFont("general.font", false)); 131 //setSize(size); 132 setPreferredSize(size); 133 134 //north_pane = new JPanel(); 135 136 //element_label = new JLabel(get("Element")); 137 //element_label.setPreferredSize(LABEL_SIZE); 138 139 //elements = new GComboBox(); 140 //elements.addItemListener(this); 141 //elements.setEditable(false); 142 //elements.setMaximumRowCount(5); 143 //elements.setPreferredSize(new Dimension(413, 24)); 144 145 center_pane = new JPanel(); 146 147 card_layout = new CardLayout(); 148 149 auto_pane = new JPanel(); 150 args[0] = "Title"; 151 auto_message = new SmarterTextArea(get("AutoMessage", args), false); 152 auto_message.setLineWrap(true); 153 auto_message.setOpaque(false); 154 auto_message.setWrapStyleWord(true); 155 156 tree_pane = new JPanel(); 157 158 JLabel tree_label = new JLabel(get("Tree")); 159 160 tree = new JTree(new GValueModel()); 161 tree.addTreeSelectionListener(this); 162 tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 163 tree.setRootVisible(true); 164 tree.putClientProperty("JTree.lineStyle", "Angled"); 165 //setTreeEnabled(false); 166 167 //list_pane = new JPanel(); 168 //JLabel list_label = new JLabel(get("List")); 169 //list_model = new DefaultListModel(); 170 //list = new JList(list_model); 171 //list.addListSelectionListener(new ListSelectionListenerImpl()); 172 173 JPanel controls_pane = new JPanel(); 174 175 value_pane = new JPanel(); 176 value_label = new JLabel(get("Value")); 177 value_label.setPreferredSize(LABEL_SIZE); 178 JPanel edit_pane = new JPanel(); 179 expand = new JButton(get("General.Expand")); 180 expand.setMnemonic(KeyEvent.VK_E); 181 expand.setPreferredSize(MetaEditPane.BUTTON_SIZE); 182 183 value = new JTextField(); 184 value.addActionListener(this); 185 value.setBackground(Gatherer.config.getColor("coloring.editable", false)); 186 value.addFocusListener(this); 187 //value.addKeyListener(this); 188 value.setPreferredSize(new Dimension(413, 24)); 189 190 button_pane = new JPanel(); 191 192 JPanel inner_button_pane = new JPanel(); 193 194 //restore = new JButton(get("Restore")); 195 //restore.setEnabled(false); 196 //restore.setMnemonic(KeyEvent.VK_T); 197 198 //search = new JButton(get("Search")); 199 //search.setMnemonic(KeyEvent.VK_S); 200 201 // Connection 202 expand.addActionListener(this); 203 //restore.addActionListener(this); 204 //search.addActionListener(this); 205 value.getDocument().addDocumentListener(new DocumentListenerImpl()); 206 207 // Layout 208 //element_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5)); 209 //north_pane.setLayout(new BorderLayout()); 210 //north_pane.add(element_label, BorderLayout.CENTER); 211 //north_pane.add(elements, BorderLayout.EAST); 212 213 tree_pane.setLayout(new BorderLayout()); 214 tree_pane.add(tree_label, BorderLayout.NORTH); 215 tree_pane.add(new JScrollPane(tree), BorderLayout.CENTER); 216 217 //list_pane.setLayout(new BorderLayout()); 218 //list_pane.add(list_label, BorderLayout.NORTH); 219 //list_pane.add(new JScrollPane(list), BorderLayout.CENTER); 220 221 auto_pane.setBorder(BorderFactory.createEmptyBorder(25,10,25,10)); 222 auto_pane.setLayout(new BorderLayout()); 223 auto_pane.add(auto_message, BorderLayout.CENTER); 224 225 center_pane.setBorder(BorderFactory.createEmptyBorder(2,0,2,0)); 226 center_pane.setLayout(card_layout); 227 center_pane.add(tree_pane, TREE); 228 //center_pane.add(list_pane, LIST); 229 center_pane.add(auto_pane, NONE); 230 231 value_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5)); 232 233 inner_button_pane.setLayout(new GridLayout(3,1,0,0)); 234 inner_button_pane.add(add); 235 inner_button_pane.add(update); 236 inner_button_pane.add(remove); 237 238 edit_pane.setLayout(new BorderLayout()); 239 edit_pane.add(expand, BorderLayout.NORTH); 240 241 button_pane.setLayout(new BorderLayout()); 242 button_pane.add(inner_button_pane, BorderLayout.WEST); 243 button_pane.add(edit_pane, BorderLayout.EAST); 244 245 value_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0)); 246 value_pane.setLayout(new BorderLayout()); 247 value_pane.add(value_label, BorderLayout.CENTER); 248 value_pane.add(value, BorderLayout.EAST); 249 value_pane.add(button_pane, BorderLayout.SOUTH); 250 251 controls_pane.setLayout(new BorderLayout()); 252 controls_pane.add(value_pane, BorderLayout.CENTER); 253 //controls_pane.add(button_pane, BorderLayout.SOUTH); 254 255 this.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 256 this.setLayout(new BorderLayout()); 257 this.add(controls_pane, BorderLayout.NORTH); 258 this.add(center_pane, BorderLayout.CENTER); 259 //this.add(south_pane, BorderLayout.SOUTH); 260 } 261 262 public void actionPerformed(ActionEvent event) { 263 ignore = true; 264 if(event.getSource() == value) { 265 StringTokenizer tokenizer = new StringTokenizer(value.getText(), "\\"); 266 TreePath closest = getClosestPath(tokenizer); 267 if(closest != null) { 268 TreePath path = tree.getSelectionPath(); 269 GValueNode node = (GValueNode)path.getLastPathComponent(); 270 ///ystem.err.println("1. Set value: " + node.toString()); 271 value.setText(Utility.stripNL(node.toString()));//formatPath(path, false))); 272 value.setCaretPosition(0); 273 // Now systematically add to selected record. 274 add.doClick(); 275 } 276 else { 277 int response = JOptionPane.showConfirmDialog(this, "Do you wish to create a new entry?", "Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); 278 if(response == JOptionPane.YES_OPTION) { 279 TreePath new_path = addNode(value.getText()); 280 tree.setSelectionPath(new_path); 281 // tree.scrollPathToVisible(new_path); 282 scrollTreePathToVisible(new_path); 283 GValueNode node = (GValueNode)new_path.getLastPathComponent(); 284 ///ystem.err.println("2. Set value: " + node.toString()); 285 value.setText(Utility.stripNL(node.toString())); 286 value.setCaretPosition(0); 287 // Now systematically add to selected record. 288 add.doClick(); 289 } 290 } 291 } 292 //else if(event.getSource() == search) { 293 // searchValues(); 294 // restore.setEnabled(true); 295 // card_layout.show(center_pane, LIST); 296 //} 297 //else if(event.getSource() == restore) { 298 // restore.setEnabled(false); 299 // card_layout.show(center_pane, TREE); 300 //} 301 else if(event.getSource() == expand) { 302 EditorDialog ed = new EditorDialog(); 303 String temp = ed.display(value.getText()); 304 if(temp != null) { 305 ///ystem.err.println("3. Set value: " + temp); 306 value.setText(temp); 307 value.setCaretPosition(0); 308 } 309 //validateControls(); 310 } 311 ignore = false; 312 } 313 /** Called whenever an element within the metadata set module changes in some way. 314 314 * @param event A <strong>MSMEvent</strong> which contains information about the change. 315 315 */ 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 316 public void elementChanged(MSMEvent event) { 317 /* @TODO - Not sure. Figure out. */ 318 } 319 public void focusGained(FocusEvent event) { 320 321 } 322 323 public void focusLost(FocusEvent event) { 324 325 } 326 327 public boolean getCommon() { 328 return common; 329 } 330 331 public ElementWrapper getSelectedMetadataElement() { 332 return selected_metadata_element; 333 } 334 335 public String getSelectedValue() { 336 String value_text = value.getText(); 337 previous_values.put(selected_metadata_element, value_text); 338 return value_text; 339 } 340 341 public GValueModel getValueModel() { 342 return (GValueModel) tree.getModel(); 343 } 344 345 /** This method is required for any ItemListener so that it can be informed when the state of an item set changes (such as when a new selection is made in a combobox). Note that this update is subtly different from the validateControls() method as it also gathers new models for each combobox as required. The reason this isn't done as part of validateControls() is that this would continously reset whatever the user is attempting to type (ie value would be found to be enabled and a new model would be loaded erasing whatever the user just typed in value to fire the validateControls() in the first place!) 346 346 * @param event An ItemEvent encapsulating all the information about this event. 347 347 */ … … 384 384 * @param event A <strong>MSMEvent</strong> which encapsulates relevant data about the change. 385 385 */ 386 387 388 386 public void metadataChanged(MSMEvent event) { 387 // Don't care. 388 } 389 389 390 390 public void newMetadataSetManager() { 391 392 393 394 395 } 396 397 398 399 400 401 391 if(Gatherer.c_man != null && Gatherer.c_man.getCollection() != null && Gatherer.c_man.getCollection().msm != null) { 392 Gatherer.c_man.getCollection().msm.addMSMListener(this); 393 } 394 setChanged(null); 395 } 396 397 public void refreshValueTree() { 398 if(em != null && em.getSize() > 0) { 399 setSelectedMetadataElement((ElementWrapper)em.getSelectedItem()); 400 } 401 } 402 402 403 403 public void registerModel(GValueModel model) { 404 405 406 407 408 } 409 404 vm = model; 405 vm.addTreeModelListener(value_listener); 406 tree.setModel(model); 407 tree.addTreeSelectionListener(this); 408 } 409 /** Called whenever a set within the metadata set module changes in some way. 410 410 * @param event A <strong>MSMEvent</strong> which contains information about the change. 411 411 */ 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 412 public void setChanged(MSMEvent event) { 413 // Update the element model. 414 MetadataComboBoxModel model = Gatherer.c_man.getCollection().msm.getElementModel(); 415 if(model.getSize() > 0) { 416 setElementModel(model); 417 model.setSelectedItem(model.getElementAt(0)); 418 vm = Gatherer.c_man.getCollection().msm.getValueTree((ElementWrapper)model.getSelectedItem()); 419 setTreeEnabled(true); 420 if(vm == null) { 421 vm = new GValueModel(get("Not_Applicable")); 422 setTreeEnabled(false); 423 } 424 } 425 else { 426 vm = new GValueModel(); 427 setTreeEnabled(true); 428 } 429 vm.addTreeModelListener(value_listener); 430 tree.setModel(vm); 431 tree.addTreeSelectionListener(this); 432 } 433 434 public void setCommon(boolean common) { 435 this.common = common; 436 } 437 437 438 438 public void setElementModel(MetadataComboBoxModel model) { 439 440 441 442 443 444 445 446 447 448 449 439 ///ystem.err.println("Reset ElementModel."); 440 setTreeEnabled(true); 441 Gatherer.c_man.getCollection().msm.addMSMListener(this); 442 if(model == null) { 443 Gatherer.println("model = null"); 444 System.exit(0); 445 } 446 ignore_item_change = true; 447 em = model; 448 //elements.setModel(em); 449 if(em.getSize() > 0) { 450 450 ///ystem.err.println("em.size() > 0"); 451 451 //elements.setEnabled(true); 452 452 //elements.setSelectedIndex(0); 453 453 selected_metadata_element = (ElementWrapper)em.getSelectedItem(); 454 454 //elements.setToolTipText(selected_metadata_element.getToolTip()); 455 455 // First we create and install a blank model, or else no updates ever work. 456 457 456 vm = new GValueModel(get("Not_Applicable")); 457 tree.setModel(vm); 458 458 // Now we create a new model if applicable, disabling otherwise. 459 460 461 462 463 464 465 466 467 468 469 459 if(selected_metadata_element.getNamespace().length() > 0) { 460 vm = Gatherer.c_man.getCollection().msm.getValueTree(selected_metadata_element); 461 vm.addTreeModelListener(value_listener); 462 ///ystem.err.println("Found existing model containing " + vm.size() + " entries"); 463 setTreeEnabled(true); 464 tree.setModel(vm); // This doesn't seem to work 465 } 466 else { 467 ///ystem.err.println("Creating new model."); 468 setTreeEnabled(false); 469 } 470 470 ///ystem.err.println("The value tree should have been updated by now."); 471 471 ///ystem.err.println("6. Set value: " + getLastValue()); 472 473 474 475 472 value.setText(Utility.stripNL(getLastValue())); 473 value.setCaretPosition(0); 474 } 475 else { 476 476 ///ystem.err.println("No elements loaded."); 477 477 //elements.setEnabled(false); 478 478 //elements.setToolTipText(get("No_Description")); 479 479 //setTreeEnabled(false); 480 481 482 483 484 485 } 486 487 488 489 490 491 492 493 480 value.setText(""); 481 } 482 previous = null; 483 ignore_item_change = false; 484 validateControls(); 485 } 486 487 public void setFileLevel(boolean file_level) { 488 this.file_level = file_level; 489 } 490 491 public void setRootVisible(boolean val) { 492 tree.setRootVisible(val); 493 } 494 494 495 495 public void setSelectedMetadataElement(ElementWrapper element) { 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 } 527 528 529 530 531 532 533 534 535 536 537 538 539 496 if(!ignore_item_change) { 497 ignore_item_change = true; 498 if(element != null) { 499 selected_metadata_element = element; 500 //elements.setSelectedItem(element); 501 //elements.setToolTipText(element.getToolTip()); 502 vm = Gatherer.c_man.getCollection().msm.getValueTree(element); 503 if(vm != null) { 504 vm.addTreeModelListener(value_listener); 505 setTreeEnabled(true); 506 } 507 else { 508 vm = new GValueModel(get("Not_Applicable")); 509 setTreeEnabled(false); 510 } 511 tree.setModel(vm); 512 ///ystem.err.println("7. Set value: " + getLastValue()); 513 //value.setText(Utility.stripNL(getLastValue())); 514 //value.setCaretPosition(0); 515 } 516 else { 517 //elements.setToolTipText(get("No_Description")); 518 setTreeEnabled(false); 519 //value.setText(""); 520 } 521 metaedit.validateControls(false); 522 } 523 previous = null; 524 ignore_item_change = false; 525 //validateControls(); 526 } 527 528 public void setSelectedValue(String val) { 529 ignore = true; 530 value.setText(Utility.stripNL(val)); 531 value.setCaretPosition(0); 532 StringTokenizer tokenizer = new StringTokenizer(val, "\\"); 533 TreePath closest = getClosestPath(tokenizer); 534 if(previous != null) { 535 tree.collapsePath(getParent(previous)); 536 previous = null; 537 } 538 if(closest != null) { 539 tree.setSelectionPath(closest); 540 540 // tree.scrollPathToVisible(closest); 541 542 543 544 545 546 547 541 scrollTreePathToVisible(closest); 542 } 543 ignore = false; 544 //validateControls(); 545 } 546 547 /** This method enables or diables the various controls based upon the 548 548 * state of the system. This method should be called after every change 549 549 * which could affect the GUI. <br> 550 550 * Hack #1: For some reason this gets called 14 times for one selection. One day I'll figure out why, but for now I'll just ignore all but every fourteenth call. 551 551 */ 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 552 public void validateControls() { 553 Metadata selected_metadata = metaedit.getSelectedMetadata(); 554 if(metaedit.getSelectedNode() != null && selected_metadata != null) { 555 ElementWrapper element = selected_metadata.getElement(); 556 if(element.getNamespace() != "") { 557 GValueNode value_node = selected_metadata.getValueNode(); 558 ///ystem.err.println("Value node = " + value_node); 559 // We may be dealing with an element that has no current value 560 if(value_node == null) { 561 String new_value = value.getText(); 562 if(new_value.length() > 0) { 563 add.setEnabled(true); 564 } 565 else { 566 add.setEnabled(false); 567 } 568 // Nothing to update 569 update.setEnabled(false); 570 // Nothing to remove 571 remove.setEnabled(false); 572 } 573 // Or else we have some current value that may or may not match the one in our value field. 574 else { 575 String current_value = selected_metadata.getValueNode().getFullPath(); 576 String new_value = value.getText(); 577 ///ystem.err.println("Current value: '" + current_value + "'"); 578 ///ystem.err.println("New value: '" + new_value + "'"); 579 if(new_value.length() > 0 && !current_value.equals(new_value)) { 580 add.setEnabled(true); 581 if(common) { 582 update.setEnabled(true); 583 } 584 else { 585 update.setEnabled(false); 586 } 587 } 588 else { 589 add.setEnabled(false); 590 update.setEnabled(false); 591 } 592 // If you have something selected, and we're at the file level, you can remove it. 593 if(file_level) { 594 remove.setEnabled(true); 595 } 596 else { 597 remove.setEnabled(false); 598 } 599 } 600 } 601 else { 602 add.setEnabled(false); 603 update.setEnabled(false); 604 remove.setEnabled(false); 605 } 606 } 607 else { 608 add.setEnabled(false); 609 update.setEnabled(false); 610 remove.setEnabled(false); 611 } 612 } 613 614 /** Called when the value tree of a certain element has changed significantly. If we are displaying the tree for the element noted in the event, then reload the value model. 615 615 * @param event A <strong>MSMEvent</strong> containing information about the event. 616 616 */ 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 617 public void valueChanged(MSMEvent event) { 618 ElementWrapper element = event.getElement(); 619 if(selected_metadata_element.equals(element)) { 620 GValueModel temp = Gatherer.c_man.getCollection().msm.getValueTree(element); 621 if(temp != null) { 622 vm = temp; 623 tree.setModel(vm); 624 vm.removeTreeModelListener(value_listener); 625 vm.addTreeModelListener(value_listener); 626 setTreeEnabled(true); 627 setSelectedValue(value.getText()); 628 } 629 else { 630 vm = new GValueModel(get("Not_Applicable")); 631 tree.setModel(vm); 632 setTreeEnabled(false); 633 } 634 } 635 } 636 636 637 637 public void valueChanged(TreeSelectionEvent event) { 638 639 640 641 642 643 644 638 if(tree.getSelectionCount() != 0 && !ignore) { 639 TreePath path = tree.getSelectionPath(); 640 GValueNode node = (GValueNode) path.getLastPathComponent(); 641 value.setText(node.getFullPath());//formatPath(tree.getSelectionPath(), false))); 642 value.setCaretPosition(0); 643 validateControls(); 644 } 645 645 } 646 646 647 647 private TreePath addNode(String reference) { 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 } 675 648 StringTokenizer tokenizer = new StringTokenizer(reference, "\\"); 649 GValueModel model = (GValueModel)tree.getModel(); 650 GValueNode current = (GValueNode)model.getRoot(); 651 while(tokenizer.hasMoreElements()) { 652 String token = tokenizer.nextToken(); 653 GValueNode node = (GValueNode)getClosestNode(token, current); 654 if(node == null) { 655 Gatherer.println("Before: '" + current.toString() + "'"); 656 for(int i = 0; i < current.getChildCount(); i++) { 657 Gatherer.println(current.getChildAt(i).toString()); 658 } 659 node = model.addValue(token, current, null); 660 Gatherer.println("After: '" + current.toString() + "'"); 661 for(int i = 0; i < current.getChildCount(); i++) { 662 Gatherer.println(current.getChildAt(i).toString()); 663 } 664 } 665 current = node; 666 } 667 tree.updateUI(); 668 if(current != null) { 669 return new TreePath(current.getPath()); 670 } 671 else { 672 return new TreePath(((GValueNode)model.getRoot()).getPath()); 673 } 674 } 675 /** Returns the given tree path as path formatted string (ie subject\subject\subject). 676 676 * @param tree The <strong>JTree</strong> the TreePath came from. Used to determine if the root node should be encoded as well. 677 677 * @param path A <strong>TreePath</strong> that you wish to encode to String. … … 681 681 * @see javax.swing.tree.TreePath 682 682 */ 683 static public String formatPath(JTree tree, TreePath path, boolean full) { 684 String text = ""; 685 int i = 0; 686 if(tree == null || (!tree.isRootVisible() && !full)) { 687 i = 1; 688 } 689 for( ; i < path.getPathCount(); i++) { 690 GValueNode node = (GValueNode)path.getPathComponent(i); 691 text = text + node.toString(); 692 if(node.getChildCount() > 0) { 693 text = text + "\\"; 694 } 695 } 696 if(full && text.endsWith("\\")) { 697 return text.substring(0, text.length() - 1); 698 } 699 return text; 700 } 701 702 private String get(String key) { 703 return get(key, null); 704 } 705 706 private String get(String key, String args[]) { 707 if(key.indexOf('.') == -1) { 708 key = "MetaEdit." + key; 709 } 710 return Gatherer.dictionary.get(key,args); 711 } 712 713 private GValueNode getClosestNode(String target, GValueNode current) { 714 // We have struck it lucky. 715 if(startsWith(current.toString(), target)) { 716 return current; 717 } 718 GValueNode closest = null; 719 for(int i = 0; i < current.getChildCount() && closest == null; i++) { 720 closest = getClosestNode(target, (GValueNode)current.getChildAt(i)); 721 } 722 return closest; 723 } 724 725 private TreePath getClosestPath(StringTokenizer tokenizer) { 726 if(tokenizer.countTokens() > 0) { 727 TreeNode tree_node = (TreeNode)tree.getModel().getRoot(); 728 if(tree_node instanceof GValueNode) { 729 GValueNode current = (GValueNode)tree_node; 730 TreePath closest = null; 731 while(tokenizer.hasMoreElements() && current != null) { 732 current = getClosestNode(tokenizer.nextToken(), current); 733 } 734 if(current != null) { 735 return new TreePath(current.getPath()); 736 } 737 return null; 738 } 739 } 740 return null; 741 } 742 743 private String getLastValue() { 744 if(previous_values.containsKey(selected_metadata_element)) { 745 return (String)previous_values.get(selected_metadata_element); 746 } 747 return ""; 748 } 749 750 private TreePath getParent(TreePath path) { 751 if(path.getPathCount() > 1) { 752 TreeNode nodes[] = new TreeNode[path.getPathCount() - 1]; 753 for(int i = 0; i < path.getPathCount() - 1; i++) { 754 nodes[i] = (TreeNode)path.getPathComponent(i); 755 } 756 return new TreePath(nodes); 683 static public String formatPath(JTree tree, TreePath path, boolean full) { 684 String text = ""; 685 int i = 0; 686 if(tree == null || (!tree.isRootVisible() && !full)) { 687 i = 1; 688 } 689 for( ; i < path.getPathCount(); i++) { 690 GValueNode node = (GValueNode)path.getPathComponent(i); 691 text = text + node.toString(); 692 if(node.getChildCount() > 0) { 693 text = text + "\\"; 694 } 695 } 696 if(full && text.endsWith("\\")) { 697 return text.substring(0, text.length() - 1); 698 } 699 return text; 700 } 701 702 private String get(String key) { 703 return get(key, null); 704 } 705 706 private String get(String key, String args[]) { 707 if(key.indexOf('.') == -1) { 708 key = "MetaEdit." + key; 709 } 710 return Gatherer.dictionary.get(key,args); 711 } 712 713 private GValueNode getClosestNode(String target, GValueNode current) { 714 // We have struck it lucky. 715 if(startsWith(current.toString(), target)) { 716 return current; 717 } 718 GValueNode closest = null; 719 for(int i = 0; i < current.getChildCount() && closest == null; i++) { 720 closest = getClosestNode(target, (GValueNode)current.getChildAt(i)); 721 } 722 return closest; 723 } 724 725 private TreePath getClosestPath(StringTokenizer tokenizer) { 726 if(tokenizer.countTokens() > 0) { 727 TreeNode tree_node = (TreeNode)tree.getModel().getRoot(); 728 if(tree_node instanceof GValueNode) { 729 GValueNode current = (GValueNode)tree_node; 730 TreePath closest = null; 731 while(tokenizer.hasMoreElements() && current != null) { 732 current = getClosestNode(tokenizer.nextToken(), current); 733 } 734 if(current != null) { 735 return new TreePath(current.getPath()); 757 736 } 758 737 return null; 759 } 760 761 private void searchValues() { 762 // Clear any old search results. 763 list_model.clear(); 764 String target = value.getText(); 765 GValueNode current = (GValueNode)tree.getModel().getRoot(); 766 Vector search_space = new Vector(); 767 search_space.add(current); 768 for(int i = 0; i < search_space.size(); i++) { 769 GValueNode node = (GValueNode)search_space.get(i); 770 // Add this nodes children. 771 for(int j = 0; j < node.getChildCount(); j++) { 772 search_space.add(node.getChildAt(j)); 773 } 774 // And if this node matches add it to list, except for the very 775 // first node as that is AssignedValues the root node! 776 if(node.toString().indexOf(target) != -1 && 777 node != current) { 778 list_model.add(list_model.size(), node); 779 } 780 } 781 } 782 783 private void setTreeEnabled(boolean value) { 784 tree.setRootVisible(!value); 785 tree.setEnabled(value); 786 if(value) { 787 card_layout.show(center_pane, TREE); 788 } 789 else { 790 Metadata selected_metadata = metaedit.getSelectedMetadata(); 791 if(selected_metadata != null && selected_metadata.getElement() != null) { 792 args[0] = selected_metadata.getElement().toString(); 793 } 794 else { 795 args[0] = ""; 796 } 797 auto_message.setText(get("AutoMessage", args)); 798 card_layout.show(center_pane, NONE); 799 } 800 } 801 802 private boolean startsWith(String longer, String shorter) { 803 if(longer.length() > shorter.length()) { 804 longer = longer.substring(0, shorter.length()); 805 } 806 return longer.equalsIgnoreCase(shorter); 807 } 738 } 739 } 740 return null; 741 } 742 743 private String getLastValue() { 744 if(previous_values.containsKey(selected_metadata_element)) { 745 return (String)previous_values.get(selected_metadata_element); 746 } 747 return ""; 748 } 749 750 private TreePath getParent(TreePath path) { 751 if(path.getPathCount() > 1) { 752 TreeNode nodes[] = new TreeNode[path.getPathCount() - 1]; 753 for(int i = 0; i < path.getPathCount() - 1; i++) { 754 nodes[i] = (TreeNode)path.getPathComponent(i); 755 } 756 return new TreePath(nodes); 757 } 758 return null; 759 } 760 761 private void searchValues() { 762 // Clear any old search results. 763 list_model.clear(); 764 String target = value.getText(); 765 GValueNode current = (GValueNode)tree.getModel().getRoot(); 766 Vector search_space = new Vector(); 767 search_space.add(current); 768 for(int i = 0; i < search_space.size(); i++) { 769 GValueNode node = (GValueNode)search_space.get(i); 770 // Add this nodes children. 771 for(int j = 0; j < node.getChildCount(); j++) { 772 search_space.add(node.getChildAt(j)); 773 } 774 // And if this node matches add it to list, except for the very 775 // first node as that is AssignedValues the root node! 776 if(node.toString().indexOf(target) != -1 && 777 node != current) { 778 list_model.add(list_model.size(), node); 779 } 780 } 781 } 782 783 private void setTreeEnabled(boolean value) { 784 tree.setRootVisible(!value); 785 tree.setEnabled(value); 786 if(value) { 787 card_layout.show(center_pane, TREE); 788 } 789 else { 790 Metadata selected_metadata = metaedit.getSelectedMetadata(); 791 if(selected_metadata != null && selected_metadata.getElement() != null) { 792 args[0] = selected_metadata.getElement().toString(); 793 } 794 else { 795 args[0] = ""; 796 } 797 auto_message.setText(get("AutoMessage", args)); 798 card_layout.show(center_pane, NONE); 799 } 800 } 801 802 private boolean startsWith(String longer, String shorter) { 803 if(longer.length() > shorter.length()) { 804 longer = longer.substring(0, shorter.length()); 805 } 806 return longer.equalsIgnoreCase(shorter); 807 } 808 808 809 809 private void printTreePaths() { … … 811 811 Vector nodes = new Vector(); 812 812 nodes.add(tree.getModel().getRoot()); 813 814 815 816 817 818 813 while(nodes.size() > 0) { 814 GValueNode node = (GValueNode)nodes.get(0); 815 nodes.remove(node); 816 for(int i = 0; i < node.getChildCount(); i++) { 817 nodes.add(node.getChildAt(i)); 818 } 819 819 ///ystem.out.println("TreePath = " + node.getPath()); 820 820 } 821 821 } 822 822 … … 840 840 } 841 841 842 843 844 845 846 847 842 private class DocumentListenerImpl 843 implements DocumentListener { 844 /** Gives notification that an attribute or set of attributes changed. */ 845 public void changedUpdate(DocumentEvent e) { 846 validate(); 847 } 848 848 849 850 851 852 849 /** Gives notification that there was an insert into the document. */ 850 public void insertUpdate(DocumentEvent e) { 851 validate(); 852 } 853 853 854 855 856 857 858 859 860 854 /** Gives notification that a portion of the document has been removed. */ 855 public void removeUpdate(DocumentEvent e) { 856 validate(); 857 } 858 859 public void validate() { 860 ignore = true; 861 861 ///ystem.err.println("Key released."); 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 862 StringTokenizer tokenizer = new StringTokenizer(value.getText(), "\\"); 863 TreePath closest = getClosestPath(tokenizer); 864 if(closest == previous) { 865 validateControls(); 866 ignore = false; 867 return; 868 } 869 else { 870 // Clear previous 871 if(previous != null) { 872 // We have a brand new match close up the old one. 873 if(closest != null) { 874 if(getParent(previous) != null && 875 !getParent(previous).toString().equals("[AssignedValues]")) { 876 tree.collapsePath(getParent(previous)); 877 } 878 previous = null; 879 } 880 // We haven't got a match, but we did have a matching path to 881 // this point. Move the selection to previous paths parent. 882 else { 883 tree.setSelectionPath(getParent(previous)); 884 } 885 } 886 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 887 if(closest != null) { 888 tree.setSelectionPath(closest); 889 // tree.scrollPathToVisible(closest); 890 scrollTreePathToVisible(closest); 891 previous = closest; 892 } 893 } 894 ignore = false; 895 validateControls(); 896 } 897 } 898 899 private class ListSelectionListenerImpl 900 implements ListSelectionListener { 901 public void valueChanged(ListSelectionEvent event) { 902 GValueNode node = (GValueNode)list.getSelectedValue(); 903 value.setText(Utility.stripNL(node.toString()));//formatPath(node.getPath(), false))); 904 value.setCaretPosition(0); 905 } 906 } 907 907 908 908 private class ValueListener 909 implements TreeModelListener { 910 /* Called when nodes have been modified, but the model hasn't changed. 909 implements TreeModelListener { 910 /* Called when nodes have been modified, but the model hasn't changed. 911 * @param event Everything you ever wanted to know about model changes 912 * in one tidy TreeModelEvent package. 913 */ 914 public void treeNodesChanged(TreeModelEvent event) { 915 update(); 916 } 917 918 /* Called when nodes have been added to the model. 911 919 * @param event Everything you ever wanted to know about model changes 912 920 * in one tidy TreeModelEvent package. 913 921 */ 914 public void treeNodesChanged(TreeModelEvent event) {915 916 922 public void treeNodesInserted(TreeModelEvent event) { 923 update(); 924 } 917 925 918 /* Called when nodes have been added tothe model.926 /* Called when nodes have been removed from the model. 919 927 * @param event Everything you ever wanted to know about model changes 920 928 * in one tidy TreeModelEvent package. 921 929 */ 922 public void treeNodesInserted(TreeModelEvent event) {923 924 930 public void treeNodesRemoved(TreeModelEvent event) { 931 update(); 932 } 925 933 926 /* Called when nodes have been removed fromthe model.934 /** Called when nodes have been rearranged within the model. 927 935 * @param event Everything you ever wanted to know about model changes 928 936 * in one tidy TreeModelEvent package. 929 937 */ 930 public void treeNodesRemoved(TreeModelEvent event) {931 932 938 public void treeStructureChanged(TreeModelEvent event) { 939 update(); 940 } 933 941 934 /** Called when nodes have been rearranged within the model. 935 * @param event Everything you ever wanted to know about model changes 936 * in one tidy TreeModelEvent package. 937 */ 938 public void treeStructureChanged(TreeModelEvent event) { 939 update(); 940 } 941 942 private void update() { 942 private void update() { 943 943 // Don't bother updating if the metaedit tab isn't selected. 944 945 946 947 948 949 950 951 952 953 954 955 956 944 if(Gatherer.g_man.getSelectedView() == Gatherer.g_man.metaedit_pane) { 945 vm = null; 946 vm = Gatherer.c_man.getCollection().msm.getValueTree((ElementWrapper)em.getSelectedItem()); 947 if(vm != null) { 948 vm.addTreeModelListener(value_listener); 949 } 950 else { 951 vm = new GValueModel(get("Not_Applicable")); 952 } 953 tree.setModel(vm); 954 tree.repaint(); 955 } 956 } 957 957 } 958 958 } 959 960 961 962 963
Note:
See TracChangeset
for help on using the changeset viewer.