Search in sources :

Example 6 with SortKey

use of javax.swing.RowSorter.SortKey in project jmeter by apache.

the class ObjectTableSorterTest method sortValueDescending.

@Test
public void sortValueDescending() {
    sorter.setSortKey(new SortKey(1, SortOrder.DESCENDING));
    List<SimpleImmutableEntry<String, Integer>> expected = asList(d4(), a3(), b2(), c1());
    assertRowOrderAndIndexes(expected);
}
Also used : SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) SortKey(javax.swing.RowSorter.SortKey) Test(org.junit.Test)

Example 7 with SortKey

use of javax.swing.RowSorter.SortKey in project jmeter by apache.

the class ObjectTableSorterTest method sortValueAscending.

@Test
public void sortValueAscending() {
    sorter.setSortKey(new SortKey(1, SortOrder.ASCENDING));
    List<SimpleImmutableEntry<String, Integer>> expected = asList(c1(), b2(), a3(), d4());
    assertRowOrderAndIndexes(expected);
}
Also used : SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) SortKey(javax.swing.RowSorter.SortKey) Test(org.junit.Test)

Example 8 with SortKey

use of javax.swing.RowSorter.SortKey in project JMRI by JMRI.

the class RowSorterUtil method addSingleSortableColumnListener.

/**
     * Add a RowSorterListener to the rowSorter that prevents multiple columns
     * from being considered while sorting.
     *
     * @param rowSorter the sorter to add the listener to
     * @return the added listener
     * @throws NullPointerException if rowSorter is null
     */
public static RowSorterListener addSingleSortableColumnListener(@Nonnull RowSorter<? extends TableModel> rowSorter) {
    Objects.requireNonNull(rowSorter, "rowSorter must be nonnull.");
    RowSorterListener listener = new RowSorterListener() {

        List<? extends SortKey> priorSortKeys = new ArrayList<>();

        @Override
        public void sorterChanged(RowSorterEvent e) {
            if (e.getType().equals(RowSorterEvent.Type.SORT_ORDER_CHANGED)) {
                List<? extends SortKey> newSortKeys = new ArrayList<>(e.getSource().getSortKeys());
                newSortKeys.removeAll(priorSortKeys);
                if (!newSortKeys.isEmpty()) {
                    priorSortKeys = newSortKeys;
                    e.getSource().setSortKeys(priorSortKeys);
                    e.getSource().allRowsChanged();
                }
            }
        }
    };
    rowSorter.addRowSorterListener(listener);
    return listener;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) SortKey(javax.swing.RowSorter.SortKey) RowSorterEvent(javax.swing.event.RowSorterEvent) RowSorterListener(javax.swing.event.RowSorterListener)

Example 9 with SortKey

use of javax.swing.RowSorter.SortKey in project JMRI by JMRI.

the class JmriJTablePersistenceManager method savePreferences.

@Override
public synchronized void savePreferences(Profile profile) {
    log.debug("Saving preferences (dirty={})...", this.dirty);
    Element element = new Element(TABLES_ELEMENT, TABLES_NAMESPACE);
    if (!this.columns.isEmpty()) {
        this.columns.entrySet().stream().map((entry) -> {
            Element table = new Element("table").setAttribute("name", entry.getKey());
            Element columnsElement = new Element("columns");
            entry.getValue().entrySet().stream().map((column) -> {
                Element columnElement = new Element("column").setAttribute("name", column.getKey());
                if (column.getValue().getOrder() != -1) {
                    columnElement.setAttribute("order", Integer.toString(column.getValue().getOrder()));
                }
                if (column.getValue().getWidth() != -1) {
                    columnElement.setAttribute("width", Integer.toString(column.getValue().getWidth()));
                }
                columnElement.setAttribute("hidden", Boolean.toString(column.getValue().getHidden()));
                return columnElement;
            }).forEach((columnElement) -> {
                columnsElement.addContent(columnElement);
            });
            table.addContent(columnsElement);
            List<SortKey> keys = this.sortKeys.get(entry.getKey());
            if (keys != null) {
                Element sorter = new Element(SORT_ORDER);
                keys.stream().forEach((key) -> {
                    sorter.addContent(new Element("sortKey").setAttribute("column", Integer.toString(key.getColumn())).setAttribute(SORT_ORDER, key.getSortOrder().name()));
                });
                table.addContent(sorter);
            }
            return table;
        }).forEach((table) -> {
            element.addContent(table);
        });
    }
    try {
        ProfileUtils.getUserInterfaceConfiguration(ProfileManager.getDefault().getActiveProfile()).putConfigurationFragment(JDOMUtil.toW3CElement(element), false);
    } catch (JDOMException ex) {
        log.error("Unable to save user preferences", ex);
    }
    this.dirty = false;
}
Also used : Arrays(java.util.Arrays) TableColumnModel(javax.swing.table.TableColumnModel) Enumeration(java.util.Enumeration) SortKey(javax.swing.RowSorter.SortKey) XTableColumnModel(jmri.util.swing.XTableColumnModel) AbstractPreferencesManager(jmri.util.prefs.AbstractPreferencesManager) LoggerFactory(org.slf4j.LoggerFactory) ProfileManager(jmri.profile.ProfileManager) HashMap(java.util.HashMap) Timer(java.util.Timer) InitializationException(jmri.util.prefs.InitializationException) ArrayList(java.util.ArrayList) JDOMException(org.jdom2.JDOMException) JDOMUtil(jmri.util.jdom.JDOMUtil) ProfileUtils(jmri.profile.ProfileUtils) Map(java.util.Map) Profile(jmri.profile.Profile) TableColumnModelEvent(javax.swing.event.TableColumnModelEvent) RowSorter(javax.swing.RowSorter) TimerTask(java.util.TimerTask) ListSelectionEvent(javax.swing.event.ListSelectionEvent) Nonnull(javax.annotation.Nonnull) PropertyChangeEvent(java.beans.PropertyChangeEvent) ChangeEvent(javax.swing.event.ChangeEvent) Logger(org.slf4j.Logger) TableColumn(javax.swing.table.TableColumn) RowSorterListener(javax.swing.event.RowSorterListener) Set(java.util.Set) Objects(java.util.Objects) SortOrder(javax.swing.SortOrder) List(java.util.List) TableColumnModelListener(javax.swing.event.TableColumnModelListener) PropertyChangeListener(java.beans.PropertyChangeListener) JTable(javax.swing.JTable) DataConversionException(org.jdom2.DataConversionException) CheckForNull(javax.annotation.CheckForNull) RowSorterEvent(javax.swing.event.RowSorterEvent) Element(org.jdom2.Element) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) List(java.util.List) JDOMException(org.jdom2.JDOMException)

Example 10 with SortKey

use of javax.swing.RowSorter.SortKey in project JMRI by JMRI.

the class JmriJTablePersistenceManager method setTableColumnPreferences.

/**
     * Transition support for the standard {@link jmri.UserPreferencesManager}
     * instance (a {@link jmri.managers.JmriUserPreferencesManager}) so it does
     * not need to maintain separate knowledge of table column state.
     *
     * @param table  the table name
     * @param column the column name
     * @param order  order of the column
     * @param width  column width
     * @param sort   how the column is sorted
     * @param hidden true if column is hidden
     * @throws NullPointerException if either name is null
     * @deprecated since 4.5.2
     */
@Deprecated
public void setTableColumnPreferences(String table, String column, int order, int width, SortOrder sort, boolean hidden) {
    Objects.requireNonNull(table, "table name must be nonnull");
    if (sort != SortOrder.UNSORTED) {
        List<SortKey> keys = new ArrayList<>();
        keys.add(new SortKey(order, sort));
        this.sortKeys.put(table, keys);
    }
    this.setPersistedState(table, column, order, width, sort, hidden);
}
Also used : ArrayList(java.util.ArrayList) SortKey(javax.swing.RowSorter.SortKey)

Aggregations

SortKey (javax.swing.RowSorter.SortKey)16 Test (org.junit.Test)10 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)9 ArrayList (java.util.ArrayList)6 List (java.util.List)4 RowSorter (javax.swing.RowSorter)3 SortOrder (javax.swing.SortOrder)3 HashMap (java.util.HashMap)2 RowSorterEvent (javax.swing.event.RowSorterEvent)2 RowSorterListener (javax.swing.event.RowSorterListener)2 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 String.format (java.lang.String.format)1 AbstractMap (java.util.AbstractMap)1 Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 Comparator (java.util.Comparator)1