Search in sources :

Example 1 with RowSorter

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;
}
Also used : RowSorter(javax.swing.RowSorter) List(java.util.List) SortKey(javax.swing.RowSorter.SortKey)

Example 2 with RowSorter

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);
    }
}
Also used : RowSorter(javax.swing.RowSorter) HashMap(java.util.HashMap) TableColumnModel(javax.swing.table.TableColumnModel) XTableColumnModel(jmri.util.swing.XTableColumnModel) XTableColumnModel(jmri.util.swing.XTableColumnModel) TableColumn(javax.swing.table.TableColumn)

Example 3 with RowSorter

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);
    }
}
Also used : RowSorter(javax.swing.RowSorter) TableColumn(javax.swing.table.TableColumn)

Example 4 with RowSorter

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;
}
Also used : RowSorter(javax.swing.RowSorter) TableColumnModel(javax.swing.table.TableColumnModel) XTableColumnModel(jmri.util.swing.XTableColumnModel) SortOrder(javax.swing.SortOrder) XTableColumnModel(jmri.util.swing.XTableColumnModel) TableColumn(javax.swing.table.TableColumn)

Example 5 with RowSorter

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);
    }
}
Also used : RowSorter(javax.swing.RowSorter) TableColumn(javax.swing.table.TableColumn)

Aggregations

RowSorter (javax.swing.RowSorter)9 TableColumn (javax.swing.table.TableColumn)5 List (java.util.List)3 SortKey (javax.swing.RowSorter.SortKey)3 XTableColumnModel (jmri.util.swing.XTableColumnModel)3 ArrayList (java.util.ArrayList)2 SortOrder (javax.swing.SortOrder)2 TableColumnModel (javax.swing.table.TableColumnModel)2 Point (java.awt.Point)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 MouseEvent (java.awt.event.MouseEvent)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 StringTokenizer (java.util.StringTokenizer)1 BoxLayout (javax.swing.BoxLayout)1 JButton (javax.swing.JButton)1 JDialog (javax.swing.JDialog)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1