use of javax.swing.RowSorter in project smile by haifengl.
the class DefaultTableHeaderCellRenderer method getSortKey.
/**
* Returns the current sort key, or null if the column is unsorted.
*
* @param table the table
* @param column the column index
* @return the SortKey, or null if the column is unsorted
*/
@SuppressWarnings("rawtypes")
protected SortKey getSortKey(JTable table, int column) {
RowSorter rowSorter = table.getRowSorter();
if (rowSorter == null) {
return null;
}
List sortedColumns = rowSorter.getSortKeys();
if (!sortedColumns.isEmpty()) {
return (SortKey) sortedColumns.get(0);
}
return null;
}
use of javax.swing.RowSorter in project JMRI by JMRI.
the class JmriJTablePersistenceManager method resetState.
@Override
public void resetState(JTable table) {
Objects.requireNonNull(table.getName(), "table name must be nonnull");
boolean persisting = this.listeners.containsKey(table.getName());
// while setting table state, don't listen to changes in table state
this.stopPersisting(table);
TableColumnModel model = table.getColumnModel();
Objects.requireNonNull(model, "table " + table.getName() + " has a null columnModel");
RowSorter sorter = table.getRowSorter();
boolean isXModel = model instanceof XTableColumnModel;
Enumeration<TableColumn> e;
if (isXModel) {
e = ((XTableColumnModel) model).getColumns(false);
} else {
e = model.getColumns();
}
while (e.hasMoreElements()) {
TableColumn column = e.nextElement();
if (column.getIdentifier() == null) {
column.setIdentifier(column.getHeaderValue().toString());
}
}
Map<Integer, String> indexes = new HashMap<>();
if (this.columns.get(table.getName()) == null) {
this.columns.put(table.getName(), new HashMap<>());
}
this.columns.get(table.getName()).entrySet().stream().forEach((entry) -> {
int index = entry.getValue().getOrder();
indexes.put(index, entry.getKey());
});
// order columns
for (int i = 0; i < model.getColumnCount(); i++) {
String name = indexes.get(i);
if (name != null) {
int dataModelIndex = -1;
for (int j = 0; j < model.getColumnCount(); j++) {
if (table.getColumnName(j).equals(name)) {
dataModelIndex = j;
break;
}
}
if (dataModelIndex != -1 && (dataModelIndex != i)) {
model.moveColumn(dataModelIndex, i);
}
}
}
// configure columns
if (isXModel) {
e = ((XTableColumnModel) model).getColumns(false);
} else {
e = model.getColumns();
}
while (e.hasMoreElements()) {
TableColumn column = e.nextElement();
String name = column.getIdentifier().toString();
TableColumnPreferences preferences = this.columns.get(table.getName()).get(name);
if (preferences != null) {
column.setPreferredWidth(preferences.getWidth());
if (isXModel) {
((XTableColumnModel) model).setColumnVisible(column, !preferences.getHidden());
}
}
}
if (sorter != null && this.sortKeys.get(table.getName()) != null) {
sorter.setSortKeys(this.sortKeys.get(table.getName()));
}
if (persisting) {
this.persist(table);
}
}
use of javax.swing.RowSorter in project JMRI by JMRI.
the class JmriJTablePersistenceManager method stopPersisting.
@Override
public void stopPersisting(JTable table) {
Objects.requireNonNull(table.getName(), "table name must be nonnull");
JTableListener listener = this.listeners.remove(table.getName());
table.removePropertyChangeListener(this);
table.removePropertyChangeListener(listener);
table.getColumnModel().removeColumnModelListener(listener);
RowSorter sorter = table.getRowSorter();
if (sorter != null) {
sorter.removeRowSorterListener(listener);
}
Enumeration<TableColumn> e = table.getColumnModel().getColumns();
while (e.hasMoreElements()) {
TableColumn column = e.nextElement();
column.removePropertyChangeListener(listener);
}
}
use of javax.swing.RowSorter in project JMRI by JMRI.
the class JmriJTablePersistenceManager method cacheState.
@Override
public void cacheState(JTable table) {
Objects.requireNonNull(table.getName(), "table name must be nonnull");
TableColumnModel model = table.getColumnModel();
Objects.requireNonNull(model, "table " + table.getName() + " has a null columnModel");
RowSorter sorter = table.getRowSorter();
boolean isXModel = model instanceof XTableColumnModel;
Enumeration<TableColumn> e = model.getColumns();
while (e.hasMoreElements()) {
TableColumn column = e.nextElement();
String name = column.getHeaderValue().toString();
int index = column.getModelIndex();
if (isXModel) {
index = ((XTableColumnModel) model).getColumnIndex(column.getIdentifier(), false);
}
int width = column.getPreferredWidth();
boolean hidden = false;
if (isXModel) {
hidden = !((XTableColumnModel) model).isColumnVisible(column);
}
SortOrder sorted = SortOrder.UNSORTED;
if (sorter != null) {
sorted = RowSorterUtil.getSortOrder(sorter, index);
log.trace("Column {} (model index {}) is {}", name, index, sorted);
}
this.setPersistedState(table.getName(), name, index, width, sorted, hidden);
}
if (sorter != null) {
this.sortKeys.put(table.getName(), new ArrayList<>(sorter.getSortKeys()));
}
this.dirty = true;
}
use of javax.swing.RowSorter in project JMRI by JMRI.
the class JmriJTablePersistenceManager method persist.
/**
* {@inheritDoc} Persisting a table that is already persisted may cause the
* persistence state to be updated, but will not cause additional listeners
* to be added to the table.
*/
@Override
public void persist(@Nonnull JTable table) throws IllegalArgumentException, NullPointerException {
Objects.requireNonNull(table.getName(), "Table name must be nonnull");
if (this.listeners.containsKey(table.getName()) && !this.listeners.get(table.getName()).getTable().equals(table)) {
throw new IllegalArgumentException("Table name must be unique");
}
if (!this.listeners.containsKey(table.getName())) {
JTableListener listener = new JTableListener(table, this);
this.listeners.put(table.getName(), listener);
if (!Arrays.asList(table.getPropertyChangeListeners()).contains(this)) {
table.addPropertyChangeListener(this);
table.addPropertyChangeListener(listener);
table.getColumnModel().addColumnModelListener(listener);
RowSorter sorter = table.getRowSorter();
if (sorter != null) {
sorter.addRowSorterListener(listener);
}
Enumeration<TableColumn> e = table.getColumnModel().getColumns();
while (e.hasMoreElements()) {
TableColumn column = e.nextElement();
column.addPropertyChangeListener(listener);
if (column.getIdentifier() == null) {
column.setIdentifier(column.getHeaderValue().toString());
}
}
}
}
if (this.columns.get(table.getName()) == null) {
this.cacheState(table);
}
}
Aggregations