dataset.java

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

JAVA
1,035
字号
    }        /**      * Removes a given DataRelation from this DataSet.      * @param relationName The name of the relation to remove.     */    public void dropRelation(String relationName) {        DataRelation relation = relations.remove(relationName);        if (relation != null) {            relation.removePropertyChangeListener("name",  nameChangeListener);            for (DataTable t : tables.values()) {                if (t instanceof DataRelationTable) {                    DataRelationTable drt = (DataRelationTable)t;                    if (drt.getRelation() == relation) {                        drt.setRelation(null);                    }                }            }        }    }        /**      * Removes a given DataValue from this DataSet.      * @param value The DataValue instance to remove.     */    public void dropValue(DataValue value) {        dropValue(value.getName());    }        /**      * Removes a given DataValue from this DataSet.      * @param valueName The name of the value to remove.     */    public void dropValue(String valueName) {        values.remove(valueName).removePropertyChangeListener("name",  nameChangeListener);    }        /**      * Checks whether this DataSet has a relation, table or value by the given      * name.     *     * @param name The name to lookup.     * @return true if this DataSet has a table, relation or value with that name.     */    protected boolean hasElement(String name) {        boolean b = relations.containsKey(name);        if (!b) {            b = tables.containsKey(name);        }        if (!b) {            b = values.containsKey(name);        }        return b;    }        /**     * Given some path, return the proper element. Paths are:     * <ul>     * <li>tableName</li>     * <li>tableName.colName</li>     * <li>dataRelationTableName</li>     * <li>dataRelationTableName.colName</li>     * <li>relationName</li>     * <li>valueName</li>     * </ul>     * @param path The identifier for the element in question; see desc.     * @return the element in question as an Object, or null if it doesn't     * identify anything in this DataSet.     */    protected Object getElement(String path) {        if (path == null) {            return null;        }        if (path.contains(".")) {            //must be a table            String[] steps = path.split("\\.");            assert steps.length == 2;            DataTable table = tables.get(steps[0]);            DataColumn col = table.getColumn(steps[1]);            if (col != null) {                return col;            } else {                return table.getSelector(steps[1]);            }        } else {            if (relations.containsKey(path)) {                return relations.get(path);            } else if (tables.containsKey(path)) {                return tables.get(path);            } else if (values.containsKey(path)) {                return values.get(path);            }        }        return null;    }        /** TODO */    /*     * TODO: this is a method of retrieving a subset of rows from a DataSet, by      * naming a table or relation and optional selectors in a path expression--     * need to document!     * if not found, will be an empty list, and if found, list is unmodifiable     */    public List<DataRow> getRows(String path) {        if (path == null || path.trim().equals("")) {            return Collections.EMPTY_LIST;        }                path = path.trim();                //first, split on "."        String[] steps = path.split("\\.");                //each step is either a specific name ("myTable"), or is a composite        //of a name and an index ("myTable[mySelector]")                //maintain a collection of results        List<DataRow> workingSet = null;                for (String step : steps) {            String name = null;            String selectorName = null;            if (step.contains("[")) {                name = step.substring(0, step.indexOf('['));                selectorName = step.substring(step.indexOf('[')+1,  step.indexOf(']'));            }                        if (workingSet == null) {                //get all of the results from the named object (better be a                //table, not a relation!)                DataTable table = tables.get(name);                if (table == null) {                    assert false;                }                workingSet = table.getRows();                if (selectorName != null) {                    // TODO: why is the reassignment for workingSet commented out (PWW 04/27/05)//                    workingSet = filterRows(workingSet, selectors.get(selectorName));                }            } else {                //better be a relation...                DataRelation relation = relations.get(name);                if (relation == null) {                    assert false;                }                workingSet = relation.getRows((DataRow[])workingSet.toArray(new DataRow[workingSet.size()]));                if (selectorName != null) {                    // TODO: why is the reassignment for workingSet commented out (PWW 04/27/05)//                    workingSet = filterRows(workingSet, selectors.get(selectorName));                }            }        }        return Collections.unmodifiableList(workingSet);    }        /**      * Returns a list of rows that match a given selector; convenience method.     *     * @param rows The rows to select from.     * @param ds The DataSelector with indices to filter with.     * @return a List of DataRows matching the given DataSelector; empty if     * none match.     */    // TODO: this has nothing to do with DataSet--put in utils? (PWW 04/27/05)    public List<DataRow> filterRows(List<DataRow> rows, DataSelector ds) {        List<Integer> indices = ds.getRowIndices();        List<DataRow> results = new ArrayList<DataRow>(indices.size());        for (int index : indices) {            results.add(rows.get(index));        }        return results;    }        // TODO: document (PWW 04/27/05)    public List<DataColumn> getColumns(String path) {        //path will either include a single table name, or a single        //relation name, followed by a single column name        String[] parts = path.split("\\.");        assert parts.length == 1 || parts.length == 2;                DataTable table = tables.get(parts[0]);        if (table == null) {            DataRelation relation = relations.get(parts[0]);            if (relation == null) {                return new ArrayList<DataColumn>();            } else {                table = relation.getChildColumn().getTable();            }        }                if (parts.length == 1) {            return table.getColumns();        } else {            List<DataColumn> results = new ArrayList<DataColumn>();            results.add(table.getColumn(parts[1]));            return Collections.unmodifiableList(results);        }    }        /**     * Looks up a DataTable in this DataSet, by name.     *     * @param tableName The name of the table to retrieve.     * @return the DataTable, if found, or null, if not.     */    public DataTable getTable(String tableName) {        return tables.get(tableName);    }        /**     * Returns a List of the DataTables in this DataSet.     *     * @return a List of the DataTables in this DataSet; empty if no tables     * in this DataSet.     */    public List<DataTable> getTables() {        List<DataTable> tableList = new ArrayList<DataTable>();        for(DataTable table : tables.values()) {            tableList.add(table);        }        return tableList;    }        /**     * Looks up a DataRelationTable in this DataSet, by name.     *     * @param name The name of the relation table to retrieve.     * @return the DataRelationTable, if found, or null, if not.     */    public DataRelationTable getRelationTable(String name) {        return (DataRelationTable)tables.get(name);    }        /**      * Looks up a DataValue in this set by name.     *     * @param valueName The name of the DataValue.     * @return the named DataValue, or null if no value with that name.     */    public DataValue getValue(String valueName) {        return values.get(valueName);    }        /**     * Retrieves a list of all DataValues in this set.     *     * @return list of all DataValues in this set, empty if none assigned.     */    public List<DataValue> getValues() {        List<DataValue> values = new ArrayList<DataValue>();        for (DataValue v : this.values.values()) {            values.add(v);        }        return values;    }        /**      * Looks up a DataRelation in this set by name.     *     * @param relationName The name of the DataRelation.     * @return the named DataRelation, or null if no value with that name.     */    public DataRelation getRelation(String relationName) {        return relations.get(relationName);    }        /**     * Retrieves a list of all DataRelation in this set.     *     * @return list of all DataRelation in this set, empty if none assigned.     */    public List<DataRelation> getRelations() {        List<DataRelation> relations = new ArrayList<DataRelation>();        for (DataRelation r : this.relations.values()) {            relations.add(r);        }        return relations;    }        /**      * Requests that each {@link DataTable} in this DataSet load itself; this is      * an <em>asynchronous</em> operation. See {@link DataTable#load()}; tables must have     * been assigned a {@link DataProvider} already.     */    public void load() {        for (DataTable table : tables.values()) {            if (!(table instanceof DataRelationTable)) {                table.load();            }        }    }        /**      * Requests that each {@link DataTable} in this DataSet load itself; this is      * a <em>synchronous</em> operation. See {@link DataTable#loadAndWait()}; tables must have     * been assigned a {@link DataProvider} already.     */    public void loadAndWait() {        for (DataTable table : tables.values()) {            if (!(table instanceof DataRelationTable)) {                table.loadAndWait();            }        }    }        /**      * Requests that each {@link DataTable} in this DataSet refresh itself; this is      * an <em>asynchronous</em> operation. See {@link DataTable#refresh()}; tables must have     * been assigned a {@link DataProvider} already.     */    public void refresh() {        for (DataTable table : tables.values()) {            if (!(table instanceof DataRelationTable)) {                table.refresh();            }        }    }        /**      * Requests that each {@link DataTable} in this DataSet refresh itself; this is      * a <em>synchronous</em> operation. See {@link DataTable#refreshAndWait()}; tables must have     * been assigned a {@link DataProvider} already.     */    public void refreshAndWait() {        for (DataTable table : tables.values()) {            if (!(table instanceof DataRelationTable)) {                table.refreshAndWait();            }        }    }        /**      * Requests that each {@link DataTable} in this DataSet clear itself; this is      * a <em>synchronous</em> operation since the clear operation is not anticipated to     * take more than a couple of milliseconds at worst.     * See {@link DataTable#clear()};     */    public void clear() {        for (DataTable table : tables.values()) {            if (!(table instanceof DataRelationTable)) {                table.clear();            }        }    }    

⌨️ 快捷键说明

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