Search in sources :

Example 1 with ColumnSet

use of org.eclipse.scout.rt.client.ui.basic.table.ColumnSet in project scout.rt by eclipse.

the class BookmarkUtility method backupTableColumns.

/**
 * Constructs a list of {@link TableColumnState} objects which describe the set of columns of the given {@link ITable}
 * .
 *
 * @param table
 *          The table with the columns to back-up.
 * @return A {@link List} of {@link TableColumnState} objects that can be restored via
 *         {@link #restoreTableColumns(ITable, List)}
 */
public static List<TableColumnState> backupTableColumns(ITable table) {
    ArrayList<TableColumnState> allColumns = new ArrayList<TableColumnState>();
    ColumnSet columnSet = table.getColumnSet();
    // add all columns but in user order
    for (IColumn<?> c : columnSet.getAllColumnsInUserOrder()) {
        TableColumnState colState = new TableColumnState();
        colState.setColumnClassName(c.getColumnId());
        colState.setDisplayable(c.isDisplayable());
        colState.setVisible(c.isDisplayable() && c.isVisible());
        colState.setWidth(c.getWidth());
        if (c instanceof INumberColumn) {
            colState.setAggregationFunction(((INumberColumn) c).getAggregationFunction());
            colState.setBackgroundEffect(((INumberColumn) c).getBackgroundEffect());
        }
        if (columnSet.isUserSortColumn(c)) {
            int sortOrder = columnSet.getSortColumnIndex(c);
            if (sortOrder >= 0) {
                colState.setSortOrder(sortOrder);
                colState.setSortAscending(c.isSortAscending());
                colState.setGroupingActive(c.isGroupingActive());
            } else {
                colState.setSortOrder(-1);
            }
        }
        allColumns.add(colState);
    }
    return allColumns;
}
Also used : INumberColumn(org.eclipse.scout.rt.client.ui.basic.table.columns.INumberColumn) ArrayList(java.util.ArrayList) ColumnSet(org.eclipse.scout.rt.client.ui.basic.table.ColumnSet) TableColumnState(org.eclipse.scout.rt.shared.services.common.bookmark.TableColumnState)

Example 2 with ColumnSet

use of org.eclipse.scout.rt.client.ui.basic.table.ColumnSet in project scout.rt by eclipse.

the class BookmarkUtility method restoreTableColumns.

/**
 * Restores a tables columns from the given list of {@link TableColumnState} objects.
 *
 * @param table
 *          The table to be restored.
 * @param oldColumns
 *          A {@link List} of {@link TableColumnState} objects to restore. Such can be retrieved by the
 *          {@link #backupTableColumns(ITable)} method.
 */
public static void restoreTableColumns(ITable table, List<TableColumnState> oldColumns) {
    if (oldColumns != null && oldColumns.size() > 0 && table != null) {
        ColumnSet columnSet = table.getColumnSet();
        // visible columns and width
        ArrayList<IColumn> visibleColumns = new ArrayList<IColumn>();
        for (TableColumnState colState : oldColumns) {
            // legacy support: null=true
            if (colState.getVisible() == null || colState.getVisible()) {
                IColumn col = resolveColumn(columnSet.getDisplayableColumns(), colState.getClassName());
                if (col != null && col.isDisplayable()) {
                    if (colState.getWidth() > 0) {
                        col.setWidth(colState.getWidth());
                    }
                    visibleColumns.add(col);
                }
            }
        }
        List<IColumn<?>> existingVisibleCols = columnSet.getVisibleColumns();
        if (!existingVisibleCols.equals(visibleColumns)) {
            columnSet.setVisibleColumns(visibleColumns);
        }
        // aggregation functions and background effect:
        for (TableColumnState colState : oldColumns) {
            IColumn<?> col = BookmarkUtility.resolveColumn(columnSet.getColumns(), colState.getClassName());
            if (col instanceof INumberColumn) {
                if (colState.getAggregationFunction() != null) {
                    ((INumberColumn<?>) col).setAggregationFunction(colState.getAggregationFunction());
                }
                ((INumberColumn<?>) col).setBackgroundEffect(colState.getBackgroundEffect());
            }
        }
        // sort order (only respect visible and user-sort columns)
        boolean userSortValid = true;
        TreeMap<Integer, IColumn> sortColMap = new TreeMap<Integer, IColumn>();
        HashMap<IColumn<?>, TableColumnState> sortColToColumnState = new HashMap<>();
        HashSet<IColumn<?>> groupedCols = new HashSet<>();
        for (TableColumnState colState : oldColumns) {
            if (colState.getSortOrder() >= 0) {
                IColumn col = BookmarkUtility.resolveColumn(columnSet.getColumns(), colState.getClassName());
                if (col != null) {
                    sortColMap.put(colState.getSortOrder(), col);
                    sortColToColumnState.put(col, colState);
                    if (colState.isGroupingActive()) {
                        groupedCols.add(col);
                    }
                    if (col.getSortIndex() != colState.getSortOrder()) {
                        userSortValid = false;
                    }
                    if (col.isSortAscending() != colState.isSortAscending()) {
                        userSortValid = false;
                    }
                    if (col.isGroupingActive() != colState.isGroupingActive()) {
                        userSortValid = false;
                    }
                }
            }
        }
        if (!sortColMap.values().containsAll(columnSet.getUserSortColumns())) {
            userSortValid = false;
        }
        if (userSortValid) {
            HashSet<IColumn<?>> existingGroupedUserSortCols = new HashSet<>();
            // check if grouping is valid also:
            for (IColumn<?> c : columnSet.getUserSortColumns()) {
                if (c.isGroupingActive()) {
                    existingGroupedUserSortCols.add(c);
                }
            }
            if (!groupedCols.containsAll(existingGroupedUserSortCols)) {
                userSortValid = false;
            }
        }
        if (!userSortValid) {
            columnSet.clearSortColumns();
            boolean groupingPossible = true;
            for (IColumn<?> headSortColumn : columnSet.getPermanentHeadSortColumns()) {
                if (!headSortColumn.isVisible() || !headSortColumn.isGroupingActive()) {
                    TableColumnState state = sortColToColumnState.get(headSortColumn);
                    if (state != null && !state.isGroupingActive()) {
                        groupingPossible = false;
                        break;
                    }
                }
            }
            for (IColumn<?> col : sortColMap.values()) {
                TableColumnState state = sortColToColumnState.get(col);
                if (groupingPossible) {
                    if (state.isGroupingActive()) {
                        columnSet.addGroupingColumn(col, state.isSortAscending());
                    } else {
                        columnSet.addSortColumn(col, state.isSortAscending());
                        groupingPossible = false;
                    }
                } else {
                    columnSet.addSortColumn(col, state.isSortAscending());
                }
            }
            table.sort();
        }
        ClientUIPreferences.getInstance().setAllTableColumnPreferences(table);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ColumnSet(org.eclipse.scout.rt.client.ui.basic.table.ColumnSet) TreeMap(java.util.TreeMap) TableColumnState(org.eclipse.scout.rt.shared.services.common.bookmark.TableColumnState) INumberColumn(org.eclipse.scout.rt.client.ui.basic.table.columns.INumberColumn) IColumn(org.eclipse.scout.rt.client.ui.basic.table.columns.IColumn) HashSet(java.util.HashSet)

Example 3 with ColumnSet

use of org.eclipse.scout.rt.client.ui.basic.table.ColumnSet in project scout.rt by eclipse.

the class ShowInvisibleColumnsFormTest method before.

@Before
public void before() {
    m_column1 = mockColumn("column1", false);
    m_column2 = mockColumn("column2", true);
    m_column3 = mockColumn("column3", false);
    m_column4 = mockColumn("column4", true);
    m_tableMock = Mockito.mock(ITable.class);
    ColumnSet mockColumnSet = Mockito.mock(ColumnSet.class);
    Mockito.when(mockColumnSet.getAllColumnsInUserOrder()).thenReturn(CollectionUtility.arrayList(m_column3, m_column4, m_column1, m_column2));
    Mockito.when(mockColumnSet.getColumns()).thenReturn(CollectionUtility.arrayList(m_column1, m_column2, m_column3, m_column4));
    Mockito.when(m_tableMock.getColumnSet()).thenReturn(mockColumnSet);
}
Also used : ITable(org.eclipse.scout.rt.client.ui.basic.table.ITable) ColumnSet(org.eclipse.scout.rt.client.ui.basic.table.ColumnSet) Before(org.junit.Before)

Aggregations

ColumnSet (org.eclipse.scout.rt.client.ui.basic.table.ColumnSet)3 ArrayList (java.util.ArrayList)2 INumberColumn (org.eclipse.scout.rt.client.ui.basic.table.columns.INumberColumn)2 TableColumnState (org.eclipse.scout.rt.shared.services.common.bookmark.TableColumnState)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 TreeMap (java.util.TreeMap)1 ITable (org.eclipse.scout.rt.client.ui.basic.table.ITable)1 IColumn (org.eclipse.scout.rt.client.ui.basic.table.columns.IColumn)1 Before (org.junit.Before)1