KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > AttachmentViewer


1 /*
2   Copyright (C) 2001-2002 Renaud Pawlak, Laurent Martelli
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui.swing;
20
21 import org.objectweb.jac.aspects.gui.FieldView;
22 import org.objectweb.jac.aspects.gui.GuiAC;
23 import org.objectweb.jac.core.rtti.FieldItem;
24 import org.objectweb.jac.lib.Attachment;
25 import org.objectweb.jac.util.Thumbnail;
26 import javax.swing.BoxLayout JavaDoc;
27 import javax.swing.ImageIcon JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30
31 /**
32  * A Swing viewer component for date values.
33  */

34
35 public class AttachmentViewer extends AbstractFieldView
36    implements FieldView
37 {
38    JLabel JavaDoc label = new JLabel JavaDoc();
39    Attachment value;
40    
41    /**
42     * Constructs a new date editor.
43     */

44
45    public AttachmentViewer(Object JavaDoc value, Object JavaDoc substance, FieldItem field) {
46       super(substance,field);
47       setValue(value);
48       add(label);
49    }
50
51    public AttachmentViewer() {
52       isCellViewer = true;
53       setLayout(new BoxLayout JavaDoc(this,BoxLayout.Y_AXIS));
54       label.setAlignmentX((float)0.5);
55       add(label);
56    }
57
58    /**
59     * Sets the value of the edited date.
60     *
61     * @param newValue a <code>Date</code> instance
62     */

63    public void setValue(Object JavaDoc newValue) {
64       // System.out.println("AttachmentViewer.setValue "+newValue);
65
if (newValue!=null) {
66          if (value==newValue)
67             return;
68          Attachment value = (Attachment)newValue;
69          
70          // System.out.println("new value "+newValue);
71

72          if (value.getMimeType()==null) {
73             label.setIcon(null);
74             label.setText(value.getName());
75          } else if (value.getMimeType().startsWith("image/")) {
76             if (isCellViewer) {
77                byte[] thumb = null;
78                try {
79                   thumb = Thumbnail.createThumbArray(
80                      value.getData(),
81                      GuiAC.THUMB_MAX_WIDTH,GuiAC.THUMB_MAX_HEIGHT,
82                      GuiAC.THUMB_QUALITY);
83                } catch(Exception JavaDoc e) {
84                   logger.error("Failed to create thumbnail for "+
85                                substance+"."+field.getName(),e);
86                }
87                label.setIcon(new ImageIcon JavaDoc(thumb));
88                setPreferredSize(label.getPreferredSize());
89             } else {
90                label.setIcon(new ImageIcon JavaDoc(value.getData()));
91             }
92          } else {
93             label.setIcon(null);
94             label.setText(value.getName());
95          }
96       } else {
97          value = (Attachment)newValue;
98          label.setIcon(null);
99          setPreferredSize(label.getPreferredSize());
100       }
101    }
102
103    protected JComponent JavaDoc getComponent() {
104       return label;
105    }
106 }
107
Popular Tags