001    package net.sourceforge.mp3dings.editors.imageeditor;
002    import java.io.FileNotFoundException;
003    import java.io.File;
004    import java.io.IOException;
005    import javax.swing.ImageIcon;
006    import javax.swing.JLabel;
007    import java.awt.Image;
008    import java.awt.MediaTracker;
009    import java.awt.Component;
010    import java.net.MalformedURLException;
011    import java.net.URL;
012    import net.sourceforge.mp3dings.Globals;
013    import net.sourceforge.mp3dings.editors.APICImageEditor;
014    
015    /*
016       Copyright (C) 2007 Moritz Ringler
017       This library is free software; you can redistribute it and/or
018       modify it under the terms of the GNU Lesser General Public
019       License as published by the Free Software Foundation; either
020       version 2.1 of the License, or (at your option) any later version.
021       This library is distributed in the hope that it will be useful,
022       but WITHOUT ANY WARRANTY; without even the implied warranty of
023       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
024       Lesser General Public License for more details.
025       You should have received a copy of the GNU Lesser General Public
026       License along with this library; if not, write to the Free Software
027       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
028      */
029    
030    public class ImageIconRenderer implements ImageRenderer{
031        private final int maxDimension;
032        private static final String[] supportedMimeTypes =
033        new String[]{
034            "image/jpeg",
035            "image/gif",
036            "image/png"
037        };
038    
039        public ImageIconRenderer(int maxDimension){
040            if(maxDimension < 1){
041                throw new IllegalArgumentException();
042            }
043            this.maxDimension = maxDimension;
044        }
045    
046        public ImageComponent getImageComponent(String mimetype, byte[] imagedata, File f, String url) throws IOException{
047             Component comp = null;
048             try{
049                 comp  = new ImageLabel(maxDimension, imagedata, f, url);
050             } catch (FileNotFoundException fnfx){
051                 throw fnfx;
052             } catch (IOException iox){
053                 if(isMimeTypeSupported(mimetype)){
054                         throw iox;
055                 }
056             }
057             return (comp == null)? null : APICImageEditor.asImageComponent(comp);
058        }
059    
060        public boolean isMimeTypeSupported(String mime){
061            boolean result = false;
062            for(int i=0; i<supportedMimeTypes.length; i++){
063                if(supportedMimeTypes[i].equals(mime)){
064                    result = true;
065                    break;
066                }
067            }
068            return result;
069        }
070    
071        public String[] getSupportedMimeTypes(){
072            return (String[]) supportedMimeTypes.clone();
073        }
074    
075        private static class ImageLabel extends JLabel{
076            private ImageIcon icon;
077            private final int maxDim;
078    
079            public ImageLabel(int maxDim, byte[] imagedata, File f, String url) throws IOException{
080                super();
081                this.maxDim = maxDim;
082                setHorizontalAlignment(JLabel.CENTER);
083                setHorizontalTextPosition(JLabel.CENTER);
084                setVerticalTextPosition(JLabel.TOP);
085                if(imagedata != null){
086                    icon = new ImageIcon(imagedata);
087                } else if (f != null){
088                    icon = new ImageIcon(f.getAbsolutePath());
089                } else {
090                    try{
091                        icon = new ImageIcon(new URL(url));
092                    } catch (MalformedURLException mux){
093                        IOException iox = new IOException(mux.toString());
094                        iox.initCause(mux);
095                        throw iox;
096                    }
097                }
098                switch(icon.getImageLoadStatus()){
099                case MediaTracker.ERRORED:
100                    throw new IOException(Globals.getString("IMAGE_IOERROR"));
101                case MediaTracker.ABORTED:
102                    throw new IOException(Globals.getString("IMAGE_ABORTED"));
103                default:
104                    setText(scale()?Globals.getString("IMAGE_SCALED"):null);
105                    setIcon(icon);
106                }
107            }
108    
109            private boolean scale(){
110                int h = icon.getIconHeight();
111                int w = icon.getIconWidth();
112                float scale = Math.max(h, w) * 1.0f/maxDim;
113                if(scale > 1){
114                    Image img = icon.getImage();
115                    img = img.getScaledInstance((int) (w/scale), (int) (h/scale), Image.SCALE_SMOOTH);
116                    icon.setImage(img);
117                    return true;
118                }
119                return false;
120            }
121        }
122    }