KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > binding > ImagePanelBinding


1 /*
2  * $Id: ImagePanelBinding.java,v 1.3 2005/02/24 20:35:29 rbair Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.binding;
9
10 import java.awt.Image JavaDoc;
11 import java.net.URL JavaDoc;
12
13 import javax.swing.Icon JavaDoc;
14 import javax.swing.ImageIcon JavaDoc;
15 import javax.swing.JComponent JavaDoc;
16
17 import org.jdesktop.swing.data.DataModel;
18
19 import org.jdesktop.swing.JXImagePanel;
20
21 public class ImagePanelBinding extends AbstractBinding {
22
23     private JXImagePanel imagePanel;
24
25     public ImagePanelBinding(JXImagePanel imagePanel,
26                  DataModel model, String JavaDoc fieldName) {
27         super(imagePanel, model, fieldName, AbstractBinding.AUTO_VALIDATE_NONE);
28     }
29
30     public JComponent JavaDoc getComponent() {
31     return imagePanel;
32     }
33
34     public void setComponent(JComponent JavaDoc component) {
35     this.imagePanel = (JXImagePanel)component;
36     }
37
38     protected Object JavaDoc getComponentValue() {
39     Class JavaDoc klazz = metaData.getElementClass();
40     if (klazz == Image JavaDoc.class) {
41         return imagePanel.getImage();
42     }
43     else if (klazz == Icon JavaDoc.class) {
44         return imagePanel.getIcon();
45     }
46     // default?
47
return null;
48     }
49
50     protected void setComponentValue(Object JavaDoc value) {
51         Class JavaDoc klazz = metaData.getElementClass();
52         if (klazz == Image JavaDoc.class) {
53             imagePanel.setImage((Image JavaDoc)value);
54         } else if (klazz == Icon JavaDoc.class) {
55             imagePanel.setIcon((Icon JavaDoc)value);
56         } else if (klazz == String JavaDoc.class) {
57             try {
58                 imagePanel.setIcon(new ImageIcon JavaDoc(new URL JavaDoc((String JavaDoc)value)));
59             } catch (Exception JavaDoc e) {
60                 e.printStackTrace();
61             }
62         }
63     }
64
65 }
66
Popular Tags