Search in sources :

Example 11 with TableView

use of org.apache.pivot.wtk.TableView in project pivot by apache.

the class TerraTableViewSkin method getPreferredHeight.

@Override
public int getPreferredHeight(int width) {
    int preferredHeight = 0;
    TableView tableView = (TableView) getComponent();
    int n = tableView.getTableData().getLength();
    if (variableRowHeight) {
        ArrayList<Integer> columnWidthsLocal = getColumnWidths(tableView, width);
        for (int i = 0; i < n; i++) {
            preferredHeight += getVariableRowHeight(i, columnWidthsLocal);
        }
    } else {
        int fixedRowHeightLocal = calculateFixedRowHeight(tableView);
        preferredHeight = fixedRowHeightLocal * n;
    }
    // Include space for horizontal grid lines
    preferredHeight += (n - 1);
    if (includeTrailingHorizontalGridLine) {
        preferredHeight++;
    }
    return preferredHeight;
}
Also used : TableView(org.apache.pivot.wtk.TableView)

Example 12 with TableView

use of org.apache.pivot.wtk.TableView in project pivot by apache.

the class TerraTableViewSkin method paint.

@Override
public void paint(Graphics2D graphics) {
    TableView tableView = (TableView) getComponent();
    @SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) tableView.getTableData();
    TableView.ColumnSequence columns = tableView.getColumns();
    int width = getWidth();
    int height = getHeight();
    // Paint the background
    if (backgroundColor != null) {
        graphics.setPaint(backgroundColor);
        graphics.fillRect(0, 0, width, height);
    }
    // Ensure that we only paint items that are visible
    int rowStart = 0;
    int rowEnd = tableData.getLength() - 1;
    Rectangle clipBounds = graphics.getClipBounds();
    if (clipBounds != null) {
        if (variableRowHeight) {
            rowStart = getRowAt(clipBounds.y);
            if (rowStart == -1) {
                rowStart = tableData.getLength();
            }
            if (rowEnd != -1) {
                int clipBottom = clipBounds.y + clipBounds.height - 1;
                clipBottom = Math.min(clipBottom, rowBoundaries.get(rowEnd).intValue() - 1);
                rowEnd = getRowAt(clipBottom);
            }
        } else {
            rowStart = Math.max(rowStart, (int) Math.floor(clipBounds.y / (double) (fixedRowHeight + 1)));
            rowEnd = Math.min(rowEnd, (int) Math.ceil((clipBounds.y + clipBounds.height) / (double) (fixedRowHeight + 1)) - 1);
        }
    }
    // Paint the row background
    if (alternateRowBackgroundColor != null) {
        for (int rowIndex = rowStart; rowIndex <= rowEnd; rowIndex++) {
            int rowY = getRowY(rowIndex);
            int rowHeight = getRowHeight(rowIndex);
            if (rowIndex % 2 > 0) {
                graphics.setPaint(alternateRowBackgroundColor);
                graphics.fillRect(0, rowY, width, rowHeight + 1);
            }
        }
    }
    // Paint the column backgrounds
    int columnX = 0;
    if (columnSelectionColor != null) {
        graphics.setColor(columnSelectionColor);
        columnX = 0;
        for (int columnIndex = 0, columnCount = columns.getLength(); columnIndex < columnCount; columnIndex++) {
            TableView.Column column = columns.get(columnIndex);
            int columnWidth = columnWidths.get(columnIndex).intValue();
            String columnName = column.getName();
            SortDirection sortDirection = tableView.getSort().get(columnName);
            if (sortDirection != null) {
                graphics.fillRect(columnX, 0, columnWidth, height);
            }
            columnX += columnWidth + 1;
        }
    }
    // Paint the row content
    for (int rowIndex = rowStart; rowIndex <= rowEnd; rowIndex++) {
        Object rowData = tableData.get(rowIndex);
        boolean rowHighlighted = (rowIndex == highlightIndex && tableView.getSelectMode() != TableView.SelectMode.NONE);
        boolean rowSelected = tableView.isRowSelected(rowIndex);
        boolean rowDisabled = tableView.isRowDisabled(rowIndex);
        int rowY = getRowY(rowIndex);
        int rowHeight = getRowHeight(rowIndex);
        // Paint selection state
        Color rowBackgroundColor = null;
        if (rowSelected) {
            rowBackgroundColor = (tableView.isFocused()) ? this.selectionBackgroundColor : inactiveSelectionBackgroundColor;
        } else {
            if (rowHighlighted && showHighlight && !rowDisabled) {
                rowBackgroundColor = highlightBackgroundColor;
            }
        }
        if (rowBackgroundColor != null) {
            graphics.setPaint(rowBackgroundColor);
            graphics.fillRect(0, rowY, width, rowHeight);
        }
        // Paint the cells
        columnX = 0;
        for (int columnIndex = 0, columnCount = columns.getLength(); columnIndex < columnCount; columnIndex++) {
            TableView.Column column = columns.get(columnIndex);
            TableView.CellRenderer cellRenderer = column.getCellRenderer();
            int columnWidth = columnWidths.get(columnIndex).intValue();
            Graphics2D rendererGraphics = (Graphics2D) graphics.create(columnX, rowY, columnWidth, rowHeight);
            cellRenderer.render(rowData, rowIndex, columnIndex, tableView, column.getName(), rowSelected, rowHighlighted, rowDisabled);
            cellRenderer.setSize(columnWidth, rowHeight);
            cellRenderer.paint(rendererGraphics);
            rendererGraphics.dispose();
            columnX += columnWidth + 1;
        }
    }
    // Paint the vertical grid lines
    graphics.setPaint(verticalGridColor);
    if (showVerticalGridLines) {
        columnX = 0;
        for (int columnIndex = 0, columnCount = columns.getLength(); columnIndex < columnCount; columnIndex++) {
            columnX += columnWidths.get(columnIndex).intValue();
            if (columnIndex < columnCount - 1 || includeTrailingVerticalGridLine) {
                if (!themeIsFlat()) {
                    GraphicsUtilities.drawLine(graphics, columnX, 0, height, Orientation.VERTICAL);
                }
            }
            columnX++;
        }
    }
    // Paint the horizontal grid lines
    graphics.setPaint(horizontalGridColor);
    if (showHorizontalGridLines) {
        int rowCount = tableData.getLength();
        for (int rowIndex = rowStart; rowIndex <= rowEnd; rowIndex++) {
            int gridY = getRowY(rowIndex + 1) - 1;
            if (rowIndex < rowCount - 1 || includeTrailingHorizontalGridLine) {
                if (!themeIsFlat()) {
                    GraphicsUtilities.drawLine(graphics, 0, gridY, width, Orientation.HORIZONTAL);
                }
            }
        }
        if (columnSelectionHorizontalGridColor != null) {
            graphics.setColor(columnSelectionHorizontalGridColor);
            columnX = 0;
            for (int columnIndex = 0, columnCount = columns.getLength(); columnIndex < columnCount; columnIndex++) {
                TableView.Column column = columns.get(columnIndex);
                int columnWidth = columnWidths.get(columnIndex).intValue();
                String columnName = column.getName();
                SortDirection sortDirection = tableView.getSort().get(columnName);
                if (sortDirection != null) {
                    for (int rowIndex = rowStart; rowIndex <= rowEnd; rowIndex++) {
                        int gridY = getRowY(rowIndex + 1) - 1;
                        if (rowIndex < rowCount - 1 || includeTrailingHorizontalGridLine) {
                            if (!themeIsFlat()) {
                                GraphicsUtilities.drawLine(graphics, columnX, gridY, columnWidth, Orientation.HORIZONTAL);
                            }
                        }
                    }
                }
                columnX += columnWidth + 1;
            }
        }
    }
}
Also used : Color(java.awt.Color) Rectangle(java.awt.Rectangle) Graphics2D(java.awt.Graphics2D) SortDirection(org.apache.pivot.wtk.SortDirection) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) TableView(org.apache.pivot.wtk.TableView)

Example 13 with TableView

use of org.apache.pivot.wtk.TableView in project pivot by apache.

the class TerraTableViewSkin method layout.

@Override
public void layout() {
    columnWidths = getColumnWidths((TableView) getComponent(), getWidth());
    TableView tableView = (TableView) getComponent();
    TableView.ColumnSequence columns = tableView.getColumns();
    if (variableRowHeight) {
        @SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) tableView.getTableData();
        int n = tableData.getLength();
        rowBoundaries = new ArrayList<>(n);
        int rowY = 0;
        for (int i = 0; i < n; i++) {
            Object rowData = tableData.get(i);
            int rowHeight = 0;
            for (int columnIndex = 0, columnCount = columns.getLength(); columnIndex < columnCount; columnIndex++) {
                TableView.Column column = columns.get(columnIndex);
                TableView.CellRenderer cellRenderer = column.getCellRenderer();
                int columnWidth = columnWidths.get(columnIndex).intValue();
                cellRenderer.render(rowData, i, columnIndex, tableView, column.getName(), false, false, false);
                rowHeight = Math.max(rowHeight, cellRenderer.getPreferredHeight(columnWidth));
            }
            rowY += rowHeight;
            rowBoundaries.add(Integer.valueOf(rowY));
            rowY++;
        }
    } else {
        fixedRowHeight = calculateFixedRowHeight(tableView);
    }
    if (validateSelection) {
        // Ensure that the selection is visible
        Sequence<Span> selectedRanges = tableView.getSelectedRanges();
        if (selectedRanges.getLength() > 0) {
            int rangeStart = selectedRanges.get(0).start;
            int rangeEnd = selectedRanges.get(selectedRanges.getLength() - 1).end;
            Bounds selectionBounds = getRowBounds(rangeStart);
            selectionBounds = selectionBounds.union(getRowBounds(rangeEnd));
            Bounds visibleSelectionBounds = tableView.getVisibleArea(selectionBounds);
            if (visibleSelectionBounds != null && visibleSelectionBounds.height < selectionBounds.height) {
                tableView.scrollAreaToVisible(selectionBounds);
            }
        }
    }
    validateSelection = false;
}
Also used : Bounds(org.apache.pivot.wtk.Bounds) Span(org.apache.pivot.wtk.Span) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) TableView(org.apache.pivot.wtk.TableView)

Example 14 with TableView

use of org.apache.pivot.wtk.TableView in project pivot by apache.

the class TerraTableViewSkin method keyPressed.

/**
 * {@link KeyCode#UP UP} Selects the previous enabled row when select mode
 * is not {@link SelectMode#NONE}<br> {@link KeyCode#DOWN DOWN} Selects the
 * next enabled row when select mode is not {@link SelectMode#NONE}<p>
 * {@link Modifier#SHIFT SHIFT} + {@link KeyCode#UP UP} Increases the
 * selection size by including the previous enabled row when select mode is
 * {@link SelectMode#MULTI}<br> {@link Modifier#SHIFT SHIFT} +
 * {@link KeyCode#DOWN DOWN} Increases the selection size by including the
 * next enabled row when select mode is {@link SelectMode#MULTI}<br>
 * {@code Cmd/Ctrl-A} in {@link SelectMode#MULTI} select mode to select everything<br>
 * {@code Cmd/Ctrl-U} will unselect whatever is selected<br>
 * {@link KeyCode#SPACE SPACE} wil select/unselect the "current" location
 */
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
    boolean consumed = super.keyPressed(component, keyCode, keyLocation);
    TableView tableView = (TableView) getComponent();
    TableView.SelectMode selectMode = tableView.getSelectMode();
    switch(keyCode) {
        case Keyboard.KeyCode.UP:
            {
                if (selectMode != TableView.SelectMode.NONE) {
                    int index = tableView.getFirstSelectedIndex();
                    do {
                        index--;
                    } while (index >= 0 && tableView.isRowDisabled(index));
                    if (index >= 0) {
                        if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && tableView.getSelectMode() == TableView.SelectMode.MULTI) {
                            tableView.addSelectedIndex(index);
                        } else {
                            tableView.setSelectedIndex(index);
                        }
                        lastKeyboardSelectIndex = index;
                    }
                    consumed = true;
                }
                break;
            }
        case Keyboard.KeyCode.DOWN:
            {
                if (selectMode != TableView.SelectMode.NONE) {
                    int index = tableView.getLastSelectedIndex();
                    int count = tableView.getTableData().getLength();
                    do {
                        index++;
                    } while (index < count && tableView.isRowDisabled(index));
                    if (index < count) {
                        if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && tableView.getSelectMode() == TableView.SelectMode.MULTI) {
                            tableView.addSelectedIndex(index);
                        } else {
                            tableView.setSelectedIndex(index);
                        }
                        lastKeyboardSelectIndex = index;
                    }
                    consumed = true;
                }
                break;
            }
        case Keyboard.KeyCode.SPACE:
            {
                if (lastKeyboardSelectIndex != -1 && selectMode != TableView.SelectMode.NONE) {
                    if (!tableView.isRowDisabled(lastKeyboardSelectIndex)) {
                        switch(selectMode) {
                            case SINGLE:
                                if (tableView.isRowSelected(lastKeyboardSelectIndex)) {
                                    tableView.setSelectedIndex(-1);
                                } else {
                                    tableView.setSelectedIndex(lastKeyboardSelectIndex);
                                }
                                break;
                            case MULTI:
                                if (tableView.isRowSelected(lastKeyboardSelectIndex)) {
                                    tableView.removeSelectedIndex(lastKeyboardSelectIndex);
                                } else {
                                    tableView.addSelectedIndex(lastKeyboardSelectIndex);
                                }
                                break;
                        }
                        consumed = true;
                    }
                }
                break;
            }
        case Keyboard.KeyCode.A:
            {
                Modifier cmdModifier = Platform.getCommandModifier();
                if (Keyboard.isPressed(cmdModifier)) {
                    if (selectMode == TableView.SelectMode.MULTI) {
                        tableView.selectAll();
                        // TODO: what should it be?
                        lastKeyboardSelectIndex = tableView.getTableData().getLength() - 1;
                        consumed = true;
                    }
                }
                break;
            }
        case Keyboard.KeyCode.U:
            {
                Modifier cmdModifier = Platform.getCommandModifier();
                if (Keyboard.isPressed(cmdModifier)) {
                    switch(selectMode) {
                        case NONE:
                            break;
                        case SINGLE:
                        case MULTI:
                            tableView.clearSelection();
                            // TODO: what should it be?
                            lastKeyboardSelectIndex = 0;
                            consumed = true;
                            break;
                    }
                }
                break;
            }
        default:
            {
                break;
            }
    }
    // Clear the highlight
    if (highlightIndex != -1 && tableView.getSelectMode() != TableView.SelectMode.NONE && showHighlight && consumed) {
        repaintComponent(getRowBounds(highlightIndex));
    }
    highlightIndex = -1;
    return consumed;
}
Also used : SelectMode(org.apache.pivot.wtk.TableView.SelectMode) Modifier(org.apache.pivot.wtk.Keyboard.Modifier) TableView(org.apache.pivot.wtk.TableView)

Example 15 with TableView

use of org.apache.pivot.wtk.TableView in project pivot by apache.

the class TerraTableViewSkin method getBaseline.

@Override
public int getBaseline(int width, int height) {
    TableView tableView = (TableView) getComponent();
    @SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) tableView.getTableData();
    int baseline = -1;
    TableView.ColumnSequence columns = tableView.getColumns();
    ArrayList<Integer> columnWidthsLocal = getColumnWidths(tableView, width);
    if (variableRowHeight) {
        int rowHeight = getVariableRowHeight(0, columnWidthsLocal);
        Object rowData = tableData.get(0);
        for (int i = 0, n = columns.getLength(); i < n; i++) {
            TableView.Column column = columns.get(i);
            TableView.CellRenderer cellRenderer = column.getCellRenderer();
            cellRenderer.render(rowData, 0, i, tableView, column.getName(), false, false, false);
            baseline = Math.max(baseline, cellRenderer.getBaseline(columnWidthsLocal.get(i).intValue(), rowHeight));
        }
    } else {
        int rowHeight = calculateFixedRowHeight(tableView);
        for (int i = 0, n = columns.getLength(); i < n; i++) {
            TableView.Column column = columns.get(i);
            TableView.CellRenderer cellRenderer = column.getCellRenderer();
            cellRenderer.render(null, -1, i, tableView, column.getName(), false, false, false);
            baseline = Math.max(baseline, cellRenderer.getBaseline(columnWidthsLocal.get(i).intValue(), rowHeight));
        }
    }
    return baseline;
}
Also used : ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) TableView(org.apache.pivot.wtk.TableView)

Aggregations

TableView (org.apache.pivot.wtk.TableView)36 ArrayList (org.apache.pivot.collections.ArrayList)12 List (org.apache.pivot.collections.List)11 TableViewHeader (org.apache.pivot.wtk.TableViewHeader)11 GradientPaint (java.awt.GradientPaint)9 Bounds (org.apache.pivot.wtk.Bounds)7 Span (org.apache.pivot.wtk.Span)6 TableViewSelectionListener (org.apache.pivot.wtk.TableViewSelectionListener)5 TableViewSortListener (org.apache.pivot.wtk.TableViewSortListener)5 IOException (java.io.IOException)4 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)4 Component (org.apache.pivot.wtk.Component)4 Keyboard (org.apache.pivot.wtk.Keyboard)4 SerializationException (org.apache.pivot.serialization.SerializationException)3 Button (org.apache.pivot.wtk.Button)3 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)3 ComponentKeyListener (org.apache.pivot.wtk.ComponentKeyListener)3 Container (org.apache.pivot.wtk.Container)3 ListButton (org.apache.pivot.wtk.ListButton)3 ListButtonSelectionListener (org.apache.pivot.wtk.ListButtonSelectionListener)3