Debian Codegraph @

documentation-sources/content/xdocs/using/swing.xml

raw
1 <?xml version="1.0"?>
2
3 <!--
4
5 Licensed to the Apache Software Foundation (ASF) under one or more
6 contributor license agreements. See the NOTICE file distributed with
7 this work for additional information regarding copyright ownership.
8 The ASF licenses this file to You under the Apache License, Version 2.0
9 (the "License"); you may not use this file except in compliance with
10 the License. You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 -->
21
22 <!-- ========================================================================= -->
23 <!-- author [email protected] -->
24 <!-- version $Id$ -->
25 <!-- ========================================================================= -->
26
27 <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
28 <document>
29 <header>
30 <title>Batik Swing components</title>
31 </header>
32
33 <body>
34 <p>
35 The goal of the Batik Swing component module is to provide a Swing
36 component that can used to display SVG documents.
37 With the
38 <a class="class" href="../javadoc/org/apache/batik/swing/JSVGCanvas.html">JSVGCanvas</a>
39 class, you can easily display an SVG document (from a URI or a DOM tree)
40 and allow the user to manipulate it, such as rotating, zooming, panning,
41 selecting text or activating hyperlinks. First this document explains how
42 to create a <code>JSVGCanvas</code> and integrate it in to a Swing
43 application. Then, it descibes some advanced features such as the listener
44 mechanism used to track all events that occurred while displaying or
45 manipulating an SVG document.
46 </p>
47
48 <section id="creatingJSVGCanvas">
49 <title>Creating a JSVGCanvas</title>
50 <p>
51 The following example illustrates how to create a
52 <code>JSVGCanvas</code>, which is a Swing component that follows the
53 Swing design rule. This means that the component is not thread safe and
54 all operations on the component or the document it is displaying must
55 be performed as described in
56 <a href="http://java.sun.com/docs/books/tutorial/uiswing/overview/threads.html">the
57 Swing tutorial</a>. The <code>JSVGCanvas</code> is also a
58 <a href="http://java.sun.com/products/javabeans/">JavaBean</a>, so it
59 can be used in visual application builders.
60 </p>
61 <note>
62 If you try this example, do not forget to set your
63 <code>CLASSPATH</code> so that it contains the Batik classes and
64 resources, as well as Xerces (<code>lib/xerces_2_5_0.jar</code>)
65 and the XML APIs jars (<code>lib/xml-apis.jar</code> and
66 <code>lib/xml-apis-ext.jar</code>).
67 </note>
68 <source>import java.awt.*;
69 import java.awt.event.*;
70 import java.io.*;
71
72 import javax.swing.*;
73
74 import org.apache.batik.swing.JSVGCanvas;
75 import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
76 import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
77 import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
78 import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
79 import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
80 import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
81
82 public class SVGApplication {
83
84 public static void main(String[] args) {
85 // Create a new JFrame.
86 JFrame f = new JFrame("Batik");
87 SVGApplication app = new SVGApplication(f);
88
89 // Add components to the frame.
90 f.getContentPane().add(app.createComponents());
91
92 // Display the frame.
93 f.addWindowListener(new WindowAdapter() {
94 public void windowClosing(WindowEvent e) {
95 System.exit(0);
96 }
97 });
98 f.setSize(400, 400);
99 f.setVisible(true);
100 }
101
102 // The frame.
103 protected JFrame frame;
104
105 // The "Load" button, which displays up a file chooser upon clicking.
106 protected JButton button = new JButton("Load...");
107
108 // The status label.
109 protected JLabel label = new JLabel();
110
111 // The SVG canvas.
112 protected JSVGCanvas svgCanvas = new JSVGCanvas();
113
114 public SVGApplication(JFrame f) {
115 frame = f;
116 }
117
118 public JComponent createComponents() {
119 // Create a panel and add the button, status label and the SVG canvas.
120 final JPanel panel = new JPanel(new BorderLayout());
121
122 JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
123 p.add(button);
124 p.add(label);
125
126 panel.add("North", p);
127 panel.add("Center", svgCanvas);
128
129 // Set the button action.
130 button.addActionListener(new ActionListener() {
131 public void actionPerformed(ActionEvent ae) {
132 JFileChooser fc = new JFileChooser(".");
133 int choice = fc.showOpenDialog(panel);
134 if (choice == JFileChooser.APPROVE_OPTION) {
135 File f = fc.getSelectedFile();
136 try {
137 svgCanvas.setURI(f.toURL().toString());
138 } catch (IOException ex) {
139 ex.printStackTrace();
140 }
141 }
142 }
143 });
144
145 // Set the JSVGCanvas listeners.
146 svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
147 public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
148 label.setText("Document Loading...");
149 }
150 public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
151 label.setText("Document Loaded.");
152 }
153 });
154
155 svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
156 public void gvtBuildStarted(GVTTreeBuilderEvent e) {
157 label.setText("Build Started...");
158 }
159 public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
160 label.setText("Build Done.");
161 frame.pack();
162 }
163 });
164
165 svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
166 public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
167 label.setText("Rendering Started...");
168 }
169 public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
170 label.setText("");
171 }
172 });
173
174 return panel;
175 }
176 }</source>
177
178 <div class="figure"><img src="images/svgapplication.png" alt="Screenshot of the example SVGApplication program"/>
179 </div>
180 </section>
181
182 <section id="eventhandling">
183 <title>Handling events</title>
184 <p>
185 Each time you set a URI or an SVG DOM tree to be displayed in a
186 <code>JSVGCanvas</code> (using the <code>setURI</code> or
187 <code>setSVGDocument</code> method), the specified document is first
188 parsed (in case of a URI), built, rendered and optionally updated. The
189 proper way to be notified of these different phases is to implement a
190 listener and attach it to the component. There are five types
191 of listener:
192 </p>
193
194 <dl>
195 <dt><code>SVGDocumentLoaderListener</code></dt>
196 <dd>
197 <p>
198 The
199 <a class="class" href="../javadoc/org/apache/batik/swing/svg/SVGDocumentLoaderListener">SVGDocumentLoaderListener</a>
200 provides a set of methods that can be used to track
201 <a class="class" href="../javadoc/org/apache/batik/swing/svg/SVGDocumentLoaderEvent">SVGDocumentLoaderEvent</a>
202 events. It describes the loading phase: contructing an SVG
203 DOM tree using an SVG file.
204 </p>
205 </dd>
206 <dt><code>GVTTreeBuilderListener</code></dt>
207 <dd>
208 <p>
209 The
210 <a class="class" href="../javadoc/org/apache/batik/swing/svg/GVTTreeBuilderListener">GVTTreeBuilderListener</a>
211 provides a set of methods that can be used to track
212 <a class="class" href="../javadoc/org/apache/batik/swing/svg/GVTTreeBuilderEvent">GVTTreeBuilderEvent</a>
213 events. It describes the building phase: contructing a GVT
214 (Graphics Vector Toolkit) tree using an SVG DOM tree. The GVT tree
215 will then be used to render the document.
216 </p>
217 </dd>
218 <dt><code>SVGLoadEventDispatcherListener</code></dt>
219 <dd>
220 <p>
221 The
222 <a class="class" href="../javadoc/org/apache/batik/swing/svg/SVGLoadEventDispatcherListener">SVGLoadEventDispatcherListener</a>
223 provides a set of methods that can be used to track
224 <a class="class" href="../javadoc/org/apache/batik/swing/svg/SVGLoadEventDispatcherEvent">SVGLoadEventDispatcherEvent</a>
225 events. It describes the DOM <code>SVGLoad</code> event dispatch phase.
226 </p>
227 <p>
228 This event is triggered only in dynamic documents.
229 </p>
230 </dd>
231 <dt><code>GVTTreeRendererListener</code></dt>
232 <dd>
233 <p>
234 The
235 <a class="class" href="../javadoc/org/apache/batik/swing/gvt/GVTTreeRendererListener.html">GVTTreeRendererListener</a>
236 provides a set of methods that can be used to track
237 <a class="class" href="../javadoc/org/apache/batik/swing/gvt/GVTTreeRendererEvent.html">GVTTreeRendererEvent</a>
238 events. It describes the rendering phase: constructing an image using
239 a GVT tree.
240 </p>
241 <p>
242 In dynamic documents this event is fired only once for the initial rendering.
243 </p>
244 </dd>
245 <dt><code>UpdateManagerListener</code></dt>
246 <dd>
247 <p>
248 The
249 <a class="class" href="../javadoc/org/apache/batik/bridge/UpdateManagerListener">UpdateManagerListener</a>
250 provides a set of methods that can be used to track
251 <a class="class" href="../javadoc/org/apache/batik/bridge/UpdateManagerEvent">UpdateManagerEvent</a>
252 events. It describes the running phase: the update manager is
253 started and then it can be suspended, resumed or stopped, and
254 graphics updates can be tracked.
255 </p>
256 <p>
257 This event is triggered only in dynamic documents.
258 </p>
259 </dd>
260 </dl>
261
262 <p>
263 These listeners give a complete description of the different steps of
264 the five phases (including error states). Adapter classes are available
265 to ease the creation of new listener implementation&#x2014;<a class="class" href="../javadoc/org/apache/batik/swing/svg/SVGDocumentLoaderAdapter.html">SVGDocumentLoaderAdapter</a>,
266 <a class="class" href="../javadoc/org/apache/batik/swing/svg/GVTTreeBuilderListenerAdapter.html">GVTTreeBuilderListenerAdapter</a>,
267 <a class="class" href="../javadoc/org/apache/batik/swing/svg/SVGLoadEventDispatcherAdapter.html">SVGLoadEventDispatcherAdapter</a>,
268 <a class="class" href="../javadoc/org/apache/batik/swing/gvt/GVTTreeRendererAdapter.html">GVTTreeRendererAdapter</a>
269 and
270 <a class="class" href="../javadoc/org/apache/batik/bridge/UpdateManagerAdapter.html">UpdateManagerAdapter</a>,
271 </p>
272
273 <p>
274 For static documents, you can assume that the JSVGCanvas has completed
275 its job (parsing, building and rendering) when the
276 <code>gvtRenderingCompleted</code> method call is called, following a
277 <code>setURI</code> or a <code>setSVGDocument</code> method call.
278 </p>
279 <p>
280 In the case of dynamic documents, the end of the computation (parsing,
281 building, SVGLoad dispatch, initial rendering and updates) is marked
282 by a call to the <code>updateManagerStopped</code> method.
283 </p>
284 </section>
285
286 <section id="interactor">
287 <title>Adding an Interactor</title>
288 <p>
289 The <code>JSVGCanvas</code> provides a set of build-in interactors that
290 let the users manipulate the displayed document, including ones for
291 zooming, panning and rotating. Interactors catch user input to the
292 <code>JSVGCanvas</code> component and translate them into behaviour.
293 If you want to add new behaviors to the <code>JSVGCanvas</code>, you
294 can implement the
295 <a class="class" href="../javadoc/org/apache/batik/swing/gvt/Interactor.html">Interactor</a>
296 interface. Then, you can register this new interactor to the component
297 by adding an element to the <code>List</code> returned by the
298 <code>getInteractors()</code> method of the canvas.
299 </p>
300 </section>
301
302 <!--
303 <section id="jsvgscrollpane">
304 <title>Using the JSVGScrollPane component</title>
305 <p>
306
307 </p>
308 </section>
309 -->
310 </body>
311 </document>