KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: LabelBinding.java,v 1.2 2004/06/24 18:52:45 davidson1 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 org.jdesktop.swing.data.DataModel;
11 import org.jdesktop.swing.data.Link;
12 import org.jdesktop.swing.data.MetaData;
13
14 import java.awt.Image JavaDoc;
15
16 import javax.swing.Icon JavaDoc;
17 import javax.swing.ImageIcon JavaDoc;
18 import javax.swing.JComponent JavaDoc;
19 import javax.swing.JLabel JavaDoc;
20
21 /**
22  * Class which binds an uneditable component (JLabel) to a data model field
23  * of arbitrary type. If the field is type Image, then the image will be
24  * displayed as an icon in the component. For all other types, the data model
25  * value will be converted and displayed as a String.
26  * @author Amy Fowler
27  * @version 1.0
28  */

29 public class LabelBinding extends AbstractBinding {
30     private JLabel JavaDoc label;
31
32     public LabelBinding(JLabel JavaDoc label,
33                         DataModel model, String JavaDoc fieldName) {
34         super(label, model, fieldName, AbstractBinding.AUTO_VALIDATE_NONE);
35     }
36
37     public boolean isModified() {
38         return false;
39     }
40
41     public boolean isValid() {
42         return true;
43     }
44
45     public boolean push() {
46         // do nothing, value was not edited
47
return true;
48     }
49
50     public JComponent JavaDoc getComponent() {
51         return label;
52     }
53
54     protected void setComponent(JComponent JavaDoc component) {
55         label = (JLabel JavaDoc)component;
56     }
57
58     protected Object JavaDoc getComponentValue() {
59     Class JavaDoc clz = metaData.getElementClass();
60
61         if (clz == Image JavaDoc.class) {
62             Icon JavaDoc icon = label.getIcon();
63             if (icon instanceof ImageIcon JavaDoc) {
64                 Image JavaDoc image = ( (ImageIcon JavaDoc) icon).getImage();
65                 return image;
66             }
67         }
68     if (clz == Link.class) {
69         return (Link)label.getClientProperty("jdnc.link.value");
70     }
71         return label.getText();
72     }
73
74     protected void setComponentValue(Object JavaDoc value) {
75     Class JavaDoc clz = metaData.getElementClass();
76
77         if (clz == Image JavaDoc.class) {
78             if (value != null) {
79                 ImageIcon JavaDoc icon = new ImageIcon JavaDoc( (Image JavaDoc) value);
80                 label.setIcon(icon);
81             }
82         }
83     if (clz == Link.class) {
84         if (value != null) {
85         label.setText("<html>" + convertFromModelType(value) + "</html>");
86         label.putClientProperty("jdnc.link.value", (Link)value);
87         }
88     }
89         else {
90             label.setText(convertFromModelType(value));
91         }
92     }
93 }
94
Popular Tags