001 package net.sourceforge.mp3dings.editors.imageeditor;
002 import java.io.FileNotFoundException;
003 import java.io.ByteArrayInputStream;
004 import java.io.File;
005 import java.io.IOException;
006 import javax.swing.ImageIcon;
007 import javax.imageio.ImageIO;
008 import javax.swing.JLabel;
009 import java.awt.Image;
010 import java.util.Arrays;
011 import java.util.logging.Logger;
012 import java.awt.Component;
013 import java.awt.event.MouseAdapter;
014 import java.awt.event.MouseEvent;
015 import java.net.MalformedURLException;
016 import java.net.URL;
017 import net.sourceforge.mp3dings.editors.APICImageEditor;
018 import net.sourceforge.mp3dings.Globals;
019 import de.mospace.lang.BrowserLauncher;
020
021 /*
022 Copyright (C) 2007 Moritz Ringler
023 This library is free software; you can redistribute it and/or
024 modify it under the terms of the GNU Lesser General Public
025 License as published by the Free Software Foundation; either
026 version 2.1 of the License, or (at your option) any later version.
027 This library is distributed in the hope that it will be useful,
028 but WITHOUT ANY WARRANTY; without even the implied warranty of
029 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
030 Lesser General Public License for more details.
031 You should have received a copy of the GNU Lesser General Public
032 License along with this library; if not, write to the Free Software
033 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
034 */
035
036 public class JAIRenderer implements ImageRenderer{
037 private final int maxDimension;
038 private static final String[] supportedMimeTypes = ImageIO.getReaderMIMETypes();
039 private static final String[] jaiToolsMimeTypes =
040 new String[]{
041 "image/bmp",
042 "image/gif",
043 "image/jp2",
044 "image/jpeg",
045 "image/jpeg2000",
046 "image/png",
047 "image/tiff",
048 "image/vnd.wap.wbmp",
049 "image/x-bmp",
050 "image/x-png",
051 "image/x-portable-anymap",
052 "image/x-portable-bitmap",
053 "image/x-portable-graymap",
054 "image/x-portable-pixmap",
055 "image/x-windows-bmp"};
056 static {
057 Arrays.sort(supportedMimeTypes);
058 }
059
060 public JAIRenderer(int maxDimension){
061 if(maxDimension < 1){
062 throw new IllegalArgumentException();
063 }
064 this.maxDimension = maxDimension;
065 }
066
067 public ImageComponent getImageComponent(String mimetype, byte[] imagedata, File f, String url) throws IOException{
068 Component comp = null;
069 try{
070 comp = new ImageLabel(mimetype, maxDimension, imagedata, f, url);
071 } catch (FileNotFoundException fnfx){
072 throw fnfx;
073 } catch (IOException iox){
074 if(isMimeTypeSupported(mimetype)){
075 throw iox;
076 }
077 }
078 return (comp == null)? null : APICImageEditor.asImageComponent(comp);
079 }
080
081 public boolean isMimeTypeSupported(String mime){
082 return (Arrays.binarySearch(supportedMimeTypes, mime) >= 0);
083 }
084
085 private static boolean isAJAIToolsType(String mime){
086 return (Arrays.binarySearch(jaiToolsMimeTypes, mime) >= 0);
087 }
088
089 public String[] getSupportedMimeTypes(){
090 return (String[]) supportedMimeTypes.clone();
091 }
092
093 private static class ImageLabel extends JLabel{
094 private static final Logger logger = Logger.getLogger(ImageLabel.class.getPackage().getName());
095 private ImageIcon icon;
096 private final int maxDim;
097
098 public ImageLabel(String mime, int maxDim, byte[] imagedata, File f, String url) throws IOException{
099 super();
100 this.maxDim = maxDim;
101 setHorizontalAlignment(JLabel.CENTER);
102 setHorizontalTextPosition(JLabel.CENTER);
103 setVerticalTextPosition(JLabel.TOP);
104 Image image = null;
105 if(imagedata != null){
106 image = ImageIO.read(new ByteArrayInputStream(imagedata));
107 } else if (f != null){
108 image = ImageIO.read(f);
109 } else {
110 try{
111 image = ImageIO.read((new URL(url)).openStream());
112 } catch (MalformedURLException mux){
113 IOException iox = new IOException(mux.toString());
114 iox.initCause(mux);
115 throw iox;
116 }
117 }
118 if(image == null){
119 StringBuffer message = new StringBuffer("<html>"+Globals.getString("IMAGE_CANNOT_DISPLAY"));
120 if(isAJAIToolsType(mime)){
121 message.append(Globals.getString("IMAGE_JAIIO_TOOLS"));
122 addMouseListener(new MouseAdapter(){
123 public void mouseClicked(MouseEvent e){
124 if(e.getButton() == MouseEvent.BUTTON1 &&
125 e.getClickCount() == 1 &&
126 !e.isPopupTrigger()){
127 try{
128 BrowserLauncher.openURL("https://jai-imageio.dev.java.net/binary-builds.html");
129 } catch (IOException ex){
130 logger.warning(ex.toString());
131 }
132 }
133 }
134 });
135 }
136 setText(message.toString());
137 } else {
138 icon = new ImageIcon(image);
139 setText(scale()?Globals.getString("IMAGE_SCALED"):null);
140 setIcon(icon);
141 }
142 }
143
144 private boolean scale(){
145 int h = icon.getIconHeight();
146 int w = icon.getIconWidth();
147 float scale = Math.max(h, w) * 1.0f/maxDim;
148 if(scale > 1){
149 Image img = icon.getImage();
150 img = img.getScaledInstance((int) (w/scale), (int) (h/scale), Image.SCALE_SMOOTH);
151 icon.setImage(img);
152 return true;
153 }
154 return false;
155 }
156 }
157 }