Search in sources :

Example 1 with SortDirection

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

the class TerraTableViewHeaderSkin method paint.

@Override
public void paint(Graphics2D graphics) {
    int width = getWidth();
    int height = getHeight();
    TableViewHeader tableViewHeader = (TableViewHeader) getComponent();
    Color backgroundColorLocal = null;
    Color bevelColorLocal = null;
    Color borderColorLocal = null;
    if (tableViewHeader.isEnabled()) {
        backgroundColorLocal = this.backgroundColor;
        bevelColorLocal = this.bevelColor;
        borderColorLocal = this.borderColor;
    } else {
        backgroundColorLocal = disabledBackgroundColor;
        bevelColorLocal = disabledBevelColor;
        borderColorLocal = disabledBorderColor;
    }
    // Paint the background
    if (!themeIsFlat()) {
        graphics.setPaint(new GradientPaint(width / 2f, 0, bevelColorLocal, width / 2f, height, backgroundColorLocal));
    } else {
        graphics.setPaint(backgroundColorLocal);
    }
    graphics.fillRect(0, 0, width, height);
    // Paint the border
    if (!themeIsFlat()) {
        graphics.setPaint(borderColorLocal);
        graphics.setStroke(new BasicStroke(1));
        graphics.draw(new Line2D.Double(0.5, height - 0.5, width - 0.5, height - 0.5));
    }
    // Paint the content
    TableView tableView = tableViewHeader.getTableView();
    if (tableView != null) {
        TableView.ColumnSequence columns = tableView.getColumns();
        int headerX = 0;
        for (int columnIndex = 0, columnCount = columns.getLength(); columnIndex < columnCount; columnIndex++) {
            TableView.Column column = columns.get(columnIndex);
            int headerWidth = headerWidths.get(columnIndex).intValue();
            // Paint the pressed bevel
            if (columnIndex == pressedHeaderIndex) {
                graphics.setPaint(new GradientPaint(width / 2f, 0, pressedBevelColor, width / 2f, height, backgroundColorLocal));
                graphics.fillRect(headerX, 0, headerWidth, height - 1);
            }
            // Paint the header data
            Object headerData = column.getHeaderData();
            TableView.HeaderDataRenderer headerDataRenderer = column.getHeaderDataRenderer();
            headerDataRenderer.render(headerData, columnIndex, tableViewHeader, column.getName(), false);
            headerDataRenderer.setSize(headerWidth, height - 1);
            Graphics2D rendererGraphics = (Graphics2D) graphics.create(headerX, 0, headerWidth, height - 1);
            headerDataRenderer.paint(rendererGraphics);
            rendererGraphics.dispose();
            // Draw the sort image
            Image sortImage = null;
            String columnName = column.getName();
            SortDirection sortDirection = tableView.getSort().get(columnName);
            if (sortDirection != null) {
                switch(sortDirection) {
                    case ASCENDING:
                        {
                            sortImage = sortAscendingImage;
                            break;
                        }
                    case DESCENDING:
                        {
                            sortImage = sortDescendingImage;
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }
            if (sortImage != null) {
                int sortImageMargin = sortImage.getWidth() + SORT_INDICATOR_PADDING * 2;
                if (headerWidth >= headerDataRenderer.getPreferredWidth(-1) + sortImageMargin) {
                    Graphics2D sortImageGraphics = (Graphics2D) graphics.create();
                    sortImageGraphics.translate(headerX + headerWidth - sortImageMargin, (height - sortImage.getHeight()) / 2);
                    sortImage.paint(sortImageGraphics);
                    sortImageGraphics.dispose();
                }
            }
            // Draw the divider
            headerX += headerWidth;
            if (columnIndex < columnCount - 1 || includeTrailingVerticalGridLine) {
                if (!themeIsFlat()) {
                    graphics.setPaint(borderColorLocal);
                    graphics.draw(new Line2D.Double(headerX + 0.5, 0.5, headerX + 0.5, height - 0.5));
                }
            }
            headerX++;
        }
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Color(java.awt.Color) GradientPaint(java.awt.GradientPaint) Image(org.apache.pivot.wtk.media.Image) Line2D(java.awt.geom.Line2D) GradientPaint(java.awt.GradientPaint) Graphics2D(java.awt.Graphics2D) TableViewHeader(org.apache.pivot.wtk.TableViewHeader) SortDirection(org.apache.pivot.wtk.SortDirection) TableView(org.apache.pivot.wtk.TableView)

Example 2 with SortDirection

use of org.apache.pivot.wtk.SortDirection 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 3 with SortDirection

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

the class TableViewRowComparator method compare.

/**
 * Compares two rows in a table view. If the column values implement
 * {@link Comparable}, the {@link Comparable#compareTo(Object)} method will
 * be used to compare the values. Otherwise, the values will be compared as
 * strings using {@link Object#toString()}. If either value is
 * <tt>null</tt>, it will be considered as less than the other value. If
 * both values are <tt>null</tt>, they will be considered equal.
 */
@Override
@SuppressWarnings("unchecked")
public int compare(Object o1, Object o2) {
    int result;
    TableView.SortDictionary sort = tableView.getSort();
    if (sort.getLength() > 0) {
        Dictionary<String, ?> row1;
        if (o1 instanceof Dictionary<?, ?>) {
            row1 = (Dictionary<String, ?>) o1;
        } else {
            row1 = new BeanAdapter(o1);
        }
        Dictionary<String, ?> row2;
        if (o2 instanceof Dictionary<?, ?>) {
            row2 = (Dictionary<String, ?>) o2;
        } else {
            row2 = new BeanAdapter(o2);
        }
        result = 0;
        int n = sort.getLength();
        int i = 0;
        while (i < n && result == 0) {
            Dictionary.Pair<String, SortDirection> pair = sort.get(i);
            String columnName = pair.key;
            SortDirection sortDirection = sort.get(columnName);
            Object value1 = row1.get(columnName);
            Object value2 = row2.get(columnName);
            if (value1 == null && value2 == null) {
                result = 0;
            } else if (value1 == null) {
                result = -1;
            } else if (value2 == null) {
                result = 1;
            } else {
                if (value1 instanceof Comparable<?>) {
                    result = ((Comparable<Object>) value1).compareTo(value2);
                } else {
                    String s1 = value1.toString();
                    String s2 = value2.toString();
                    result = s1.compareTo(s2);
                }
            }
            result *= (sortDirection == SortDirection.ASCENDING ? 1 : -1);
            i++;
        }
    } else {
        result = 0;
    }
    return result;
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) SortDirection(org.apache.pivot.wtk.SortDirection) BeanAdapter(org.apache.pivot.beans.BeanAdapter) TableView(org.apache.pivot.wtk.TableView)

Example 4 with SortDirection

use of org.apache.pivot.wtk.SortDirection 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)

Example 5 with SortDirection

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

the class TerraVFSBrowserSkin method refreshFileList.

private void refreshFileList() {
    // Cancel any outstanding task
    if (refreshFileListTask != null) {
        refreshFileListTask.abort();
        if (indicator != null) {
            indicator.setActive(false);
            fileStackPane.remove(fileStackPane.getLength() - 1, 1);
        }
    }
    if (indicator == null) {
        indicator = new ActivityIndicator();
        activityGrid = new GridPane(5);
        GridPane.Row row1 = new GridPane.Row(activityGrid);
        GridPane.Row row2 = new GridPane.Row(activityGrid);
        GridPane.Row row3 = new GridPane.Row(activityGrid);
        for (int i = 0; i < 5; i++) {
            row1.add(new GridPane.Filler());
            if (i == 2) {
                row2.add(indicator);
            } else {
                row2.add(new GridPane.Filler());
            }
            row3.add(new GridPane.Filler());
        }
    }
    fileStackPane.add(activityGrid);
    indicator.setActive(true);
    fileTableView.setTableData(new ArrayList<FileObject>());
    String text = searchTextInput.getText().trim();
    Filter<FileObject> disabledFileFilter = hideDisabledFiles ? ((VFSBrowser) getComponent()).getDisabledFileFilter() : null;
    Filter<FileObject> includeFileFilter = text.length() != 0 ? new IncludeFileFilter(text) : null;
    TableView.SortDictionary sort = fileTableView.getSort();
    final FileComparator fileComparator;
    if (sort.isEmpty()) {
        fileComparator = null;
    } else {
        Dictionary.Pair<String, SortDirection> pair = fileTableView.getSort().get(0);
        fileComparator = getFileComparator(pair.key, pair.value);
    }
    refreshFileListTask = new RefreshFileListTask(includeFileFilter, disabledFileFilter, fileComparator);
    refreshFileListTask.execute(new TaskAdapter<>(new TaskListener<ArrayList<FileObject>>() {

        @Override
        public void taskExecuted(Task<ArrayList<FileObject>> task) {
            if (task == refreshFileListTask) {
                indicator.setActive(false);
                fileStackPane.remove(fileStackPane.getLength() - 1, 1);
                ArrayList<FileObject> fileList = task.getResult();
                fileTableView.setTableData(fileList);
                updateSelectedFiles((VFSBrowser) getComponent());
                refreshFileListTask = null;
            }
        }

        @Override
        public void executeFailed(Task<ArrayList<FileObject>> task) {
            if (task == refreshFileListTask) {
                indicator.setActive(false);
                fileStackPane.remove(fileStackPane.getLength() - 1, 1);
                refreshFileListTask = null;
            }
        }
    }));
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) Task(org.apache.pivot.util.concurrent.Task) GridPane(org.apache.pivot.wtk.GridPane) ActivityIndicator(org.apache.pivot.wtk.ActivityIndicator) Point(org.apache.pivot.wtk.Point) SortDirection(org.apache.pivot.wtk.SortDirection) TaskListener(org.apache.pivot.util.concurrent.TaskListener) FileObject(org.apache.commons.vfs2.FileObject) TableView(org.apache.pivot.wtk.TableView)

Aggregations

SortDirection (org.apache.pivot.wtk.SortDirection)5 TableView (org.apache.pivot.wtk.TableView)5 Color (java.awt.Color)2 GradientPaint (java.awt.GradientPaint)2 Graphics2D (java.awt.Graphics2D)2 Dictionary (org.apache.pivot.collections.Dictionary)2 TableViewHeader (org.apache.pivot.wtk.TableViewHeader)2 BasicStroke (java.awt.BasicStroke)1 Rectangle (java.awt.Rectangle)1 Line2D (java.awt.geom.Line2D)1 FileObject (org.apache.commons.vfs2.FileObject)1 BeanAdapter (org.apache.pivot.beans.BeanAdapter)1 ArrayList (org.apache.pivot.collections.ArrayList)1 List (org.apache.pivot.collections.List)1 Task (org.apache.pivot.util.concurrent.Task)1 TaskListener (org.apache.pivot.util.concurrent.TaskListener)1 ActivityIndicator (org.apache.pivot.wtk.ActivityIndicator)1 GridPane (org.apache.pivot.wtk.GridPane)1 Point (org.apache.pivot.wtk.Point)1 SortMode (org.apache.pivot.wtk.TableViewHeader.SortMode)1