Search in sources :

Example 11 with List

use of org.apache.pivot.collections.List in project pivot by apache.

the class LargeData method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    basePath = properties.get(BASE_PATH_KEY);
    if (basePath == null) {
        throw new IllegalArgumentException(BASE_PATH_KEY + " is required.");
    }
    origin = ApplicationContext.getOrigin();
    if (origin == null) {
        System.out.println("Running as a Standalone Java Application, with user home: \"" + USER_HOME + "\"");
        if (USER_HOME != null) {
            System.out.println("Set as origin the user home");
            origin = (new File(USER_HOME).toURI()).toURL();
        }
    }
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    window = (Window) bxmlSerializer.readObject(LargeData.class, "large_data.bxml");
    fileListButton = (ListButton) bxmlSerializer.getNamespace().get("fileListButton");
    loadDataButton = (PushButton) bxmlSerializer.getNamespace().get("loadDataButton");
    cancelButton = (PushButton) bxmlSerializer.getNamespace().get("cancelButton");
    clearButton = (PushButton) bxmlSerializer.getNamespace().get("clearButton");
    statusLabel = (Label) bxmlSerializer.getNamespace().get("statusLabel");
    tableView = (TableView) bxmlSerializer.getNamespace().get("tableView");
    fileListButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {

        @Override
        public void selectedItemChanged(ListButton listButtonArgument, Object previousSelectedItem) {
            Object selectedItem = listButtonArgument.getSelectedItem();
            System.out.println("Selected: " + selectedItem.toString() + ", now clear table data ...");
            // empty the table
            tableView.getTableData().clear();
        }
    });
    loadDataButton.getButtonPressListeners().add((button) -> {
        loadDataButton.setEnabled(false);
        cancelButton.setEnabled(true);
        loadData();
    });
    cancelButton.getButtonPressListeners().add((button) -> {
        if (loadDataTask != null) {
            loadDataTask.abort();
        }
        loadDataButton.setEnabled(true);
        cancelButton.setEnabled(false);
    });
    clearButton.getButtonPressListeners().add((button) -> {
        if (loadDataTask != null) {
            loadDataTask.abort();
        }
        // empty the table
        tableView.getTableData().clear();
        statusLabel.setText("");
    });
    tableView.getTableViewSortListeners().add(new TableViewSortListener() {

        @Override
        public void sortChanged(TableView tableViewArgument) {
            @SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) tableViewArgument.getTableData();
            long startTime = System.currentTimeMillis();
            tableData.setComparator(new TableViewRowComparator(tableViewArgument));
            long endTime = System.currentTimeMillis();
            statusLabel.setText("Data sorted in " + (endTime - startTime) + " ms.");
        }
    });
    window.open(display);
}
Also used : ListButton(org.apache.pivot.wtk.ListButton) TableViewRowComparator(org.apache.pivot.wtk.content.TableViewRowComparator) ListButtonSelectionListener(org.apache.pivot.wtk.ListButtonSelectionListener) TableViewSortListener(org.apache.pivot.wtk.TableViewSortListener) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) File(java.io.File) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) TableView(org.apache.pivot.wtk.TableView)

Example 12 with List

use of org.apache.pivot.collections.List in project pivot by apache.

the class TerraTableViewSkin method getCellBounds.

@Override
public Bounds getCellBounds(int rowIndex, int columnIndex) {
    TableView tableView = (TableView) getComponent();
    @SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) tableView.getTableData();
    Utils.checkZeroBasedIndex(rowIndex, tableData.getLength());
    int cellX = 0;
    for (int i = 0; i < columnIndex; i++) {
        cellX += (columnWidths.get(i).intValue() + 1);
    }
    int rowHeight = getRowHeight(rowIndex);
    return new Bounds(cellX, rowIndex * (rowHeight + 1), columnWidths.get(columnIndex).intValue(), rowHeight);
}
Also used : Bounds(org.apache.pivot.wtk.Bounds) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) TableView(org.apache.pivot.wtk.TableView)

Example 13 with List

use of org.apache.pivot.collections.List 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 14 with List

use of org.apache.pivot.collections.List 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 15 with List

use of org.apache.pivot.collections.List 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

List (org.apache.pivot.collections.List)47 ArrayList (org.apache.pivot.collections.ArrayList)37 TableView (org.apache.pivot.wtk.TableView)11 Span (org.apache.pivot.wtk.Span)10 Button (org.apache.pivot.wtk.Button)8 ListView (org.apache.pivot.wtk.ListView)8 Point (org.apache.pivot.wtk.Point)8 IOException (java.io.IOException)7 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)7 SerializationException (org.apache.pivot.serialization.SerializationException)6 Component (org.apache.pivot.wtk.Component)6 File (java.io.File)5 HashMap (org.apache.pivot.collections.HashMap)5 Map (org.apache.pivot.collections.Map)5 JSONSerializer (org.apache.pivot.json.JSONSerializer)5 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)5 TableViewSortListener (org.apache.pivot.wtk.TableViewSortListener)5 Color (java.awt.Color)4 FileObject (org.apache.commons.vfs2.FileObject)4 Task (org.apache.pivot.util.concurrent.Task)4