You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Ned <ne...@gmail.com> on 2007/02/02 11:16:16 UTC

EventListeners

Hi all,

I was just wondering if anyone could help me out. I m working on an app that
displays SVG content in JSVGCanvas. The SVG document contains a few hotspots
that when clicked will trigger an action. (I have listeners for these and
they work fine). One of these actions is to open another document in the
JSVGCanvas when an element is clicked - this also works fine. Now what I
want to do is register listeners on elements of this document and this where
it all falls apart. Even though the document has been updated it just wont
add a listener to the element in the new SVG document.

At the moment I m just trying to do simple navigation between different SVG
documents i.e. load SVG file, click on an element on the file, load another
svg file, click on an element on that file and that returns you to the
original.

Now I m just wondering if I m missing something or if this is possible. Any
help would be appreciated.

Thanks
Ned

Here is sample code...

// get the location of the doc and set it to canvas
        String loc = "sample.svg";
        File f = new File(loc);
        try{
             jSVGCanvas1.setURI(f.toURL().toString());
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }

       jSVGCanvas1.addSVGLoadEventDispatcherListener
        (new SVGLoadEventDispatcherAdapter() {
                public void svgLoadEventDispatchStarted
                    (SVGLoadEventDispatcherEvent e) {
                    document = jSVGCanvas1.getSVGDocument();
                    window = jSVGCanvas1.getUpdateManager
().getScriptingEnvironment().createWindow();
                    // Registers the listeners on the document just before
the SVGLoad event is dispatched.
                    registerListeners();
                  }
            });

        jSVGCanvas1.addSVGDocumentLoaderListener(new
SVGDocumentLoaderAdapter(){
            public void documentLoadingStarted(SVGDocumentLoaderEvent e){
                System.out.println("loading...");
            }

            public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
                System.out.println("finished loading...");
            }
        });

        jSVGCanvas1.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter(){
            public void gvtBuildStarted(GVTTreeBuilderEvent e){
              System.out.println("building...");
            }

            public void gvtBuildCompleted(GVTTreeBuilderEvent e){
                System.out.println("finished building...");
            }
        });

        jSVGCanvas1.addGVTTreeRendererListener(new GVTTreeRendererAdapter(){
            public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
                System.out.println("rendering...");
            }
            public void gvtRenderingCompleted(GVTTreeRendererEvent e){
                System.out.println("Finished rendering...");
                // want to set the document element to what ever is in the
JSVGCanvas and register listenrs again
                document = jSVGCanvas1.getSVGDocument();
                registerListeners();
            }
        });

       public void registerListeners() {
            // element "Primary" belongs to samle.svg file
            Element elt = document.getElementById("Primary");
            if(elt != null){
                         EventTarget t = (EventTarget)elt;
                         // Adds a 'onclick' listener
                        t.addEventListener("click", new OnClickAction(),
false);
             }

            // element home belongs to sample2.svg file
             Element elt2 = document.getElementById("home");
             if(elt2 != null){
                   EventTarget t2 = (EventTarget)elt2;
                   t2.addEventListener("SVGLoad", new OnLoadAction1(),
true);
                  t2.addEventListener("click", new OnClickAct(), true);
        }
    }

    // when a hotspot is clicked then change the document in JSVGCanvas
    public class OnClickAction implements EventListener {
        public void handleEvent(Event evt) {
             String loc1 = "sample1.svg";
             File f = new File(loc1);
             try{
                  jSVGCanvas1.setURI(f.toURL().toString());
             }
             catch (Exception ex){
                 ex.printStackTrace();
             }
        }
    }

    // when hotspot is clicked bring back to original
    public class OnClickAct implements EventListener {
        public void handleEvent(Event evt) {
             String loc = "sample.svg";
             File f = new File(loc);
             try{
                  jSVGCanvas1.setURI(f.toURL().toString());
             }
             catch (Exception ex){
                 ex.printStackTrace();
             }
        }
    }

Re: EventListeners

Posted by Ned <ne...@gmail.com>.
Hi Thomas,

Thank you for the reply, Its working now. I forgot to set the set the canvas
to 'ALWAYS_DYNAMIC'....


On 2/2/07, thomas.deweese@kodak.com <th...@kodak.com> wrote:
>
> Hi Ned,
>
> Ned <ne...@gmail.com> wrote on 02/02/2007 05:16:16 AM:
>
> > Now what I want to do is register listeners on elements of this
> > [newly loaded] document and this where it all falls
> > apart. Even though the document has been updated it just wont add a
> > listener to the element in the new SVG document.
>
> > Now I m just wondering if I m missing something or if this is
> > possible. Any help would be appreciated.
>
>    It should be possible, I saw to suspicious things
> in the code below;
>    1) You should register your SVGLoadEventDispatcherListener
>       before calling setURI, since loading is async it's
>       possible the thread get's past the SVGLoad dispatch
>       before your listener is attached.
>    2) The call the 'createWindow()' does just that creates
>       the window object, it doesn't tie it into the document
>       fully, so if your register code is depending on this
>       you might have problems...
>
>    That all said I'm not very confident that either of
> those is really your problem.
>
>    Perhaps Batik doesn't think your document is dynamic?
> Did you set the canvas to 'ALWAYS_DYNAMIC'?  Otherwise
> if your document lacks any event attributes Batik will
> assume it's a static document and not dispatch any
> events.
>
>
>
> > Here is sample code...
> >
> > // get the location of the doc and set it to canvas
> >         String loc = "sample.svg";
> >         File f = new File(loc);
> >         try{
> >              jSVGCanvas1.setURI(f.toURL().toString());
> >         }
> >         catch (Exception ex) {
> >             ex.printStackTrace();
> >         }
> >
> >        jSVGCanvas1.addSVGLoadEventDispatcherListener
> >         (new SVGLoadEventDispatcherAdapter() {
> >                 public void svgLoadEventDispatchStarted
> >                     (SVGLoadEventDispatcherEvent e) {
> >                     document = jSVGCanvas1.getSVGDocument();
> >                     window = jSVGCanvas1.getUpdateManager().
> > getScriptingEnvironment().createWindow();
> >                     // Registers the listeners on the document just
> > before the SVGLoad event is dispatched.
> >                     registerListeners();
> >                   }
> >             });
> >
> >         jSVGCanvas1.addSVGDocumentLoaderListener(new
> > SVGDocumentLoaderAdapter(){
> >             public void documentLoadingStarted(SVGDocumentLoaderEvent
> e){
> >                 System.out.println("loading...");
> >             }
> >
> >             public void documentLoadingCompleted(SVGDocumentLoaderEvent
> e) {
> >                 System.out.println("finished loading...");
> >             }
> >         });
> >
> >         jSVGCanvas1.addGVTTreeBuilderListener (new
> GVTTreeBuilderAdapter(){
> >             public void gvtBuildStarted(GVTTreeBuilderEvent e){
> >               System.out.println("building...");
> >             }
> >
> >             public void gvtBuildCompleted(GVTTreeBuilderEvent e){
> >                 System.out.println("finished building...");
> >             }
> >         });
> >
> >         jSVGCanvas1.addGVTTreeRendererListener(new
> GVTTreeRendererAdapter(){
> >             public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
> >                 System.out.println("rendering...");
> >             }
> >             public void gvtRenderingCompleted(GVTTreeRendererEvent e){
> >                 System.out.println("Finished rendering...");
> >                 // want to set the document element to what ever is
> > in the JSVGCanvas and register listenrs again
> >                 document = jSVGCanvas1.getSVGDocument();
> >                 registerListeners();
> >             }
> >         });
> >
> >        public void registerListeners() {
> >             // element "Primary" belongs to samle.svg file
> >             Element elt = document.getElementById("Primary");
> >             if(elt != null){
> >                          EventTarget t = (EventTarget)elt;
> >                          // Adds a 'onclick' listener
> >                         t.addEventListener("click", new
> > OnClickAction(), false);
> >              }
> >
> >             // element home belongs to sample2.svg file
> >              Element elt2 = document.getElementById("home");
> >              if(elt2 != null){
> >                    EventTarget t2 = (EventTarget)elt2;
> >                    t2.addEventListener("SVGLoad", new OnLoadAction1(),
> true);
> >                   t2.addEventListener("click", new OnClickAct(), true);
> >         }
> >     }
> >
> >     // when a hotspot is clicked then change the document in JSVGCanvas
> >     public class OnClickAction implements EventListener {
> >         public void handleEvent(Event evt) {
> >              String loc1 = "sample1.svg";
> >              File f = new File(loc1);
> >              try{
> >                   jSVGCanvas1.setURI(f.toURL().toString());
> >              }
> >              catch (Exception ex){
> >                  ex.printStackTrace();
> >              }
> >         }
> >     }
> >
> >     // when hotspot is clicked bring back to original
> >     public class OnClickAct implements EventListener {
> >         public void handleEvent(Event evt) {
> >              String loc = "sample.svg";
> >              File f = new File(loc);
> >              try{
> >                   jSVGCanvas1.setURI(f.toURL().toString());
> >              }
> >              catch (Exception ex){
> >                  ex.printStackTrace();
> >              }
> >         }
> >     }
> >
> >
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>

Re: EventListeners

Posted by th...@kodak.com.
Hi Ned,

Ned <ne...@gmail.com> wrote on 02/02/2007 05:16:16 AM:

> Now what I want to do is register listeners on elements of this 
> [newly loaded] document and this where it all falls 
> apart. Even though the document has been updated it just wont add a 
> listener to the element in the new SVG document. 

> Now I m just wondering if I m missing something or if this is 
> possible. Any help would be appreciated.

   It should be possible, I saw to suspicious things
in the code below;
   1) You should register your SVGLoadEventDispatcherListener
      before calling setURI, since loading is async it's
      possible the thread get's past the SVGLoad dispatch
      before your listener is attached.
   2) The call the 'createWindow()' does just that creates
      the window object, it doesn't tie it into the document
      fully, so if your register code is depending on this
      you might have problems...

   That all said I'm not very confident that either of
those is really your problem.

   Perhaps Batik doesn't think your document is dynamic?
Did you set the canvas to 'ALWAYS_DYNAMIC'?  Otherwise
if your document lacks any event attributes Batik will
assume it's a static document and not dispatch any
events.



> Here is sample code...
> 
> // get the location of the doc and set it to canvas
>         String loc = "sample.svg";
>         File f = new File(loc);
>         try{
>              jSVGCanvas1.setURI(f.toURL().toString());  
>         }
>         catch (Exception ex) { 
>             ex.printStackTrace();
>         }
> 
>        jSVGCanvas1.addSVGLoadEventDispatcherListener
>         (new SVGLoadEventDispatcherAdapter() {
>                 public void svgLoadEventDispatchStarted
>                     (SVGLoadEventDispatcherEvent e) {
>                     document = jSVGCanvas1.getSVGDocument();
>                     window = jSVGCanvas1.getUpdateManager().
> getScriptingEnvironment().createWindow(); 
>                     // Registers the listeners on the document just 
> before the SVGLoad event is dispatched.
>                     registerListeners();
>                   }
>             });
> 
>         jSVGCanvas1.addSVGDocumentLoaderListener(new 
> SVGDocumentLoaderAdapter(){
>             public void documentLoadingStarted(SVGDocumentLoaderEvent 
e){ 
>                 System.out.println("loading...");
>             } 
> 
>             public void documentLoadingCompleted(SVGDocumentLoaderEvent 
e) {
>                 System.out.println("finished loading...");
>             }
>         });
> 
>         jSVGCanvas1.addGVTTreeBuilderListener (new 
GVTTreeBuilderAdapter(){
>             public void gvtBuildStarted(GVTTreeBuilderEvent e){
>               System.out.println("building...");
>             }
> 
>             public void gvtBuildCompleted(GVTTreeBuilderEvent e){ 
>                 System.out.println("finished building...");
>             }
>         });
> 
>         jSVGCanvas1.addGVTTreeRendererListener(new 
GVTTreeRendererAdapter(){
>             public void gvtRenderingPrepare(GVTTreeRendererEvent e) { 
>                 System.out.println("rendering...");
>             }
>             public void gvtRenderingCompleted(GVTTreeRendererEvent e){
>                 System.out.println("Finished rendering..."); 
>                 // want to set the document element to what ever is 
> in the JSVGCanvas and register listenrs again
>                 document = jSVGCanvas1.getSVGDocument();
>                 registerListeners();
>             }
>         });
> 
>        public void registerListeners() {
>             // element "Primary" belongs to samle.svg file
>             Element elt = document.getElementById("Primary"); 
>             if(elt != null){
>                          EventTarget t = (EventTarget)elt;
>                          // Adds a 'onclick' listener
>                         t.addEventListener("click", new 
> OnClickAction(), false); 
>              }
> 
>             // element home belongs to sample2.svg file
>              Element elt2 = document.getElementById("home");
>              if(elt2 != null){
>                    EventTarget t2 = (EventTarget)elt2; 
>                    t2.addEventListener("SVGLoad", new OnLoadAction1(), 
true);
>                   t2.addEventListener("click", new OnClickAct(), true);
>         } 
>     }
> 
>     // when a hotspot is clicked then change the document in JSVGCanvas 
>     public class OnClickAction implements EventListener {
>         public void handleEvent(Event evt) { 
>              String loc1 = "sample1.svg";
>              File f = new File(loc1);
>              try{
>                   jSVGCanvas1.setURI(f.toURL().toString());
>              }
>              catch (Exception ex){
>                  ex.printStackTrace();
>              }
>         }
>     }
> 
>     // when hotspot is clicked bring back to original
>     public class OnClickAct implements EventListener {
>         public void handleEvent(Event evt) {
>              String loc = "sample.svg";
>              File f = new File(loc);
>              try{
>                   jSVGCanvas1.setURI(f.toURL().toString());
>              }
>              catch (Exception ex){
>                  ex.printStackTrace();
>              } 
>         }
>     }
> 
> 
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org