KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > display > ExportDisplayAction


1 package prefuse.util.display;
2
3 import java.awt.event.ActionEvent JavaDoc;
4 import java.io.BufferedOutputStream JavaDoc;
5 import java.io.File JavaDoc;
6 import java.io.FileOutputStream JavaDoc;
7 import java.io.OutputStream JavaDoc;
8 import java.util.HashSet JavaDoc;
9
10 import javax.imageio.ImageIO JavaDoc;
11 import javax.swing.AbstractAction JavaDoc;
12 import javax.swing.JFileChooser JavaDoc;
13 import javax.swing.JOptionPane JavaDoc;
14
15 import prefuse.Display;
16 import prefuse.util.io.IOLib;
17 import prefuse.util.io.SimpleFileFilter;
18
19
20 /**
21  * Swing ActionListener that reveals a dialog box that allows users to
22  * export the current Display view to an image file.
23  *
24  * @author <a HREF="http://jheer.org">jeffrey heer</a>
25  */

26 public class ExportDisplayAction extends AbstractAction JavaDoc {
27
28     private Display display;
29     private JFileChooser JavaDoc chooser;
30     private ScaleSelector scaler;
31     
32     /**
33      * Create a new ExportDisplayAction for the given Display.
34      * @param display the Display to capture
35      */

36     public ExportDisplayAction(Display display) {
37         this.display = display;
38     }
39     
40     private void init() {
41         scaler = new ScaleSelector();
42         chooser = new JFileChooser JavaDoc();
43         chooser.setDialogType(JFileChooser.SAVE_DIALOG);
44         chooser.setDialogTitle("Export Prefuse Display...");
45         chooser.setAcceptAllFileFilterUsed(false);
46         
47         HashSet JavaDoc seen = new HashSet JavaDoc();
48         String JavaDoc[] fmts = ImageIO.getWriterFormatNames();
49         for ( int i=0; i<fmts.length; i++ ) {
50             String JavaDoc s = fmts[i].toLowerCase();
51             if ( s.length() == 3 && !seen.contains(s) ) {
52                 seen.add(s);
53                 chooser.setFileFilter(new SimpleFileFilter(s,
54                         s.toUpperCase()+" Image (*."+s+")"));
55             }
56         }
57         seen.clear(); seen = null;
58         chooser.setAccessory(scaler);
59     }
60     
61     /**
62      * Shows the image export dialog and processes the results.
63      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
64      */

65     public void actionPerformed(ActionEvent JavaDoc evt) {
66         // lazy initialization
67
if ( chooser == null )
68             init();
69         
70         // open image save dialog
71
File JavaDoc f = null;
72         scaler.setImage(display.getOffscreenBuffer());
73         int returnVal = chooser.showSaveDialog(display);
74         if (returnVal == JFileChooser.APPROVE_OPTION) {
75            f = chooser.getSelectedFile();
76         } else {
77             return;
78         }
79         String JavaDoc format =
80             ((SimpleFileFilter)chooser.getFileFilter()).getExtension();
81         String JavaDoc ext = IOLib.getExtension(f);
82         if ( !format.equals(ext) ) {
83             f = new File JavaDoc(f.toString()+"."+format);
84         }
85         
86         double scale = scaler.getScale();
87         
88         // save image
89
boolean success = false;
90         try {
91             OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(
92                                 new FileOutputStream JavaDoc(f));
93             System.out.print("Saving image "+f.getName()+", "+
94                              format+" format...");
95             success = display.saveImage(out, format, scale);
96             out.flush();
97             out.close();
98             System.out.println("\tDONE");
99         } catch ( Exception JavaDoc e ) {
100             success = false;
101         }
102         // show result dialog on failure
103
if ( !success ) {
104             JOptionPane.showMessageDialog(display,
105                     "Error Saving Image!",
106                     "Image Save Error",
107                     JOptionPane.ERROR_MESSAGE);
108         }
109     }
110     
111 } // end of class SaveImageAction
112
Popular Tags