KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > swing > JSVGMemoryLeakTest


1 /*
2
3    Copyright 2003-2004 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.swing;
19
20 import java.io.File JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22
23 import javax.swing.JFrame JavaDoc;
24 import javax.swing.SwingUtilities JavaDoc;
25
26 import org.w3c.dom.Element JavaDoc;
27
28 import org.apache.batik.bridge.BridgeContext;
29 import org.apache.batik.bridge.UpdateManager;
30 import org.apache.batik.dom.svg.SVGContext;
31 import org.apache.batik.dom.svg.SVGOMElement;
32 import org.apache.batik.gvt.GraphicsNode;
33 import org.apache.batik.test.DefaultTestReport;
34 import org.apache.batik.test.MemoryLeakTest;
35 import org.apache.batik.test.TestReport;
36
37 /**
38  * One line Class Desc
39  *
40  * Complete Class Desc
41  *
42  * @author <a HREF="mailto:deweese@apache.org">l449433</a>
43  * @version $Id: JSVGMemoryLeakTest.java,v 1.11 2005/03/27 08:58:37 cam Exp $
44  */

45 public class JSVGMemoryLeakTest extends MemoryLeakTest
46     implements JSVGCanvasHandler.Delegate {
47     public JSVGMemoryLeakTest() {
48     }
49
50     public String JavaDoc getName() { return "JSVGMemoryLeakTest."+getId(); }
51
52     TestReport failReport = null;
53     boolean done;
54     JSVGCanvasHandler handler;
55     JFrame JavaDoc theFrame;
56     JSVGCanvas theCanvas;
57
58     public static String JavaDoc fmt(String JavaDoc key, Object JavaDoc []args) {
59         return TestMessages.formatMessage(key, args);
60     }
61
62     public JSVGCanvasHandler createHandler() {
63         return new JSVGCanvasHandler(this, this);
64     }
65
66     public TestReport doSomething() throws Exception JavaDoc {
67         handler = createHandler();
68         registerObjectDesc(handler, "Handler");
69         done = false;
70         handler.runCanvas(getId());
71
72         SwingUtilities.invokeAndWait( new Runnable JavaDoc() {
73                 public void run() {
74                     // System.out.println("In Invoke");
75
theFrame.remove(theCanvas);
76                     theCanvas.dispose();
77
78                     theFrame.dispose();
79                     theFrame=null;
80                     theCanvas=null;
81                 }
82             });
83
84         try { Thread.sleep(100); } catch (InterruptedException JavaDoc ie) { }
85
86         SwingUtilities.invokeAndWait( new Runnable JavaDoc() {
87                 public void run() {
88                     // Create a new Frame to take focus for Swing so old one
89
// can be GC'd.
90
theFrame = new JFrame JavaDoc("FocusFrame");
91                     // registerObjectDesc(jframe, "FocusFrame");
92
theFrame.setSize(new java.awt.Dimension JavaDoc(40, 50));
93                     theFrame.setVisible(true);
94                 }});
95
96         try { Thread.sleep(100); } catch (InterruptedException JavaDoc ie) { }
97         
98         SwingUtilities.invokeAndWait( new Runnable JavaDoc() {
99                 public void run() {
100                     theFrame.setVisible(false);
101                     theFrame.dispose();
102                 }});
103         
104         handler = null;
105         if (failReport != null) return failReport;
106         DefaultTestReport report = new DefaultTestReport(this);
107         report.setPassed(true);
108         return report;
109     }
110
111     public void scriptDone() {
112         synchronized (this) {
113             done = true;
114             handler.scriptDone();
115         }
116     }
117
118     public void registerElement(Element JavaDoc e, String JavaDoc desc) {
119         registerObjectDesc(e, desc);
120         UpdateManager um = theCanvas.getUpdateManager();
121         BridgeContext bc = um.getBridgeContext();
122         GraphicsNode gn = bc.getGraphicsNode(e);
123         if (gn != null)
124             registerObjectDesc(gn, desc+"_GN");
125         if (e instanceof SVGOMElement) {
126             SVGOMElement svge = (SVGOMElement)e;
127             SVGContext svgctx = svge.getSVGContext();
128             if (svgctx != null)
129                 registerObjectDesc(svgctx, desc+"_CTX");
130         }
131     }
132
133     /* JSVGCanvasHandler.Delegate Interface */
134     public boolean canvasInit(JSVGCanvas canvas) {
135         // System.err.println("In Init");
136
theCanvas = canvas;
137         theFrame = handler.getFrame();
138
139         File JavaDoc f = new File JavaDoc(getId());
140         try {
141             canvas.setURI(f.toURL().toString());
142         } catch (MalformedURLException JavaDoc mue) {
143         }
144         registerObjectDesc(canvas, "JSVGCanvas");
145         registerObjectDesc(handler.getFrame(), "JFrame");
146
147         return true;
148     }
149
150     public void canvasLoaded(JSVGCanvas canvas) {
151         // System.err.println("Loaded");
152
registerObjectDesc(canvas.getSVGDocument(), "SVGDoc");
153     }
154
155     public void canvasRendered(JSVGCanvas canvas) {
156         // System.err.println("Rendered");
157
registerObjectDesc(canvas.getGraphicsNode(), "GVT");
158         registerObjectDesc(canvas.getUpdateManager(), "updateManager");
159     }
160
161     public boolean canvasUpdated(JSVGCanvas canvas) {
162         // System.err.println("Updated");
163
synchronized (this) {
164             return done;
165         }
166     }
167     public void canvasDone(final JSVGCanvas canvas) {
168         // System.err.println("Done");
169
}
170
171     public void failure(TestReport report) {
172         synchronized (this) {
173             done = true;
174             failReport = report;
175         }
176     }
177 }
178
Popular Tags