Search in sources :

Example 1 with TextSpan

use of org.apache.pivot.wtk.text.TextSpan 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 2 with TextSpan

use of org.apache.pivot.wtk.text.TextSpan in project pivot by apache.

the class TextPaneDemo method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    System.out.println("startup(...)");
    System.out.println("\n" + "In this test application as a sample for setting the display scale on startup, use startup argument \"--scale=n\" property; \n" + "for instance, using \"--scale=2.0\" will set double scale on the whole application.\n" + "\n" + "Anyway, using Ctrl-Shift-MouseWheel will scale the display up and down as well, for the user of your application.\n");
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    window = (Window) bxmlSerializer.readObject(TextPaneDemo.class, "text_pane_demo.bxml");
    bxmlSerializer.bind(this, TextPaneDemo.class);
    window.setTitle("Apache Pivot Rich Text Editor Demo");
    // make the text on the "bold" button bold
    Font boldButtonFont = boldButton.getStyles().getFont(Style.font);
    boldButton.getStyles().put(Style.font, boldButtonFont.deriveFont(Font.BOLD));
    // make the text on the "italic" button italic
    Font italicButtonFont = italicButton.getStyles().getFont(Style.font);
    italicButton.getStyles().put(Style.font, italicButtonFont.deriveFont(Font.ITALIC));
    fontFamilyListButton.setListData(new ArrayList<>(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()));
    fontSizeListButton.setSelectedItem(fontFamilyListButton.getListData().get(0));
    fontFamilyListButton.setItemRenderer(new ListViewItemRenderer() {

        @Override
        public void render(Object item, int index, ListView listView, boolean selected, Button.State state, boolean highlighted, boolean disabled) {
            super.render(item, index, listView, selected, state, highlighted, disabled);
            if (item != null) {
                String fontFamilyName = (String) item;
                label.getStyles().put(Style.font, Font.decode(fontFamilyName + "-12"));
            }
        }
    });
    fontFamilyListButton.setDataRenderer(new ListButtonDataRenderer() {

        @Override
        public void render(Object data, Button button, boolean highlight) {
            super.render(data, button, highlight);
            if (data != null) {
                String fontFamilyName = (String) data;
                label.getStyles().put(Style.font, Font.decode(fontFamilyName + "-12"));
            }
        }
    });
    fontSizeListButton.setListData(new NumericSpinnerData(12, 30, 1));
    fontSizeListButton.setSelectedItem(12);
    openFileButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            final FileBrowserSheet fileBrowserSheet = new FileBrowserSheet();
            fileBrowserSheet.setMode(FileBrowserSheet.Mode.OPEN);
            fileBrowserSheet.open(window, new SheetCloseListener() {

                @Override
                public void sheetClosed(Sheet sheet) {
                    if (sheet.getResult()) {
                        loadedFile = fileBrowserSheet.getSelectedFile();
                        try {
                            BufferedReader reader = new BufferedReader(new FileReader(loadedFile));
                            PlainTextSerializer serializer = new PlainTextSerializer();
                            textPane.setDocument(serializer.readObject(reader));
                            reader.close();
                            window.setTitle(loadedFile.getCanonicalPath());
                        } catch (IOException ex) {
                            ex.printStackTrace();
                            Alert.alert(ex.getMessage(), window);
                        }
                    }
                }
            });
        }
    });
    saveFileButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            final FileBrowserSheet fileBrowserSheet = new FileBrowserSheet();
            if (loadedFile != null) {
                fileBrowserSheet.setSelectedFile(loadedFile);
            }
            fileBrowserSheet.setMode(FileBrowserSheet.Mode.SAVE_AS);
            fileBrowserSheet.open(window, new SheetCloseListener() {

                @Override
                public void sheetClosed(Sheet sheet) {
                    if (sheet.getResult()) {
                        File selectedFile = fileBrowserSheet.getSelectedFile();
                        try {
                            FileWriter writer = new FileWriter(selectedFile);
                            PlainTextSerializer serializer = new PlainTextSerializer();
                            serializer.writeObject(textPane.getDocument(), writer);
                            writer.close();
                            loadedFile = selectedFile;
                            window.setTitle(loadedFile.getCanonicalPath());
                        } catch (IOException ex) {
                            ex.printStackTrace();
                            Alert.alert(ex.getMessage(), window);
                        }
                    }
                }
            });
        }
    });
    boldButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    if (span.getFont() != null) {
                        Font font = span.getFont();
                        if (font.getStyle() == Font.PLAIN) {
                            font = font.deriveFont(Font.BOLD);
                        } else if (font.getStyle() == Font.BOLD) {
                            font = font.deriveFont(Font.PLAIN);
                        } else {
                            // the font is BOLD+ITALIC
                            font = font.deriveFont(Font.ITALIC);
                        }
                        span.setFont(font);
                    } else {
                        span.setFont("Arial BOLD 12");
                    }
                }
            });
            requestTextPaneFocus();
        }
    });
    italicButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    if (span.getFont() != null) {
                        Font font = span.getFont();
                        if (font.getStyle() == Font.PLAIN) {
                            font = font.deriveFont(Font.ITALIC);
                        } else if (font.getStyle() == Font.ITALIC) {
                            font = font.deriveFont(Font.PLAIN);
                        } else {
                            // the font is BOLD+ITALIC
                            font = font.deriveFont(Font.BOLD);
                        }
                        span.setFont(font);
                    } else {
                        span.setFont("Arial ITALIC 12");
                    }
                }
            });
            requestTextPaneFocus();
        }
    });
    underlineButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    span.setUnderline(!span.isUnderline());
                }
            });
            requestTextPaneFocus();
        }
    });
    strikethroughButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    span.setStrikethrough(!span.isStrikethrough());
                }
            });
            requestTextPaneFocus();
        }
    });
    foregroundColorChooserButton.getColorChooserButtonSelectionListeners().add(new ColorChooserButtonSelectionListener() {

        @Override
        public void selectedColorChanged(ColorChooserButton colorChooserButton, Color previousSelectedColor) {
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    span.setForegroundColor(foregroundColorChooserButton.getSelectedColor());
                }
            });
            requestTextPaneFocus();
        }
    });
    backgroundColorChooserButton.getColorChooserButtonSelectionListeners().add(new ColorChooserButtonSelectionListener() {

        @Override
        public void selectedColorChanged(ColorChooserButton colorChooserButton, Color previousSelectedColor) {
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    span.setBackgroundColor(backgroundColorChooserButton.getSelectedColor());
                }
            });
            requestTextPaneFocus();
        }
    });
    ListButtonSelectionListener fontButtonPressListener = new ListButtonSelectionListener() {

        @Override
        public void selectedItemChanged(ListButton listButton, Object previousSelectedItem) {
            int selectedFontSize = ((Integer) fontSizeListButton.getSelectedItem()).intValue();
            String selectedFontFamily = (String) fontFamilyListButton.getSelectedItem();
            final Font derivedFont = Font.decode(selectedFontFamily + " " + selectedFontSize);
            applyStyleToSelection(new StyleApplicator() {

                @Override
                public void apply(TextSpan span) {
                    span.setFont(derivedFont);
                }
            });
            requestTextPaneFocus();
        }
    };
    fontFamilyListButton.getListButtonSelectionListeners().add(fontButtonPressListener);
    fontSizeListButton.getListButtonSelectionListeners().add(fontButtonPressListener);
    wrapTextCheckbox.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            textPane.getStyles().put(Style.wrapText, wrapTextCheckbox.isSelected());
            requestTextPaneFocus();
        }
    });
    alignLeftButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyAlignmentStyle(HorizontalAlignment.LEFT);
            requestTextPaneFocus();
        }
    });
    alignCentreButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyAlignmentStyle(HorizontalAlignment.CENTER);
            requestTextPaneFocus();
        }
    });
    alignRightButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            applyAlignmentStyle(HorizontalAlignment.RIGHT);
            requestTextPaneFocus();
        }
    });
    String scaleProperty = properties.get("scale");
    if (scaleProperty != null && !scaleProperty.isEmpty()) {
        try {
            double scaleFactor = Double.parseDouble(scaleProperty);
            System.out.println("Got scaling factor \"" + scaleProperty + "\" from command line arguments, now applying to display");
            display.getDisplayHost().setScale(scaleFactor);
        } catch (NumberFormatException nfe) {
            System.err.println("(NumberFormatException: " + nfe.getMessage());
        }
    }
    window.open(display);
    requestTextPaneFocus();
}
Also used : ListViewItemRenderer(org.apache.pivot.wtk.content.ListViewItemRenderer) ListButtonSelectionListener(org.apache.pivot.wtk.ListButtonSelectionListener) ListButtonDataRenderer(org.apache.pivot.wtk.content.ListButtonDataRenderer) FileWriter(java.io.FileWriter) NumericSpinnerData(org.apache.pivot.wtk.content.NumericSpinnerData) Font(java.awt.Font) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) ListView(org.apache.pivot.wtk.ListView) ColorChooserButton(org.apache.pivot.wtk.ColorChooserButton) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) ListButton(org.apache.pivot.wtk.ListButton) ColorChooserButton(org.apache.pivot.wtk.ColorChooserButton) FileReader(java.io.FileReader) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) FileBrowserSheet(org.apache.pivot.wtk.FileBrowserSheet) PlainTextSerializer(org.apache.pivot.wtk.text.PlainTextSerializer) Color(java.awt.Color) SheetCloseListener(org.apache.pivot.wtk.SheetCloseListener) IOException(java.io.IOException) TextSpan(org.apache.pivot.wtk.text.TextSpan) ListButton(org.apache.pivot.wtk.ListButton) ColorChooserButtonSelectionListener(org.apache.pivot.wtk.ColorChooserButtonSelectionListener) BufferedReader(java.io.BufferedReader) Sheet(org.apache.pivot.wtk.Sheet) FileBrowserSheet(org.apache.pivot.wtk.FileBrowserSheet) File(java.io.File)

Example 3 with TextSpan

use of org.apache.pivot.wtk.text.TextSpan in project pivot by apache.

the class TextPaneDemo method applyStyleToTextNode.

private static void applyStyleToTextNode(Span selectionSpan, StyleApplicator styleApplicator, org.apache.pivot.wtk.text.TextNode textNode, int characterCount, Span textSpan) {
    if (selectionSpan.contains(textSpan)) {
        // if the text-node is contained wholly inside the selection, remove
        // the text-node, replace it with a Span, and apply the style
        Element parent = textNode.getParent();
        TextSpan newSpanNode = new TextSpan();
        newSpanNode.add(new TextNode(textNode.getText()));
        styleApplicator.apply(newSpanNode);
        int index = parent.remove(textNode);
        parent.insert(newSpanNode, index);
    } else if (selectionSpan.start <= textSpan.start) {
        // if the selection covers the first part of the text-node, split
        // off the first part of the text-node, and apply the style to it
        int intersectionLength = selectionSpan.end - textSpan.start + 1;
        String part1 = textNode.getSubstring(0, intersectionLength);
        String part2 = textNode.getSubstring(intersectionLength, characterCount);
        TextSpan newSpanNode = new TextSpan();
        newSpanNode.add(new TextNode(part1));
        styleApplicator.apply(newSpanNode);
        Element parent = textNode.getParent();
        int index = parent.remove(textNode);
        parent.insert(newSpanNode, index);
        parent.insert(new TextNode(part2), index + 1);
    } else if (selectionSpan.end >= textSpan.end) {
        // if the selection covers the last part of the text-node, split off
        // the last part of the text-node, and apply the style to it
        int intersectionStart = selectionSpan.start - textSpan.start;
        String part1 = textNode.getSubstring(0, intersectionStart);
        String part2 = textNode.getSubstring(intersectionStart, characterCount);
        TextSpan newSpanNode = new TextSpan();
        newSpanNode.add(new TextNode(part2));
        styleApplicator.apply(newSpanNode);
        Element parent = textNode.getParent();
        int index = parent.remove(textNode);
        parent.insert(new TextNode(part1), index);
        parent.insert(newSpanNode, index + 1);
    } else {
        // if the selection covers an internal part of the text-node, split the
        // text-node into 3 parts, and apply the style to the second part
        int part2Start = selectionSpan.start - textSpan.start;
        int part2End = selectionSpan.end - textSpan.start + 1;
        String part1 = textNode.getSubstring(0, part2Start);
        String part2 = textNode.getSubstring(part2Start, part2End);
        String part3 = textNode.getSubstring(part2End, characterCount);
        TextSpan newSpanNode = new TextSpan();
        newSpanNode.add(new TextNode(part2));
        styleApplicator.apply(newSpanNode);
        Element parent = textNode.getParent();
        int index = parent.remove(textNode);
        parent.insert(new TextNode(part1), index);
        parent.insert(newSpanNode, index + 1);
        parent.insert(new TextNode(part3), index + 2);
    }
}
Also used : TextSpan(org.apache.pivot.wtk.text.TextSpan) Element(org.apache.pivot.wtk.text.Element) TextNode(org.apache.pivot.wtk.text.TextNode)

Example 4 with TextSpan

use of org.apache.pivot.wtk.text.TextSpan in project pivot by apache.

the class TextPaneDemo method collectNodes.

private void collectNodes(org.apache.pivot.wtk.text.Node node, List<Node> nodeList) {
    // don't worry about the text-nodes that are children of Span nodes.
    if (node instanceof TextSpan) {
        return;
    }
    if (node instanceof org.apache.pivot.wtk.text.Element) {
        Element element = (Element) node;
        for (Node child : element) {
            nodeList.add(child);
            collectNodes(child, nodeList);
        }
    }
}
Also used : TextSpan(org.apache.pivot.wtk.text.TextSpan) Element(org.apache.pivot.wtk.text.Element) Node(org.apache.pivot.wtk.text.Node) TextNode(org.apache.pivot.wtk.text.TextNode)

Example 5 with TextSpan

use of org.apache.pivot.wtk.text.TextSpan in project pivot by apache.

the class TextPaneDemo method applyStyleToSpanNode.

private static void applyStyleToSpanNode(Span selectionSpan, StyleApplicator styleApplicator, TextSpan spanNode, int characterCount, Span textSpan) {
    if (selectionSpan.contains(textSpan)) {
        // if the span-node is contained wholly inside the
        // selection, apply the style
        styleApplicator.apply(spanNode);
    } else if (selectionSpan.start <= textSpan.start) {
        // if the selection covers the first part of the span-node, split
        // off the first part of the span-node, and apply the style to it
        int intersectionLength = selectionSpan.end - textSpan.start + 1;
        TextSpan node1 = spanNode.getRange(0, intersectionLength);
        styleApplicator.apply(node1);
        Node node2 = spanNode.getRange(intersectionLength, characterCount - intersectionLength);
        Element parent = spanNode.getParent();
        int index = parent.remove(spanNode);
        parent.insert(node1, index);
        parent.insert(node2, index + 1);
    } else if (selectionSpan.end >= textSpan.end) {
        // if the selection covers the last part of the span-node, split off
        // the last part of the span-node, and apply the style to it
        int intersectionStart = selectionSpan.start - textSpan.start;
        TextSpan part1 = spanNode.getRange(0, intersectionStart);
        TextSpan part2 = spanNode.getRange(intersectionStart, characterCount - intersectionStart);
        styleApplicator.apply(part2);
        Element parent = spanNode.getParent();
        int index = parent.remove(spanNode);
        parent.insert(part1, index);
        parent.insert(part2, index + 1);
    } else {
        // if the selection covers an internal part of the span-node, split the
        // span-node into 3 parts, and apply the style to the second part
        int part2Start = selectionSpan.start - textSpan.start;
        int part2End = selectionSpan.end - textSpan.start + 1;
        TextSpan part1 = spanNode.getRange(0, part2Start);
        TextSpan part2 = spanNode.getRange(part2Start, part2End - part2Start);
        TextSpan part3 = spanNode.getRange(part2End, characterCount - part2End);
        styleApplicator.apply(part2);
        Element parent = spanNode.getParent();
        int index = parent.remove(spanNode);
        parent.insert(part1, index);
        parent.insert(part2, index + 1);
        parent.insert(part3, index + 2);
    }
}
Also used : TextSpan(org.apache.pivot.wtk.text.TextSpan) Node(org.apache.pivot.wtk.text.Node) TextNode(org.apache.pivot.wtk.text.TextNode) Element(org.apache.pivot.wtk.text.Element)

Aggregations

TextSpan (org.apache.pivot.wtk.text.TextSpan)6 TextNode (org.apache.pivot.wtk.text.TextNode)4 Element (org.apache.pivot.wtk.text.Element)3 Node (org.apache.pivot.wtk.text.Node)3 Color (java.awt.Color)1 Font (java.awt.Font)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 ArrayList (org.apache.pivot.collections.ArrayList)1 ApplicationContext (org.apache.pivot.wtk.ApplicationContext)1 Button (org.apache.pivot.wtk.Button)1 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)1 ColorChooserButton (org.apache.pivot.wtk.ColorChooserButton)1 ColorChooserButtonSelectionListener (org.apache.pivot.wtk.ColorChooserButtonSelectionListener)1 DesktopApplicationContext (org.apache.pivot.wtk.DesktopApplicationContext)1 FileBrowserSheet (org.apache.pivot.wtk.FileBrowserSheet)1