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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id $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.beans.PropertyChangeSupport;
  25. import java.util.ArrayList;
  26. import java.util.Iterator;
  27. import java.util.List;
  28. import javax.swing.SwingUtilities;
  29. import org.jdesktop.binding.DataModel;
  30. import org.jdesktop.binding.metadata.Validator;
  31. /**
  32.  * Container of Bindings. Responsible for doing validation/push/pull. 
  33.  * Listens to propertyChanges of contained Bindings.
  34.  * 
  35.  * @author Jeanette Winzenburg
  36.  */
  37. public class BindingHandler {
  38.     private List bindings;
  39.     private boolean modified;
  40.     private PropertyChangeSupport propertySupport;
  41.     private PropertyChangeListener bindingListener;
  42.     private boolean autoCommit;
  43.     public boolean isModified() {
  44.         return modified;
  45.     }
  46.     public void add(Binding binding) {
  47.         if (binding == null)
  48.             return;
  49.         getBindingList().add(binding);
  50.         binding.addPropertyChangeListener(getBindingListener());
  51.         // todo: update modified flag
  52.     }
  53.     public void remove(Binding binding) {
  54.         if (binding == null)
  55.             return;
  56.         getBindingList().remove(binding);
  57.         binding.removePropertyChangeListener(getBindingListener());
  58.         // todo: update modified flag
  59.     }
  60.     public void removeAll() {
  61.         for (Iterator iter = getBindingList().iterator(); iter.hasNext();) {
  62.             Binding element = (Binding) iter.next();
  63.             element.removePropertyChangeListener(getBindingListener());
  64.             iter.remove();
  65.         }
  66.         setModified(false);
  67.     }
  68.     public Binding[] getBindings() {
  69.         if (bindings != null) {
  70.             return (Binding[]) bindings.toArray(new Binding[bindings.size()]);
  71.         }
  72.         return new Binding[0];
  73.     }
  74.     public boolean pull() {
  75.         boolean result = true;
  76.         for (Iterator iter = getBindingList().iterator(); iter.hasNext();) {
  77.             Binding element = (Binding) iter.next();
  78.             if (!element.pull()) {
  79.                 result = false;
  80.             }
  81.         }
  82.         return result;
  83.     }
  84.     public boolean validate() {
  85.         boolean result = true;
  86.         ArrayList models = new ArrayList();
  87.         for (Iterator iter = getBindingList().iterator(); iter.hasNext();) {
  88.             Binding element = (Binding) iter.next();
  89.             DataModel bindingModel = element.getDataModel();
  90.             if (!models.contains(bindingModel)) {
  91.                 models.add(bindingModel);
  92.             }
  93.             if (!element.isValid()) {
  94.                 result = false;
  95.             }
  96.         }
  97.         if (result) {
  98.             for (int i = 0; i < models.size(); i++) {
  99.                 DataModel model = (DataModel) models.get(i);
  100.                 Validator validators[] = model.getValidators();
  101.                 for (int j = 0; j < validators.length; j++) {
  102.                     String error[] = new String[1];
  103.                     /** @todo aim: where to put error? */
  104.                     if (!validators[j].validate(model, /* getLocale() */null,
  105.                             error)) {
  106.                         result = false;
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.         return result;
  112.     }
  113.     public boolean push() {
  114.         if (!validate()) {
  115.             return false;
  116.         }
  117.         boolean result = true;
  118.         for (Iterator iter = getBindingList().iterator(); iter.hasNext();) {
  119.             Binding binding = (Binding) iter.next();
  120.             if (!binding.push()) {
  121.                 result = false;
  122.             }
  123.         }
  124.         return result;
  125.     }
  126.     
  127.     // ---------------- property change support
  128.     public void addPropertyChangeListener(PropertyChangeListener l) {
  129.         if (propertySupport == null) {
  130.             propertySupport = new PropertyChangeSupport(this);
  131.         }
  132.         propertySupport.addPropertyChangeListener(l);
  133.     }
  134.     public void removePropertyChangeListener(PropertyChangeListener l) {
  135.         if (propertySupport == null)
  136.             return;
  137.         propertySupport.removePropertyChangeListener(l);
  138.     }
  139.     protected void firePropertyChange(String name, Object oldValue,
  140.             Object newValue) {
  141.         if (propertySupport == null)
  142.             return;
  143.         propertySupport.firePropertyChange(name, oldValue, newValue);
  144.     }
  145.     private PropertyChangeListener getBindingListener() {
  146.         if (bindingListener == null) {
  147.             bindingListener = createBindingListener();
  148.         }
  149.         return bindingListener;
  150.     }
  151.     protected PropertyChangeListener createBindingListener() {
  152.         PropertyChangeListener l = new PropertyChangeListener() {
  153.             public void propertyChange(PropertyChangeEvent evt) {
  154.                 if ("modified".equals(evt.getPropertyName())) {
  155.                    updateModifiedFromBinding((Binding) evt.getSource(), ((Boolean) evt.getNewValue())
  156.                             .booleanValue());
  157.                 }
  158.             }
  159.         };
  160.         return l;
  161.     }
  162.     protected void updateModifiedFromBinding(Binding binding, boolean modified) {
  163.         if (isAutoCommit() && modified) {
  164.             invokePush(binding);
  165.             return;
  166.         }
  167.         if (isModified())
  168.             return;
  169.         setModified(isModified() || modified);
  170.     }
  171.     private void invokePush(final Binding binding) {
  172.         SwingUtilities.invokeLater(new Runnable() {
  173.             public void run() {
  174.                 // PENDING: validation errors?
  175.                 boolean result = binding.push();
  176.                 setModified(!result);
  177.             }
  178.         });
  179.     }
  180.     private List getBindingList() {
  181.         if (bindings == null) {
  182.             bindings = new ArrayList();
  183.         }
  184.         return bindings;
  185.     }
  186.     private void setModified(boolean modified) {
  187.         boolean oldModified = isModified();
  188.         this.modified = modified;
  189.         firePropertyChange("modified", oldModified, isModified());
  190.     }
  191.     /**
  192.      * if true will push the values on every modified notification.
  193.      * 
  194.      * @param autoCommit
  195.      */
  196.     public void setAutoCommit(boolean autoCommit) {
  197.         boolean old = isAutoCommit();
  198.         if (old == autoCommit) {
  199.             return;
  200.         }
  201.         this.autoCommit = autoCommit;
  202.         // think: before or after firing autocommit change?
  203.         if (autoCommit && isModified()) {
  204.             push();
  205.         }
  206.         firePropertyChange("autoCommit", old, isAutoCommit());
  207.         
  208.     }
  209.     public boolean isAutoCommit() {
  210.         // TODO Auto-generated method stub
  211.         return autoCommit;
  212.     }
  213.     
  214. }