source: trunk/gli/src/org/greenstone/gatherer/util/SynchronizedTreeModelTools.java@ 4364

Last change on this file since 4364 was 4364, checked in by mdewsnip, 21 years ago

Fixed tabbing.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1package org.greenstone.gatherer.util;
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 * Author: John Thompson, Greenstone Digital Library, University of Waikato
10 *
11 * Copyright (C) 1999 New Zealand Digital Library Project
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *########################################################################
27 */
28import java.lang.Runnable;
29import java.lang.Thread;
30import javax.swing.SwingUtilities;
31import javax.swing.tree.*;
32/** Due to the TreeModel objects not having any synchronization, certain assumptions, such as the model state remaining constant during a repaint, don't always hold - especially given that I'm changing the tree model on a different thread. In order to get around this I will use the latest swing paradigm wherein you flag a section of code to be executed by the AWT GUI Event queue, as soon as other gui tasks have finished. This way I shouldn't have tree redraws throwing NPEs because the array size of the children of a certain node has changed -while- the repaint call was made, i.e. repaint() calls getChildCount() = 13, removeNodeFromParent() called, repaint calls getChildAt(12) = ArrayIndexOutOfBoundsException.
33 * @author John Thompson, Greenstone Digital Library, University of Waikato
34 * @version 2.3c
35 */
36public class SynchronizedTreeModelTools {
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 //SwingUtilities.invokeLater(doInsertNodeInto);
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 //SwingUtilities.invokeLater(doRemoveNodeFromParent);
99 SwingUtilities.invokeAndWait(doRemoveNodeFromParent);
100 }
101 catch (Exception e) {
102 e.printStackTrace();
103 ///ystem.err.println(e);
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 //SwingUtilities.invokeLater(doReplaceNode);
131 SwingUtilities.invokeAndWait(doReplaceNode);
132 }
133 catch (Exception e) {
134 e.printStackTrace();
135 }
136 }
137}
Note: See TracBrowser for help on using the repository browser.