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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: TextBinding.java,v 1.2 2005/10/10 17:00:56 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 javax.swing.JComponent;
  25. import javax.swing.UIManager;
  26. import javax.swing.event.DocumentEvent;
  27. import javax.swing.event.DocumentListener;
  28. import javax.swing.text.BadLocationException;
  29. import javax.swing.text.Document;
  30. import javax.swing.text.JTextComponent;
  31. import org.jdesktop.binding.DataModel;
  32. /**
  33.  * Class which binds a component that supports editing text values
  34.  * (JTextField, JTextArea, JEditorPane) to a data model field. <p>
  35.  * 
  36.  * JW: hmm, what's the use case? I would prefer to better not do it now, 
  37.  * can cope with that later if necessary.
  38.  * 
  39.  * [Although this binding is most commonly used for Swing's text
  40.  * components, it may be used with any component that defines a
  41.  * <code>javax.swing.text.Document</code> to represent its contents.]
  42.  * 
  43.  * @author Amy Fowler
  44.  * @version 1.0
  45.  */
  46. public class TextBinding extends AbstractBinding {
  47.     private JComponent component;
  48.     private Document document;
  49.     public TextBinding(JTextComponent textComponent,
  50.                             DataModel model, String fieldName) {
  51.         super(textComponent, model, fieldName, AbstractBinding.AUTO_VALIDATE);
  52.         initDocument(textComponent.getDocument());
  53.     }
  54.     public TextBinding(JTextComponent textComponent,
  55.                             DataModel model, String fieldName,
  56.                            int validationPolicy) {
  57.         super(textComponent, model, fieldName, validationPolicy);
  58.         initDocument(textComponent.getDocument());
  59.     }
  60.     public TextBinding(JComponent component, Document document,
  61.                             DataModel dataModel, String fieldName,
  62.                             int validationPolicy) {
  63.         super(component, dataModel, fieldName, validationPolicy);
  64.         initDocument(document);
  65.     }
  66.     public JComponent getComponent() {
  67.         return component;
  68.     }
  69.     protected void setComponent(JComponent component) {
  70.         this.component = component;
  71.         configureEditability();
  72.     }
  73.     protected void configureEditability() {
  74.         if (!(component instanceof JTextComponent)) return;
  75.         JTextComponent textComponent = (JTextComponent) component;
  76.         textComponent.setEditable(!metaData.isReadOnly());
  77.         
  78.     }
  79.     protected void installMetaDataListener() {
  80.         PropertyChangeListener l = new PropertyChangeListener() {
  81.             public void propertyChange(PropertyChangeEvent evt) {
  82.                 if ("readOnly".equals(evt.getPropertyName())) {
  83.                     configureEditability();
  84.                 }
  85.                 
  86.             }
  87.             
  88.         };
  89. //        if (metaData != null) {
  90.             metaData.addPropertyChangeListener(l); 
  91. //        }
  92.     }
  93.     protected Object getComponentValue() {
  94.         String txt;
  95.         try {
  96.             txt = document.getText(0, document.getLength());
  97.         }
  98.         catch (BadLocationException e) {
  99.             txt = null;
  100.         }
  101.         return txt;
  102.     }
  103.     protected void setComponentValue(Object value) {
  104.         try {
  105.             document.remove(0, document.getLength());
  106.             document.insertString(0, convertFromModelType(value), null);
  107.         }
  108.         catch (BadLocationException e) {
  109.             UIManager.getLookAndFeel().provideErrorFeedback(component);
  110.         }
  111.     }
  112.     private void initDocument(Document document) {
  113.         this.document = document;
  114.         document.addDocumentListener(new DocumentListener() {
  115.             public void changedUpdate(DocumentEvent e) {
  116.                 maybeModified();
  117.             }
  118.             public void insertUpdate(DocumentEvent e) {
  119.                 maybeModified();
  120.             }
  121.             public void removeUpdate(DocumentEvent e) {
  122.                 maybeModified();
  123.             }
  124.             public void maybeModified() {
  125.                 if (!pulling) {
  126.                     setModified(true);
  127.                 }
  128.             }
  129.         });
  130.     }
  131. }