datarelationtable.java

来自「java实现浏览器等本地桌面的功能」· Java 代码 · 共 396 行 · 第 1/2 页

JAVA
396
字号
/* * $Id: DataRelationTable.java,v 1.9 2005/10/15 11:43:19 pdoubleya Exp $ * * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. *  * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. *  * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */package org.jdesktop.dataset;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.logging.Logger;import org.jdesktop.dataset.event.DataTableListener;import org.jdesktop.dataset.event.RowChangeEvent;import org.jdesktop.dataset.event.TableChangeEvent;/** * * @author rbair */public class DataRelationTable extends DataTable {    /**     * The Logger     */    private static final Logger LOG = Logger.getLogger(DataRelationTable.class.getName());    /**     * The relation that is used to populate this DataRelationTable     */    private DataRelation relation;    /**     * The selector in the parent table that is used to indicate what parent     * rows to retrieve child rows for     */    private DataSelector parentSelector;    /**     * The parentTable that is used to indicate what child rows to retrieve. If     * parentSelector is defined, then parentTable is ignored. Otherwise, all     * of the contents of the parentTable will be used to retrieve rows in the     * childTable.     * <p>     * This is of particular use when the parentTable is another DataRelationTable,     * and therefore a subset of the original DataTable.     */    private DataTable parentTable;    /**     * A selection listener that is affixed to the parentSelector. Whenever the     * selection changes in the parentSelector, this is notified and refreshes     * this table     */    private SelectionListener listener = new SelectionListener();    /**     * Listens to changes on the parentTable, if there IS a parentTable     */    private ParentTableListener pListener = new ParentTableListener();    /**     * Listens to changes on the childTable, if there IS a childTable     */    private ChildTableListener cListener = new ChildTableListener();    /**      * Creates a new instance of DataRelationTable      */    public DataRelationTable(DataSet ds) {        super(ds);    }        public DataRelationTable(DataSet ds, String name) {        super(ds, name);    }        /**     * Set the relation that this DataRelationTable uses     * @param relation the DataRelation to use     */    public void setRelation(DataRelation relation) {        if (this.relation != relation) {            if (this.relation != null && this.relation.getChildColumn() != null) {                this.relation.getChildColumn().getTable().removeDataTableListener(cListener);            }            //TODO affix a DataSet listener so that if this relation is removed            //from the DataSet, I can remove my reference to it as well.            this.relation = relation;            addChildListener();            if (relation != null) {                relation.addPropertyChangeListener("childColumn", new PropertyChangeListener() {                    public void propertyChange(PropertyChangeEvent evt) {                        addChildListener();                    }                });            }        }    }        private void addChildListener() {        if (this.relation != null && this.relation.getChildColumn() != null) {            this.relation.getChildColumn().getTable().removeDataTableListener(cListener);        }        if (this.relation != null && this.relation.getChildColumn() != null) {            this.relation.getChildColumn().getTable().addDataTableListener(cListener);        }    }        /**     * @return the relation used to populate this DataRelationTable     */    public DataRelation getRelation() {        return relation;    }        /**     * Sets the selector for this DataRelationTable. This selector is used to     * retrieve the currently selected rows from the parent table so that the     * proper child rows can be retrieved from the child table using the     * relation.     *     * @param selector The selector on the parent table. The selector must belong     * to the same table as the relation's parentColumn     */    public void setParentSelector(DataSelector selector) {        if (this.parentSelector != null) {            this.parentSelector.removePropertyChangeListener("rowIndices", listener);        }        this.parentSelector = selector;        if (this.parentSelector != null) {            this.parentSelector.addPropertyChangeListener("rowIndices", listener);        }    }        /**     * @return the DataSelector used to retrieve rows from the DataRelation     */    public DataSelector getParentSelector() {        return parentSelector;    }        public void setParentTable(DataTable parent) {        if (this.parentTable != null) {            this.parentTable.removeDataTableListener(pListener);        }        this.parentTable = parent;        if (this.parentTable != null) {            this.parentTable.addDataTableListener(pListener);        }    }        public DataTable getParentTable() {        return parentTable;    }        /**     * This method makes no sense for a DataRelationTable. A cleaner refactoring     * might remove the need to override this method. Nothing bad happens if     * this method is called, it just doesn't make sense. A Warning is logged     * if this method is called.     */    public void setDataProvider(DataProvider dataProvider) {        LOG.warning("An attempt was made to set the DataProvider for a " +                "DataRelationTable. This is not honored because a " +                "DataRelationTable, by definition, gets its records " +                "by querying a DataSelector from the Parent table, and " +                "the DataRelation leading to the Child table.");    }    /**     * This method makes no sense for a DataRelationTable. A cleaner refactoring     * might remove the need to override this method. Nothing bad happens if     * this method is called, it just doesn't make sense. A Warning is logged     * if this method is called     */    public void save() {        LOG.warning("An attempt was made to save a DataRelationTable. " +                "This is not honored because a DataRelationTable, " +                "by definition, gets its records by querying a DataSelector " +                "from the Parent table, and the DataRelation leading to the " +                "Child table. Therefore, to save, the child table should be " +                "asked to save, not the DataRelationTable.");    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?