- 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/TreeSynchronizer.java
r4293 r4364 40 40 */ 41 41 final public class TreeSynchronizer 42 43 44 45 46 47 48 49 42 extends Vector 43 implements TreeExpansionListener, TreeSelectionListener { 44 /** <i>true</i> if we should temporarily ignore further events, most likely because we know our actions are causing them. */ 45 private boolean ignore; 46 /** A list of tree selection listeners. */ 47 private Vector selection_listeners = new Vector(); 48 /** Add a new tree to the synchronization list of trees to be synchronized. 49 * @param tree The lastest victim, a <strong>JTree</strong>. 50 50 */ 51 52 53 54 55 51 public void add(JTree tree) { 52 super.add(tree); 53 tree.addTreeExpansionListener(this); 54 //tree.addTreeSelectionListener(this); 55 } 56 56 57 58 59 60 61 62 57 /** We allow the Gatherer to add tree listeners to this class, as it persists between collection changes transparently. Thus there is no need to reattach listeners everytime the collection changes. */ 58 public void addTreeSelectionListener(TreeSelectionListener listener) { 59 if(!selection_listeners.contains(listener)) { 60 selection_listeners.add(listener); 61 } 62 } 63 63 64 64 /** Called whenever an item in the tree has been collapsed. 65 65 * @param event A <strong>TreeExpansionEvent</strong> containing information about the event. 66 66 */ 67 68 69 67 public void treeCollapsed(TreeExpansionEvent event) { 68 if(!ignore) { 69 ignore = true; 70 70 // Collapse that path in all registered trees. 71 72 73 74 75 76 77 78 79 80 81 82 71 JTree tree = (JTree)event.getSource(); 72 TreePath path = event.getPath(); 73 for(int i = size(); i != 0; i--) { 74 JTree sibling = (JTree) get(i - 1); 75 if(!sibling.equals(tree)) { 76 sibling.collapsePath(path); 77 } 78 } 79 ignore = false; 80 } 81 } 82 /** Called whenever an item in the tree has been expanded. 83 83 * @param event A <strong>TreeExpansionEvent</strong> containing information about the event. 84 84 */ 85 86 87 85 public void treeExpanded(TreeExpansionEvent event) { 86 if(!ignore) { 87 ignore = true; 88 88 // Expand that path in all registered trees. 89 90 91 92 93 94 95 96 97 98 99 100 89 JTree tree = (JTree)event.getSource(); 90 TreePath path = event.getPath(); 91 for(int i = size(); i != 0; i--) { 92 JTree sibling = (JTree) get(i - 1); 93 if(!sibling.equals(tree)) { 94 sibling.expandPath(path); 95 } 96 } 97 ignore = false; 98 } 99 } 100 /** Called whenever the one of the trees selection changes. 101 101 * @param event A <strong>TreeSelectionEvent</strong> containing information about the event. 102 102 */ 103 104 105 103 public void valueChanged(TreeSelectionEvent event) { 104 if(!ignore) { 105 ignore = true; 106 106 // Recover the tree that is the source. 107 107 JTree tree = (JTree) event.getSource(); 108 108 // Brute Force approach. 109 109 // Extract the currently selected paths. 110 110 TreePath paths[] = tree.getSelectionPaths(); 111 111 // Then for every registered tree, that isn't this one, ensure those paths are selected. 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 112 for(int i = size(); paths != null && i != 0; i--) { 113 JTree sibling = (JTree) get(i - 1); 114 if(!sibling.equals(tree)) { 115 // One last thing to do. If this is actually a DragTree we are dealing with we have to tell it to set the selection values immediately, not wait for the clear until it is sure it is no the pre-cursor to a drag. 116 if(sibling instanceof DragTree) { 117 DragTree gtree = (DragTree)sibling; 118 gtree.setImmediate(true); 119 gtree.setSelectionPaths(paths); 120 // I'm going to ensure that the last selected path is visible. 121 gtree.scrollPathToVisible(paths[paths.length - 1]); 122 gtree.setImmediate(false); 123 } 124 else { 125 sibling.setSelectionPaths(paths); 126 sibling.scrollPathToVisible(paths[paths.length - 1]); 127 } 128 } 129 } 130 130 // Pass on message to all listeners. 131 132 133 134 135 136 131 for(int i = 0; i < selection_listeners.size(); i++) { 132 ((TreeSelectionListener) selection_listeners.get(i)).valueChanged(event); 133 } 134 ignore = false; 135 } 136 } 137 137 }
Note:
See TracChangeset
for help on using the changeset viewer.