1 package org.freehep.demo.iconbrowser;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.util.*;
7 import java.io.*;
8 import org.freehep.graphicsbase.util.export.ExportDialog;
9 import org.freehep.graphicsbase.util.export.ExportFileType;
10
11 import org.freehep.graphicsio.exportchooser.*;
12 import org.freehep.graphicsio.*;
13 import org.freehep.graphicsio.gif.*;
14 import org.freehep.graphicsio.ppm.*;
15 import org.freehep.graphicsio.png.*;
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class SaveAsDialog extends ExportDialog implements SaveAs
30 {
31 private static org.freehep.application.Application app = org.freehep.application.Application.getApplication();
32 private org.freehep.application.services.FileAccess fileAccess;
33
34
35
36
37 public SaveAsDialog()
38 {
39 super(app.getAppName()+" "+app.getVersion(),false);
40 setUserProperties(app.getUserProperties());
41 addExportFileType(new PNGImageExporter());
42
43
44
45 addExportFileType(new GIFImageExporter());
46 }
47 protected boolean writeFile(Component component, ExportFileType t) throws IOException
48 {
49 try
50 {
51 return super.writeFile(component,t);
52 }
53 catch (SecurityException x)
54 {
55 ByteArrayOutputStream os = new ByteArrayOutputStream(100000);
56 try
57 {
58 t.exportToFile(os,component,this, new Properties(), "FreeHEP IconBrowser");
59 }
60 finally
61 {
62 os.close();
63 }
64 InputStream is = new ByteArrayInputStream(os.toByteArray());
65 org.freehep.application.services.FileAccess acc = app.getServiceManager().saveFileAsDialog(null,null,"exportFile",is);
66 return acc != null;
67 }
68 }
69
70
71 private static class GIFImageExporter extends GIFExportFileType
72 {
73 public void exportToFile(OutputStream os, Component c, File file, Component parent,
74 Properties options, String creator) throws IOException
75 {
76 JLabel label = (JLabel) c;
77 GIFEncoder encoder = new GIFEncoder(((ImageIcon) label.getIcon()).getImage(),os,progressive.isSelected());
78 encoder.encode();
79 }
80 }
81 private static class PNGImageExporter extends ImageExportFileType
82 {
83 public PNGImageExporter()
84 {
85 super("png");
86 }
87 public void exportToFile(OutputStream os, Component c, File file, Component parent,
88 Properties options, String creator) throws IOException
89 {
90 JLabel label = (JLabel) c;
91 PNGEncoder encoder = new PNGEncoder(((ImageIcon) label.getIcon()).getImage(),true);
92 byte[] ba = encoder.pngEncode();
93 if (ba == null) throw new IOException("Cannot encode PNG.");
94 os.write(ba);
95 }
96 }
97 private static class PPMImageExporter extends ImageExportFileType
98 {
99 public PPMImageExporter()
100 {
101 super("ppm");
102 }
103 public void exportToFile(OutputStream os, Component c, File file, Component parent,
104 Properties options, String creator) throws IOException
105 {
106 JLabel label = (JLabel) c;
107 PPMEncoder encoder = new PPMEncoder(((ImageIcon) label.getIcon()).getImage(),os);
108 encoder.encode();
109 }
110 }
111
112 }