001: /*
002: * $Id: JGraphpadSVGAction.java,v 1.2 2005/08/07 10:28:29 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.svgplugin;
011:
012: import java.awt.Component;
013: import java.awt.event.ActionEvent;
014: import java.awt.geom.Rectangle2D;
015: import java.io.File;
016: import java.io.IOException;
017: import java.io.OutputStream;
018: import java.io.OutputStreamWriter;
019: import java.io.UnsupportedEncodingException;
020: import java.io.Writer;
021: import java.net.URL;
022:
023: import javax.swing.RepaintManager;
024:
025: import org.apache.batik.dom.GenericDOMImplementation;
026: import org.apache.batik.svggen.SVGGraphics2D;
027: import org.apache.batik.svggen.SVGGraphics2DIOException;
028: import org.jgraph.JGraph;
029: import org.w3c.dom.DOMImplementation;
030: import org.w3c.dom.Document;
031:
032: import com.jgraph.JGraphEditor;
033: import com.jgraph.editor.JGraphEditorAction;
034: import com.jgraph.editor.JGraphEditorResources;
035: import com.jgraph.pad.action.JGraphpadFileAction;
036:
037: /**
038: * Implements all actions that require Batik in the classpath.
039: */
040: public class JGraphpadSVGAction extends JGraphpadFileAction {
041:
042: /**
043: * Defines the mime type for SVG content (image/svg+xml).
044: */
045: public static final String MIME_SVG = "image/svg+xml";
046:
047: /**
048: * Specifies the name for the <code>saveSVG</code> action.
049: */
050: public static final String NAME_SAVESVG = "saveSVG";
051:
052: /**
053: * Specifies the name for the <code>svgServer</code> action.
054: */
055: public static final String NAME_SVGSERVER = "svgServer";
056:
057: /**
058: * Holds the running server instance.
059: */
060: protected JGraphpadSVGServer server = null;
061:
062: /**
063: * Constructs a new Batik action for the specified name.
064: */
065: public JGraphpadSVGAction(String name, JGraphEditor editor) {
066: super (name, editor);
067: if (name.equals(NAME_SVGSERVER))
068: setToggleAction(true);
069: }
070:
071: /**
072: * Executes the action based on the action name.
073: *
074: * @param e
075: * The object that describes the event.
076: */
077: public void actionPerformed(ActionEvent e) {
078: Component component = getPermanentFocusOwner();
079: try {
080: if (getName().equals(NAME_SVGSERVER))
081: doSVGServer();
082: if (component instanceof JGraph) {
083: JGraph graph = (JGraph) component;
084: if (getName().equals(NAME_SAVESVG))
085: doSaveSVG(graph, 0, dlgs.fileDialog(
086: getPermanentFocusOwnerOrParent(),
087: getString("SaveSVGFile"), false, ".svg",
088: getString("SVGFileDescription"),
089: lastDirectory));
090: }
091: } catch (IOException e1) {
092: dlgs.errorDialog(getPermanentFocusOwner(), e1
093: .getLocalizedMessage());
094: }
095: }
096:
097: /**
098: * Starts or stops the {@link #server}.
099: */
100: protected void doSVGServer() throws IOException {
101: setSelected(false);
102: if (server == null) {
103: String port = dlgs
104: .valueDialog(getString("EnterPortNumber"));
105: if (port != null) {
106: server = new JGraphpadSVGServer(editor, Integer
107: .parseInt(port));
108: dlgs.informationDialog(getPermanentFocusOwner(),
109: JGraphEditorResources.getString(
110: "ServerRunningOnPort", String
111: .valueOf(port)));
112: }
113: setSelected(true);
114: } else {
115: server.getServerSocket().close();
116: server = null;
117: }
118: }
119:
120: /**
121: * Saves the specified graph as an SVG vector graphics file.
122: *
123: * @param graph
124: * The graph to write as an SVG file.
125: * @param inset
126: * The inset to use for the SVG graphics.
127: * @param filename
128: * The filename to write the SVG.
129: */
130: public void doSaveSVG(JGraph graph, int inset, String filename)
131: throws IOException {
132: if (filename != null) {
133: OutputStream out = editor.getModel().getOutputStream(
134: filename);
135: writeSVG(graph, out, inset);
136: out.close();
137: if (JGraphEditor.isURL(filename)) {
138: URL url = new URL(filename);
139: post(url, url.getFile(), MIME_SVG, out);
140: } else
141: lastDirectory = new File(filename).getParentFile();
142: }
143: }
144:
145: /**
146: * Writes the specified graph as an SVG stream to the specified output
147: * stream.
148: *
149: * @param graph
150: * The graph to be converted into an SVG stream.
151: * @param out
152: * The output stream to use for writing out the SVG stream.
153: * @param inset
154: * The inset of the SVG graphics.
155: */
156: public static void writeSVG(JGraph graph, OutputStream out,
157: int inset) throws UnsupportedEncodingException,
158: SVGGraphics2DIOException {
159: Object[] cells = graph.getRoots();
160: Rectangle2D bounds = graph.toScreen(graph.getCellBounds(cells));
161: if (bounds != null) {
162: // Constructs the svg generator used for painting the graph to
163: DOMImplementation domImpl = GenericDOMImplementation
164: .getDOMImplementation();
165: Document document = domImpl.createDocument(null, "svg",
166: null);
167: SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
168: svgGenerator.translate(-bounds.getX() + inset, -bounds
169: .getY()
170: + inset);
171:
172: // Paints the graph to the svg generator with no double
173: // buffering enabled to make sure we get a vector image.
174: RepaintManager currentManager = RepaintManager
175: .currentManager(graph);
176: currentManager.setDoubleBufferingEnabled(false);
177: graph.paint(svgGenerator);
178:
179: // Writes the graph to the specified file as an SVG stream
180: Writer writer = new OutputStreamWriter(out, "UTF-8");
181: svgGenerator.stream(writer, false);
182:
183: currentManager.setDoubleBufferingEnabled(true);
184: }
185: }
186:
187: /**
188: * Bundle of all actions in this class.
189: */
190: public static class AllActions implements Bundle {
191:
192: /**
193: * Holds the actions. The actions are constructed in the constructor as
194: * they require an editor instance.
195: */
196: public JGraphEditorAction actionSaveSVG, actionSVGServer;
197:
198: /**
199: * Constructs the action bundle for the specified editor.
200: *
201: * @param editor
202: * The enclosing editor for this bundle.
203: */
204: public AllActions(JGraphEditor editor) {
205: actionSaveSVG = new JGraphpadSVGAction(NAME_SAVESVG, editor);
206: actionSVGServer = new JGraphpadSVGAction(NAME_SVGSERVER,
207: editor);
208: }
209:
210: /*
211: * (non-Javadoc)
212: */
213: public JGraphEditorAction[] getActions() {
214: return new JGraphEditorAction[] { actionSaveSVG,
215: actionSVGServer };
216: }
217:
218: /*
219: * (non-Javadoc)
220: */
221: public void update() {
222: Component component = getPermanentFocusOwner();
223: boolean e = component instanceof JGraph;
224: actionSaveSVG.setEnabled(e);
225: }
226:
227: }
228:
229: }
|