|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| PNGTools.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* EJTools, the Enterprise Java Tools
|
|
| 3 |
*
|
|
| 4 |
* Distributable under LGPL license.
|
|
| 5 |
* See terms of license at www.gnu.org.
|
|
| 6 |
*/
|
|
| 7 |
package org.ejtools.util.export;
|
|
| 8 |
|
|
| 9 |
import java.awt.Component;
|
|
| 10 |
import java.awt.image.BufferedImage;
|
|
| 11 |
import java.awt.image.RenderedImage;
|
|
| 12 |
import java.io.File;
|
|
| 13 |
import java.io.IOException;
|
|
| 14 |
|
|
| 15 |
import javax.swing.JFileChooser;
|
|
| 16 |
import javax.swing.filechooser.FileFilter;
|
|
| 17 |
|
|
| 18 |
import org.apache.log4j.Logger;
|
|
| 19 |
import org.ejtools.util.Platform;
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
/**
|
|
| 23 |
* Description of the Class
|
|
| 24 |
*
|
|
| 25 |
* @author Laurent Etiemble
|
|
| 26 |
* @version $Revision: 1.2 $
|
|
| 27 |
*/
|
|
| 28 |
public class PNGTools |
|
| 29 |
{
|
|
| 30 |
/** Description of the Field */
|
|
| 31 |
private static FileFilter PNG_FILE_FILTER = |
|
| 32 |
new FileFilter()
|
|
| 33 |
{
|
|
| 34 | 0 |
public boolean accept(File file) |
| 35 |
{
|
|
| 36 | 0 |
return file.getName().endsWith(".png"); |
| 37 |
} |
|
| 38 |
|
|
| 39 |
|
|
| 40 | 0 |
public String getDescription()
|
| 41 |
{
|
|
| 42 | 0 |
return "PNG image (*.png)"; |
| 43 |
} |
|
| 44 |
}; |
|
| 45 |
/** Description of the Field */
|
|
| 46 |
private static Logger logger = Logger.getLogger(PNGTools.class); |
|
| 47 |
|
|
| 48 |
|
|
| 49 |
/**Constructor for the PNGCreator object */
|
|
| 50 | 0 |
private PNGTools()
|
| 51 |
{
|
|
| 52 | 0 |
super();
|
| 53 |
} |
|
| 54 |
|
|
| 55 |
|
|
| 56 |
/**
|
|
| 57 |
* Description of the Method
|
|
| 58 |
*
|
|
| 59 |
* @param image Description of the Parameter
|
|
| 60 |
* @param output Description of the Parameter
|
|
| 61 |
*/
|
|
| 62 | 0 |
public static void exportAsPNG(RenderedImage image, File output) |
| 63 |
{
|
|
| 64 | 0 |
if (Platform.isJavaVersionCompatible(Platform.JAVA_1_4))
|
| 65 |
{
|
|
| 66 | 0 |
try
|
| 67 |
{
|
|
| 68 |
// Qualified name is mandatory
|
|
| 69 | 0 |
javax.imageio.ImageIO.write(image, "PNG", output);
|
| 70 |
} |
|
| 71 |
catch (IOException ioe)
|
|
| 72 |
{
|
|
| 73 | 0 |
logger.error("Can't export image as PNG", ioe);
|
| 74 |
} |
|
| 75 |
} |
|
| 76 |
} |
|
| 77 |
|
|
| 78 |
|
|
| 79 |
/**
|
|
| 80 |
* Description of the Method
|
|
| 81 |
*
|
|
| 82 |
* @param component Description of the Parameter
|
|
| 83 |
* @param output Description of the Parameter
|
|
| 84 |
*/
|
|
| 85 | 0 |
public static void exportAsPNG(Component component, File output) |
| 86 |
{
|
|
| 87 | 0 |
if (Platform.isJavaVersionCompatible(Platform.JAVA_1_4))
|
| 88 |
{
|
|
| 89 | 0 |
try
|
| 90 |
{
|
|
| 91 | 0 |
BufferedImage image = paintAsPNG(component); |
| 92 |
// Qualified name is mandatory
|
|
| 93 | 0 |
javax.imageio.ImageIO.write(image, "PNG", output);
|
| 94 |
} |
|
| 95 |
catch (IOException ioe)
|
|
| 96 |
{
|
|
| 97 | 0 |
logger.error("Can't export image as PNG", ioe);
|
| 98 |
} |
|
| 99 |
} |
|
| 100 |
} |
|
| 101 |
|
|
| 102 |
|
|
| 103 |
/**
|
|
| 104 |
* Description of the Method
|
|
| 105 |
*
|
|
| 106 |
* @param component Description of the Parameter
|
|
| 107 |
* @return Description of the Return Value
|
|
| 108 |
*/
|
|
| 109 | 0 |
public static BufferedImage paintAsPNG(Component component) |
| 110 |
{
|
|
| 111 | 0 |
BufferedImage image = null;
|
| 112 |
|
|
| 113 | 0 |
image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
| 114 | 0 |
component.paint(image.getGraphics()); |
| 115 |
|
|
| 116 | 0 |
return image;
|
| 117 |
} |
|
| 118 |
|
|
| 119 |
|
|
| 120 |
/**
|
|
| 121 |
* Description of the Method
|
|
| 122 |
*
|
|
| 123 |
* @return Description of the Return Value
|
|
| 124 |
*/
|
|
| 125 | 0 |
public static File selectPNGFile() |
| 126 |
{
|
|
| 127 |
// Fix for JFileChooser/SecurityManager bug (#4264750)
|
|
| 128 | 0 |
SecurityManager s = System.getSecurityManager(); |
| 129 | 0 |
System.setSecurityManager(null);
|
| 130 |
|
|
| 131 |
// Choose file
|
|
| 132 | 0 |
JFileChooser chooser = new JFileChooser(System.getProperty("user.dir")); |
| 133 | 0 |
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); |
| 134 | 0 |
chooser.setFileFilter(PNG_FILE_FILTER); |
| 135 | 0 |
int returnVal = chooser.showSaveDialog(null); |
| 136 | 0 |
System.setSecurityManager(s); |
| 137 | 0 |
if (returnVal != JFileChooser.APPROVE_OPTION)
|
| 138 |
{
|
|
| 139 | 0 |
return null; |
| 140 |
} |
|
| 141 | 0 |
return chooser.getSelectedFile();
|
| 142 |
} |
|
| 143 |
} |
|
| 144 |
|
|
||||||||||