KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > actions > SaveAsSVG


1 package org.enhydra.jawe.actions;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.io.*;
6
7 import javax.swing.*;
8 import org.apache.batik.dom.*;
9 import org.apache.batik.svggen.*;
10 import org.w3c.dom.*;
11
12 import org.enhydra.jawe.*;
13
14 public class SaveAsSVG extends ActionBase {
15
16    public SaveAsSVG (AbstractEditor editor) {
17       super(editor);
18    }
19
20    public void actionPerformed(ActionEvent e) {
21       // Create file output stream
22
try {
23          String JavaDoc file =
24             JaWE.getInstance().saveDialog(
25                ResourceManager.getLanguageDependentString("SaveAsSVGLabel"),2,
26                editor.getGraph().get("Id").toString());
27          if (file!=null && file.length()>0) {
28             saveGraphAsSVG(file,editor.getGraph());
29          }
30       } catch (Exception JavaDoc ex) {
31          String JavaDoc msg=ResourceManager.getLanguageDependentString("ErrorSVGSavingFailed");
32          JaWE.getInstance().message(msg,JOptionPane.WARNING_MESSAGE);
33       }
34    }
35
36    public static void saveGraphAsSVG (String JavaDoc file,AbstractGraph graph) throws Exception JavaDoc {
37       FileOutputStream fos=new FileOutputStream(new File(file));
38       // Created writer with UTF-8 encoding
39
Writer out = new OutputStreamWriter(fos,JaWEConfig.getInstance().getEncoding());
40       // Get a DOMImplementation
41
DOMImplementation domImpl =
42             GenericDOMImplementation.getDOMImplementation();
43       // Create an instance of org.w3c.dom.Document
44
Document document=domImpl.createDocument(null,"svg",null);
45       // Create an instance of the SVG Generator
46
SVGGraphics2D svgGenerator=new SVGGraphics2D(document);
47       // Render into the SVG Graphics2D implementation
48
graph.paint(svgGenerator);
49       // Use CSS style attribute
50
boolean useCSS=true;
51       // Finally, stream out SVG to the writer
52
svgGenerator.stream(out,useCSS);
53       // Close the file output stream
54
fos.flush();
55       fos.close();
56    }
57
58 }
59
Popular Tags