01: package org.enhydra.jawe.components.graph.actions;
02:
03: import java.awt.Dimension;
04: import java.awt.Graphics2D;
05: import java.awt.Rectangle;
06: import java.awt.event.ActionEvent;
07: import java.awt.image.BufferedImage;
08: import java.io.FileOutputStream;
09:
10: import javax.swing.JOptionPane;
11:
12: import org.enhydra.jawe.ActionBase;
13: import org.enhydra.jawe.JaWEComponent;
14: import org.enhydra.jawe.JaWEManager;
15: import org.enhydra.jawe.ResourceManager;
16: import org.enhydra.jawe.components.graph.Graph;
17: import org.enhydra.jawe.components.graph.GraphController;
18:
19: import com.sun.image.codec.jpeg.JPEGCodec;
20: import com.sun.image.codec.jpeg.JPEGImageEncoder;
21:
22: public class SaveAsJPG extends ActionBase {
23:
24: public SaveAsJPG(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: try {
39: String file = JaWEManager
40: .getInstance()
41: .getJaWEController()
42: .getJaWEFrame()
43: .saveDialog(
44: ResourceManager
45: .getLanguageDependentString("SaveAsJPGLabel"),
46: 1,
47: JaWEManager
48: .getInstance()
49: .getDisplayNameGenerator()
50: .getDisplayName(
51: ((GraphController) jawecomponent)
52: .getSelectedGraph()
53: .getXPDLObject()));
54: if (file != null && file.length() > 0) {
55: saveGraphAsJPG(file, ((GraphController) jawecomponent)
56: .getSelectedGraph());
57: }
58: } catch (Exception ex) {
59: String msg = ResourceManager
60: .getLanguageDependentString("ErrorJPGSavingFailed");
61: JaWEManager.getInstance().getJaWEController()
62: .getJaWEFrame().message(msg,
63: JOptionPane.WARNING_MESSAGE);
64: }
65: }
66:
67: public static void saveGraphAsJPG(String file, Graph graph)
68: throws Exception {
69: BufferedImage img = null;
70: Object[] cells = graph.getRoots();
71:
72: if (cells.length > 0) {
73: Rectangle bounds = graph.getCellBounds(cells).getBounds();// HM, JGraph3.4.1
74: graph.toScreen(bounds);
75:
76: // Create a Buffered Image
77: Dimension d = bounds.getSize();
78: img = new BufferedImage(d.width, d.height,
79: BufferedImage.TYPE_INT_RGB);
80: Graphics2D graphics = img.createGraphics();
81: graph.paint(graphics);
82: }
83:
84: FileOutputStream fos = new FileOutputStream(file);
85: JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
86: encoder.encode(img);
87: fos.flush();
88: fos.close();
89: }
90:
91: }
|