- Timestamp:
- 2003-05-27T15:40:47+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/util/SynchronizedTreeModelTools.java
r4293 r4364 35 35 */ 36 36 public class SynchronizedTreeModelTools { 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 37 /** Adds an insertNodeInto model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above. */ 38 static final public Runnable insertNodeInto(final DefaultTreeModel model, final MutableTreeNode parent, final MutableTreeNode target_node) { 39 final Runnable doInsertNodeInto = new Runnable() { 40 public void run() { 41 int index = -1; 42 int pos = 0; 43 while(index == -1 && pos < parent.getChildCount()) { 44 TreeNode node = parent.getChildAt(pos); 45 int result = 0; 46 ///ystem.err.println("Compare " + target_node + " to " + node); 47 if((target_node.isLeaf() && node.isLeaf()) || (!target_node.isLeaf() && !node.isLeaf())) { 48 result = target_node.toString().toLowerCase().compareTo(node.toString().toLowerCase()); 49 } 50 else if(target_node.isLeaf()) { 51 result = -1; 52 } 53 else { 54 result = 1; 55 } 56 if(result > 0) { 57 ///ystem.err.println("Keep searching..."); 58 pos++; 59 } 60 else { 61 ///ystem.err.println("Found!"); 62 index = pos; 63 } 64 } 65 if(index == -1) { 66 index = parent.getChildCount(); 67 } 68 model.insertNodeInto(target_node, parent, index); 69 } 70 }; 71 try { 72 72 //SwingUtilities.invokeLater(doInsertNodeInto); 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 73 SwingUtilities.invokeAndWait(doInsertNodeInto); 74 } 75 catch (Exception e) { 76 e.printStackTrace(); 77 } 78 // If (and only if) we've thrown an error because we tried to invoke the runnable task and wait, when we are in the AWTEvent thread already, then try agin but with an invoke later. 79 catch (java.lang.Error error) { 80 if(error.toString().equals("java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread")) { 81 SwingUtilities.invokeLater(doInsertNodeInto); 82 } 83 } 84 return doInsertNodeInto; 85 } 86 /** Adds a removeNodeFromParent model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above. 87 * @param model The <strong>GTreeModel</strong> we want to remove the node from. 88 * @param target_node The <strong>GTreeNode</strong> to remove. 89 */ 90 static final public void removeNodeFromParent(final DefaultTreeModel model, final MutableTreeNode target_node) { 91 ///ystem.err.println("Remove " + target_node + " from parent in model " + model); 92 final Runnable doRemoveNodeFromParent = new Runnable() { 93 public void run() { 94 model.removeNodeFromParent(target_node); 95 } 96 }; 97 try { 98 98 //SwingUtilities.invokeLater(doRemoveNodeFromParent); 99 100 101 102 99 SwingUtilities.invokeAndWait(doRemoveNodeFromParent); 100 } 101 catch (Exception e) { 102 e.printStackTrace(); 103 103 ///ystem.err.println(e); 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 104 } 105 // If we've thrown an error because we tried to invoke the runnable task and wait, when we are in the AWTEvent thread already, then try agin but with an invoke later. 106 catch (java.lang.Error error) { 107 if(error.toString().equals("java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread")) { 108 SwingUtilities.invokeLater(doRemoveNodeFromParent); 109 } 110 } 111 } 112 /** Adds a replaceNode model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above. 113 * @param model The <strong>GTreeModel</strong> we want to change. 114 * @param old_node The <strong>GTreeNode</strong> to replace. 115 * @param new_node The <strong>GTreeNode</strong> to replace it with. 116 */ 117 static final public void replaceNode(final DefaultTreeModel model, final MutableTreeNode old_node, final MutableTreeNode new_node) { 118 final Runnable doReplaceNode = new Runnable() { 119 public void run() { 120 MutableTreeNode parent = (MutableTreeNode) old_node.getParent(); 121 if(parent != null) { 122 int index = parent.getIndex(old_node); 123 model.removeNodeFromParent(old_node); 124 model.insertNodeInto(new_node, parent, index); 125 } 126 parent = null; 127 } 128 }; 129 try { 130 130 //SwingUtilities.invokeLater(doReplaceNode); 131 132 133 134 135 136 131 SwingUtilities.invokeAndWait(doReplaceNode); 132 } 133 catch (Exception e) { 134 e.printStackTrace(); 135 } 136 } 137 137 }
Note:
See TracChangeset
for help on using the changeset viewer.