datarelationtable.java
来自「java实现浏览器等本地桌面的功能」· Java 代码 · 共 396 行 · 第 1/2 页
JAVA
396 行
/** * Appends a row to the child table, retrieved by * <code>relation.getChildColumn().getTable()</code>. If the DataTable * doesn't exist, null is returned * * @return The newly added DataRow, or null if no DataRow was added. */ public DataRow appendRow() { //append a row to the child table, and view it here. //when the row is added to the child table, the child table //will fire an event notification indicating that a new row has //been added. When this happens, this child as well as all other //relation tables will be notified that they need to refresh themselves. if (relation != null && relation.getChildColumn() != null) { DataTable t = relation.getChildColumn().getTable(); DataRow row = t.appendRow(); if (parentSelector.getRowIndices().size() == 1) { row.setValue(relation.getChildColumn().getName(), relation.getParentColumn().getTable().getValue( parentSelector.getRowIndices().get(0), relation.getParentColumn().getName() )); } rows.add(row); return row; } return null; } /** * Removes all of the rows from this DataRelationTable, and reloads it with * the proper rows from the DataRelation using the parentSelector * DataSelector. When rows are removed from a DataRelationTable, they still * exist in the child DataTable, hence it is always safe to call this method * without losing any data.<br> * * This method attempts to reset all of its selectors to the same rows, * where possible. It does this by remembering all of the values for the * rows key fields, and looking for them again after the refresh. If the * row cannot be found, it is no longer selected<br> * * TODO This code for remembering the selected rows and trying to locate * them again after refresh is horrible and needs to be rewritten. */ public void refresh() { //for each DataSelector, save its selection state. Map<DataSelector,Object[][]> selectorState = new HashMap<DataSelector,Object[][]>(); List<DataColumn> keyColumns = new ArrayList<DataColumn>(); for (DataColumn c : columns.values()) { if (c.isKeyColumn()) { keyColumns.add(c); } } for (DataSelector sel : selectors.values()) { List<Integer> indices = sel.getRowIndices(); Object[][] values = new Object[indices.size()][keyColumns.size()]; for (int i=0; i<indices.size(); i++) { for (int j=0; j<keyColumns.size(); j++) { DataRow row = rows.get(indices.get(i)); values[i][j] = getValue(row, keyColumns.get(j)); } } sel.setRowIndices(new int[0]); selectorState.put(sel, values); } //clear out the DataRelationTable, and reload it clear(); if (relation != null && relation.getChildColumn() != null) { fireDataTableChanged(TableChangeEvent.newLoadStartEvent(this)); if (parentSelector != null) { //use the selector and the relation to get the rows// assert relation.getParentColumn() != null && relation.getParentColumn().getTable() == selector.getTable(); super.rows.addAll(relation.getRows(parentSelector.getRowIndices())); } else if (parentTable != null) { //use all of the rows in the parent table to get the children List<DataRow> list = parentTable.getRows(); super.rows.addAll(relation.getRows(list.toArray(new DataRow[list.size()]))); } else { //get all of the rows from the relations child table super.rows.addAll(relation.getChildColumn().getTable().getRows()); } fireDataTableChanged(TableChangeEvent.newLoadCompleteEvent(this)); } //try to restore the selection state, where possible for (DataSelector sel : selectors.values()) { Object[][] values = selectorState.get(sel); //for each selector, find its values. Look for a row who's columns //values match the specified set, and match it. //NOTE: This algorithm is a brute force algorithm, and not very //efficient where multiple rows are selected in a large DataTable. List<Integer> indices = new ArrayList<Integer>(values.length); for (int i=0; i<values.length; i++) { boolean found = true; for (int rowIndex=0; rowIndex<rows.size(); rowIndex++) { found = true; //reset for (int j=0; j<keyColumns.size(); j++) { DataRow row = rows.get(rowIndex); if (found && values[i][j].equals(row.getValue(keyColumns.get(j)))) { //do nothing } else { found = false; } } if (found) { indices.add(rowIndex); } } } //ok, set the selector state //reset the selectors int[] rows = new int[indices.size()]; for (int i=0; i<rows.length; i++) { rows[i] = indices.get(i); } if (super.rows.size() > 0) { if (rows.length == 0) { //reset all of the selectors to 0 sel.setRowIndices(new int[]{0}); } else { sel.setRowIndices(rows); } } } } /** * Overridden so that it will defer to the child Table first. A * DataRelationTable contains all of the columns in the child Table, but * may also contain its own set of calculated columns as well. This method * must therefore check the child table for a given column before checking * its own set of columns. * * @param colName The String name for the column to retrieve. * @return The DataColumn identified by the given name. */ public DataColumn getColumn(String colName) { DataColumn col = null; if (relation != null && relation.getChildColumn() != null && relation.getChildColumn().getTable() != null) { col = relation.getChildColumn().getTable().getColumn(colName); } if (col == null) { return super.getColumn(colName); } else { return col; } } /** * @return the set of columns that comprise this DataRelationTable. This is * the union of the child tables columns, and any native columns to this * DataRelationTable */ public List<DataColumn> getColumns() { List<DataColumn> cols = new ArrayList<DataColumn>(); if (relation != null && relation.getChildColumn() != null && relation.getChildColumn().getTable() != null) { cols.addAll(relation.getChildColumn().getTable().getColumns()); } cols.addAll(super.getColumns()); return Collections.unmodifiableList(cols); } /** * A listener to the parentSelector. When selection change events occur in * the parentSelector, this DataRelationTable is automatically refreshed. */ private final class SelectionListener implements PropertyChangeListener { public void propertyChange(PropertyChangeEvent evt) { refresh(); } } private final class ParentTableListener implements DataTableListener { public void rowChanged(RowChangeEvent evt) { } public void tableChanged(TableChangeEvent evt) { refresh(); } } private final class ChildTableListener implements DataTableListener { public void rowChanged(RowChangeEvent evt) { } public void tableChanged(TableChangeEvent evt) { refresh(); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?