001: /*
002: * $Id: JGraphpadEPSAction.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.epsplugin;
011:
012: import java.awt.Component;
013: import java.awt.Dimension;
014: import java.awt.event.ActionEvent;
015: import java.awt.geom.Rectangle2D;
016: import java.io.File;
017: import java.io.IOException;
018: import java.io.OutputStream;
019: import java.net.URL;
020:
021: import javax.swing.RepaintManager;
022:
023: import org.jgraph.JGraph;
024: import org.jibble.epsgraphics.EpsGraphics2D;
025:
026: import com.jgraph.JGraphEditor;
027: import com.jgraph.editor.JGraphEditorAction;
028: import com.jgraph.pad.action.JGraphpadFileAction;
029:
030: /**
031: * Implements all actions that require EPSGraphics in the classpath.
032: */
033: public class JGraphpadEPSAction extends JGraphpadFileAction {
034:
035: /**
036: * Specifies the name for the <code>saveEPS</code> action.
037: */
038: public static final String NAME_SAVEEPS = "saveEPS";
039:
040: /**
041: * Constructs a new Batik action for the specified name.
042: */
043: public JGraphpadEPSAction(String name, JGraphEditor editor) {
044: super (name, editor);
045: }
046:
047: /**
048: * Executes the action based on the action name.
049: *
050: * @param e
051: * The object that describes the event.
052: */
053: public void actionPerformed(ActionEvent e) {
054: Component component = getPermanentFocusOwner();
055: try {
056: if (component instanceof JGraph) {
057: JGraph graph = (JGraph) component;
058: if (getName().equals(NAME_SAVEEPS))
059: doSaveEPS(graph, 5, dlgs.fileDialog(
060: getPermanentFocusOwnerOrParent(),
061: getString("SaveEPSFile"), false, ".eps",
062: getString("EPSFileDescription"),
063: lastDirectory));
064: }
065: } catch (IOException e1) {
066: dlgs.errorDialog(getPermanentFocusOwner(), e1
067: .getLocalizedMessage());
068: }
069: }
070:
071: /**
072: * Saves the specified graph as an EPS vector graphics file.
073: *
074: * @param graph
075: * The graph to write as an EPS file.
076: * @param inset
077: * The inset to use for the EPS graphics.
078: * @param filename
079: * The filename to write the EPS.
080: */
081: public void doSaveEPS(JGraph graph, int inset, String filename)
082: throws IOException {
083: if (filename != null) {
084: OutputStream out = editor.getModel().getOutputStream(
085: filename);
086: Object[] cells = graph.getDescendants(graph.getRoots());
087: if (cells.length > 0) {
088: EpsGraphics2D graphics = new EpsGraphics2D();
089: graphics.setColor(graph.getBackground());
090: Rectangle2D bounds = graph.toScreen(graph
091: .getCellBounds(cells));
092: Dimension d = bounds.getBounds().getSize();
093: graphics.fillRect(0, 0, d.width + 2 * inset, d.height
094: + 2 * inset);
095: graphics.translate(-bounds.getX() + inset, -bounds
096: .getY()
097: + inset);
098:
099: // Paints the graph to the svg generator with no double
100: // buffering enabled to make sure we get a vector image.
101: RepaintManager currentManager = RepaintManager
102: .currentManager(graph);
103: currentManager.setDoubleBufferingEnabled(false);
104: graph.paint(graphics);
105: out.write(graphics.toString().getBytes());
106: currentManager.setDoubleBufferingEnabled(true);
107: }
108:
109: out.close();
110: if (JGraphEditor.isURL(filename)) {
111: URL url = new URL(filename);
112: post(url, url.getFile(), MIME_HTML, out);
113: } else
114: lastDirectory = new File(filename).getParentFile();
115: }
116: }
117:
118: /**
119: * Bundle of all actions in this class.
120: */
121: public static class AllActions implements Bundle {
122:
123: /**
124: * Holds the actions. The actions are constructed in the constructor as
125: * they require an editor instance.
126: */
127: public JGraphEditorAction actionSaveEPS;
128:
129: /**
130: * Constructs the action bundle for the specified editor.
131: *
132: * @param editor
133: * The enclosing editor for this bundle.
134: */
135: public AllActions(JGraphEditor editor) {
136: actionSaveEPS = new JGraphpadEPSAction(NAME_SAVEEPS, editor);
137: }
138:
139: /*
140: * (non-Javadoc)
141: */
142: public JGraphEditorAction[] getActions() {
143: return new JGraphEditorAction[] { actionSaveEPS };
144: }
145:
146: /*
147: * (non-Javadoc)
148: */
149: public void update() {
150: Component component = getPermanentFocusOwner();
151: boolean e = component instanceof JGraph;
152: actionSaveEPS.setEnabled(e);
153: }
154:
155: }
156:
157: }
|