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

Last change on this file since 6540 was 6212, checked in by jmt12, 21 years ago

Exceptions are now being sent to debug rather than screen

  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 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.Gatherer;
34/** 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.
35 * @author John Thompson, Greenstone Digital Library, University of Waikato
36 * @version 2.3c
37 */
38public class SynchronizedTreeModelTools {
39 /** Adds an insertNodeInto model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above. */
40 static final public Runnable insertNodeInto(DefaultTreeModel model, MutableTreeNode parent, MutableTreeNode target_node) {
41 return insertNodeInto(model, parent, target_node, true);
42 }
43
44 static final public Runnable insertNodeInto(final DefaultTreeModel model, final MutableTreeNode parent, final MutableTreeNode target_node, boolean wait_allowed) {
45 final Runnable doInsertNodeInto = new Runnable() {
46 public void run() {
47 ///ystem.err.print("Running task... ");
48 int index = -1;
49 int pos = 0;
50 while(index == -1 && pos < parent.getChildCount()) {
51 TreeNode node = parent.getChildAt(pos);
52 int result = 0;
53 ///ystem.err.println("Compare " + target_node + " to " + node);
54 if((target_node.isLeaf() && node.isLeaf()) || (!target_node.isLeaf() && !node.isLeaf())) {
55 result = target_node.toString().toLowerCase().compareTo(node.toString().toLowerCase());
56 }
57 else if(target_node.isLeaf()) {
58 result = -1;
59 }
60 else {
61 result = 1;
62 }
63 if(result > 0) {
64 ///ystem.err.println("Keep searching...");
65 pos++;
66 }
67 else {
68 ///ystem.err.println("Found!");
69 index = pos;
70 }
71 }
72 if(index == -1) {
73 index = parent.getChildCount();
74 }
75 model.insertNodeInto(target_node, parent, index);
76 }
77 };
78 ///ystem.err.print("Queuing Task... ");
79 try {
80 if(wait_allowed && !SwingUtilities.isEventDispatchThread()) {
81 ///ystem.err.print("In another thread - invoke and wait... ");
82 SwingUtilities.invokeAndWait(doInsertNodeInto);
83 }
84 else {
85 ///ystem.err.print("In Event Thread or wait not allowed - invoke later... ");
86 SwingUtilities.invokeLater(doInsertNodeInto);
87 }
88 }
89 catch (Exception exception) {
90 Gatherer.printStackTrace(exception);
91 }
92 ///ystem.err.print("Added Task... ");
93 return doInsertNodeInto;
94 }
95 /** Adds a removeNodeFromParent model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above.
96 * @param model The <strong>GTreeModel</strong> we want to remove the node from.
97 * @param target_node The <strong>GTreeNode</strong> to remove.
98 */
99 static final public void removeNodeFromParent(final DefaultTreeModel model, final MutableTreeNode target_node) {
100 ///ystem.err.println("Remove " + target_node + " from parent in model " + model);
101 final Runnable doRemoveNodeFromParent = new Runnable() {
102 public void run() {
103 model.removeNodeFromParent(target_node);
104 }
105 };
106 try {
107 //SwingUtilities.invokeLater(doRemoveNodeFromParent);
108 SwingUtilities.invokeAndWait(doRemoveNodeFromParent);
109 }
110 catch (Exception exception) {
111 Gatherer.printStackTrace(exception);
112 ///ystem.err.println(e);
113 }
114 // 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.
115 catch (java.lang.Error error) {
116 if(error.toString().equals("java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread")) {
117 SwingUtilities.invokeLater(doRemoveNodeFromParent);
118 }
119 }
120 }
121 /** Adds a replaceNode model update onto the AWT Event queue. This gets around the lack of synchronization illustrated above.
122 * @param model The <strong>GTreeModel</strong> we want to change.
123 * @param old_node The <strong>GTreeNode</strong> to replace.
124 * @param new_node The <strong>GTreeNode</strong> to replace it with.
125 */
126 static final public void replaceNode(final DefaultTreeModel model, final MutableTreeNode old_node, final MutableTreeNode new_node) {
127 final Runnable doReplaceNode = new Runnable() {
128 public void run() {
129 MutableTreeNode parent = (MutableTreeNode) old_node.getParent();
130 if(parent != null) {
131 int index = parent.getIndex(old_node);
132 model.removeNodeFromParent(old_node);
133 model.insertNodeInto(new_node, parent, index);
134 }
135 parent = null;
136 }
137 };
138 try {
139 //SwingUtilities.invokeLater(doReplaceNode);
140 SwingUtilities.invokeAndWait(doReplaceNode);
141 }
142 catch (Exception exception) {
143 Gatherer.printStackTrace(exception);
144 }
145 }
146}
Note: See TracBrowser for help on using the repository browser.