Search in sources :

Example 61 with Element

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

the class ExtendedHTMLEditorKit method getListItemParent.

/**
     * Get the parent of the list item
     * @param eleSearch
     * @return the parent of the list item
     */
public static Element getListItemParent(Element eleSearch) {
    String listItemTag = HTML.Tag.LI.toString();
    Element workingElement = eleSearch;
    do {
        if (listItemTag.equals(workingElement.getName())) {
            return workingElement;
        }
        workingElement = workingElement.getParentElement();
    } while (!workingElement.getName().equals(HTML.Tag.HTML.toString()));
    return null;
}
Also used : Element(javax.swing.text.Element)

Example 62 with Element

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

the class ElementTreePanel method caretUpdate.

// CaretListener
/**
     * Messaged when the selection in the editor has changed. Will update
     * the selection in the tree.
     */
public void caretUpdate(CaretEvent e) {
    if (!updatingSelection) {
        int selBegin = Math.min(e.getDot(), e.getMark());
        int end = Math.max(e.getDot(), e.getMark());
        List<TreePath> paths = new ArrayList<TreePath>();
        TreeModel model = getTreeModel();
        Object root = model.getRoot();
        int rootCount = model.getChildCount(root);
        // in the selection.
        for (int counter = 0; counter < rootCount; counter++) {
            int start = selBegin;
            while (start <= end) {
                TreePath path = getPathForIndex(start, root, (Element) model.getChild(root, counter));
                Element charElement = (Element) path.getLastPathComponent();
                paths.add(path);
                if (start >= charElement.getEndOffset()) {
                    start++;
                } else {
                    start = charElement.getEndOffset();
                }
            }
        }
        // If a path was found, select it (them).
        int numPaths = paths.size();
        if (numPaths > 0) {
            TreePath[] pathArray = new TreePath[numPaths];
            paths.toArray(pathArray);
            updatingSelection = true;
            try {
                getTree().setSelectionPaths(pathArray);
                getTree().scrollPathToVisible(pathArray[0]);
            } finally {
                updatingSelection = false;
            }
        }
    }
}
Also used : TreeModel(javax.swing.tree.TreeModel) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) TreePath(javax.swing.tree.TreePath) Element(javax.swing.text.Element)

Example 63 with Element

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

the class ElementTreePanel method getPathForIndex.

/**
     * Returns a TreePath to the element at <code>position</code>.
     */
protected TreePath getPathForIndex(int position, Object root, Element rootElement) {
    TreePath path = new TreePath(root);
    Element child = rootElement.getElement(rootElement.getElementIndex(position));
    path = path.pathByAddingChild(rootElement);
    path = path.pathByAddingChild(child);
    while (!child.isLeaf()) {
        child = child.getElement(child.getElementIndex(position));
        path = path.pathByAddingChild(child);
    }
    return path;
}
Also used : TreePath(javax.swing.tree.TreePath) Element(javax.swing.text.Element)

Example 64 with Element

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

the class ElementTreePanel method valueChanged.

// TreeSelectionListener
/**
     * Called whenever the value of the selection changes.
     * @param e the event that characterizes the change.
     */
public void valueChanged(TreeSelectionEvent e) {
    if (!updatingSelection && tree.getSelectionCount() == 1) {
        TreePath selPath = tree.getSelectionPath();
        Object lastPathComponent = selPath.getLastPathComponent();
        if (!(lastPathComponent instanceof DefaultMutableTreeNode)) {
            Element selElement = (Element) lastPathComponent;
            updatingSelection = true;
            try {
                getEditor().select(selElement.getStartOffset(), selElement.getEndOffset());
            } finally {
                updatingSelection = false;
            }
        }
    }
}
Also used : TreePath(javax.swing.tree.TreePath) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Element(javax.swing.text.Element)

Example 65 with Element

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

the class NotesView method handleBackspace.

//methods dealing with Key Events
private void handleBackspace() {
    // TODO: This sucks, clean it up
    Element elem;
    int pos = editor.getCaretPosition();
    StyledDocument htmlDoc = (ExtendedHTMLDocument) editor.getStyledDocument();
    try {
        if (pos > 0) {
            if ((editor.getSelectedText()) != null) {
                ExtendedHTMLEditorKit.delete(editor);
                return;
            }
            int sOffset = htmlDoc.getParagraphElement(pos).getStartOffset();
            if (sOffset == editor.getSelectionStart()) {
                if (ExtendedHTMLEditorKit.checkParentsTag(htmlDoc.getParagraphElement(editor.getCaretPosition()), HTML.Tag.LI)) {
                    elem = ExtendedHTMLEditorKit.getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
                    boolean content = false;
                    int so = elem.getStartOffset();
                    int eo = elem.getEndOffset();
                    if ((so + 1) < eo) {
                        char[] temp = editor.getText(so, eo - so).toCharArray();
                        for (char aTemp : temp) {
                            if (!Character.isWhitespace(aTemp)) {
                                content = true;
                            }
                        }
                    }
                    if (!content) {
                        elem.getParentElement();
                        ExtendedHTMLEditorKit.removeTag(editor, elem, true);
                        editor.setCaretPosition(sOffset - 1);
                        return;
                    }
                    editor.setCaretPosition(editor.getCaretPosition() - 1);
                    editor.moveCaretPosition(editor.getCaretPosition() - 2);
                    editor.replaceSelection("");
                    return;
                }
            }
            editor.replaceSelection("");
        }
    } catch (BadLocationException ble) {
        Logging.errorPrint(ble.getMessage(), ble);
    }
}
Also used : ExtendedHTMLDocument(gmgen.gui.ExtendedHTMLDocument) Element(javax.swing.text.Element) StyledDocument(javax.swing.text.StyledDocument) Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

Element (javax.swing.text.Element)100 BadLocationException (javax.swing.text.BadLocationException)35 Point (java.awt.Point)19 AttributeSet (javax.swing.text.AttributeSet)15 Document (javax.swing.text.Document)15 HTMLDocument (javax.swing.text.html.HTMLDocument)11 FontMetrics (java.awt.FontMetrics)6 Dimension (java.awt.Dimension)5 Rectangle (java.awt.Rectangle)5 View (javax.swing.text.View)5 Font (java.awt.Font)4 Insets (java.awt.Insets)4 IOException (java.io.IOException)4 AbstractDocument (javax.swing.text.AbstractDocument)4 StyledDocument (javax.swing.text.StyledDocument)4 java.awt (java.awt)3 Objects (java.util.Objects)3 javax.swing (javax.swing)3 AbstractElement (javax.swing.text.AbstractDocument.AbstractElement)3 HTML (javax.swing.text.html.HTML)3