001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.ui.products.ad.viewfactory;
043:
044: import org.netbeans.modules.uml.core.support.umlsupport.Log;
045: import org.netbeans.modules.uml.ui.swing.drawingarea.SaveAsGraphicKind;
046: import com.tomsawyer.editor.TSEGraphImageEncoder;
047: import com.tomsawyer.editor.TSEGraphWindow;
048: import com.tomsawyer.editor.TSEObjectUI;
049: import com.tomsawyer.editor.TSTransform;
050: import java.awt.Image;
051: import java.awt.geom.AffineTransform;
052: import java.awt.image.BufferedImage;
053: import java.io.File;
054: import java.io.FileOutputStream;
055: import java.io.IOException;
056: import java.io.OutputStream;
057: import java.io.OutputStreamWriter;
058: import java.io.Writer;
059: import java.util.Iterator;
060: import javax.imageio.IIOImage;
061: import javax.imageio.ImageIO;
062: import javax.imageio.ImageWriteParam;
063: import javax.imageio.ImageWriter;
064: import javax.imageio.stream.FileImageOutputStream;
065: import javax.imageio.stream.ImageOutputStream;
066: import org.apache.batik.dom.GenericDOMImplementation;
067: import org.apache.batik.svggen.SVGGeneratorContext;
068: import org.apache.batik.svggen.SVGGraphics2D;
069: import org.openide.ErrorManager;
070: import org.w3c.dom.DOMImplementation;
071: import org.w3c.dom.Document;
072:
073: /**
074: * @author KevinM
075: * @author Sheryl
076: *
077: * This class extends the TSS image Encoder so we can save the transform.
078: * You can only use the transform only after the image has been saved,
079: */
080: public class ETEGraphImageEncoder extends TSEGraphImageEncoder {
081: protected TSTransform encoderTransform = null;
082: protected String fileName;
083: protected int kind;
084: private float QUALITY = 75;
085:
086: public ETEGraphImageEncoder(TSEGraphWindow graphWindow) {
087: super (graphWindow);
088: }
089:
090: public ETEGraphImageEncoder(TSEGraphWindow graphWindow,
091: String sFilename, int saveAsGraphicKind) {
092: super (graphWindow);
093:
094: fileName = sFilename;
095: kind = saveAsGraphicKind;
096: }
097:
098: /*
099: * Converts the graph window into an image output stream.
100: */
101: public boolean save(double scale) {
102: boolean rc = false;
103: try {
104: TSEObjectUI currentUI = getGraphWindow().getGraph().getUI();
105: ETImageExportGraphUI ui = new ETImageExportGraphUI();
106: getGraphWindow().getGraph().setUI(ui);
107:
108: switch (kind) {
109: case SaveAsGraphicKind.SAFK_JPG:
110: FileImageOutputStream fio = new FileImageOutputStream(
111: new File(fileName));
112: write("jpg",
113: fio,
114: false, // NOI18N
115: TSEGraphWindow.CUSTOM_SIZE, false, false,
116: QUALITY, (int) (getGraphWindow().getGraph()
117: .getFrameBounds().getWidth() * scale),
118: (int) (getGraphWindow().getGraph()
119: .getFrameBounds().getHeight() * scale));
120: rc = true;
121: break;
122: case SaveAsGraphicKind.SAFK_PNG:
123: writePNGFormat(new FileOutputStream(fileName), false,
124: TSEGraphWindow.CUSTOM_SIZE, false, false,
125: (int) (getGraphWindow().getGraph()
126: .getFrameBounds().getWidth() * scale),
127: (int) (getGraphWindow().getGraph()
128: .getFrameBounds().getHeight() * scale));
129: rc = true;
130: break;
131: case SaveAsGraphicKind.SAFK_SVG:
132: writeSVGFormat(new FileOutputStream(fileName));
133: rc = true;
134: break;
135: }
136: encoderTransform = ui.getImageTransform();
137: getGraphWindow().getGraph().setUI(currentUI);
138: } catch (Exception e) {
139: Log.stackTrace(e);
140: }
141: return rc;
142: }
143:
144: /*
145: * Only valid after a call to save(), it can be used to locate objects on the graphic.
146: */
147: public TSTransform getEncoderTransform() {
148: return encoderTransform;
149: }
150:
151: // override TSEGraphImageEncoder.writeJPEGFormat() to use ImageIO API
152: public void writeJPEGFormat(OutputStream fo) throws IOException,
153: com.sun.image.codec.jpeg.ImageFormatException {
154: writeJPEGFormat(fo, false, TSEGraphWindow.ACTUAL_SIZE, false,
155: false, 100, 0, 0);
156: }
157:
158: public void writeJPEGFormat(OutputStream fo,
159: boolean visibleAreaOnly, int zoomType, boolean drawGrid,
160: boolean selectedOnly, float quality, int width, int height)
161: throws IOException,
162: com.sun.image.codec.jpeg.ImageFormatException {
163: write("jpg", fo, visibleAreaOnly, zoomType,
164: drawGrid,
165: selectedOnly, // NOI18N
166: quality, (int) getGraphWindow().getGraph()
167: .getFrameBounds().getWidth(),
168: (int) getGraphWindow().getGraph().getFrameBounds()
169: .getHeight());
170: }
171:
172: public void writePNGFormat(OutputStream fo) throws IOException,
173: com.sun.image.codec.jpeg.ImageFormatException {
174: writePNGFormat(fo, false, TSEGraphWindow.ACTUAL_SIZE, false,
175: false, (int) getGraphWindow().getGraph()
176: .getFrameBounds().getWidth(),
177: (int) getGraphWindow().getGraph().getFrameBounds()
178: .getHeight());
179: }
180:
181: public void writePNGFormat(OutputStream fo,
182: boolean visibleAreaOnly, int zoomType, boolean drawGrid,
183: boolean selectedOnly, int width, int height)
184: throws IOException,
185: com.sun.image.codec.jpeg.ImageFormatException {
186: write("png", fo, visibleAreaOnly, zoomType, drawGrid,
187: selectedOnly, 0, width, height); // NOI18N
188: }
189:
190: public void writeSVGFormat(OutputStream fo,
191: boolean visibleAreaOnly, int zoomType, boolean drawGrid,
192: boolean selectedOnly, int width, int height)
193: throws IOException,
194: com.sun.image.codec.jpeg.ImageFormatException {
195: write("svg", fo, visibleAreaOnly, zoomType, drawGrid,
196: selectedOnly, 0, width, height); // NOI18N
197: }
198:
199: public void writeSVGFormat(OutputStream fo)
200:
201: throws IOException, com.sun.image.codec.jpeg.ImageFormatException {
202: write("svg", fo, false, TSEGraphWindow.ACTUAL_SIZE, false,
203: false, 0, // NOI18N
204: (int) getGraphWindow().getGraph().getFrameBounds()
205: .getWidth(), (int) getGraphWindow().getGraph()
206: .getFrameBounds().getHeight());
207: }
208:
209: public void write(String format, Object fo,
210: boolean visibleAreaOnly, int zoomType, boolean drawGrid,
211: boolean selectedOnly, float quality, int width, int height) {
212:
213: BufferedImage bufferedImage = new BufferedImage(width, height,
214: BufferedImage.TYPE_INT_RGB);
215: if (visibleAreaOnly) {
216: Image visible = getGraphWindow().getVisibleGraphImage(
217: drawGrid, selectedOnly);
218: if (visible instanceof BufferedImage)
219: bufferedImage = (BufferedImage) visible;
220: } else {
221: if (zoomType == TSEGraphWindow.CUSTOM_SIZE)
222: getGraphWindow()
223: .createEntireGraphImage(bufferedImage,
224: zoomType, drawGrid, selectedOnly,
225: width, height);
226: else {
227: int w = (int) getGraphWindow().getGraph()
228: .getFrameBounds().getWidth();
229: int h = (int) getGraphWindow().getGraph()
230: .getFrameBounds().getHeight();
231: if (zoomType == TSEGraphWindow.CURRENT_ZOOM_LEVEL) {
232: w = (int) (width * getGraphWindow().getZoomLevel());
233: h = (int) (height * getGraphWindow().getZoomLevel());
234: } else if (zoomType == TSEGraphWindow.FIT_IN_WINDOW) {
235: double d1 = getGraphWindow().getWidth()
236: / getGraphWindow().getGraph()
237: .getFrameBounds().getWidth();
238: double d2 = getGraphWindow().getHeight()
239: / getGraphWindow().getGraph()
240: .getFrameBounds().getHeight();
241: double ratio = Math.max(d1, d2);
242: w = (int) (w * ratio);
243: h = (int) (h * ratio);
244: }
245:
246: bufferedImage = new BufferedImage(w, h,
247: BufferedImage.TYPE_INT_RGB);
248: getGraphWindow().createEntireGraphImage(bufferedImage,
249: zoomType, drawGrid, selectedOnly, w, h);
250: }
251: }
252: try {
253: if ("jpg".equals(format)
254: && (fo instanceof ImageOutputStream)) // NOI18N
255: {
256: Iterator iter = ImageIO
257: .getImageWritersByFormatName("jpg"); // NOI18N
258: ImageWriter writer = (ImageWriter) iter.next();
259:
260: ImageWriteParam iwp = writer.getDefaultWriteParam();
261: iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
262: iwp.setCompressionQuality(quality / 100);
263: writer.setOutput(fo);
264: IIOImage image = new IIOImage(bufferedImage, null, null);
265: writer.write(null, image, iwp);
266:
267: ((ImageOutputStream) fo).flush();
268: ((ImageOutputStream) fo).close();
269:
270: writer.dispose();
271: }
272:
273: else {
274: if (fo instanceof OutputStream) {
275: if ("svg".equals(format)) // NOI18N
276: {
277: // Get a DOMImplementation.
278: DOMImplementation impl = GenericDOMImplementation
279: .getDOMImplementation();
280:
281: // Create an instance of org.w3c.dom.Document.
282: String svgNS = "http://www.w3.org/2000/svg"; // NOI18N
283: Document myFactory = impl.createDocument(svgNS,
284: "svg", null); // NOI18N
285:
286: SVGGeneratorContext ctx = SVGGeneratorContext
287: .createDefault(myFactory);
288: // # 78585 embed the fonts for drawing strings, so that it does not rely
289: // on system fonts for display
290: ctx.setEmbeddedFontsOn(true);
291: SVGGraphics2D svgGenerator = new SVGGraphics2D(
292: ctx, true);
293:
294: svgGenerator.drawRenderedImage(bufferedImage,
295: new AffineTransform());
296:
297: // Finally, stream out SVG to the output using UTF-8 encoding.
298: boolean useCSS = true; // we want to use CSS style attributes
299: Writer out = new OutputStreamWriter(
300: (OutputStream) fo, "UTF-8"); // NOI18N
301: svgGenerator.stream(out, useCSS);
302: } else {
303: ImageIO.write(bufferedImage, format,
304: (OutputStream) fo);
305: }
306: ((OutputStream) fo).flush();
307: ((OutputStream) fo).close();
308: }
309: }
310: } catch (IOException ioe) {
311: ErrorManager.getDefault().notify(ioe);
312: }
313: }
314:
315: }
|