LabelBinding.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:3k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: LabelBinding.java,v 1.2 2005/10/10 17:00:51 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.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.binding.swingx;
  22. import java.awt.Image;
  23. import javax.swing.Icon;
  24. import javax.swing.ImageIcon;
  25. import javax.swing.JComponent;
  26. import javax.swing.JLabel;
  27. import org.jdesktop.binding.DataModel;
  28. import org.jdesktop.swingx.LinkModel;
  29. /**
  30.  * Class which binds an uneditable component (JLabel) to a data model field
  31.  * of arbitrary type.  If the field is type Image, then the image will be
  32.  * displayed as an icon in the component.  For all other types, the data model
  33.  * value will be converted and displayed as a String.
  34.  * @author Amy Fowler
  35.  * @version 1.0
  36.  */
  37. public class LabelBinding extends AbstractBinding {
  38.     private JLabel label;
  39.     public LabelBinding(JLabel label,
  40.                         DataModel model, String fieldName) {
  41.         super(label, model, fieldName, AbstractBinding.AUTO_VALIDATE_NONE);
  42.     }
  43.     public boolean isModified() {
  44.         return false;
  45.     }
  46.     public boolean isValid() {
  47.         return true;
  48.     }
  49.     public boolean push() {
  50.         // do nothing, value was not edited
  51.         return true;
  52.     }
  53.     public JComponent getComponent() {
  54.         return label;
  55.     }
  56.     protected void setComponent(JComponent component) {
  57.         label = (JLabel)component;
  58.     }
  59.     protected Object getComponentValue() {
  60. Class clz = metaData.getElementClass();
  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 == LinkModel.class) {
  69.     return (LinkModel)label.getClientProperty("jdnc.link.value");
  70. }
  71.         return label.getText();
  72.     }
  73.     protected void setComponentValue(Object value) {
  74. Class clz = metaData.getElementClass();
  75.         if (clz == Image.class) {
  76.             if (value != null) {
  77.                 ImageIcon icon = new ImageIcon( (Image) value);
  78.                 label.setIcon(icon);
  79.             }
  80.         }
  81. if (clz == LinkModel.class) {
  82.     if (value != null) {
  83. label.setText("<html>" + convertFromModelType(value) + "</html>");
  84. label.putClientProperty("jdnc.link.value", (LinkModel)value);
  85.     }
  86. }
  87.         else {
  88.             label.setText(convertFromModelType(value));
  89.         }
  90.     }
  91. }