Search in sources :

Example 11 with Element

use of javax.swing.text.Element in project jdk8u_jdk by JetBrains.

the class bug8048110 method createAndShowGUI.

private static void createAndShowGUI() {
    try {
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    HTMLEditorKit editorKit = new HTMLEditorKit();
    JTextPane textPane = new JTextPane();
    textPane.setContentType("text/html");
    textPane.setEditorKit(editorKit);
    textPane.setText("Initial text without table");
    JFrame frame = new JFrame("bug8048110");
    frame.getContentPane().add(textPane, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setSize(500, 200);
    frame.setVisible(true);
    textPane.setDocument(textPane.getEditorKit().createDefaultDocument());
    HTMLDocument htmlDocument = (HTMLDocument) textPane.getDocument();
    Element firstParagraph = findFirstElement(textPane.getDocument().getDefaultRootElement(), "p");
    try {
        htmlDocument.setInnerHTML(firstParagraph, htmlText);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.Element) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit)

Example 12 with Element

use of javax.swing.text.Element in project pcgen by PCGen.

the class ExtendedHTMLEditorKit method insertListElement.

/**
     * Insert a list element
     * @param pane
     * @param content
     */
public static void insertListElement(JTextPane pane, String content) {
    int pos = pane.getCaretPosition();
    ExtendedHTMLDocument htmlDoc = (ExtendedHTMLDocument) pane.getStyledDocument();
    String source = pane.getText();
    boolean hit;
    String idString;
    int counter = 0;
    do {
        hit = false;
        idString = "diesisteineidzumsuchenimsource" + counter;
        if (source.contains(idString)) {
            counter++;
            hit = true;
            if (counter > 10000) {
                return;
            }
        }
    } while (hit);
    Element element = getListItemParent(htmlDoc.getCharacterElement(pane.getCaretPosition()));
    if (element == null) {
        return;
    }
    SimpleAttributeSet sa = new SimpleAttributeSet(element.getAttributes());
    sa.addAttribute("id", idString);
    ((ExtendedHTMLDocument) pane.getStyledDocument()).replaceAttributes(element, sa, HTML.Tag.LI);
    source = pane.getText();
    StringBuilder newHtmlString = new StringBuilder();
    int[] positions = getPositions(element, source, true, idString);
    newHtmlString.append(source.substring(0, positions[3]));
    newHtmlString.append("<li>");
    newHtmlString.append(content);
    newHtmlString.append("</li>");
    newHtmlString.append(source.substring(positions[3] + 1, source.length()));
    pane.setText(newHtmlString.toString());
    pane.setCaretPosition(pos - 1);
    element = getListItemParent(htmlDoc.getCharacterElement(pane.getCaretPosition()));
    sa = new SimpleAttributeSet(element.getAttributes());
    sa = removeAttributeByKey(sa, "id");
    ((ExtendedHTMLDocument) pane.getStyledDocument()).replaceAttributes(element, sa, HTML.Tag.LI);
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) Element(javax.swing.text.Element)

Example 13 with Element

use of javax.swing.text.Element in project pcgen by PCGen.

the class NotesView method manageListElement.

private void manageListElement(ExtendedHTMLDocument htmlDoc) {
    Element h = ExtendedHTMLEditorKit.getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
    h.getParentElement();
    ExtendedHTMLEditorKit.removeTag(editor, h, true);
}
Also used : Element(javax.swing.text.Element)

Example 14 with Element

use of javax.swing.text.Element in project pcgen by PCGen.

the class NotesView method handleEnter.

private void handleEnter() {
    // TODO: this sucks.  clean it up
    Element elem;
    int pos = editor.getCaretPosition();
    ExtendedHTMLDocument htmlDoc = (ExtendedHTMLDocument) editor.getStyledDocument();
    try {
        if (ExtendedHTMLEditorKit.checkParentsTag(htmlDoc.getParagraphElement(editor.getCaretPosition()), HTML.Tag.UL) || ExtendedHTMLEditorKit.checkParentsTag(htmlDoc.getParagraphElement(editor.getCaretPosition()), HTML.Tag.OL)) {
            elem = ExtendedHTMLEditorKit.getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
            int so = elem.getStartOffset();
            int eo = elem.getEndOffset();
            char[] temp = editor.getText(so, eo - so).toCharArray();
            boolean content = false;
            for (char aTemp : temp) {
                if (!Character.isWhitespace(aTemp)) {
                    content = true;
                }
            }
            int repos = -1;
            if (content) {
                int end = -1;
                int j = temp.length;
                do {
                    j--;
                    if (Character.isLetterOrDigit(temp[j])) {
                        end = j;
                    }
                } while ((end == -1) && (j >= 0));
                j = end;
                do {
                    j++;
                    if (!Character.isSpaceChar(temp[j])) {
                        repos = j - end - 1;
                    }
                } while ((repos == -1) && (j < temp.length));
                if (repos == -1) {
                    repos = 0;
                }
            }
            if ((elem.getStartOffset() == elem.getEndOffset()) || !content) {
                manageListElement(htmlDoc);
            } else {
                if ((editor.getCaretPosition() + 1) == elem.getEndOffset()) {
                    ExtendedHTMLEditorKit.insertListElement(editor, "");
                    editor.setCaretPosition(pos - repos);
                } else {
                    int caret = editor.getCaretPosition();
                    String tempString = editor.getText(caret, eo - caret);
                    editor.select(caret, eo - 1);
                    editor.replaceSelection("");
                    ExtendedHTMLEditorKit.insertListElement(editor, tempString);
                    Element newLi = ExtendedHTMLEditorKit.getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
                    editor.setCaretPosition(newLi.getEndOffset());
                }
            }
        }
    } catch (BadLocationException ble) {
        Logging.errorPrint(ble.getMessage(), ble);
    }
}
Also used : ExtendedHTMLDocument(gmgen.gui.ExtendedHTMLDocument) Element(javax.swing.text.Element) Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException)

Example 15 with Element

use of javax.swing.text.Element in project enclojure by EricThorsen.

the class ClojureCodeCompletion_Provider method getRowFirstNonWhite.

static int getRowFirstNonWhite(StyledDocument doc, int offset) throws BadLocationException {
    Element lineElement = doc.getParagraphElement(offset);
    int start = lineElement.getStartOffset();
    while (start + 1 < lineElement.getEndOffset()) {
        try {
            if (doc.getText(start, 1).charAt(0) != ' ') {
                break;
            }
        } catch (BadLocationException ex) {
            throw (BadLocationException) new BadLocationException("calling getText(" + start + ", " + (start + 1) + ") on doc of length: " + doc.getLength(), start).initCause(ex);
        }
        start++;
    }
    return start;
}
Also used : Element(javax.swing.text.Element) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

Element (javax.swing.text.Element)40 BadLocationException (javax.swing.text.BadLocationException)15 Point (java.awt.Point)13 AttributeSet (javax.swing.text.AttributeSet)6 HTMLDocument (javax.swing.text.html.HTMLDocument)6 FontMetrics (java.awt.FontMetrics)4 Dimension (java.awt.Dimension)3 IOException (java.io.IOException)3 Document (javax.swing.text.Document)3 TreePath (javax.swing.tree.TreePath)3 ExtendedHTMLDocument (gmgen.gui.ExtendedHTMLDocument)2 Font (java.awt.Font)2 Insets (java.awt.Insets)2 Rectangle (java.awt.Rectangle)2 Accessible (javax.accessibility.Accessible)2 JTextComponent (javax.swing.text.JTextComponent)2 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)2 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)2 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1