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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: LabelMetaBinding.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.beans.PropertyChangeEvent;
  23. import java.beans.PropertyChangeListener;
  24. import java.net.URL;
  25. import javax.swing.Icon;
  26. import javax.swing.ImageIcon;
  27. import javax.swing.JComponent;
  28. import javax.swing.JLabel;
  29. import org.jdesktop.binding.DataModel;
  30. import org.jdesktop.swingx.JXHyperlink;
  31. /**
  32.  * Class which binds an uneditable component (JLabel) to the metaData label
  33.  * property of a dataModel's field, adding a colon.
  34.  * @author Amy Fowler
  35.  * @author Jeanette Winzenburg
  36.  */
  37. public class LabelMetaBinding extends AbstractBinding {
  38.     private JLabel label;
  39.     private Icon requiredIcon;
  40.     public LabelMetaBinding(JLabel label,
  41.                         DataModel model, String fieldName) {
  42.         super(label, model, fieldName, AbstractBinding.AUTO_VALIDATE_NONE);
  43.         // auto-pull - we are accessing metaData only
  44.         pull();
  45.     }
  46.     public boolean isModified() {
  47.         return false;
  48.     }
  49.     public boolean isValid() {
  50.         return true;
  51.     }
  52.     public boolean push() {
  53.         // do nothing, value was not edited
  54.         return true;
  55.     }
  56.     public boolean pull() {
  57.         if (metaData != null) {
  58.             setComponentValue(metaData.getLabel());
  59.             updateRequiredFeedBack(metaData.isRequired());
  60.         }
  61.         return true;
  62.     }
  63.     public JComponent getComponent() {
  64.         return label;
  65.     }
  66.     protected void setComponent(JComponent component) {
  67.         label = (JLabel)component;
  68.     }
  69.     protected void installDataModelListener() {
  70.         // do nothing - the label is bound to the metaData.label property
  71.     }
  72.     
  73.     protected void installMetaDataListener() {
  74.         metaData.addPropertyChangeListener(new PropertyChangeListener() {
  75.             // PENDING: listen to enabled
  76.                     public void propertyChange(PropertyChangeEvent evt) {
  77.                         if (!"label".equals(evt.getPropertyName())) return;
  78.                         pull();
  79.                         
  80.                     }
  81.                     
  82.                 });
  83.     }
  84.     protected Object getComponentValue() {
  85.         return label.getText();
  86.     }
  87.     protected void setComponentValue(Object value) {
  88.       label.setText(createColonedText(value));
  89.     }
  90.     protected void updateRequiredFeedBack(boolean required) {
  91.         if (required) {
  92.            label.setIcon(getRequiredIcon()); 
  93.         } else {
  94.             label.setIcon(null);
  95.         }
  96.         
  97.     }
  98.     protected Icon getRequiredIcon() {
  99.         if (requiredIcon == null) {
  100.             URL url = JXHyperlink.class.getResource("resources/asterisk.8x8.png");
  101.             requiredIcon = new ImageIcon(url);
  102.         }
  103.         return requiredIcon;
  104.     }
  105.     protected String createColonedText(Object value) {
  106.         if (isEmpty(value)) {
  107.             return "";
  108.         }
  109.         String text = value.toString();
  110.         return getComponent().getComponentOrientation().isLeftToRight() ?
  111.                 text + getColon() : getColon() + text;
  112.     }
  113.     protected String getColon() {
  114.         return ":";
  115.     }
  116. }