1 /*------------------------------------------------------------------------------
  2 Name:      XmlUtility.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Demo code for a svg client using batik (the demo appl. itself)
  6 Version:   $Id: ExtendedApplication.java 16476 2007-09-06 22:36:52Z laghi $
  7 ------------------------------------------------------------------------------*/
  8 package javaclients.svg.batik;
  9 
 10 import java.awt.*;
 11 import java.awt.event.*;
 12 import java.io.*;
 13 import javax.swing.*;
 14 
 15 // import org.apache.batik.swing.JSVGCanvas;
 16 import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
 17 import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
 18 import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
 19 import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
 20 import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
 21 import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
 22 
 23 import java.util.logging.Logger;
 24 import org.xmlBlaster.util.Global;
 25 
 26 
 27 /**
 28  *
 29  * @author $Author: laghi $ (michele@laghi.eu)
 30  */
 31 public class ExtendedApplication
 32 {
 33 
 34    private static final String ME = "ExtendedApplication";
 35    private final Global glob;
 36    private static Logger log = Logger.getLogger(ExtendedApplication.class.getName());
 37 
 38    protected class SimpleLoaderAdapter extends SVGDocumentLoaderAdapter
 39    {
 40    // remember to invoke this in the parent class ...
 41 
 42       public SimpleLoaderAdapter (Global glob, Interactor specialInteractor)
 43       {
 44          super();
 45          log.fine(" constructor");
 46       }
 47 
 48       public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
 49          log.fine(".documentLoadingStarted");
 50       }
 51 
 52       public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
 53          log.info(".documentLoadingCompleted");
 54       }
 55 
 56    }
 57 
 58 
 59 
 60    protected class SimpleGVTTreeRendererAdapter extends GVTTreeRendererAdapter
 61    {
 62    // remember to invoke this in the parent class ...
 63 
 64       private JSVGCanvasExtended canvas = null;
 65 
 66       public SimpleGVTTreeRendererAdapter (Global glob, JSVGCanvasExtended canvas)
 67       {
 68          super();
 69 
 70          this.canvas = canvas;
 71          log.fine(" constructor");
 72       }
 73 
 74 
 75       public void gvtRenderingPrepare (GVTTreeRendererEvent e)
 76       {
 77          label.setText("Rendering Started...");
 78          log.fine("Rendering Started...");
 79       }
 80 
 81 
 82       public void gvtRenderingCompleted (GVTTreeRendererEvent e)
 83       {
 84          label.setText("");
 85          // it must be done here because the graphicsNode will be set when
 86          // all the loading & rendering process has been completed.
 87          this.canvas.updateDocument();
 88          log.info("Rendering ended...");
 89       }
 90    }
 91 
 92 
 93    public static void main (String[] args)
 94    {
 95       Global glob = new Global();
 96       if (glob.init(args) != 0) {
 97          System.out.println(ME + " Init failed");
 98          System.exit(1);
 99       }
100 
101       JFrame f = new JFrame("Batik");
102       ExtendedApplication app = new ExtendedApplication(glob, f);
103       f.getContentPane().add(app.createComponents());
104 
105       f.addWindowListener(new WindowAdapter()
106          {
107             public void windowClosing(WindowEvent e) {
108               System.exit(0);
109             }
110          });
111        f.setSize(400, 400);
112        f.setVisible(true);
113    }
114 
115 
116    JFrame frame;
117    JButton button = new JButton("Load...");
118    JLabel label = new JLabel();
119    JSVGCanvasExtended svgCanvasExtended = null;
120 
121    public ExtendedApplication (Global glob, JFrame f)
122    {
123       this.glob = glob;
124 
125       frame = f;
126       svgCanvasExtended = new JSVGCanvasExtended(glob);
127    }
128 
129 
130    public JComponent createComponents ()
131    {
132       final JPanel panel = new JPanel(new BorderLayout());
133 
134       JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
135       p.add(button);
136       p.add(label);
137 
138       panel.add("North", p);
139       panel.add("Center", svgCanvasExtended);
140 
141       // Set the button action.
142       button.addActionListener(new ActionListener()
143          {
144             public void actionPerformed (ActionEvent ae)
145             {
146                JFileChooser fc = new JFileChooser(".");
147                int choice = fc.showOpenDialog(panel);
148                if (choice == JFileChooser.APPROVE_OPTION) {
149                   File f = fc.getSelectedFile();
150                   try {
151                      svgCanvasExtended.setURI(f.toURL().toString());
152                   }
153                   catch (IOException ex) {
154                      ex.printStackTrace();
155                   }
156                }
157             }
158          });
159 
160 
161       // Set the JSVGCanvas listeners.
162       svgCanvasExtended.addSVGDocumentLoaderListener(new SimpleLoaderAdapter(glob, svgCanvasExtended.getSpecificInteractor()));
163 
164       svgCanvasExtended.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter()
165          {
166 
167             public void gvtBuildStarted (GVTTreeBuilderEvent e)
168             {
169                label.setText("Build Started...");
170                log.fine("Build Started...");
171             }
172 
173             public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
174                label.setText("Build Done.");
175                log.info("Build Done.");
176                frame.pack();
177             }
178          });
179 
180       svgCanvasExtended.addGVTTreeRendererListener(new SimpleGVTTreeRendererAdapter(glob, svgCanvasExtended));
181       return panel;
182    }
183 }


syntax highlighted by Code2HTML, v. 0.9.1