📄 datatable.java
字号:
/** * Append a new DataRow to this DataTable, and return the newly appended * Row. If appendRowSupported is false, then this method returns null * @return null if !appendRowSupported, else the newly created row */ public DataRow appendRow() { final DataRow row = appendRowNoEvent(); if (row != null) { fireDataTableChanged(TableChangeEvent.newRowAddedEvent(this, row)); //I added this, don't know if I really want it here if (selectors.get("current") == null) { createSelector("current"); } // ASK: why the PCL here? row.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { fireDataTableChanged(TableChangeEvent.newRowAddedEvent(DataTable.this, row)); } }); selectors.get("current").setRowIndices(new int[]{indexOfRow(row)}); } return row; } /** * Appends a new DataRow to this DataTable, and returns the newly appended * row. This method differs from {@link #appendRow()} in that it does not fire * any event. This is useful to the DataProvider, which will be adding many * rows and may not want many event notifications * @return The row added to the table. */ public DataRow appendRowNoEvent() { if (appendRowSupported) { DataRow row = new DataRow(this); int oldSize = rows.size(); rows.add(row); if (oldSize == 0 && rows.size() > 0) { //check the selectors. Any selectors that are not //set, set. for (DataSelector s : selectors.values()) { s.setRowIndices(new int[]{0}); } } return row; } else { return null; } } /** * Doesn't actually remove the row, just marks it for deletion. If * deletion of rows is not supported, nothing happens. * @param rowIndex the index of the row to delete. If the index is invalid, * an assertion will be raised */ public void deleteRow(int rowIndex) { assert rowIndex >= 0 && rowIndex < rows.size(); deleteRow(rows.get(rowIndex)); } /** * Doesn't actually remove the row, just marks it for deletion. If * deletion of rows is not supported, nothing happens. * @param row the row to delete. The row must belong to this DataTable */ public void deleteRow(DataRow row) { assert row.getTable() == this; if (deleteRowSupported) { row.setStatus(DataRow.DataRowStatus.DELETED); fireDataTableChanged(TableChangeEvent.newRowDeletedEvent(this, row)); } } /** * Actually removes the row from the DataTable. Unlike the #deleteRow method, * this does not later remove the record from the data store. Rather, it simply * discards the row entirely * @param rowIndex Index of the row to discard. */ public void discardRow(int rowIndex) { assert rowIndex > 0 && rowIndex < rows.size(); DataRow row = rows.remove(rowIndex); fireDataTableChanged(TableChangeEvent.newRowDiscardedEvent(this, row)); } /** * Actually removes the row from the DataTable. Unlike the {@link #deleteRow(DataRow)} method, * this does not later remove the record from the data store. Rather, it simply * discards the row entirely. * @param row The row to discard. */ public void discardRow(DataRow row) { discardRow(indexOfRow(row)); } /** * Loads this DataTable using this tables DataProvider. If DataProvider is * null, then nothing is loaded. This method <b>does not</b> clear out the * DataTable prior to loading. Calling load() <i>n</i> times will cause the * DataTable to contain <i>rowCount * n</i> rows to be added. */ public void load() { if (dataProvider != null) { fireDataTableChanged(TableChangeEvent.newLoadStartEvent(this)); dataProvider.load(this); } } /** * Loads this DataTable <b>synchronously</b> using this table&s * DataProvider. That is, this method blocks until the load is completed. * If DataProvider * is null, then nothing is loaded. This method <b>does not</b> clear out * the DataTable prior to loading. Calling load() <i>n</i> times will cause * the DataTable to contain <i>rowCount * n</i> rows to be added */ public void loadAndWait() { if (dataProvider != null) { fireDataTableChanged(TableChangeEvent.newLoadStartEvent(this)); dataProvider.loadAndWait(this); } } /** * Saves this DataTable to the underlying DataStore. This calls the * DataProviders save method. If no DataProvider is specified, then nothing * is done. */ public void save() { if (dataProvider != null) { fireDataTableChanged(TableChangeEvent.newSaveStartEvent(this)); dataProvider.save(this); } } /** * Saves this DataTable to the underyling DataStore <b>synchronously</b>. * That is, this method blocks until the save is complete. This calls the * DataProvider&s save method. If no DataProvider is specified, then * nothing is done. */ public void saveAndWait() { if (dataProvider != null) { fireDataTableChanged(TableChangeEvent.newSaveStartEvent(this)); dataProvider.saveAndWait(this); } } /** * Clears all of the rows from this DataTable. <em>If any rows were altered, * these changes are lost!</em>. Call #save to save the changes before * clearing. An {@linkplain TableChangeEvent event} is posted indicating that the table was cleared. */ public void clear() { rows.clear(); //clear out all of the DataSelectors for (DataSelector sel : selectors.values()) { sel.setRowIndices(new int[0]); } fireDataTableChanged(TableChangeEvent.newTableClearedEvent(this)); } /** * Refreshes the DataSet. This is symantically the same as: * <code> * clear(); * load(); * </code> */ public void refresh() { clear(); load(); } /** * Refreshes the DataSet <b>synchronously</b>. That is, this method blocks * until the refresh is completed. This is symantically the same as: * <code> * clear(); * loadAndWait(); * </code> */ public void refreshAndWait() { clear(); loadAndWait(); } /** * Returns the current value for the row at the given row index, for the given column. * @param index the row index of the row that you want to retrieve a value * for * @param columnName the name of the column who's value you want retrieved * @return the value at the given rowIndex, for the given columnName. If * either the index is invalid or the columnName, assertions will be raised. */ public Object getValue(int index, String columnName) { assert index >= 0 && index < rows.size(); assert columns.containsKey(columnName); return rows.get(index).getValue(columnName); } /** * Sets the field value at the given row idnex and column to the given * value. If either the index is invalid or the columnName, assertions will * be raised. * @param index The row index at which to set the value * @param columnName Name of the column to change * @param value The new value for the cell */ public void setValue(int index, String columnName, Object value) { assert index >= 0 && index < rows.size(); assert columns.containsKey(columnName); rows.get(index).setValue(columnName, value); } /** * Retrieves the current value for the given row and column. * @param row The DataRow to retrieve the value from. This row must be * a member of this table, or an assertion will be raised. If null, a * NullPointerException will be thrown. * @param col The DataColumn to retrieve the value from. This col must * be a member of this table, or an assertion will be raised. If null, a * NullPointerException will be thrown. * @return the current value for the given row and column. */ public Object getValue(DataRow row, DataColumn col) { assert row.getTable() == this; assert col.getTable() == this; return row.getValue(col); } /** * Adds a PropertyChangeListener to this class for any changes to bean * properties. * * @param listener The PropertyChangeListener to notify of changes to this * instance. */ public void addPropertyChangeListener(PropertyChangeListener listener) { pcs.addPropertyChangeListener(listener); } /** * Adds a PropertyChangeListener to this class for specific property changes. * * @param property The name of the property to listen to changes for. * @param listener The PropertyChangeListener to notify of changes to this * instance. */ public void addPropertyChangeListener(String property, PropertyChangeListener listener) { pcs.addPropertyChangeListener(property, listener); } /** * Stops notifying a specific listener of any changes to bean properties. * * @param listener The listener to stop receiving notifications. */ public void removePropertyChangeListener(PropertyChangeListener listener) { pcs.removePropertyChangeListener(listener); } /** * Stops notifying a specific listener of changes to a specific property. * * @param propertyName The name of the property to ignore from now on. * @param listener The listener to stop receiving notifications. */ public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { pcs.removePropertyChangeListener(propertyName, listener); } /** * Adds a {@link DataTableListener} to the table for event propagation. If the DTL is also a {@link PropertyChangeListener}, * like {@link DataTableEventAdapter}, then this is automatically added as a property change listener on the table as well. * @param listener The listener to add for event notification. */ public void addDataTableListener(DataTableListener listener) { if (!listeners.contains(listener)) { listeners.add(listener); } if ( listener instanceof PropertyChangeListener ) { addPropertyChangeListener((PropertyChangeListener)listener); } } // CHANGED /** * Removes an event listener from the table; if the listener is also a {@link PropertyChangeListener}, then * it is also removed as a property change listener on the table as well. * @param listener The event listener to remove from the table. */ public void removeDataTableListener(DataTableListener listener) { listeners.remove(listener); if ( listener instanceof PropertyChangeListener ) { removePropertyChangeListener((PropertyChangeListener)listener); } } /** * Broadcasts to listeners on the table that the table has changed, using a {@link TableChangeEvent}. * @param evt The {@link TableChangeEvent} recording the event. */ public void fireDataTableChanged(TableChangeEvent evt) { for (DataTableListener listener : new ArrayList<DataTableListener>(listeners)) { listener.tableChanged(evt); } } /** * Broadcasts to listeners on the table that a row has changed using a {@link RowChangeEvent}. * @param evt The {@link RowChangeEvent} capturing the event on a row. */ public void fireRowChanged(RowChangeEvent evt) { for (DataTableListener listener : new ArrayList<DataTableListener>(listeners)) { listener.rowChanged(evt); } } /** * Internal method that returns the int index of the given DataRow. This is * currently only used for constructing toString on DataRow, and testing. */ protected int indexOfRow(DataRow row) { return rows.indexOf(row); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -