1 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 ; 15 16 import javax.swing.Icon ; 17 import javax.swing.ImageIcon ; 18 import javax.swing.JComponent ; 19 import javax.swing.JLabel ; 20 21 29 public class LabelBinding extends AbstractBinding { 30 private JLabel label; 31 32 public LabelBinding(JLabel label, 33 DataModel model, String 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 return true; 48 } 49 50 public JComponent getComponent() { 51 return label; 52 } 53 54 protected void setComponent(JComponent component) { 55 label = (JLabel )component; 56 } 57 58 protected Object getComponentValue() { 59 Class clz = metaData.getElementClass(); 60 61 if (clz == Image .class) { 62 Icon icon = label.getIcon(); 63 if (icon instanceof ImageIcon ) { 64 Image image = ( (ImageIcon ) 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 value) { 75 Class clz = metaData.getElementClass(); 76 77 if (clz == Image .class) { 78 if (value != null) { 79 ImageIcon icon = new ImageIcon ( (Image ) 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 |