01: package org.enhydra.jawe.components.graph.actions;
02:
03: import java.awt.event.ActionEvent;
04: import java.io.File;
05: import java.io.FileOutputStream;
06: import java.io.OutputStreamWriter;
07: import java.io.Writer;
08:
09: import javax.swing.JOptionPane;
10:
11: import org.apache.batik.dom.GenericDOMImplementation;
12: import org.apache.batik.svggen.SVGGraphics2D;
13: import org.enhydra.jawe.ActionBase;
14: import org.enhydra.jawe.JaWEComponent;
15: import org.enhydra.jawe.JaWEManager;
16: import org.enhydra.jawe.ResourceManager;
17: import org.enhydra.jawe.components.graph.Graph;
18: import org.enhydra.jawe.components.graph.GraphController;
19: import org.w3c.dom.DOMImplementation;
20: import org.w3c.dom.Document;
21:
22: public class SaveAsSVG extends ActionBase {
23:
24: public SaveAsSVG(JaWEComponent jawecomponent) {
25: super (jawecomponent);
26: }
27:
28: public void enableDisableAction() {
29: GraphController gc = (GraphController) jawecomponent;
30:
31: if (gc.getSelectedGraph() != null)
32: setEnabled(true);
33: else
34: setEnabled(false);
35: }
36:
37: public void actionPerformed(ActionEvent e) {
38: // Create file output stream
39: try {
40: String file = JaWEManager
41: .getInstance()
42: .getJaWEController()
43: .getJaWEFrame()
44: .saveDialog(
45: ResourceManager
46: .getLanguageDependentString("SaveAsSVGLabel"),
47: 2,
48: JaWEManager
49: .getInstance()
50: .getDisplayNameGenerator()
51: .getDisplayName(
52: ((GraphController) jawecomponent)
53: .getSelectedGraph()
54: .getXPDLObject()));
55: if (file != null && file.length() > 0) {
56: saveGraphAsSVG(file, ((GraphController) jawecomponent)
57: .getSelectedGraph());
58: }
59: } catch (Exception ex) {
60: String msg = ResourceManager
61: .getLanguageDependentString("ErrorSVGSavingFailed");
62: JaWEManager.getInstance().getJaWEController()
63: .getJaWEFrame().message(msg,
64: JOptionPane.WARNING_MESSAGE);
65: }
66: }
67:
68: public static void saveGraphAsSVG(String file, Graph graph)
69: throws Exception {
70: FileOutputStream fos = new FileOutputStream(new File(file));
71: // Created writer with UTF-8 encoding
72: Writer out = new OutputStreamWriter(fos, JaWEManager
73: .getInstance().getJaWEController()
74: .getControllerSettings().getEncoding());
75: // Get a DOMImplementation
76: DOMImplementation domImpl = GenericDOMImplementation
77: .getDOMImplementation();
78: // Create an instance of org.w3c.dom.Document
79: Document document = domImpl.createDocument(null, "svg", null);
80: // Create an instance of the SVG Generator
81: SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
82: // Render into the SVG Graphics2D implementation
83: graph.paint(svgGenerator);
84: // Use CSS style attribute
85: boolean useCSS = true;
86: // Finally, stream out SVG to the writer
87: svgGenerator.stream(out, useCSS);
88: // Close the file output stream
89: fos.flush();
90: fos.close();
91: }
92:
93: }
|