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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DataValue.java,v 1.5 2005/10/10 17:01:00 rbair Exp $
  3.  *
  4.  * Copyright 2005 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.dataset;
  22. import java.beans.PropertyChangeListener;
  23. import java.beans.PropertyChangeSupport;
  24. import net.sf.jga.fn.EvaluationException;
  25. import net.sf.jga.fn.Generator;
  26. import net.sf.jga.fn.adaptor.Constant;
  27. import net.sf.jga.parser.ParseException;
  28. import net.sf.jga.parser.UncheckedParseException;
  29. /**
  30.  * <p>A DataValue is represents an expression attached to a DataSet, which 
  31.  * can be evaluated in the context of that DataSet to produce an Object end-value.
  32.  * The expression is a String expression intended to be processed by a subclass
  33.  * so that the {@link #getValue()} method returns an Object representing the 
  34.  * expression's result. 
  35.  *
  36.  * <p>A <code>DataValue</code> has a name, which should be unique for a given 
  37.  * <code>DataSet</code>.
  38.  *
  39.  * <p>A <code>DataValue</code> belongs to a single <code>DataSet</code>.
  40.  *
  41.  * @see DataSet
  42.  *
  43.  * @author rbair
  44.  */
  45.  // TODO implement a PropertyChangeListener on the synthetic "value" field.
  46.  // When some value that the DataValue expression depends on changes, it
  47.  // becomes necessary to recompute the "value", and then notify that the
  48.  // "value" has changed.
  49. public class DataValue {
  50.     //protected for testing
  51.     /** Used as a prefix for auto-generated names. */
  52.     protected static final String DEFAULT_NAME_PREFIX = "DataValue";
  53.     
  54.     /** The shared instance of the NameGenerator for DataValues not assigned a name. */
  55.     private static final NameGenerator NAMEGEN = new NameGenerator(DEFAULT_NAME_PREFIX);
  56.     //used for communicating changes to this JavaBean, especially necessary for
  57.     //IDE tools, but also handy for any other component listening to this data value
  58.     private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
  59.     /** The DataSet instance that owns this DataValue. */
  60.     private DataSet dataSet;
  61.     
  62.     /** The name of this DataValue. */
  63.     private String name;
  64.    
  65.     /** 
  66.      * The expression which, when evaluated, returns the actual value for this
  67.      * DataValue. 
  68.      */
  69.     private String expression;
  70.     private Generator<?> exprImpl = new Constant<Object>(null);
  71.     
  72.     /** 
  73.      * Creates a new instance of DataValue with an auto-generated name, for a 
  74.      * given DataSet.
  75.      *
  76.      * @param ds The DataSet that owns this DataValue
  77.      */
  78.     public DataValue(DataSet ds) {
  79.         assert ds != null;
  80.         this.dataSet = ds;
  81.         name = NAMEGEN.generateName(this);
  82.     }
  83.     
  84.     /** 
  85.      * Creates a new instance of DataValue, 
  86.      * for a given DataSet.
  87.      *
  88.      * @param ds The DataSet that owns this DataValue
  89.      * @param name The DataValue's name.
  90.      */
  91.     public DataValue(DataSet ds, String name) {
  92.         this(ds);
  93.         if (name != null) {
  94.             setName(name);
  95.         }
  96.     }
  97.     
  98. /**
  99.      * Changes the name for this DataValue.
  100.  * @param name The new name.
  101.  */
  102. public void setName(String name) {
  103.         if (this.name != name) {
  104.             assert DataSetUtils.isValidName(name);
  105.             String oldName = this.name;
  106.             this.name = name;
  107.             pcs.firePropertyChange("name", oldName, name);
  108.         }
  109. }
  110.     /** 
  111.      * Returns the current name for this DataValue.
  112.      *
  113.      * @return the DataValue's name.
  114.      */
  115. public String getName() {
  116. return name;
  117. }
  118.     /** 
  119.      * Returns the DataSet this DataValue belongs to.
  120.      *
  121.      * @return the DataSet this DataValue belongs to.
  122.      */
  123.     public DataSet getDataSet() {
  124.         return dataSet;
  125.     }
  126.     
  127.     /**
  128.      * Returns the expression which will be evaluated to result in an actual
  129.      * value for this DataValue.
  130.      *
  131.      * @return the expression that underlies this DataValue
  132.      */
  133.     public String getExpression() {
  134.         return expression;
  135.     }
  136.     
  137.     /**
  138.      * Sets the string expression which, when evaluated, returns a DataValue.
  139.      * 
  140.      * @param expression The new expression for this DataValue.
  141.      */
  142.     public void setExpression(String expression) {
  143.         if (expression ==  null || expression.equals(""))
  144.             exprImpl = new Constant<Object>(null);
  145.         else {
  146.             try {
  147.                 exprImpl = getParser().parseDataValue(expression);
  148.             }
  149.             catch (ParseException x) { throw new UncheckedParseException(x); }
  150.         }
  151.         this.expression = expression;
  152.     }
  153.     
  154.     /**
  155.      * Returns the actual value resulting from the DataValue's expression being
  156.      * evaluated in the context of a DataSet. This value may be constant or may
  157.      * change on each invocation--that's up to the expression.
  158.      *
  159.      * @return the actual value of this DataValue, once evaluated.
  160.      */
  161.     // TODO: this should be abstract (PWW 04/28/04)
  162.     public Object getValue() {
  163.         try {
  164.             return exprImpl.gen();
  165.         } catch (EvaluationException e) {
  166.             e.printStackTrace();
  167.             return null;
  168.         }
  169.     }
  170.     Parser getParser() { 
  171.         return dataSet.getParser();
  172.     }
  173.     /**
  174.      * Adds a PropertyChangeListener to this class for any changes to bean 
  175.      * properties.
  176.      *
  177.      * @param listener The PropertyChangeListener to notify of changes to this 
  178.      * instance.
  179.      */
  180.     public void addPropertyChangeListener(PropertyChangeListener listener) {
  181.         pcs.addPropertyChangeListener(listener);
  182.     }
  183.     
  184.    /**
  185.      * Adds a PropertyChangeListener to this class for specific property changes.
  186.      *
  187.      * @param property The name of the property to listen to changes for.
  188.      * @param listener The PropertyChangeListener to notify of changes to this 
  189.      * instance.
  190.      */
  191.     public void addPropertyChangeListener(String property, PropertyChangeListener listener) {
  192.         pcs.addPropertyChangeListener(property,  listener);
  193.     }
  194.     
  195.     /**
  196.      * Stops notifying a specific listener of any changes to bean properties.
  197.      *
  198.      * @param listener The listener to stop receiving notifications.
  199.      */
  200.     public void removePropertyChangeListener(PropertyChangeListener listener) {
  201.         pcs.removePropertyChangeListener(listener);
  202.     }
  203.     
  204.     /**
  205.      * Stops notifying a specific listener of changes to a specific property.
  206.      *
  207.      * @param propertyName The name of the property to ignore from now on.
  208.      * @param listener The listener to stop receiving notifications.
  209.      */
  210.     public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
  211.         pcs.removePropertyChangeListener(propertyName,  listener);
  212.     }    
  213. }