1 7 8 package org.jdesktop.swing.binding; 9 10 import java.awt.Image ; 11 import java.net.URL ; 12 13 import javax.swing.Icon ; 14 import javax.swing.ImageIcon ; 15 import javax.swing.JComponent ; 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 fieldName) { 27 super(imagePanel, model, fieldName, AbstractBinding.AUTO_VALIDATE_NONE); 28 } 29 30 public JComponent getComponent() { 31 return imagePanel; 32 } 33 34 public void setComponent(JComponent component) { 35 this.imagePanel = (JXImagePanel)component; 36 } 37 38 protected Object getComponentValue() { 39 Class klazz = metaData.getElementClass(); 40 if (klazz == Image .class) { 41 return imagePanel.getImage(); 42 } 43 else if (klazz == Icon .class) { 44 return imagePanel.getIcon(); 45 } 46 return null; 48 } 49 50 protected void setComponentValue(Object value) { 51 Class klazz = metaData.getElementClass(); 52 if (klazz == Image .class) { 53 imagePanel.setImage((Image )value); 54 } else if (klazz == Icon .class) { 55 imagePanel.setIcon((Icon )value); 56 } else if (klazz == String .class) { 57 try { 58 imagePanel.setIcon(new ImageIcon (new URL ((String )value))); 59 } catch (Exception e) { 60 e.printStackTrace(); 61 } 62 } 63 } 64 65 } 66 | Popular Tags |