Search in sources :

Example 11 with SortKey

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

the class JmriJTablePersistenceManager method initialize.

@Override
public void initialize(Profile profile) throws InitializationException {
    try {
        Element element = JDOMUtil.toJDOMElement(ProfileUtils.getUserInterfaceConfiguration(ProfileManager.getDefault().getActiveProfile()).getConfigurationFragment(TABLES_ELEMENT, TABLES_NAMESPACE, false));
        element.getChildren("table").stream().forEach((table) -> {
            String tableName = table.getAttributeValue("name");
            int sortColumn = -1;
            SortOrder sortOrder = SortOrder.UNSORTED;
            Element sortElement = table.getChild(SORT_ORDER);
            if (sortElement != null) {
                List<SortKey> keys = new ArrayList<>();
                for (Element sortKey : sortElement.getChildren()) {
                    sortOrder = SortOrder.valueOf(sortKey.getAttributeValue(SORT_ORDER));
                    try {
                        sortColumn = sortKey.getAttribute("column").getIntValue();
                        SortKey key = new SortKey(sortColumn, sortOrder);
                        keys.add(key);
                    } catch (DataConversionException ex) {
                        log.error("Unable to get sort column as integer");
                    }
                }
                this.sortKeys.put(tableName, keys);
            }
            log.debug("Table {} column {} is sorted {}", tableName, sortColumn, sortOrder);
            for (Element column : table.getChild("columns").getChildren()) {
                String columnName = column.getAttribute("name").getValue();
                int order = -1;
                int width = -1;
                boolean hidden = false;
                try {
                    if (column.getAttributeValue("order") != null) {
                        order = column.getAttribute("order").getIntValue();
                    }
                    if (column.getAttributeValue("width") != null) {
                        width = column.getAttribute("width").getIntValue();
                    }
                    if (column.getAttribute("hidden") != null) {
                        hidden = column.getAttribute("hidden").getBooleanValue();
                    }
                } catch (DataConversionException ex) {
                    log.error("Unable to parse column \"{}\"", columnName);
                    continue;
                }
                if (sortColumn == order) {
                    this.setPersistedState(tableName, columnName, order, width, sortOrder, hidden);
                } else {
                    this.setPersistedState(tableName, columnName, order, width, SortOrder.UNSORTED, hidden);
                }
            }
        });
    } catch (NullPointerException ex) {
        log.info("Table preferences not found.\nThis is expected on the first time the \"{}\" profile is used on this computer.", ProfileManager.getDefault().getActiveProfile().getName());
    }
    this.setInitialized(profile, true);
}
Also used : Element(org.jdom2.Element) ArrayList(java.util.ArrayList) SortOrder(javax.swing.SortOrder) SortKey(javax.swing.RowSorter.SortKey) DataConversionException(org.jdom2.DataConversionException)

Example 12 with SortKey

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

the class RowSorterUtil method setSortOrder.

/**
     * Set the sort order for a table using the specified column given a
     * RowSorter for the TableModel containing the column.
     * <p>
     * This makes all other columns unsorted, even if the specified column is
     * also specified to be unsorted.
     *
     * @param rowSorter the sorter
     * @param column    the column index in the model, not the view
     * @param sortOrder the sort order
     */
public static void setSortOrder(@Nonnull RowSorter<? extends TableModel> rowSorter, int column, @Nonnull SortOrder sortOrder) {
    List<SortKey> keys = new ArrayList<>();
    if (!sortOrder.equals(SortOrder.UNSORTED)) {
        keys.add(new RowSorter.SortKey(column, sortOrder));
    }
    rowSorter.setSortKeys(keys);
}
Also used : RowSorter(javax.swing.RowSorter) ArrayList(java.util.ArrayList) SortKey(javax.swing.RowSorter.SortKey) SortKey(javax.swing.RowSorter.SortKey)

Example 13 with SortKey

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

the class ObjectTableSorterTest method fixLastRowWithAscendingValue.

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

Example 14 with SortKey

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

the class ObjectTableSorterTest method sortKeyAscending.

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

Example 15 with SortKey

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

the class ObjectTableSorterTest method fixLastRowWithDescendingKey.

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

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