View Javadoc

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   * A "Save As" dialog for saving files in a variety of formats.
19   * This dialog is designed to work in both signed and unsigned web start
20   * applications, as well as standalone applications. WebStart puts some limits
21   * on the possible functionality, for example we can get the full file name,
22   * and it has limited support for setting file filters, so the dialog has
23   * to work around these limitations.
24   *
25   * @author tonyj
26   * @version $Id: SaveAsDialog.java 14939 2013-05-17 20:58:48Z turri $
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      * Creates a new instance of SaveAsDialog.
36      */
37     public SaveAsDialog()
38     {
39        super(app.getAppName()+" "+app.getVersion(),false);
40        setUserProperties(app.getUserProperties());
41        addExportFileType(new PNGImageExporter());
42        //TODO: Understand why this doesn't work, or better build list of available
43        //      export types
44        //addExportFileType(new PPMImageExporter());
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     // FIXME: MD these classes should just use the ExportFileType and their options
70     // and use VectorGraphics to do the imaging, to make sure all options are set.
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 }