Search in sources :

Example 6 with SortOrder

use of javax.swing.SortOrder 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 7 with SortOrder

use of javax.swing.SortOrder 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 8 with SortOrder

use of javax.swing.SortOrder in project JMRI by JMRI.

the class JmriJTablePersistenceManagerTest method testSetPersistedState.

/**
     * Test of setPersistedState method, of class JmriJTablePersistenceManager.
     */
@Test
@Ignore
public void testSetPersistedState() {
    System.out.println("setPersistedState");
    String table = "";
    String column = "";
    int order = 0;
    int width = 0;
    SortOrder sort = null;
    boolean hidden = false;
    JmriJTablePersistenceManager instance = new JmriJTablePersistenceManager();
    instance.setPersistedState(table, column, order, width, sort, hidden);
    // TODO review the generated test code and remove the default call to fail.
    fail("The test case is a prototype.");
}
Also used : SortOrder(javax.swing.SortOrder) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

SortOrder (javax.swing.SortOrder)8 RowSorter (javax.swing.RowSorter)2 Element (org.jdom2.Element)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 SortKey (javax.swing.RowSorter.SortKey)1 TableColumn (javax.swing.table.TableColumn)1 TableColumnModel (javax.swing.table.TableColumnModel)1 XTableColumnModel (jmri.util.swing.XTableColumnModel)1 DataConversionException (org.jdom2.DataConversionException)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1