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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DataRelation.java,v 1.5 2005/10/10 17:00:57 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 java.util.ArrayList;
  25. import java.util.Collections;
  26. import java.util.List;
  27. import java.util.logging.Logger;
  28. /**
  29.  * 
  30.  * @author rbair
  31.  */
  32. public class DataRelation {
  33.     /**
  34.      * The Logger
  35.      */
  36.     private static final Logger LOG = Logger.getLogger(DataRelation.class.getName());
  37.     
  38.     //protected for testing
  39.     /** Used as a prefix for auto-generated DataRelation names. */
  40.     protected static final String DEFAULT_NAME_PREFIX = "DataRelation";
  41.     /** The shared instance of the NameGenerator for DataRelations not assigned a name. */
  42.     private static final NameGenerator NAMEGEN = new NameGenerator(DEFAULT_NAME_PREFIX);
  43.     /**
  44.      * The DataSet that created this DataRelation. This is a readonly property
  45.      */
  46.     private DataSet dataSet;
  47.     /**
  48.      * The name of the DataRelation.
  49.      */
  50.     private String name;
  51.     /**
  52.      * The DataColumn from a "parent" DataTable to use in establishing this
  53.      * relationship. If you need multiple columns in the parent, create a
  54.      * single calculated column in the parent and use that instead
  55.      */
  56.     private DataColumn parentColumn;
  57.     /**
  58.      * The DataColumn from a "child" DataTable to use in establishing this
  59.      * relationship. childColumn cannot equal DataColumn, but it can come
  60.      * from the same table.
  61.      */
  62.     private DataColumn childColumn;
  63.     
  64.     private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
  65.     /**
  66.      * Create a new DataRelation
  67.      */
  68.     protected DataRelation(DataSet ds) {
  69.         assert ds != null;
  70.         this.dataSet = ds;
  71.         name = NAMEGEN.generateName(this);
  72.     }
  73.     
  74.     protected DataRelation(DataSet ds, String name) {
  75.         this(ds);
  76.         if (name != null) {
  77.             setName(name);
  78.         }
  79.     }
  80.     
  81.     /**
  82.      * @return the DataSet that this DataRelation belongs to
  83.      */
  84.     public DataSet getDataSet() {
  85.         return dataSet;
  86.     }
  87.     
  88. /**
  89.      * Set the name of the DataRelation
  90.  * @param name
  91.  */
  92. public void setName(String name) {
  93.         if (this.name != name) {
  94.             assert DataSetUtils.isValidName(name);
  95.             assert !dataSet.hasElement(name);
  96.             String oldName = this.name;
  97.             this.name = name;
  98.             pcs.firePropertyChange("name", oldName, name);
  99.         }
  100. }
  101.     /**
  102.      * @return The name of this DataRelation
  103.      */
  104. public String getName() {
  105. return name;
  106. }
  107.     /**
  108.      * @return the DataColumn that is the parent in this parent/child relation
  109.      */
  110.     public DataColumn getParentColumn() {
  111.         return parentColumn;
  112.     }
  113.     /**
  114.      * Sets the DataColumn that is the parent in this parent/child relation. The
  115.      * value in the parentColumn for a specified range of field indices will be
  116.      * used to generate the list of child Rows.
  117.      * @param parentColumn
  118.      */
  119.     public void setParentColumn(DataColumn parentColumn) {
  120.         if (this.parentColumn != parentColumn) {
  121.             assert parentColumn != this.childColumn;
  122.             DataColumn oldValue = this.parentColumn;
  123.             this.parentColumn = parentColumn;
  124.             pcs.firePropertyChange("parentColumn",  oldValue, parentColumn);
  125.         }
  126.     }
  127.     /**
  128.      * @return The child DataColumn in this parent/child relation
  129.      */
  130.     public DataColumn getChildColumn() {
  131.         return childColumn;
  132.     }
  133.     /**
  134.      * sets the child DataColumn in this parent/child relation
  135.      * @param childColumn
  136.      */
  137.     public void setChildColumn(DataColumn childColumn) {
  138.         if (this.childColumn != childColumn) {
  139.             assert childColumn != this.parentColumn;
  140.             DataColumn oldValue = this.childColumn;
  141.             this.childColumn = childColumn;
  142.             pcs.firePropertyChange("childColumn",  oldValue, childColumn);
  143.         }
  144.     }
  145.     
  146.     /**
  147.      * Given a DataRow from the parent DataTable, return a list of
  148.      * related DataRows from the child DataTable.
  149.      * @param parentRow
  150.      */
  151.     public List<DataRow> getRows(DataRow parentRow) {
  152.         //return an empty list if I don't have enough info to produce any
  153.         //child rows. Short circuits this method
  154.         if (parentColumn == null || childColumn == null || parentRow == null) {
  155.             return Collections.unmodifiableList(Collections.EMPTY_LIST);
  156.         }
  157.         
  158.         //make sure that this DataRow is from the parent DataTable!
  159.         assert parentRow.getTable().equals(parentColumn.getTable());
  160.         
  161.         DataTable childTable = childColumn.getTable();
  162.         Object parentKey = parentColumn.getTable().getValue(parentRow, parentColumn);
  163.         List<DataRow> rows = new ArrayList<DataRow>();
  164.         for (DataRow childRow : childTable.getRows()) {
  165.             Object childKey = childTable.getValue(childRow,  childColumn);
  166.             if (parentKey != null && childKey != null && parentKey.equals(childKey)) {
  167.                 rows.add(childRow);
  168.             }
  169.         }
  170.         return Collections.unmodifiableList(rows);
  171.     }
  172.     /**
  173.      * Given the index of a row in the parent DataTable, produce a corrosponding
  174.      * list of related rows from the child DataTable
  175.      *
  176.      * @param parentRowIndex
  177.      */
  178.     public List<DataRow> getRows(int parentRowIndex) {
  179.         if (parentColumn == null || childColumn == null || parentRowIndex < 0) {
  180.             return Collections.unmodifiableList(Collections.EMPTY_LIST);
  181.         }
  182.         //NOTE: No need to check the parentRowIndex for an upper out of bounds
  183.         //condition because the table will do so in the parentTable.getRow()
  184.         //method call.
  185.         DataTable parentTable = parentColumn.getTable();
  186.         return getRows(parentTable.getRow(parentRowIndex));
  187.     }
  188.     
  189.     /**
  190.      * Given an array of DataRows, produce the union of the results for each
  191.      * DataRow from the child DataTable
  192.      *
  193.      * @param parentRows
  194.      */
  195.     public List<DataRow> getRows(DataRow[] parentRows) {
  196.         List<DataRow> rows = new ArrayList<DataRow>();
  197.         for (DataRow parentRow : parentRows) {
  198.             rows.addAll(getRows(parentRow));
  199.         }
  200.         return Collections.unmodifiableList(rows);
  201.     }
  202.     
  203.     /**
  204.      * Given an array if parent row indices, produce the union of the results
  205.      * for each index from the child DataTable
  206.      * 
  207.      * @param parentRowIndices
  208.      */
  209.     public List<DataRow> getRows(int[] parentRowIndices) {
  210.         //short circuit the method if I don't have enough info to produce proper
  211.         //results
  212.         if (parentColumn == null || childColumn == null || parentRowIndices == null) {
  213.             return Collections.unmodifiableList(Collections.EMPTY_LIST);
  214.         }
  215.         
  216.         DataTable parentTable = parentColumn.getTable();
  217.         DataRow[] parentRows = new DataRow[parentRowIndices.length];
  218.         for (int i=0; i<parentRows.length; i++) {
  219.             parentRows[i] = parentTable.getRow(parentRowIndices[i]);
  220.         }
  221.         return getRows(parentRows);
  222.     }
  223.     
  224.     /**
  225.      * Given a List of indices, produce the union of the results for each index
  226.      * from the child DataTable
  227.      *
  228.      * @param parentRowIndices
  229.      */
  230.     public List<DataRow> getRows(List<Integer> parentRowIndices) {
  231.         //short circuit this method if I don't have enough info
  232.         if (parentColumn == null || childColumn == null || parentRowIndices == null) {
  233.             return Collections.unmodifiableList(Collections.EMPTY_LIST);
  234.         }
  235.         
  236.         DataTable parentTable = parentColumn.getTable();
  237.         DataRow[] parentRows = new DataRow[parentRowIndices.size()];
  238.         for (int i=0; i<parentRows.length; i++) {
  239.             parentRows[i] = parentTable.getRow(parentRowIndices.get(i));
  240.         }
  241.         return getRows(parentRows);
  242.     }
  243.     /**
  244.      * Adds a PropertyChangeListener to this class for any changes to bean 
  245.      * properties.
  246.      *
  247.      * @param listener The PropertyChangeListener to notify of changes to this 
  248.      * instance.
  249.      */
  250.     public void addPropertyChangeListener(PropertyChangeListener listener) {
  251.         pcs.addPropertyChangeListener(listener);
  252.     }
  253.     
  254.    /**
  255.      * Adds a PropertyChangeListener to this class for specific property changes.
  256.      *
  257.      * @param property The name of the property to listen to changes for.
  258.      * @param listener The PropertyChangeListener to notify of changes to this 
  259.      * instance.
  260.      */
  261.     public void addPropertyChangeListener(String property, PropertyChangeListener listener) {
  262.         pcs.addPropertyChangeListener(property,  listener);
  263.     }
  264.     
  265.     /**
  266.      * Stops notifying a specific listener of any changes to bean properties.
  267.      *
  268.      * @param listener The listener to stop receiving notifications.
  269.      */
  270.     public void removePropertyChangeListener(PropertyChangeListener listener) {
  271.         pcs.removePropertyChangeListener(listener);
  272.     }
  273.     
  274.     /**
  275.      * Stops notifying a specific listener of changes to a specific property.
  276.      *
  277.      * @param propertyName The name of the property to ignore from now on.
  278.      * @param listener The listener to stop receiving notifications.
  279.      */
  280.     public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
  281.         pcs.removePropertyChangeListener(propertyName,  listener);
  282.     }
  283. }