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 Peppe <pe...@gmail.com> on 2006/11/11 12:08:15 UTC

save into a file

Hi, i have to save, through a button, into a svg file my document loaded and
modified into a JSVGCanvas...i tried 
org.apache.batik.dom.util.DOMUtilities.writeDocument(SVGDocument, new
FileWriter("c:\\test.svg"));  but it doesn't work, the svg file created is
empty...why?
-- 
View this message in context: http://www.nabble.com/save-into-a-file-tf2612562.html#a7291628
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: save into a file

Posted by Peppe <pe...@gmail.com>.
Thanks V. de Weger, now it works fine...



V. de Weger wrote:
> 
> I use this:
>         
>         TransformerFactory tf = TransformerFactory.newInstance();
>         Transformer transformer=null;
>         try {
>             transformer = tf.newTransformer();
>             transformer.transform(new DOMSource(svgDocument), new
> StreamResult(os));
>         } ...
> 
> where svgDocument is a SvgDocument and the transformer stuff comes from
> javax.xml.transform, and os is an OutputStream.
> 
> 

-- 
View this message in context: http://www.nabble.com/save-into-a-file-tf2612562.html#a7334129
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: save into a file

Posted by Peppe <pe...@gmail.com>.
Thanks V. de Weger, now it work fine...



V. de Weger wrote:
> 
> I use this:
>         
>         TransformerFactory tf = TransformerFactory.newInstance();
>         Transformer transformer=null;
>         try {
>             transformer = tf.newTransformer();
>             transformer.transform(new DOMSource(svgDocument), new
> StreamResult(os));
>         } ...
> 
> where svgDocument is a SvgDocument and the transformer stuff comes from
> javax.xml.transform, and os is an OutputStream.
> 
> 

-- 
View this message in context: http://www.nabble.com/save-into-a-file-tf2612562.html#a7334129
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: save into a file

Posted by Phil Wright <ph...@gmail.com>.
Oh yeah.  I saw that in the docs.

Thanks,

     Phil


On 11/19/06, thomas.deweese@kodak.com <th...@kodak.com> wrote:
>
> Hi Phil,
>
> "Phil Wright" <ph...@gmail.com> wrote on 11/19/2006 03:48:54 PM:
>
> > This was also helpful to me so my thanks as well.
> >
> > Is there some way to get the output in a more human-readable format?
> (i.e.,
> > not in one long unbroken line)
>
>    You could run it through the SVG Pretty Printer:
>         batik.transcoder.SVGTranscoder
>
> > On 11/13/06, V. de Weger <ba...@beno-delft.nl> wrote:
> >
> > I use this:
> >
> >         TransformerFactory tf = TransformerFactory.newInstance();
> >         Transformer transformer=null;
> >         try {
> >             transformer = tf.newTransformer();
> >             transformer.transform (new DOMSource(svgDocument), new
> > StreamResult(os));
> >         } ...
> >
> > where svgDocument is a SvgDocument and the transformer stuff comes from
> > javax.xml.transform, and os is an OutputStream.
> >
> > --
> > View this message in context: http://www.nabble.com/save-into-a-file-
> > tf2612562.html#a7327216
> > Sent from the Batik - Users mailing list archive at Nabble.com.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> > For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
> >
> >
> >
> > --
> > Visit http://www.darkisle.com for photos of castles, cairns and other
> historic
> > sites in the British Isles.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>


-- 
Visit http://www.darkisle.com for photos of castles, cairns and other
historic sites in the British Isles.

Re: save into a file

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

"Phil Wright" <ph...@gmail.com> wrote on 11/19/2006 03:48:54 PM:

> This was also helpful to me so my thanks as well.
> 
> Is there some way to get the output in a more human-readable format? 
(i.e., 
> not in one long unbroken line)

   You could run it through the SVG Pretty Printer:
        batik.transcoder.SVGTranscoder

> On 11/13/06, V. de Weger <ba...@beno-delft.nl> wrote:
> 
> I use this:
> 
>         TransformerFactory tf = TransformerFactory.newInstance();
>         Transformer transformer=null;
>         try {
>             transformer = tf.newTransformer();
>             transformer.transform (new DOMSource(svgDocument), new
> StreamResult(os));
>         } ...
> 
> where svgDocument is a SvgDocument and the transformer stuff comes from
> javax.xml.transform, and os is an OutputStream.
> 
> --
> View this message in context: http://www.nabble.com/save-into-a-file-
> tf2612562.html#a7327216
> Sent from the Batik - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org 
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org

> 
> 
> 
> -- 
> Visit http://www.darkisle.com for photos of castles, cairns and other 
historic
> sites in the British Isles. 

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


Re: save into a file

Posted by Phil Wright <ph...@gmail.com>.
This was also helpful to me so my thanks as well.

Is there some way to get the output in a more human-readable format?  (i.e.,
not in one long unbroken line)

Thanks,

      Phil


On 11/13/06, V. de Weger <ba...@beno-delft.nl> wrote:
>
>
> I use this:
>
>         TransformerFactory tf = TransformerFactory.newInstance();
>         Transformer transformer=null;
>         try {
>             transformer = tf.newTransformer();
>             transformer.transform(new DOMSource(svgDocument), new
> StreamResult(os));
>         } ...
>
> where svgDocument is a SvgDocument and the transformer stuff comes from
> javax.xml.transform, and os is an OutputStream.
>
> --
> View this message in context:
> http://www.nabble.com/save-into-a-file-tf2612562.html#a7327216
> Sent from the Batik - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>


-- 
Visit http://www.darkisle.com for photos of castles, cairns and other
historic sites in the British Isles.

Re: save into a file

Posted by "V. de Weger" <ba...@beno-delft.nl>.
I use this:
        
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer=null;
        try {
            transformer = tf.newTransformer();
            transformer.transform(new DOMSource(svgDocument), new
StreamResult(os));
        } ...

where svgDocument is a SvgDocument and the transformer stuff comes from
javax.xml.transform, and os is an OutputStream.

-- 
View this message in context: http://www.nabble.com/save-into-a-file-tf2612562.html#a7327216
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: save into a file

Posted by Peppe <pe...@gmail.com>.
Try this code...it does compile...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import org.apache.batik.dom.events.DOMMouseEvent;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.svg.SVGDocument;
import org.w3c.dom.svg.SVGElement;
import org.w3c.dom.svg.SVGLocatable;
import org.w3c.dom.svg.SVGMatrix;
import org.w3c.dom.svg.SVGPoint;


public class Test {

	public static JSVGCanvas svgCanvas;
	public static JFrame f, frame;
	public static Test app;
	public static String parser, uri;
	public static SAXSVGDocumentFactory factory;
	public static SVGDocument doc;
	final String svgNS = "http://www.w3.org/2000/svg";
	JButton button = new JButton("Load...");
	JLabel label = new JLabel();
	JButton prova;
	JToggleButton muovi;
	JButton save;
	
	String SCALE = "scale";
    String DRAG = "drag";
    String action;
    EventTarget actionNode, actionTgt;
    SVGPoint startPt;
    boolean dragged = false;
    boolean move;
	
    
	public static void main(String[] args) {
        f = new JFrame("Batik");
        app = new Test(f);
        f.getContentPane().add(app.createComponents());
	    
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        f.setSize(400, 400);
        f.setVisible(true);
    }
	
	 public Test (JFrame f) {
		 	svgCanvas = new JSVGCanvas();
	        frame = f;
	 }
	 
	 public JComponent createComponents() {
		 final JPanel panel = new JPanel(new BorderLayout());

	        JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
	        p.setSize(new Dimension(184, 91));
	        p.add(button);
	        p.add(label);

	        p.add(getMuovi());
	        p.add(getProva());
	        p.add(getSave());
	        panel.add("North", p);
	        panel.add("Center", svgCanvas);
	        
	        //Set the button action.
	        button.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent ae) {
	                JFileChooser fc = new JFileChooser(".");
	                int choice = fc.showOpenDialog(panel);
	                if (choice == JFileChooser.APPROVE_OPTION) {
	                    File f = fc.getSelectedFile();
	                    try {
	            	        svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
	            	        parser =
XMLResourceDescriptor.getXMLParserClassName();
	            	        factory = new SAXSVGDocumentFactory(parser);
	                        svgCanvas.setURI(f.toURL().toString());
	                        uri = svgCanvas.getURI();
	                        doc = factory.createSVGDocument(uri);
	                        svgCanvas.setSVGDocument(doc);
	                    }catch(IOException ex){
	                    	 ex.printStackTrace();
	                    }
	                }
	            }
	        });
	 
     // Set the JSVGCanvas listeners.
     svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
         public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
             label.setText("Document Loading...");
         }
         public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
             label.setText("Document Loaded.");
         }
     });

     svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
         public void gvtBuildStarted(GVTTreeBuilderEvent e) {
             label.setText("Build Started...");
         }
         public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
             label.setText("Build Done.");
             frame.pack();
         }
     });

     svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
         public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
             label.setText("Rendering Started...");
         }
         public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
             label.setText("");
         }
     });

     return panel;
	}

	 public JButton getProva() {
		prova = new JButton();
		prova.setText("rect");
		prova.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {	
               
svgCanvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(new
Runnable() {
					public void run() {
							SVGElement svgRoot = doc.getRootElement();
							Element elem = doc.createElementNS(svgNS, "rect");
							elem.setAttributeNS(null, "id", "bgrectangle");
							elem.setAttributeNS(null, "x","200");
							elem.setAttributeNS(null, "y","100");
							elem.setAttributeNS(null, "width", "150");
							elem.setAttributeNS(null, "height", "150");
							elem.setAttributeNS(null, "style", "fill:red;stroke:none"); 
							svgRoot.appendChild(elem);
					}
                });
				//svgCanvas.repaint();
			}

		});
		return prova;
	 }
	    
	    private SVGPoint localPt(Element elem, int x, int y) {
	        doc = svgCanvas.getSVGDocument();
	        SVGMatrix mat = ((SVGLocatable)elem).getScreenCTM();
	        SVGMatrix imat = mat.inverse();
	        SVGPoint cPt = doc.getRootElement().createSVGPoint();
	        cPt.setX(x);
	        cPt.setY(y);
	        cPt = cPt.matrixTransform(imat);
	        return cPt;
	    }


	    private class OnDownAction implements EventListener {
	        public void handleEvent(Event evt) {
	        	if(move == true){
	            DOMMouseEvent elEvt = (DOMMouseEvent)evt;
	            actionNode = elEvt.getTarget();
	            action = DRAG;
	            Node n = ((Element)elEvt.getTarget());
	            startPt = localPt((Element)n, elEvt.getClientX(),
elEvt.getClientY());
	            dragged = false;
	        	}
	        }
	    }

	    private class OnUpAction implements EventListener {
	        public void handleEvent(Event evt) {
	            if (actionNode != null) {
	                actionNode = null;
	            }
	        }
	    }

	    private class OnMoveAction implements EventListener {
	        public void handleEvent(Event evt) {
	            doc = svgCanvas.getSVGDocument();
	            if (actionNode == null)return;
	            dragged = true;
	            DOMMouseEvent elEvt = (DOMMouseEvent)evt;
	            if (action == DRAG) {
	                Element ee = (Element)actionNode;           
	                SVGPoint pt = localPt((Element)ee.getParentNode(),
elEvt.getClientX(), elEvt.getClientY());
	                float dx = pt.getX() - startPt.getX();
	                float dy = pt.getY() - startPt.getY();
	                ee.setAttribute("transform", "translate(" + dx + ", " + dy
+ ")");
	            }
	        }
	    }

	 
		/**
		 * This method initializes muovi	
		 * 	
		 * @return javax.swing.JToggleButton	
		 */
		private JToggleButton getMuovi() {		
				muovi = new JToggleButton();
				muovi.setText("muovi");
				muovi.addItemListener(new MuoviItemListener());
			return muovi;
		}

		public class MuoviItemListener implements ItemListener { 
			public void itemStateChanged(ItemEvent e) {
				int status = e.getStateChange(); 
				if(status == ItemEvent.SELECTED){
					move = true;
					SVGElement svgRoot = svgCanvas.getSVGDocument().getRootElement();
					EventTarget t = (EventTarget)svgRoot;
					t.addEventListener("mousemove", new OnMoveAction(), false);
					t.addEventListener("mousedown", new OnDownAction(), false);
					t.addEventListener("mouseup", new OnUpAction(), false);
				}else move = false;
			}
		}
	
		
		private JButton getSave(){
			save = new JButton();
			save.setText("Save");
			save.addActionListener(new ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {	

					try {
						org.apache.batik.dom.util.DOMUtilities.writeNode(doc.getFirstChild(),
						new FileWriter("c:\\prova.svg"));
					} catch (IOException e1) {

						e1.printStackTrace();
					}
				}
			});
			return save;
		}
		
	
}
-- 
View this message in context: http://www.nabble.com/save-into-a-file-tf2612562.html#a7314030
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: save into a file

Posted by Peppe <pe...@gmail.com>.

Try this code...it does compile...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import org.apache.batik.dom.events.DOMMouseEvent;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.svg.SVGDocument;
import org.w3c.dom.svg.SVGElement;
import org.w3c.dom.svg.SVGLocatable;
import org.w3c.dom.svg.SVGMatrix;
import org.w3c.dom.svg.SVGPoint;


public class Test {

	public static JSVGCanvas svgCanvas;
	public static JFrame f, frame;
	public static Test app;
	public static String parser, uri;
	public static SAXSVGDocumentFactory factory;
	public static SVGDocument doc;
	final String svgNS = "http://www.w3.org/2000/svg";
	JButton button = new JButton("Load...");
	JLabel label = new JLabel();
	JButton prova;
	JToggleButton muovi;
	JButton save;
	
	String SCALE = "scale";
    String DRAG = "drag";
    String action;
    EventTarget actionNode, actionTgt;
    SVGPoint startPt;
    boolean dragged = false;
    boolean move;
	
    
	public static void main(String[] args) {
        f = new JFrame("Batik");
        app = new Test(f);
        f.getContentPane().add(app.createComponents());
	    
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        f.setSize(400, 400);
        f.setVisible(true);
    }
	
	 public Test (JFrame f) {
		 	svgCanvas = new JSVGCanvas();
	        frame = f;
	 }
	 
	 public JComponent createComponents() {
		 final JPanel panel = new JPanel(new BorderLayout());

	        JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
	        p.setSize(new Dimension(184, 91));
	        p.add(button);
	        p.add(label);

	        p.add(getMuovi());
	        p.add(getProva());
	        p.add(getSave());
	        panel.add("North", p);
	        panel.add("Center", svgCanvas);
	        
	        //Set the button action.
	        button.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent ae) {
	                JFileChooser fc = new JFileChooser(".");
	                int choice = fc.showOpenDialog(panel);
	                if (choice == JFileChooser.APPROVE_OPTION) {
	                    File f = fc.getSelectedFile();
	                    try {
	            	        svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
	            	        parser =
XMLResourceDescriptor.getXMLParserClassName();
	            	        factory = new SAXSVGDocumentFactory(parser);
	                        svgCanvas.setURI(f.toURL().toString());
	                        uri = svgCanvas.getURI();
	                        doc = factory.createSVGDocument(uri);
	                        svgCanvas.setSVGDocument(doc);
	                    }catch(IOException ex){
	                    	 ex.printStackTrace();
	                    }
	                }
	            }
	        });
	 
     // Set the JSVGCanvas listeners.
     svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
         public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
             label.setText("Document Loading...");
         }
         public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
             label.setText("Document Loaded.");
         }
     });

     svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
         public void gvtBuildStarted(GVTTreeBuilderEvent e) {
             label.setText("Build Started...");
         }
         public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
             label.setText("Build Done.");
             frame.pack();
         }
     });

     svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
         public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
             label.setText("Rendering Started...");
         }
         public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
             label.setText("");
         }
     });

     return panel;
	}

	 public JButton getProva() {
		prova = new JButton();
		prova.setText("rect");
		prova.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {	
               
svgCanvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(new
Runnable() {
					public void run() {
							SVGElement svgRoot = doc.getRootElement();
							Element elem = doc.createElementNS(svgNS, "rect");
							elem.setAttributeNS(null, "id", "bgrectangle");
							elem.setAttributeNS(null, "x","200");
							elem.setAttributeNS(null, "y","100");
							elem.setAttributeNS(null, "width", "150");
							elem.setAttributeNS(null, "height", "150");
							elem.setAttributeNS(null, "style", "fill:red;stroke:none"); 
							svgRoot.appendChild(elem);
					}
                });
				//svgCanvas.repaint();
			}

		});
		return prova;
	 }
	    
	    private SVGPoint localPt(Element elem, int x, int y) {
	        doc = svgCanvas.getSVGDocument();
	        SVGMatrix mat = ((SVGLocatable)elem).getScreenCTM();
	        SVGMatrix imat = mat.inverse();
	        SVGPoint cPt = doc.getRootElement().createSVGPoint();
	        cPt.setX(x);
	        cPt.setY(y);
	        cPt = cPt.matrixTransform(imat);
	        return cPt;
	    }


	    private class OnDownAction implements EventListener {
	        public void handleEvent(Event evt) {
	        	if(move == true){
	            DOMMouseEvent elEvt = (DOMMouseEvent)evt;
	            actionNode = elEvt.getTarget();
	            action = DRAG;
	            Node n = ((Element)elEvt.getTarget());
	            startPt = localPt((Element)n, elEvt.getClientX(),
elEvt.getClientY());
	            dragged = false;
	        	}
	        }
	    }

	    private class OnUpAction implements EventListener {
	        public void handleEvent(Event evt) {
	            if (actionNode != null) {
	                actionNode = null;
	            }
	        }
	    }

	    private class OnMoveAction implements EventListener {
	        public void handleEvent(Event evt) {
	            doc = svgCanvas.getSVGDocument();
	            if (actionNode == null)return;
	            dragged = true;
	            DOMMouseEvent elEvt = (DOMMouseEvent)evt;
	            if (action == DRAG) {
	                Element ee = (Element)actionNode;           
	                SVGPoint pt = localPt((Element)ee.getParentNode(),
elEvt.getClientX(), elEvt.getClientY());
	                float dx = pt.getX() - startPt.getX();
	                float dy = pt.getY() - startPt.getY();
	                ee.setAttribute("transform", "translate(" + dx + ", " + dy
+ ")");
	            }
	        }
	    }

	 
		/**
		 * This method initializes muovi	
		 * 	
		 * @return javax.swing.JToggleButton	
		 */
		private JToggleButton getMuovi() {		
				muovi = new JToggleButton();
				muovi.setText("muovi");
				muovi.addItemListener(new MuoviItemListener());
			return muovi;
		}

		public class MuoviItemListener implements ItemListener { 
			public void itemStateChanged(ItemEvent e) {
				int status = e.getStateChange(); 
				if(status == ItemEvent.SELECTED){
					move = true;
					SVGElement svgRoot = svgCanvas.getSVGDocument().getRootElement();
					EventTarget t = (EventTarget)svgRoot;
					t.addEventListener("mousemove", new OnMoveAction(), false);
					t.addEventListener("mousedown", new OnDownAction(), false);
					t.addEventListener("mouseup", new OnUpAction(), false);
				}else move = false;
			}
		}
	
		
		private JButton getSave(){
			save = new JButton();
			save.setText("Save");
			save.addActionListener(new ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {	

					try {
						org.apache.batik.dom.util.DOMUtilities.writeNode(doc.getFirstChild(),
						new FileWriter("c:\\prova.svg"));
					} catch (IOException e1) {

						e1.printStackTrace();
					}
				}
			});
			return save;
		}
		
	
}
-- 
View this message in context: http://www.nabble.com/save-into-a-file-tf2612562.html#a7314035
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: save into a file

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

    You code doesn't compile (it looks like you left out imports?):

Test.java:11: cannot resolve symbol
symbol  : class JSVGCanvas 
location: class org.test.Test
    public static JSVGCanvas svgCanvas;

[...]

Test.java:147: cannot resolve symbol
symbol  : class ActionListener 
location: class org.test.Test
        save.addActionListener(new ActionListener() {
                                   ^
36 errors


Peppe <pe...@gmail.com> wrote on 11/11/2006 10:31:59 AM:

> 
> This is a piece of code that i wrote:
> 
> public class Test {
> 
>    public static JSVGCanvas svgCanvas;


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


Re: save into a file

Posted by Peppe <pe...@gmail.com>.
This is a piece of code that i wrote:

public class Test {

	public static JSVGCanvas svgCanvas;
	public static JFrame f, frame;
	public static Test app;
	public static String parser, uri;
	public static SAXSVGDocumentFactory factory;
	public static SVGDocument doc;
	final String svgNS = "http://www.w3.org/2000/svg";
	JButton button = new JButton("Load...");
	JLabel label = new JLabel();
	JButton prova;
	JToggleButton muovi;
	JButton save;
	
	String SCALE = "scale";
    String DRAG = "drag";
    String action;
    EventTarget actionNode, actionTgt;
    SVGPoint startPt;
    boolean dragged = false;
	
	
	
	public static void main(String[] args) {
        f = new JFrame("Batik");
        app = new Test(f);
        f.getContentPane().add(app.createComponents());
	    
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        f.setSize(400, 400);
        f.setVisible(true);
    }
	
	 public Test (JFrame f) {
		 	svgCanvas = new JSVGCanvas();
	        frame = f;
	 }
	 
	 public JComponent createComponents() {
		 final JPanel panel = new JPanel(new BorderLayout());

	        JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
	        p.setSize(new Dimension(184, 91));
	        p.add(button);
	        p.add(label);

	        p.add(getMuovi());
	        p.add(getProva());
	        p.add(getSave());
	        panel.add("North", p);
	        panel.add("Center", svgCanvas);
	        
	        //Set the button action.
	        button.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent ae) {
	                JFileChooser fc = new JFileChooser(".");
	                int choice = fc.showOpenDialog(panel);
	                if (choice == JFileChooser.APPROVE_OPTION) {
	                    File f = fc.getSelectedFile();
	                    try {
	            	        svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
	            	        parser =
XMLResourceDescriptor.getXMLParserClassName();
	            	        factory = new SAXSVGDocumentFactory(parser);
	                        svgCanvas.setURI(f.toURL().toString());
	                        uri = svgCanvas.getURI();
	                        doc = factory.createSVGDocument(uri);
	                        svgCanvas.setSVGDocument(doc);
	                    }catch(IOException ex){
	                    	 ex.printStackTrace();
	                    }
	                }
	            }
	        });
	 
     // Set the JSVGCanvas listeners.
     svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
         public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
             label.setText("Document Loading...");
         }
         public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
             label.setText("Document Loaded.");
         }
     });

     svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
         public void gvtBuildStarted(GVTTreeBuilderEvent e) {
             label.setText("Build Started...");
         }
         public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
             label.setText("Build Done.");
             frame.pack();
         }
     });

     svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
         public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
             label.setText("Rendering Started...");
         }
         public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
             label.setText("");
         }
     });

     return panel;
	}

	 public JButton getProva() {
		prova = new JButton();
		prova.setText("rect");
		prova.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {	
               
svgCanvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(new
Runnable() {
					public void run() {
							//SVGDocument doc = svgCanvas.getSVGDocument();
                			//svgCanvas.setDocument(doc);
							SVGElement svgRoot = doc.getRootElement();
							Element elem = doc.createElementNS(svgNS, "rect");
							elem.setAttributeNS(null, "id", "bgrectangle");
							elem.setAttributeNS(null, "x","200");
							elem.setAttributeNS(null, "y","100");
							elem.setAttributeNS(null, "width", "150");
							elem.setAttributeNS(null, "height", "150");
							//elem.setAttributeNS(null, "pointer-events", "fill");
							elem.setAttributeNS(null, "style", "fill:red;stroke:none"); 
							svgRoot.appendChild(elem);
					}
                });
				//svgCanvas.repaint();
			}

		});
		return prova;
	 }

private JButton getSave(){
			save = new JButton();
			save.setText("Save");
			save.addActionListener(new ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {	

					try {
						org.apache.batik.dom.util.DOMUtilities.writeNode(doc.getFirstChild(),
						new FileWriter("c:\\prova.svg"));
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
			return save;
		}

-- 
View this message in context: http://www.nabble.com/save-into-a-file-tf2612562.html#a7293645
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: save into a file

Posted by Peppe <pe...@gmail.com>.
This is a piece of code that i wrote:

public class Test {

	public static JSVGCanvas svgCanvas;
	public static JFrame f, frame;
	public static Test app;
	public static String parser, uri;
	public static SAXSVGDocumentFactory factory;
	public static SVGDocument doc;
	final String svgNS = "http://www.w3.org/2000/svg";
	JButton button = new JButton("Load...");
	JLabel label = new JLabel();
	JButton prova;
	JToggleButton muovi;
	JButton save;
	
	
	
	public static void main(String[] args) {
        f = new JFrame("Batik");
        app = new Test(f);
        f.getContentPane().add(app.createComponents());
	    
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        f.setSize(400, 400);
        f.setVisible(true);
    }
	
	 public Test (JFrame f) {
		 	svgCanvas = new JSVGCanvas();
	        frame = f;
	 }
	 
	 public JComponent createComponents() {
		 final JPanel panel = new JPanel(new BorderLayout());

	        JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
	        p.setSize(new Dimension(184, 91));
	        p.add(button);
	        p.add(label);

	        p.add(getMuovi());
	        p.add(getProva());
	        p.add(getSave());
	        panel.add("North", p);
	        panel.add("Center", svgCanvas);
	        
	        //Set the button action.
	        button.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent ae) {
	                JFileChooser fc = new JFileChooser(".");
	                int choice = fc.showOpenDialog(panel);
	                if (choice == JFileChooser.APPROVE_OPTION) {
	                    File f = fc.getSelectedFile();
	                    try {
	            	        svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
	            	        parser =
XMLResourceDescriptor.getXMLParserClassName();
	            	        factory = new SAXSVGDocumentFactory(parser);
	                        svgCanvas.setURI(f.toURL().toString());
	                        uri = svgCanvas.getURI();
	                        doc = factory.createSVGDocument(uri);
	                        svgCanvas.setSVGDocument(doc);
	                    }catch(IOException ex){
	                    	 ex.printStackTrace();
	                    }
	                }
	            }
	        });
	 
     // Set the JSVGCanvas listeners.
     svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
         public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
             label.setText("Document Loading...");
         }
         public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
             label.setText("Document Loaded.");
         }
     });

     svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
         public void gvtBuildStarted(GVTTreeBuilderEvent e) {
             label.setText("Build Started...");
         }
         public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
             label.setText("Build Done.");
             frame.pack();
         }
     });

     svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
         public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
             label.setText("Rendering Started...");
         }
         public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
             label.setText("");
         }
     });

     return panel;
	}

	 public JButton getProva() {
		prova = new JButton();
		prova.setText("rect");
		prova.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {	
               
svgCanvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(new
Runnable() {
					public void run() {
							
							SVGElement svgRoot = doc.getRootElement();
							Element elem = doc.createElementNS(svgNS, "rect");
							elem.setAttributeNS(null, "id", "bgrectangle");
							elem.setAttributeNS(null, "x","200");
							elem.setAttributeNS(null, "y","100");
							elem.setAttributeNS(null, "width", "150");
							elem.setAttributeNS(null, "height", "150");
							//elem.setAttributeNS(null, "pointer-events", "fill");
							elem.setAttributeNS(null, "style", "fill:red;stroke:none"); 
							svgRoot.appendChild(elem);
					}
                });
				//svgCanvas.repaint();
			}

		});
		return prova;
	 }

private JButton getSave(){
			save = new JButton();
			save.setText("Save");
			save.addActionListener(new ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {	

					try {
						org.apache.batik.dom.util.DOMUtilities.writeNode(doc.getFirstChild(),
						new FileWriter("c:\\prova.svg"));
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
			return save;
		}

-- 
View this message in context: http://www.nabble.com/save-into-a-file-tf2612562.html#a7293645
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: save into a file

Posted by Peppe <pe...@gmail.com>.
I modified my code, so i'm working with JSVGCanvas into JFrame...



> You might try printing the result of
> SVGDocument.getFirstChild().
> 

how can i print a node in my console?
i wrote this but it doesn't nothing:
                                        StringWriter stringWriter = new
java.io.StringWriter();
					try {
						org.apache.batik.dom.util.DOMUtilities.writeNode(doc.getFirstChild(),
						stringWriter);
					} catch (IOException e1) {
						e1.printStackTrace();
					}

-- 
View this message in context: http://www.nabble.com/save-into-a-file-tf2612562.html#a7292358
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: save into a file

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

Peppe <pe...@gmail.com> wrote on 11/11/2006 06:08:15 AM:

> Hi, i have to save, through a button, into a svg file my document loaded 
and
> modified into a JSVGCanvas...

   Can you do this?  I thought you were working on an Applet,
normally Applet's aren't allowed to write to the disk.

> i tried 
> org.apache.batik.dom.util.DOMUtilities.writeDocument(SVGDocument, new
> FileWriter("c:\\test.svg"));  but it doesn't work, the svg file created 
is
> empty...why?

   This would happen if the Document had no children.  I don't really
know why that would happen.  You might try printing the result of
SVGDocument.getFirstChild().


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