001    /*
002      Copyright (C) 2001-2002 Renaud Pawlak, Laurent Martelli
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful, but
010      WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012      Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.aspects.gui.swing;
020    
021    import javax.swing.ImageIcon;
022    import javax.swing.JComponent;
023    import javax.swing.JLabel;
024    import org.objectweb.jac.aspects.gui.FieldView;
025    import org.objectweb.jac.aspects.gui.GuiAC;
026    import org.objectweb.jac.core.rtti.FieldItem;
027    import org.objectweb.jac.util.Thumbnail;
028    
029    /**
030     * A Swing viewer component for image values.
031     */
032    public class ImageViewer extends AbstractFieldView 
033        implements FieldView
034    {
035        JLabel label = new JLabel();
036       
037        /**
038         * Constructs a new date editor. */
039    
040        public ImageViewer(byte[] value, Object substance, FieldItem field) {
041            super(substance,field);
042            setValue(value);
043            add(label);
044        }
045    
046        public ImageViewer() {
047            add(label);
048        }
049    
050        /**
051         * Sets the value of the edited image
052         *
053         * @param value an array of byte
054         */
055        public void setValue(Object value) {
056            if (value!=null) {
057                byte[] data = (byte[])value;
058                if (isCellViewer) {
059                    try {
060                        data = Thumbnail.createThumbArray(
061                            data,
062                            GuiAC.THUMB_MAX_WIDTH,GuiAC.THUMB_MAX_HEIGHT,
063                            GuiAC.THUMB_QUALITY);
064                    } catch(Exception e) {
065                        logger.error("Failed to create thumbnail for "+
066                                     substance+"."+field.getName(),e);
067                    }
068                    label.setIcon(new ImageIcon(data));
069                    setPreferredSize(label.getPreferredSize());
070                } else {
071                    label.setIcon(new ImageIcon(data));
072                }
073            } else {
074                label.setIcon(null);
075            }
076        }
077    
078        protected JComponent getComponent() {
079            return label;
080        }
081    }