Search in sources :

Example 1 with SortMode

use of org.apache.pivot.wtk.TableViewHeader.SortMode in project pivot by apache.

the class TerraTableViewHeaderSkin method mouseClick.

@Override
public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
    boolean consumed = super.mouseClick(component, button, x, y, count);
    if (button == Mouse.Button.LEFT) {
        TableViewHeader tableViewHeader = (TableViewHeader) getComponent();
        TableView tableView = tableViewHeader.getTableView();
        if (resizeHeaderIndex != -1) {
            TableView.Column column = tableView.getColumns().get(resizeHeaderIndex);
            if (count == 2 && !column.isRelative() && column.getWidth() != -1) {
                // Size the column to fit its contents
                int columnWidth = 0;
                TableView.CellRenderer cellRenderer = column.getCellRenderer();
                List<?> tableData = tableView.getTableData();
                int rowIndex = 0;
                for (Object rowData : tableData) {
                    cellRenderer.render(rowData, rowIndex++, resizeHeaderIndex, tableView, column.getName(), false, false, false);
                    columnWidth = Math.max(cellRenderer.getPreferredWidth(-1), columnWidth);
                }
                column.setWidth(columnWidth);
            }
        } else if (pressedHeaderIndex != -1) {
            // Press the header
            tableViewHeader.pressHeader(pressedHeaderIndex);
            // Update the sort
            TableViewHeader.SortMode sortMode = tableViewHeader.getSortMode();
            if (sortMode != TableViewHeader.SortMode.NONE) {
                TableView.Column column = tableView.getColumns().get(pressedHeaderIndex);
                String columnName = column.getName();
                SortDirection sortDirection = tableView.getSort().get(columnName);
                if (sortDirection == null) {
                    sortDirection = SortDirection.ASCENDING;
                } else if (sortDirection == SortDirection.ASCENDING) {
                    sortDirection = SortDirection.DESCENDING;
                } else {
                    sortDirection = SortDirection.ASCENDING;
                }
                if (sortMode == TableViewHeader.SortMode.MULTI_COLUMN && Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                    tableView.getSort().put(columnName, sortDirection);
                } else {
                    tableView.setSort(columnName, sortDirection);
                }
                consumed = true;
            }
        }
        resizeHeaderIndex = -1;
        pressedHeaderIndex = -1;
    }
    return consumed;
}
Also used : TableViewHeader(org.apache.pivot.wtk.TableViewHeader) SortDirection(org.apache.pivot.wtk.SortDirection) SortMode(org.apache.pivot.wtk.TableViewHeader.SortMode) GradientPaint(java.awt.GradientPaint) TableView(org.apache.pivot.wtk.TableView)

Aggregations

GradientPaint (java.awt.GradientPaint)1 SortDirection (org.apache.pivot.wtk.SortDirection)1 TableView (org.apache.pivot.wtk.TableView)1 TableViewHeader (org.apache.pivot.wtk.TableViewHeader)1 SortMode (org.apache.pivot.wtk.TableViewHeader.SortMode)1