Search in sources :

Example 1 with TreeContentHelper

use of org.eclipse.wst.xml.ui.internal.tabletree.TreeContentHelper in project webtools.sourceediting by eclipse.

the class UpdateTextValueCommand method execute.

public void execute() {
    try {
        beginRecording(element);
        Node textNode = null;
        TreeContentHelper helper = new TreeContentHelper();
        for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
            if (node.getNodeType() == Node.TEXT_NODE && !helper.isIgnorableText(node)) {
                textNode = node;
                break;
            }
        }
        if (textNode == null) {
            textNode = element.getOwnerDocument().createTextNode("");
            element.appendChild(textNode);
        }
        helper.setNodeValue(textNode, value);
    } finally {
        endRecording();
    }
}
Also used : Node(org.w3c.dom.Node) TreeContentHelper(org.eclipse.wst.xml.ui.internal.tabletree.TreeContentHelper)

Example 2 with TreeContentHelper

use of org.eclipse.wst.xml.ui.internal.tabletree.TreeContentHelper in project webtools.sourceediting by eclipse.

the class DOMExtensionDetailsContentProvider method getItems.

public Object[] getItems(Object input) {
    HashMap resultMap = new HashMap();
    if (input instanceof Element) {
        Element element = (Element) input;
        // here we compute items for the attributes that physically in the document
        // 
        NamedNodeMap attributes = element.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr) attributes.item(i);
            if (// $NON-NLS-1$ //$NON-NLS-2$
            !XMLNS.equals(attr.getName()) && !XMLNS.equals(attr.getPrefix())) {
                resultMap.put(attr.getName(), DOMExtensionItem.createItemForElementAttribute(element, attr));
            }
        }
        // here we compute an item for the text node that is physically in the document
        // 
        String textNodeValue = new TreeContentHelper().getNodeValue(element);
        if (textNodeValue != null) {
            resultMap.put(TEXT_NODE_KEY, DOMExtensionItem.createItemForElementText(element));
        }
        ModelQuery modelQuery = ModelQueryUtil.getModelQuery(element.getOwnerDocument());
        if (modelQuery != null) {
            CMElementDeclaration ed = modelQuery.getCMElementDeclaration(element);
            if (ed != null) {
                // here we compute items for the attributes that may be added to the document according to the grammar
                // 
                List list = modelQuery.getAvailableContent(element, ed, ModelQuery.INCLUDE_ATTRIBUTES);
                for (Iterator i = list.iterator(); i.hasNext(); ) {
                    CMAttributeDeclaration ad = (CMAttributeDeclaration) i.next();
                    if (ad != null && resultMap.get(ad.getNodeName()) == null) {
                        resultMap.put(ad.getNodeName(), DOMExtensionItem.createItemForElementAttribute(element, ad));
                    }
                }
                if (resultMap.get(TEXT_NODE_KEY) == null) {
                    // here we compute an item for the text node that may be added to the document according to the grammar
                    // 
                    int contentType = ed.getContentType();
                    if (contentType == CMElementDeclaration.PCDATA || contentType == CMElementDeclaration.MIXED) {
                        resultMap.put(TEXT_NODE_KEY, DOMExtensionItem.createItemForElementText(element));
                    }
                }
            }
        }
        Collection collection = resultMap.values();
        // 
        for (Iterator i = collection.iterator(); i.hasNext(); ) {
            initPropertyEditorConfiguration((DOMExtensionItem) i.next());
        }
        DOMExtensionItem[] items = new DOMExtensionItem[collection.size()];
        resultMap.values().toArray(items);
        // 
        if (items.length > 0) {
            Comparator comparator = new Comparator() {

                public int compare(Object arg0, Object arg1) {
                    DOMExtensionItem a = (DOMExtensionItem) arg0;
                    DOMExtensionItem b = (DOMExtensionItem) arg1;
                    // begin special case to ensure 'text nodes' come last
                    if (a.isTextValue() && !b.isTextValue()) {
                        return 1;
                    } else if (b.isTextValue() && !a.isTextValue()) {
                        return -1;
                    } else // end special case
                    {
                        return Collator.getInstance().compare(a.getName(), b.getName());
                    }
                }
            };
            Arrays.sort(items, comparator);
        }
        return items;
    } else if (input instanceof Attr) {
        Attr attr = (Attr) input;
        DOMExtensionItem item = DOMExtensionItem.createItemForAttributeText(attr.getOwnerElement(), attr);
        DOMExtensionItem[] items = { item };
        return items;
    }
    return EMPTY_ARRAY;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr) Comparator(java.util.Comparator) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) Iterator(java.util.Iterator) Collection(java.util.Collection) TreeContentHelper(org.eclipse.wst.xml.ui.internal.tabletree.TreeContentHelper) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) List(java.util.List) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)

Aggregations

TreeContentHelper (org.eclipse.wst.xml.ui.internal.tabletree.TreeContentHelper)2 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)1 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)1 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)1 Attr (org.w3c.dom.Attr)1 Element (org.w3c.dom.Element)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1