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

Last change on this file since 6096 was 6096, checked in by jmt12, 20 years ago

Added and then removed some debug comments

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