Java Code Examples for javax.swing.text.AttributeSet#getAttributeNames()

The following examples show how to use javax.swing.text.AttributeSet#getAttributeNames() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Highlighting.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Creates a new instance of Highlighting */
public Highlighting(Document doc) {
    AttributeSet firstLineFontColor = MimeLookup.getLookup(MimePath.get("text/x-java")).lookup(FontColorSettings.class).getTokenFontColors("javadoc-first-sentence"); //NOI18N
    AttributeSet commentFontColor = MimeLookup.getLookup(MimePath.get("text/x-java")).lookup(FontColorSettings.class).getTokenFontColors("comment"); //NOI18N
    if(firstLineFontColor != null && commentFontColor != null) {
        Collection<Object> attrs = new LinkedList<>();
        for (Enumeration<?> e = firstLineFontColor.getAttributeNames(); e.hasMoreElements(); ) {
            Object key = e.nextElement();
            Object value = firstLineFontColor.getAttribute(key);

            if (!commentFontColor.containsAttribute(key, value)) {
                attrs.add(key);
                attrs.add(value);
            }
        }
        fontColor = AttributesUtilities.createImmutable(attrs.toArray());
    } else {
        fontColor = AttributesUtilities.createImmutable();
        LOG.warning("FontColorSettings for javadoc-first-sentence or comment are not available."); //NOI18N
    }
    this.document = doc;
    hierarchy = TokenHierarchy.get(document);
    if (hierarchy != null) {
        hierarchy.addTokenHierarchyListener(WeakListeners.create(TokenHierarchyListener.class, this, hierarchy));
    }
}
 
Example 2
Source File: CSS.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private void translateEmbeddedAttributes(AttributeSet htmlAttrSet,
                                         MutableAttributeSet cssAttrSet) {
    Enumeration<?> keys = htmlAttrSet.getAttributeNames();
    if (htmlAttrSet.getAttribute(StyleConstants.NameAttribute) ==
        HTML.Tag.HR) {
        // HR needs special handling due to us treating it as a leaf.
        translateAttributes(HTML.Tag.HR, htmlAttrSet, cssAttrSet);
    }
    while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        if (key instanceof HTML.Tag) {
            HTML.Tag tag = (HTML.Tag)key;
            Object o = htmlAttrSet.getAttribute(tag);
            if (o != null && o instanceof AttributeSet) {
                translateAttributes(tag, (AttributeSet)o, cssAttrSet);
            }
        } else if (key instanceof CSS.Attribute) {
            cssAttrSet.addAttribute(key, htmlAttrSet.getAttribute(key));
        }
    }
}
 
Example 3
Source File: AttributesUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public boolean containsAttributes(AttributeSet attributes) {
    for(Enumeration<?> keys = attributes.getAttributeNames(); keys.hasMoreElements(); ) {
        Object key = keys.nextElement();
        Object value = attributes.getAttribute(key);
        
        if (!containsAttribute(key, value)) {
            return false;
        }
    }
    
    return true;
}
 
Example 4
Source File: HtmlPane.java    From Girinoscope with Apache License 2.0 5 votes vote down vote up
private static Object getAttribute(AttributeSet attributes, String name) {
    for (Enumeration<?> enumeration = attributes.getAttributeNames(); enumeration.hasMoreElements();) {
        Object nameKey = enumeration.nextElement();
        if (name.equals(nameKey.toString())) {
            return attributes.getAttribute(nameKey);
        }
    }
    return null;
}
 
Example 5
Source File: AttributesUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public synchronized boolean containsAttributes(AttributeSet attributes) {
    for(Enumeration<?> names = attributes.getAttributeNames(); names.hasMoreElements(); ) {
        Object name = names.nextElement();
        Object value = attributes.getAttribute(name);

        if (!containsAttribute(name, value)) {
            return false;
        }
    }

    return true;
}
 
Example 6
Source File: AttributesUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean containsAttributes(AttributeSet attributes) {
    for(Enumeration<?> keys = attributes.getAttributeNames(); keys.hasMoreElements(); ) {
        Object key = keys.nextElement();
        Object value = attributes.getAttribute(key);

        if (!containsAttribute(key, value)) {
            return false;
        }
    }

    return true;
}
 
Example 7
Source File: CompositeFCS.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void dumpAttribs(AttributeSet attribs, String name, boolean tokenColoring) {
//        if (!allFcsi[0].getMimePath().getPath().equals("text/x-java")) { //NOI18N
//            return;
//        }

        StringBuilder sb = new StringBuilder();
        sb.append("Attribs for base mime path '"); //NOI18N
        sb.append(allFcsi[0].getMimePath().getPath());
        sb.append("' and "); //NOI18N
        if (tokenColoring) {
            sb.append("token '"); //NOI18N
        } else {
            sb.append("highlight '"); //NOI18N
        }
        sb.append(name);
        sb.append("' = {"); //NOI18N

        Enumeration<?> keys = attribs.getAttributeNames();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            Object value = attribs.getAttribute(key);

            sb.append("'").append(key).append("' = '").append(value).append("'"); //NOI18N
            if (keys.hasMoreElements()) {
                sb.append(", "); //NOI18N
            }
        }

        sb.append("} CompoundFCS.this = "); //NOI18N
        sb.append(this.toString());

        System.out.println(sb.toString());
    }
 
Example 8
Source File: Pre90403Phase1CompatibilityTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Map<String, Map<String, String>> normalize(Collection<AttributeSet> colorings) {
    Map<String, Map<String, String>> norm = new TreeMap<String, Map<String, String>>();
    
    for(AttributeSet as : colorings) {
        String name = (String) as.getAttribute(StyleConstants.NameAttribute);
        
        assertNotNull("NameAttribute should not be null", name);
        
        name = "'" + name + "'";
        assertFalse("Duplicate AttributeSet with name " + name, norm.containsKey(name));
        
        Map<String, String> attribs = new TreeMap<String, String>();
        norm.put(name, attribs);
        
        Enumeration<? extends Object> names = as.getAttributeNames();
        while(names.hasMoreElements()) {
            Object attrName = names.nextElement();
            Object attrValue = as.getAttribute(attrName);
            String normalizedName = attrName == null ? "'null'" : "'" + attrName.toString() + "'";
            String normalizedValue = attrValue == null ? "'null'" : "'" + attrValue.toString() + "'";
            
            assertFalse("Duplicate attribute '" + normalizedName + "'", attribs.containsKey(normalizedName));
            attribs.put(normalizedName, normalizedValue);
        }
    }
    
    return norm;
}
 
Example 9
Source File: MockAttributeSet.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void addAttributes(AttributeSet attr)
{
    Enumeration as = attr.getAttributeNames();
    while(as.hasMoreElements()) {
        Object el = as.nextElement();
        backing.put(el, attr.getAttribute(el));
    }
}
 
Example 10
Source File: MockAttributeSet.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public void addAttributes(AttributeSet attr)
{
    Enumeration<?> as = attr.getAttributeNames();
    while(as.hasMoreElements()) {
        Object el = as.nextElement();
        backing.put(el, attr.getAttribute(el));
    }
}
 
Example 11
Source File: MockAttributeSet.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void addAttributes(AttributeSet attr)
{
    Enumeration as = attr.getAttributeNames();
    while(as.hasMoreElements()) {
        Object el = as.nextElement();
        backing.put(el, attr.getAttribute(el));
    }
}
 
Example 12
Source File: AttributesUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public boolean containsAttributes(AttributeSet attributes) {
    for(Enumeration<?> keys = attributes.getAttributeNames(); keys.hasMoreElements(); ) {
        Object key = keys.nextElement();
        Object value = attributes.getAttribute(key);

        if (!containsAttribute(key, value)) {
            return false;
        }
    }

    return true;
}
 
Example 13
Source File: FSwingHtml.java    From pra with MIT License 5 votes vote down vote up
public static String getAttribute(Element e, Attribute ab){
	AttributeSet mAb=e.getAttributes();
   for (Enumeration it = mAb.getAttributeNames() ; it.hasMoreElements() ;) {
   	Object s= it.nextElement();
     if (s.equals(ab))
     	return (String) mAb.getAttribute(ab);
   }
	return null;
}
 
Example 14
Source File: MockAttributeSet.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void addAttributes(AttributeSet attr)
{
    Enumeration as = attr.getAttributeNames();
    while(as.hasMoreElements()) {
        Object el = as.nextElement();
        backing.put(el, attr.getAttribute(el));
    }
}
 
Example 15
Source File: RemoveSurroundingTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void dumpAndSelect(int row, int col, int select) {
    oper.setCaretPosition(row, col);
    oper.pressKey(KeyEvent.VK_BACK_SPACE, KeyEvent.ALT_DOWN_MASK);
    JDialogOperator jdo = new JDialogOperator(MainWindowOperator.getDefault());
    JListOperator jlo = new JListOperator(jdo);
    ListModel model = jlo.getModel();
    int i;
    for (i = 0; i < model.getSize(); i++) {
        Object item = model.getElementAt(i);
        if(item instanceof CodeDeleter) {
            CodeDeleter codeDeleter = (CodeDeleter) item;
            ref(codeDeleter.getDisplayName());                
            HighlightsSequence highlights = codeDeleter.getHighlight().getHighlights(0, oper.getText().length());
            while(highlights.moveNext()) {
                ref(highlights.getStartOffset()+" "+highlights.getEndOffset());
                AttributeSet attributes = highlights.getAttributes();
                Enumeration<?> attributeNames = attributes.getAttributeNames();
                while(attributeNames.hasMoreElements()) {
                    Object nextElement = attributeNames.nextElement();
                    ref(nextElement+" "+attributes.getAttribute(nextElement));
                }
            }
        }            
    }
    if(select>-1) {
        jlo.selectItem(select);
        ref(oper.getText());
    }        
}
 
Example 16
Source File: ElementTreePanel.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@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);

    // Add a label above tree to describe what is being shown
    JLabel label = new JLabel("Elements that make up the current document",
            SwingConstants.CENTER);

    label.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label, BorderLayout.NORTH);

    setPreferredSize(new Dimension(400, 400));
}
 
Example 17
Source File: CSS.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private void translateAttributes(HTML.Tag tag,
                                        AttributeSet htmlAttrSet,
                                        MutableAttributeSet cssAttrSet) {
    Enumeration<?> names = htmlAttrSet.getAttributeNames();
    while (names.hasMoreElements()) {
        Object name = names.nextElement();

        if (name instanceof HTML.Attribute) {
            HTML.Attribute key = (HTML.Attribute)name;

            /*
             * HTML.Attribute.ALIGN needs special processing.
             * It can map to 1 of many(3) possible CSS attributes
             * depending on the nature of the tag the attribute is
             * part off and depending on the value of the attribute.
             */
            if (key == HTML.Attribute.ALIGN) {
                String htmlAttrValue = (String)htmlAttrSet.getAttribute(HTML.Attribute.ALIGN);
                if (htmlAttrValue != null) {
                    CSS.Attribute cssAttr = getCssAlignAttribute(tag, htmlAttrSet);
                    if (cssAttr != null) {
                        Object o = getCssValue(cssAttr, htmlAttrValue);
                        if (o != null) {
                            cssAttrSet.addAttribute(cssAttr, o);
                        }
                    }
                }
            } else {
                if (key == HTML.Attribute.SIZE && !isHTMLFontTag(tag)) {
                    /*
                     * The html size attribute has a mapping in the CSS world only
                     * if it is par of a font or base font tag.
                     */
                } else if (tag == HTML.Tag.TABLE && key == HTML.Attribute.BORDER) {
                    int borderWidth = getTableBorder(htmlAttrSet);

                    if (borderWidth > 0) {
                        translateAttribute(HTML.Attribute.BORDER, Integer.toString(borderWidth), cssAttrSet);
                    }
                } else {
                    translateAttribute(key, (String) htmlAttrSet.getAttribute(key), cssAttrSet);
                }
            }
        } else if (name instanceof CSS.Attribute) {
            cssAttrSet.addAttribute(name, htmlAttrSet.getAttribute(name));
        }
    }
}
 
Example 18
Source File: ElementTreePanel.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@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);

    // Add a label above tree to describe what is being shown
    JLabel label = new JLabel("Elements that make up the current document",
            SwingConstants.CENTER);

    label.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label, BorderLayout.NORTH);

    setPreferredSize(new Dimension(400, 400));
}
 
Example 19
Source File: ElementTreePanel.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@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);

    // Add a label above tree to describe what is being shown
    JLabel label = new JLabel("Elements that make up the current document",
            SwingConstants.CENTER);

    label.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label, BorderLayout.NORTH);

    setPreferredSize(new Dimension(400, 400));
}
 
Example 20
Source File: ElementTreePanel.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@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);

    // Add a label above tree to describe what is being shown
    JLabel label = new JLabel("Elements that make up the current document",
            SwingConstants.CENTER);

    label.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label, BorderLayout.NORTH);

    setPreferredSize(new Dimension(400, 400));
}