Code example for JTextComponent

Methods: addPropertyChangeListeneraddCaretListenergetDocument

0
 
    @SuppressWarnings("LeakingThisInConstructor") 
    public ElementTreePanel(JTextComponent editor) {
        this.editor = editor;
 
        Document document = editor.getDocument();
 
        // Create the tree. 
        treeModel = new ElementTreeModel(document);
        tree = new JTree(treeModel) {
 
            @Override 
            public String convertValueToText(Object value, boolean selected,
                    boolean expanded, boolean leaf,
                    int row, boolean hasFocus) {
                // Should only happen for the root 
                if (!(value instanceof Element)) {
                    return value.toString(); 
                } 
 
                Element e = (Element) value;
                AttributeSet as = e.getAttributes().copyAttributes();
                String asString;
 
                if (as != null) {
                    StringBuilder retBuffer = new StringBuilder("[");
                    Enumeration names = as.getAttributeNames();
 
                    while (names.hasMoreElements()) {
                        Object nextName = names.nextElement();
 
                        if (nextName != StyleConstants.ResolveAttribute) {
                            retBuffer.append(" ");
                            retBuffer.append(nextName);
                            retBuffer.append("=");
                            retBuffer.append(as.getAttribute(nextName));
                        } 
                    } 
                    retBuffer.append(" ]");
                    asString = retBuffer.toString();
                } else { 
                    asString = "[ ]";
                } 
 
                if (e.isLeaf()) {
                    return e.getName() + " [" + e.getStartOffset() + ", " + e.
                            getEndOffset() + "] Attributes: " + asString;
                } 
                return e.getName() + " [" + e.getStartOffset() + ", " + e.
                        getEndOffset() + "] Attributes: " + asString;
            } 
        }; 
        tree.addTreeSelectionListener(this);
        tree.setDragEnabled(true);
        // Don't show the root, it is fake. 
        tree.setRootVisible(false);
        // Since the display value of every node after the insertion point 
        // changes every time the text changes and we don't generate a change 
        // event for all those nodes the display value can become off. 
        // This can be seen as '...' instead of the complete string value. 
        // This is a temporary workaround, increase the needed size by 15, 
        // hoping that will be enough. 
        tree.setCellRenderer(new DefaultTreeCellRenderer() {
 
            @Override 
            public Dimension getPreferredSize() {
                Dimension retValue = super.getPreferredSize();
                if (retValue != null) {
                    retValue.width += 15;
                } 
                return retValue;
            } 
        }); 
        // become a listener on the document to update the tree. 
        document.addDocumentListener(this);
 
        // become a PropertyChangeListener to know when the Document has 
        // changed. 
        editor.addPropertyChangeListener(this);
 
        // Become a CaretListener 
        editor.addCaretListener(this);
 
        // configure the panel and frame containing it. 
        setLayout(new BorderLayout());
        add(new JScrollPane(tree), BorderLayout.CENTER);
 
Contextual code suggestions in your IDE  Get Codota for Java