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

Last change on this file since 8236 was 8236, checked in by mdewsnip, 20 years ago

Replaced all Gatherer.print* with DebugStream.print*.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.util;
28
29import java.lang.Runnable;
30import java.lang.Thread;
31import javax.swing.SwingUtilities;
32import javax.swing.tree.*;
33import org.greenstone.gatherer.DebugStream;
34
35/** 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.
36 * @author John Thompson, Greenstone Digital Library, University of Waikato
37 * @version 2.3c
38 */
39public class SynchronizedTreeModelTools {
40 /** Adds an insertNodeInto model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above. */
41 static final public Runnable insertNodeInto(DefaultTreeModel model, MutableTreeNode parent, MutableTreeNode target_node) {
42 return insertNodeInto(model, parent, target_node, true);
43 }
44
45 static final public Runnable insertNodeInto(final DefaultTreeModel model, final MutableTreeNode parent, final MutableTreeNode target_node, final boolean wait_allowed) {
46 final Runnable doInsertNodeInto = new Runnable() {
47 public void run() {
48 ///ystem.err.print("Running task... ");
49 DebugStream.println("insertNodeInto(" + model + ", " + parent + ", " + target_node + ", " + wait_allowed);
50 int index = -1;
51 int pos = 0;
52 while(index == -1 && pos < parent.getChildCount()) {
53 TreeNode node = parent.getChildAt(pos);
54 int result = 0;
55 ///ystem.err.println("Compare " + target_node + " to " + node);
56 if((target_node.isLeaf() && node.isLeaf()) || (!target_node.isLeaf() && !node.isLeaf())) {
57 result = target_node.toString().toLowerCase().compareTo(node.toString().toLowerCase());
58 }
59 else if(target_node.isLeaf()) {
60 result = -1;
61 }
62 else {
63 result = 1;
64 }
65 if(result > 0) {
66 ///ystem.err.println("Keep searching...");
67 pos++;
68 }
69 else {
70 ///ystem.err.println("Found!");
71 index = pos;
72 }
73 }
74 if(index == -1) {
75 index = parent.getChildCount();
76 }
77 model.insertNodeInto(target_node, parent, index);
78 }
79 };
80 ///ystem.err.print("Queuing Task... ");
81 try {
82 if(wait_allowed && !SwingUtilities.isEventDispatchThread()) {
83 ///ystem.err.print("In another thread - invoke and wait... ");
84 SwingUtilities.invokeAndWait(doInsertNodeInto);
85 }
86 else {
87 ///ystem.err.print("In Event Thread or wait not allowed - invoke later... ");
88 SwingUtilities.invokeLater(doInsertNodeInto);
89 }
90 }
91 catch (Exception exception) {
92 DebugStream.printStackTrace(exception);
93 }
94 ///ystem.err.print("Added Task... ");
95 return doInsertNodeInto;
96 }
97 /** Adds a removeNodeFromParent model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above.
98 * @param model The <strong>GTreeModel</strong> we want to remove the node from.
99 * @param target_node The <strong>GTreeNode</strong> to remove.
100 */
101 static final public void removeNodeFromParent(final DefaultTreeModel model, final MutableTreeNode target_node) {
102 ///ystem.err.println("Remove " + target_node + " from parent in model " + model);
103 final Runnable doRemoveNodeFromParent = new Runnable() {
104 public void run() {
105 model.removeNodeFromParent(target_node);
106 }
107 };
108 try {
109 //SwingUtilities.invokeLater(doRemoveNodeFromParent);
110 SwingUtilities.invokeAndWait(doRemoveNodeFromParent);
111 }
112 catch (Exception exception) {
113 DebugStream.printStackTrace(exception);
114 ///ystem.err.println(e);
115 }
116 // 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.
117 catch (java.lang.Error error) {
118 if(error.toString().equals("java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread")) {
119 SwingUtilities.invokeLater(doRemoveNodeFromParent);
120 }
121 }
122 }
123 /** Adds a replaceNode model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above.
124 * @param model The <strong>GTreeModel</strong> we want to change.
125 * @param old_node The <strong>GTreeNode</strong> to replace.
126 * @param new_node The <strong>GTreeNode</strong> to replace it with.
127 */
128 static final public void replaceNode(final DefaultTreeModel model, final MutableTreeNode old_node, final MutableTreeNode new_node) {
129 final Runnable doReplaceNode = new Runnable() {
130 public void run() {
131 MutableTreeNode parent = (MutableTreeNode) old_node.getParent();
132 if(parent != null) {
133 int index = parent.getIndex(old_node);
134 model.removeNodeFromParent(old_node);
135 model.insertNodeInto(new_node, parent, index);
136 }
137 parent = null;
138 }
139 };
140 try {
141 //SwingUtilities.invokeLater(doReplaceNode);
142 SwingUtilities.invokeAndWait(doReplaceNode);
143 }
144 catch (Exception exception) {
145 DebugStream.printStackTrace(exception);
146 }
147 }
148}
Note: See TracBrowser for help on using the repository browser.