1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.accessibility.awb.canvas;
25 
26 import java.awt.Rectangle;
27 import java.util.Iterator;
28 import javax.swing.event.TreeModelListener;
29 import javax.swing.event.TreeExpansionListener;
30 import javax.swing.event.TreeWillExpandListener;
31 import javax.swing.event.TreeExpansionEvent;
32 import javax.swing.event.TreeModelEvent;
33 import javax.swing.tree.TreeNode;
34 
35 import com.sun.star.accessibility.XAccessibleEventBroadcaster;
36 
37 /** Each canvas has a shape container that is responsible for maintaining
38 	a collection of shapes that are displayed by the canvas.
39 */
40 public class ShapeContainer
41     implements TreeModelListener,
42                TreeExpansionListener,
43                TreeWillExpandListener
44 {
ShapeContainer(Canvas aCanvas)45 	public ShapeContainer (Canvas aCanvas)
46 	{
47         maShapeList = new java.util.Hashtable();
48         maBoundingBox = new Rectangle (0,0,100,100);
49 		maCanvas = aCanvas;
50         maTree = null;
51 	}
52 
53 
54 
55 
SetTree(javax.swing.JTree aTree)56     public synchronized void SetTree (javax.swing.JTree aTree)
57     {
58 		if (aTree != maTree)
59 		{
60             if (maTree != null)
61             {
62                 maTree.getModel().removeTreeModelListener (this);
63                 maTree.removeTreeExpansionListener (this);
64                 maTree.removeTreeWillExpandListener (this);
65             }
66 
67 			Clear();
68 
69 			maTree = aTree;
70 
71 			maTree.getModel().addTreeModelListener (this);
72 			maTree.addTreeExpansionListener (this);
73 			maTree.addTreeWillExpandListener (this);
74 		}
75     }
76 
77 
78 
79 
AddNode(TreeNode aNode)80     public synchronized boolean AddNode (TreeNode aNode)
81     {
82 		CanvasShape aShape = (CanvasShape)maShapeList.get (aNode);
83 		if (aShape == null)
84         {
85 			aShape = new CanvasShape (aNode, maCanvas);
86 
87                         if (aNode instanceof XAccessibleEventBroadcaster)
88                            ((XAccessibleEventBroadcaster) aNode).addEventListener(aShape);
89 
90 			// Update bounding box that includes all objects.
91 		   if (maShapeList.size() == 0)
92 			   maBoundingBox = aShape.GetBBox();
93 		   else
94 			   maBoundingBox = maBoundingBox.union (aShape.GetBBox());
95 
96 			maShapeList.put (aNode, aShape);
97 
98 			maCanvas.repaint();
99 
100 			return true;
101         }
102 		else
103 			return false;
104     }
105 
106 
107 	/**
108 	*/
RemoveNode(TreeNode aNode)109     public synchronized boolean RemoveNode (TreeNode aNode)
110     {
111         CanvasShape aShape = (CanvasShape)maShapeList.get (aNode);
112         if (aShape != null)
113         {
114                         if (aNode instanceof XAccessibleEventBroadcaster)
115                            ((XAccessibleEventBroadcaster) aNode).removeEventListener(aShape);
116 
117 			maShapeList.remove (aNode);
118 			maCanvas.SelectObject (null);
119 			maCanvas.repaint ();
120 			return true;
121         }
122 		else
123 			return false;
124     }
125 
126 
127 
128 
Clear()129 	public synchronized void Clear ()
130 	{
131 		maShapeList.clear ();
132 	}
133 
134 
135 
136 
GetIterator()137 	public Iterator GetIterator ()
138 	{
139 		return maShapeList.values().iterator ();
140 	}
141 
142 
143 
144 
Get(TreeNode aNode)145 	public CanvasShape Get (TreeNode aNode)
146 	{
147             if (aNode != null) {
148 		return (CanvasShape)maShapeList.get (aNode);
149             }
150             return null;
151 	}
152 
153 
PrintMessage(String aMessage, java.util.EventObject aEvent)154     private void PrintMessage (String aMessage, java.util.EventObject aEvent)
155     {
156         //        System.out.println ("ShapeContainer: " + aMessage + ": " + aEvent);
157     }
158 
treeNodesChanged(TreeModelEvent aEvent)159     public void treeNodesChanged (TreeModelEvent aEvent)
160     {
161         PrintMessage ("treeNodesChanged", aEvent);
162 	}
treeNodesInserted(TreeModelEvent aEvent)163     public void treeNodesInserted (TreeModelEvent aEvent)
164     {
165         PrintMessage ("treeNodesInserted", aEvent);
166         Object[] aNewNodes = aEvent.getChildren();
167         for (int i=0; i<aNewNodes.length; i++)
168             AddNode ((TreeNode)aNewNodes[i]);
169     }
treeNodesRemoved(TreeModelEvent aEvent)170     public void treeNodesRemoved (TreeModelEvent aEvent)
171     {
172         PrintMessage ("treeNodesRemoved", aEvent);
173         Object[] aOldNodes = aEvent.getChildren();
174         for (int i=0; i<aOldNodes.length; i++)
175             RemoveNode ((TreeNode)aOldNodes[i]);
176     }
treeStructureChanged(TreeModelEvent aEvent)177     public void treeStructureChanged (TreeModelEvent aEvent)
178     {
179         PrintMessage ("treeStructureChanged", aEvent);
180         TreeNode aNode = (TreeNode)aEvent.getTreePath().getLastPathComponent();
181         RemoveAllChildren(aNode);
182         AddAllChildren(aNode);
183     }
184 
treeWillExpand(TreeExpansionEvent aEvent)185     public void treeWillExpand (TreeExpansionEvent aEvent)
186     {
187         PrintMessage ("treeWillExpand", aEvent);
188     }
treeWillCollapse(TreeExpansionEvent aEvent)189     public void treeWillCollapse (TreeExpansionEvent aEvent)
190     {
191         PrintMessage ("treeWillCollapse", aEvent);
192         TreeNode aNode = (TreeNode)aEvent.getPath().getLastPathComponent();
193         RemoveAllChildren (aNode);
194     }
treeExpanded(TreeExpansionEvent aEvent)195     public void treeExpanded (TreeExpansionEvent aEvent)
196     {
197         PrintMessage ("treeExpanded", aEvent);
198         TreeNode aNode = (TreeNode)aEvent.getPath().getLastPathComponent();
199         AddAllChildren (aNode);
200     }
treeCollapsed(TreeExpansionEvent aEvent)201     public void treeCollapsed (TreeExpansionEvent aEvent)
202     {
203         PrintMessage ("treeCollapsed", aEvent);
204 	}
205 
AddAllChildren(TreeNode aNode)206     private void AddAllChildren (TreeNode aNode) {
207         java.util.Enumeration aChildList = aNode.children();
208         while (aChildList.hasMoreElements()) {
209             TreeNode aChild = (TreeNode) aChildList.nextElement();
210             if (aChild != null) {
211                 AddAllChildren (aChild);
212                 AddNode (aChild);
213             }
214         }
215     }
216 
RemoveAllChildren(TreeNode aNode)217     private void RemoveAllChildren (TreeNode aNode) {
218         java.util.Enumeration aChildList = aNode.children();
219         while (aChildList.hasMoreElements()) {
220             TreeNode aChild = (TreeNode) aChildList.nextElement();
221             if (aChild != null) {
222                 RemoveAllChildren (aChild);
223                 RemoveNode (aChild);
224             }
225         }
226     }
227 
228 
229     private java.util.Hashtable maShapeList;
230     private Rectangle maBoundingBox;
231 	private Canvas maCanvas;
232     private javax.swing.JTree maTree;
233 }
234