Search in sources :

Example 6 with Span

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

the class TextPaneDemo method applyStyle.

private void applyStyle(Document document, Span selectionSpan, StyleApplicator styleApplicator) {
    // I can't apply the styles while iterating over the tree, because I
    // need to update the tree.
    // So first collect a list of all the nodes in the tree.
    List<Node> nodeList = new ArrayList<>();
    collectNodes(document, nodeList);
    final int selectionStart = textPane.getSelectionStart();
    final int selectionLength = textPane.getSelectionLength();
    for (Node node : nodeList) {
        if (node instanceof TextSpan) {
            TextSpan span = (TextSpan) node;
            int documentOffset = node.getDocumentOffset();
            int characterCount = node.getCharacterCount();
            Span textSpan = new Span(documentOffset, documentOffset + characterCount - 1);
            if (selectionSpan.intersects(textSpan)) {
                applyStyleToSpanNode(selectionSpan, styleApplicator, span, characterCount, textSpan);
            }
        }
        if (node instanceof org.apache.pivot.wtk.text.TextNode) {
            org.apache.pivot.wtk.text.TextNode textNode = (org.apache.pivot.wtk.text.TextNode) node;
            int documentOffset = node.getDocumentOffset();
            int characterCount = node.getCharacterCount();
            Span textSpan = new Span(documentOffset, documentOffset + characterCount - 1);
            if (selectionSpan.intersects(textSpan)) {
                applyStyleToTextNode(selectionSpan, styleApplicator, textNode, characterCount, textSpan);
            }
        }
    }
    // maintain the selected range
    textPane.setSelection(selectionStart, selectionLength);
}
Also used : TextNode(org.apache.pivot.wtk.text.TextNode) Node(org.apache.pivot.wtk.text.Node) TextNode(org.apache.pivot.wtk.text.TextNode) ArrayList(org.apache.pivot.collections.ArrayList) TextNode(org.apache.pivot.wtk.text.TextNode) Span(org.apache.pivot.wtk.Span) TextSpan(org.apache.pivot.wtk.text.TextSpan) TextSpan(org.apache.pivot.wtk.text.TextSpan) DesktopApplicationContext(org.apache.pivot.wtk.DesktopApplicationContext) ApplicationContext(org.apache.pivot.wtk.ApplicationContext)

Example 7 with Span

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

the class TextPaneDemo method applyStyleToSelection.

private void applyStyleToSelection(StyleApplicator styleApplicator) {
    Span span = textPane.getSelection();
    if (span == null) {
        return;
    }
    applyStyle(textPane.getDocument(), span, styleApplicator);
}
Also used : Span(org.apache.pivot.wtk.Span) TextSpan(org.apache.pivot.wtk.text.TextSpan)

Example 8 with Span

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

the class FileDropTargetDemo method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    fileList = new FileList();
    fileTableView.setTableData(fileList);
    fileList.getListListeners().add(new ListListener<File>() {

        @Override
        public void itemInserted(List<File> list, int index) {
            uploadButton.setEnabled(list.getLength() > 0);
        }

        @Override
        public void itemsRemoved(List<File> list, int index, Sequence<File> files) {
            uploadButton.setEnabled(list.getLength() > 0);
            if (fileTableView.isFocused() && index < list.getLength()) {
                fileTableView.setSelectedIndex(index);
            }
        }
    });
    fileTableView.getComponentKeyListeners().add(new ComponentKeyListener() {

        @Override
        public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
            if (keyCode == Keyboard.KeyCode.DELETE || keyCode == Keyboard.KeyCode.BACKSPACE) {
                Sequence<Span> selectedRanges = fileTableView.getSelectedRanges();
                for (int i = selectedRanges.getLength() - 1; i >= 0; i--) {
                    Span range = selectedRanges.get(i);
                    int index = range.start;
                    int count = range.end - index + 1;
                    fileList.remove(index, count);
                }
            }
            return false;
        }
    });
    fileTableView.setDropTarget(new DropTarget() {

        @Override
        public DropAction dragEnter(Component component, Manifest dragContent, int supportedDropActions, DropAction userDropAction) {
            DropAction dropAction = null;
            if (dragContent.containsFileList() && DropAction.COPY.isSelected(supportedDropActions)) {
                dropAction = DropAction.COPY;
            }
            return dropAction;
        }

        @Override
        public void dragExit(Component component) {
        // empty block
        }

        @Override
        public DropAction dragMove(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
            return (dragContent.containsFileList() ? DropAction.COPY : null);
        }

        @Override
        public DropAction userDropActionChange(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
            return (dragContent.containsFileList() ? DropAction.COPY : null);
        }

        @Override
        public DropAction drop(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
            DropAction dropAction = null;
            if (dragContent.containsFileList()) {
                try {
                    FileList tableData = (FileList) fileTableView.getTableData();
                    FileList fileListLocal = dragContent.getFileList();
                    for (File file : fileListLocal) {
                        if (file.isDirectory()) {
                        // TODO Expand recursively
                        }
                        tableData.add(file);
                    }
                    dropAction = DropAction.COPY;
                } catch (IOException exception) {
                    System.err.println(exception);
                }
            }
            dragExit(component);
            return dropAction;
        }
    });
    uploadButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            Prompt.prompt(MessageType.INFO, "Pretending to upload...", FileDropTargetDemo.this);
        }
    });
}
Also used : FileList(org.apache.pivot.io.FileList) Keyboard(org.apache.pivot.wtk.Keyboard) DropAction(org.apache.pivot.wtk.DropAction) Sequence(org.apache.pivot.collections.Sequence) IOException(java.io.IOException) Manifest(org.apache.pivot.wtk.Manifest) Span(org.apache.pivot.wtk.Span) ComponentKeyListener(org.apache.pivot.wtk.ComponentKeyListener) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) DropTarget(org.apache.pivot.wtk.DropTarget) Component(org.apache.pivot.wtk.Component) File(java.io.File)

Example 9 with Span

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

the class CheckedListViewTest method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    BXMLSerializer serializer = new BXMLSerializer();
    mainWindow = (Window) serializer.readObject(CheckedListViewTest.class, "checked_list_view_test.bxml");
    serializer.bind(this);
    allowMixedStateCheckbox.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            listView.setAllowTriStateCheckmarks(button.isSelected());
            // Not sure why, but changing this setting clears all the checks but doesn't
            // trigger the item state listener (it's documented, but ...)
            selectedItemsLabel.setText("");
        }
    });
    showMixedAsSelectedCheckbox.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            listView.setCheckmarksMixedAsChecked(button.isSelected());
        }
    });
    listView.getComponentKeyListeners().add(new ComponentKeyListener() {

        @Override
        public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
            if (keyCode == Keyboard.KeyCode.DELETE) {
                @SuppressWarnings("unchecked") List<Object> listData = (List<Object>) listView.getListData();
                Sequence<Span> selectedRanges = listView.getSelectedRanges();
                for (int i = selectedRanges.getLength() - 1; i >= 0; i--) {
                    Span selectedRange = selectedRanges.get(i);
                    listData.remove(selectedRange.start, selectedRange.end - selectedRange.start + 1);
                }
            }
            return false;
        }
    });
    listView.getListViewItemStateListeners().add(new ListViewItemStateListener() {

        private void displayCheckedItems(ListView listView) {
            List<?> listData = listView.getListData();
            StringBuffer buf = new StringBuffer();
            for (Integer i : listView.getCheckedIndexes()) {
                if (buf.length() > 0) {
                    buf.append(",");
                }
                Object item = listData.get(i);
                buf.append(item.toString());
            }
            selectedItemsLabel.setText(buf.toString());
        }

        @Override
        public void itemCheckedChanged(ListView listView, int index) {
            displayCheckedItems(listView);
        }

        @Override
        public void itemCheckedStateChanged(ListView listView, int index) {
            displayCheckedItems(listView);
        }
    });
    listView.setItemChecked(0, true);
    listView.setItemChecked(2, true);
    mainWindow.open(display);
}
Also used : ListViewItemStateListener(org.apache.pivot.wtk.ListViewItemStateListener) Keyboard(org.apache.pivot.wtk.Keyboard) Sequence(org.apache.pivot.collections.Sequence) Span(org.apache.pivot.wtk.Span) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) ComponentKeyListener(org.apache.pivot.wtk.ComponentKeyListener) ListView(org.apache.pivot.wtk.ListView) Button(org.apache.pivot.wtk.Button) List(org.apache.pivot.collections.List) Component(org.apache.pivot.wtk.Component) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 10 with Span

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

Aggregations

Span (org.apache.pivot.wtk.Span)25 ArrayList (org.apache.pivot.collections.ArrayList)11 List (org.apache.pivot.collections.List)10 Button (org.apache.pivot.wtk.Button)7 Keyboard (org.apache.pivot.wtk.Keyboard)7 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)6 Component (org.apache.pivot.wtk.Component)6 ListView (org.apache.pivot.wtk.ListView)6 TableView (org.apache.pivot.wtk.TableView)6 Sequence (org.apache.pivot.collections.Sequence)5 Bounds (org.apache.pivot.wtk.Bounds)5 ComponentKeyListener (org.apache.pivot.wtk.ComponentKeyListener)5 IOException (java.io.IOException)4 PushButton (org.apache.pivot.wtk.PushButton)4 TableViewSelectionListener (org.apache.pivot.wtk.TableViewSelectionListener)4 TableViewSortListener (org.apache.pivot.wtk.TableViewSortListener)4 TextInput (org.apache.pivot.wtk.TextInput)4 TextSpan (org.apache.pivot.wtk.text.TextSpan)4 File (java.io.File)3 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)3