File: java.xml

package info (click to toggle)
batik 1.8-4%2Bdeb9u2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 31,992 kB
  • sloc: java: 196,107; xml: 16,921; javascript: 1,276; sh: 160; makefile: 41
file content (300 lines) | stat: -rw-r--r-- 11,791 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?xml version="1.0"?>

<!--

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

-->

<!-- ========================================================================= -->
<!-- author shillion@ilog.fr                                                   -->
<!-- version $Id: javaScripting.xml 201374 2004-08-18 07:17:26Z vhardy $ -->      
<!-- ========================================================================= -->

<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
<document>
  <header>
    <title>Scripting With Java</title>
  </header>

  <body>
    <p>
      This page explains how to manipulate the DOM tree of an SVG
      document from a Java program.
    </p>

    <section id="Swing">
      <title>How to manipulate a document in a JSVGCanvas</title>
      <p>
        The follow code template demonstrates how to manipulate an SVG
        document displayed in a
        <a class="class" href="../../javadoc/org/apache/batik/swing/JSVGCanvas.html">JSVGCanvas</a>
        directly from a Java program.
      </p>
      <note>
        You don’t have to worry about graphics updates; after each event
        listener invocation the canvas is updated if needed.
      </note>
      <source>import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.svg.SVGLoadEventDispatcherAdapter;
import org.apache.batik.swing.svg.SVGLoadEventDispatcherEvent;
import org.apache.batik.script.Window;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;

public class SVGApplication {

    public static void main(String[] args) {
        new SVGApplication();
    }

    JFrame frame;
    JSVGCanvas canvas;
    Document document;
    Window window;

    public SVGApplication() {
        frame = new JFrame();
        canvas = new JSVGCanvas();
        // Forces the canvas to always be dynamic even if the current
        // document does not contain scripting or animation.
        canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        canvas.addSVGLoadEventDispatcherListener
            (new SVGLoadEventDispatcherAdapter() {
                    public void svgLoadEventDispatchStarted
                        (SVGLoadEventDispatcherEvent e) {
                        // At this time the document is available...
                        document = canvas.getSVGDocument();
                        // ...and the window object too.
                        window = canvas.getUpdateManager().
                            getScriptingEnvironment().createWindow();
                        // Registers the listeners on the document
                        // just before the SVGLoad event is
                        // dispatched.
                        registerListeners();
                        // It is time to pack the frame.
                        frame.pack();
                    }
                });
        
        frame.addWindowListener(new WindowAdapter() {
                public void windowOpened(WindowEvent e) {
                    // The canvas is ready to load the base document
                    // now, from the AWT thread.
                    canvas.setURI("doc.svg");
                }
            });

        frame.getContentPane().add(canvas);
        frame.setSize(800, 600);
        frame.show();
    }

    public void registerListeners() {
        // Gets an element from the loaded document.
        Element elt = document.getElementById("elt-id");
        EventTarget t = (EventTarget)elt;

        // Adds a 'onload' listener
        t.addEventListener("SVGLoad", new OnLoadAction(), false);

        // Adds a 'onclick' listener
        t.addEventListener("click", new OnClickAction(), false);
    }

    public class OnLoadAction implements EventListener {
        public void handleEvent(Event evt) {
            // Perform some actions here...
            
            // ...for example start an animation loop:
            window.setInterval(new Animation(), 50);
        }
    }

    public class OnClickAction implements EventListener {
        public void handleEvent(Event evt) {
            // Perform some actions here...

            // ...for example schedule an action for later:
            window.setTimeout(new DelayedTask(), 500);
        }
    }

    public class Animation implements Runnable {
        public void run() {
            // Insert animation code here...
        }
    }

    public class DelayedTask implements Runnable {
        public void run() {
            // Perform some actions here...

            // ...for example displays an alert dialog:
            window.alert("Delayed Action invoked!");
        }
    }
}
</source>
      </section>
  
    <section id="Threads">
      <title>Writing thread-safe code</title>
      <p>
        The DOM listeners registered on the SVG document are called from
        the canvas update thread. To avoid race conditions, do not manipulate
        the DOM tree from another thread.
      </p>
      <p>
        The way to switch from an external thread to the canvas update
        thread is to use the following code:
      </p>
      <source>// Returns immediately
canvas.getUpdateManager().getUpdateRunnableQueue().
    invokeLater(new Runnable() {
        public void run() {
            // Insert some actions on the DOM here
        }
    });</source>
      <p>or:</p>
      <source>// Waits until the Runnable is invoked
canvas.getUpdateManager().getUpdateRunnableQueue().
    invokeAndWait(new Runnable() {
        public void run() {
            // Insert some actions on the DOM here
        }
    });</source>
      <p>
        Like with event listeners, when a
        <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runnable.html">Runnable</a>
        is invoked from the update thread, the graphics are updated.
      </p>
      <note>
        It is very dangerous to call <code>invokeAndWait</code> from the
        Swing event thread.  This is the only thread that can be
        used to interact with Swing components.  In some cases
        DOM calls may need to query the canvas for information
        (such as actual Swing component size, etc).  If this
        happens you will get a thread deadlock.  You should
        only make invokeAndWait calls from “third party” threads.
      </note>
    </section>

    <section id="SVGDOM">
      <title>Using the SVG DOM</title>
      <p>
        Batik provides a fairly complete implementation of the
        SVG DOM.  The SVG DOM provides all the functionality most 
        applications require.  In particular, the DOM implements
        DOM Events, including mutation and UI Events.  As an example,
        DOM events allow you to get notified when the cursor moves over
        elements of interest:
      </p>
      <source>// Element of Interest.
Element theElem = ...;
((EventTarget) theElem).addEventListener("mouseover", mouseOverListener, true);</source>
      <p>
        where <code>mouseOverListener</code> implements the
        <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/events/EventListener.html">org.w3c.dom.events.EventListener</a>
        interface.  This interface consists of the method:
      </p>
      <source>void handleEvent(Event evt);</source>
      <p>
        This is called whenever the event occurs with the
        element it is registered on.  It is worth reviewing the
        DOM Events specification as there are many useful features
        to this interface that are not immediately obvious.
      </p>
      <p>
        It is also worth looking at the DOM interfaces defined
        by the SVG specification as they provide a large number
        of useful methods, in particular those of
        <a class="class" href="../../javadoc/org/w3c/dom/svg/SVGLocatable.html">SVGLocatable</a>:
      </p>
<source>// Returns Bounding box of SVG Element.
public SVGRect   getBBox (  );
// Returns the transformation matrix to the screen.
public SVGMatrix getScreenCTM (  );
// Returns the transformation matrix to the given element.
public SVGMatrix getTransformToElement ( SVGElement element )
                throws SVGException;</source>
      <p>
        In particular, <code>getScreenCTM</code> is very useful for
        taking the <code>clientX</code> and <code>clientY</code> attributes of
        the DOM
        <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/events/UIEvent.html">UIEvent</a>
        and mapping them to the coordinate system of an element in the SVG
        document:
      </p>
      <source>SVGMatrix mat  = elem.getScreenCTM();
SVGMatrix imat = mat.inverse();
SVGPoint  cPt  = document.getRootElement().createSVGPoint();
cPt.setX(uiEvt.getClientX());
cPt.setY(uiEvt.getClientY());
cPt = cPt.matrixTransform(imat);</source>
    </section>

    <section id="javaInDocument">
      <title>Referencing Java code from a document</title>
      <p>
        Batik implements the Java bindings for SVG, and thus allows Java
        code to be referenced from <code>script</code> elements.
        This feature is available to any application of Batik that uses
        the bridge module (for example the Swing component and the
        transcoders).
      </p>
      <p>
        In order to use this extension, the <code>type</code> attribute
        of a <code>script</code> element must be set to
        <code>application/java-archive</code>. In addition, the
        <code>xlink:href</code> attribute must be the URI of a jar file
        that contains the code to run.
      </p>
      <p>
        The manifest of this jar file must contains an entry of the form:
      </p>
      <source>SVG-Handler-Class: <em>classname</em></source>
      <p>
        where <em>classname</em> must be the name of a class that
        implements the
        <a class="class" href="../../javadoc/org/w3c/dom/svg/EventListenerInitializer.html">org.w3c.dom.svg.EventListenerInitializer</a>
        interface.  Just before the document <code>SVGLoad</code> event
        is fired, an instance of this class is created, and this instance has
        its <code>initializeEventListeners</code> method invoked.  Note that
        there is no way to specify Java handlers in event attributes on
        SVG elements, so having the <code>initializeEventListeners</code>
        call <code>addEventListener</code> on a node is the only way to
        attach a Java listener from within the document.
      </p>
      <p>
        The class specified by <code>SVG-Handler-Class</code> can be contained
        directly in the jar file, but it is also possible for it to be
        contained in a jar file added to the classpath using the
        <code>Class-Path</code> entry of the manifest.
      </p>
    </section>

  </body>
</document>