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 org.objectweb.jac.aspects.gui.FieldView;
022    import org.objectweb.jac.aspects.gui.GuiAC;
023    import org.objectweb.jac.core.rtti.FieldItem;
024    import org.objectweb.jac.lib.Attachment;
025    import org.objectweb.jac.util.Thumbnail;
026    import javax.swing.BoxLayout;
027    import javax.swing.ImageIcon;
028    import javax.swing.JComponent;
029    import javax.swing.JLabel;
030    
031    /**
032     * A Swing viewer component for date values.
033     */
034    
035    public class AttachmentViewer extends AbstractFieldView 
036       implements FieldView
037    {
038       JLabel label = new JLabel();
039       Attachment value;
040       
041       /**
042        * Constructs a new date editor. 
043        */
044    
045       public AttachmentViewer(Object value, Object substance, FieldItem field) {
046          super(substance,field);
047          setValue(value);
048          add(label);
049       }
050    
051       public AttachmentViewer() {
052          isCellViewer = true;
053          setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
054          label.setAlignmentX((float)0.5);
055          add(label);
056       }
057    
058       /**
059        * Sets the value of the edited date.
060        *
061        * @param newValue a <code>Date</code> instance 
062        */
063       public void setValue(Object newValue) {
064          //      System.out.println("AttachmentViewer.setValue "+newValue);
065          if (newValue!=null) {
066             if (value==newValue)
067                return;
068             Attachment value = (Attachment)newValue;
069             
070             //         System.out.println("new value "+newValue);
071    
072             if (value.getMimeType()==null) {
073                label.setIcon(null);
074                label.setText(value.getName());
075             } else if (value.getMimeType().startsWith("image/")) {
076                if (isCellViewer) {
077                   byte[] thumb = null;
078                   try {
079                      thumb = Thumbnail.createThumbArray(
080                         value.getData(),
081                         GuiAC.THUMB_MAX_WIDTH,GuiAC.THUMB_MAX_HEIGHT,
082                         GuiAC.THUMB_QUALITY);
083                   } catch(Exception e) {
084                      logger.error("Failed to create thumbnail for "+
085                                   substance+"."+field.getName(),e);
086                   }
087                   label.setIcon(new ImageIcon(thumb));
088                   setPreferredSize(label.getPreferredSize());
089                } else {
090                   label.setIcon(new ImageIcon(value.getData()));
091                }
092             } else {
093                label.setIcon(null);
094                label.setText(value.getName());
095             }
096          } else {
097             value = (Attachment)newValue;
098             label.setIcon(null);
099             setPreferredSize(label.getPreferredSize());
100          }
101       }
102    
103       protected JComponent getComponent() {
104          return label;
105       }
106    }