com.datastax.data.dataset.DataTable Maven / Gradle / Ivy
The newest version!
package com.datastax.data.dataset;
import com.datastax.data.dataset.event.DataTableListener;
import com.datastax.data.dataset.event.RowChangeEvent;
import com.datastax.data.dataset.event.TableChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.*;
import java.util.logging.Logger;
public class DataTable {
private static final Logger LOG = Logger.getLogger(DataTable.class.getName());
protected static final String DEFAULT_NAME_PREFIX = "DataTable";
private static final NameGenerator NAMEGEN = new NameGenerator(DEFAULT_NAME_PREFIX);
private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
private DataSet dataSet;
private DataProvider dataProvider;
private String name;
protected Map columns = new LinkedHashMap();
protected Map classComparators = new HashMap();
protected Map columnComparators = new HashMap();
private final static Comparator EQUALS_COMPARATOR = new Comparator() {
public boolean equals(Object obj) {
return obj == this;
}
public int compare(Object o1, Object o2) {
return ( o1.equals(o2) ? 0 : -1 );
}
};
protected List rows = new ArrayList();
protected Map selectors = new HashMap();
private boolean deleteRowSupported = true;
private boolean appendRowSupported = true;
private boolean identityComparisonEnabled;
private List listeners = new ArrayList();
private final PropertyChangeListener nameChangeListener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getSource() instanceof DataSelector) {
DataSelector sel = selectors.remove(evt.getOldValue());
if (sel != null) {
selectors.put((String)evt.getNewValue(), sel);
}
} else if (evt.getSource() instanceof DataColumn) {
DataColumn c = columns.remove(evt.getOldValue());
if (c != null) {
columns.put((String)evt.getNewValue(), c);
}
}
}
};
protected DataTable(DataSet ds) {
assert ds != null;
this.dataSet = ds;
this.name = NAMEGEN.generateName(this);
this.identityComparisonEnabled = true;
}
protected DataTable(DataSet ds, String name) {
this(ds);
if (name != null) {
setName(name);
}
}
public DataColumn createColumn() {
return createColumn(null);
}
public DataColumn createColumn(String colName) {
DataColumn col = new DataColumn(this);
if (colName != null) {
col.setName(colName);
}
columns.put(col.getName(), col);
col.addPropertyChangeListener("name", nameChangeListener);
fireDataTableChanged(TableChangeEvent.newColumnAddedEvent(this, col));
return col;
}
public void createColumns(String... colNames) {
for (String name : colNames) {
createColumn(name);
}
}
public void dropColumn(String colName) {
DataColumn col = columns.remove(colName);
if (col != null) {
col.removeFromTable();
col.removePropertyChangeListener("name", nameChangeListener);
for (DataRelation r : dataSet.getRelations()) {
DataColumn c = r.getChildColumn();
if (c == col) {
r.setChildColumn(null);
}
c = r.getParentColumn();
if (c == col) {
r.setParentColumn(null);
}
}
columnComparators.remove(col);
fireDataTableChanged(TableChangeEvent.newColumnRemovedEvent(this, col));
}
}
public List getColumns() {
return Collections.unmodifiableList(new ArrayList(columns.values()));
}
public DataColumn getColumn(String colName) {
return columns.get(colName);
}
public List getRows() {
return Collections.unmodifiableList(rows);
}
public DataRow getRow(int index) {
return rows.get(index);
}
public int getRowCount() {
return rows.size();
}
public DataSelector createSelector() {
return createSelector(null);
}
public DataSelector createSelector(String name) {
DataSelector sel = new DataSelector(this);
if (name != null) {
sel.setName(name);
}
if (rows.size() > 0) {
sel.setRowIndex(0);
}
selectors.put(sel.getName(), sel);
sel.addPropertyChangeListener("name", nameChangeListener);
return sel;
}
public List getSelectors() {
return Collections.unmodifiableList(new ArrayList(selectors.values()));
}
public DataSelector getSelector(String name) {
if (!selectors.containsKey(name)) {
return createSelector(name);
} else {
return selectors.get(name);
}
}
public void dropSelector(DataSelector selector) {
dropSelector(selector.getName());
}
public void dropSelector(String selectorName) {
DataSelector sel = selectors.remove(selectorName);
if (sel != null) {
sel.removeFromTable();
sel.removePropertyChangeListener("name", nameChangeListener);
}
}
public void setName(String name) {
if (this.name != name) {
assert DataSetUtils.isValidName(name);
assert !dataSet.hasElement(name);
String oldName = this.name;
this.name = name;
pcs.firePropertyChange("name", oldName, name);
}
}
public String getName() {
return name;
}
public boolean isIdentityComparisonEnabled() {
return identityComparisonEnabled;
}
public void setIdentityComparisonEnabled(boolean identityComparisonEnabled) {
this.identityComparisonEnabled = identityComparisonEnabled;
}
public void setClassComparator(Class klass, Comparator comp) {
assert klass != null;
assert comp != null;
classComparators.put(klass, comp);
}
public void removeClassComparator(Class klass) {
assert klass != null;
classComparators.remove(klass);
}
public boolean hasClassComparator(Class klass) {
return classComparators.get(klass) != null;
}
public Comparator getClassComparator(Class klass) {
Comparator comp = classComparators.get(klass);
if ( comp == null ) {
comp = EQUALS_COMPARATOR;
}
return comp;
}
public void setColumnComparator(DataColumn col, Comparator comp) {
assert col != null;
assert comp != null;
columnComparators.put(col, comp);
}
public void removeColumnComparator(DataColumn col) {
assert col != null;
columnComparators.remove(col);
}
public boolean hasColumnComparator(DataColumn col) {
return columnComparators.get(col) != null;
}
public Comparator getColumnComparator(DataColumn col) {
assert col != null;
Comparator comp = columnComparators.get(col);
if ( comp == null ) {
comp = getClassComparator(col.getType());
}
return comp;
}
public boolean isDeleteRowSupported() {
return deleteRowSupported;
}
public void setDeleteRowSupported(boolean deleteRowSupported) {
if (this.deleteRowSupported != deleteRowSupported) {
boolean oldValue = this.deleteRowSupported;
this.deleteRowSupported = deleteRowSupported;
pcs.firePropertyChange("deleteRowSupported", oldValue, deleteRowSupported);
}
}
public boolean isAppendRowSupported() {
return appendRowSupported;
}
public void setAppendRowSupported(boolean appendRowSupported) {
if (this.appendRowSupported != appendRowSupported) {
boolean oldValue = this.appendRowSupported;
this.appendRowSupported = appendRowSupported;
pcs.firePropertyChange("appendRowSupported", oldValue, appendRowSupported);
}
}
public DataProvider getDataProvider() {
return dataProvider;
}
public void setDataProvider(DataProvider dataProvider) {
if (this.dataProvider != dataProvider) {
DataProvider oldValue = this.dataProvider;
this.dataProvider = dataProvider;
pcs.firePropertyChange("dataProvider", oldValue, dataProvider);
}
}
public DataSet getDataSet() {
return dataSet;
}
void removeFromDataSet() {
dataSet = null;
}
public DataRow appendRow() {
final DataRow row = appendRowNoEvent();
if (row != null) {
fireDataTableChanged(TableChangeEvent.newRowAddedEvent(this, row));
if (selectors.get("current") == null) {
createSelector("current");
}
selectors.get("current").setRowIndices(new int[]{indexOfRow(row)});
}
return row;
}
public void appendRow(Object[] row) {
DataRow dataRow=this.appendRow();
List cols=getColumns();
for(int i=0;i 0) {
for (DataSelector s : selectors.values()) {
s.setRowIndices(new int[]{0});
}
}
return row;
} else {
return null;
}
}
public void deleteRow(int rowIndex) {
assert rowIndex >= 0 && rowIndex < rows.size();
deleteRow(rows.get(rowIndex));
}
public void deleteRow(DataRow row) {
assert row.getTable() == this;
if (deleteRowSupported) {
row.setStatus(DataRow.DataRowStatus.DELETED);
fireDataTableChanged(TableChangeEvent.newRowDeletedEvent(this, row));
}
}
public void discardRow(int rowIndex) {
assert rowIndex >= 0 && rowIndex < rows.size();
DataRow row = rows.remove(rowIndex);
row.removeFromTable();
fireDataTableChanged(TableChangeEvent.newRowDiscardedEvent(this, row));
}
public void discardRow(DataRow row) {
discardRow(indexOfRow(row));
}
public void load() {
if (dataProvider != null) {
fireDataTableChanged(TableChangeEvent.newLoadStartEvent(this));
dataProvider.load(this);
}
}
public void loadAndWait() {
if (dataProvider != null) {
fireDataTableChanged(TableChangeEvent.newLoadStartEvent(this));
dataProvider.loadAndWait(this);
}
}
public void save() {
if (dataProvider != null) {
fireDataTableChanged(TableChangeEvent.newSaveStartEvent(this));
dataProvider.save(this);
}
}
public void saveAndWait() {
if (dataProvider != null) {
fireDataTableChanged(TableChangeEvent.newSaveStartEvent(this));
dataProvider.saveAndWait(this);
}
}
public void clear() {
rows.clear();
for (DataSelector sel : selectors.values()) {
sel.setRowIndices(new int[0]);
}
fireDataTableChanged(TableChangeEvent.newTableClearedEvent(this));
}
public void refresh() {
clear();
load();
}
public void refreshAndWait() {
clear();
loadAndWait();
}
public Object getValue(int index, String columnName) {
assert index >= 0 && index < rows.size();
assert columns.containsKey(columnName);
return rows.get(index).getValue(columnName);
}
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);
}
public Object getValue(DataRow row, DataColumn col) {
assert row.getTable() == this;
assert col.getTable() == this;
return row.getValue(col);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcs.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(String property, PropertyChangeListener listener) {
pcs.addPropertyChangeListener(property, listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcs.removePropertyChangeListener(listener);
}
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
pcs.removePropertyChangeListener(propertyName, listener);
}
public void addDataTableListener(DataTableListener listener) {
if (!listeners.contains(listener)) {
listeners.add(listener);
}
if ( listener instanceof PropertyChangeListener) {
addPropertyChangeListener((PropertyChangeListener)listener);
}
}
public void removeDataTableListener(DataTableListener listener) {
listeners.remove(listener);
if ( listener instanceof PropertyChangeListener) {
removePropertyChangeListener((PropertyChangeListener)listener);
}
}
public void fireDataTableChanged(TableChangeEvent evt) {
for (DataTableListener listener : new ArrayList(listeners)) {
listener.tableChanged(evt);
}
}
public void fireRowChanged(RowChangeEvent evt) {
for (DataTableListener listener : new ArrayList(listeners)) {
listener.rowChanged(evt);
}
}
protected int indexOfRow(DataRow row) {
return rows.indexOf(row);
}
}