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