Search in sources :

Example 16 with TreeModel

use of javax.swing.tree.TreeModel 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 17 with TreeModel

use of javax.swing.tree.TreeModel in project jdk8u_jdk by JetBrains.

the class ElementTreePanel method updateTree.

/**
     * Updates the tree based on the event type. This will invoke either
     * updateTree with the root element, or handleChange.
     */
protected void updateTree(DocumentEvent event) {
    updatingSelection = true;
    try {
        TreeModel model = getTreeModel();
        Object root = model.getRoot();
        for (int counter = model.getChildCount(root) - 1; counter >= 0; counter--) {
            updateTree(event, (Element) model.getChild(root, counter));
        }
    } finally {
        updatingSelection = false;
    }
}
Also used : TreeModel(javax.swing.tree.TreeModel) DefaultTreeModel(javax.swing.tree.DefaultTreeModel)

Aggregations

TreeModel (javax.swing.tree.TreeModel)17 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)8 TreePath (javax.swing.tree.TreePath)4 Test (org.junit.Test)4 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)3 NavigatablePsiElement (com.intellij.psi.NavigatablePsiElement)1 PsiElement (com.intellij.psi.PsiElement)1 Insets (java.awt.Insets)1 Rectangle (java.awt.Rectangle)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 HashSet (java.util.HashSet)1 JLabel (javax.swing.JLabel)1 TreeModelEvent (javax.swing.event.TreeModelEvent)1 TreeModelListener (javax.swing.event.TreeModelListener)1 TreeSelectionEvent (javax.swing.event.TreeSelectionEvent)1 TreeSelectionListener (javax.swing.event.TreeSelectionListener)1 Element (javax.swing.text.Element)1 DefaultTreeCellRenderer (javax.swing.tree.DefaultTreeCellRenderer)1 TreeCellRenderer (javax.swing.tree.TreeCellRenderer)1