Search in sources :

Example 26 with ICSSModel

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSModel in project webtools.sourceediting by eclipse.

the class StyleElementAdapter method contentChanged.

/**
 * Apply changes from HTML model to CSS sub-model
 */
private void contentChanged() {
    Element element = getElement();
    if (element == null)
        return;
    ICSSModel model = getExistingModel();
    if (model == null)
        return;
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    if (structuredDocument == null)
        return;
    String data = null;
    Node child = element.getFirstChild();
    if (child != null && child.getNodeType() == Node.TEXT_NODE && child.getNextSibling() == null) {
        data = child.getNodeValue();
    }
    if (data == null)
        // $NON-NLS-1$
        data = "";
    // minimize replace range
    int start = 0, end = 0;
    String oldData = structuredDocument.get();
    if (oldData == null)
        // $NON-NLS-1$
        oldData = "";
    // search differenct character position from first
    for (; start < oldData.length() && start < data.length(); start++) if (oldData.charAt(start) != data.charAt(start))
        break;
    if (start == oldData.length() && start == data.length())
        // no change
        return;
    else if (start == oldData.length()) {
        // append text to last
        structuredDocument.replaceText(getRequesterH2C(), start, 0, data.substring(start));
    } else if (start == data.length()) {
        // remove text of last //$NON-NLS-1$
        structuredDocument.replaceText(getRequesterH2C(), start, oldData.length() - start, "");
    } else {
        // search differenct character position from last
        for (; start < oldData.length() - end && start < data.length() - end; end++) {
            if (oldData.charAt(oldData.length() - end - 1) != data.charAt(data.length() - end - 1))
                break;
        }
        structuredDocument.replaceText(getRequesterH2C(), start, oldData.length() - end - start, data.substring(start, data.length() - end));
    }
}
Also used : ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) Element(org.w3c.dom.Element) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 27 with ICSSModel

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSModel in project webtools.sourceediting by eclipse.

the class StyleElementAdapter method setModel.

/**
 */
protected void setModel(ICSSModel model, boolean setupListener) {
    ICSSModel oldModel = getExistingModel();
    if (model == oldModel)
        return;
    super.setModel(model);
    if (!setupListener)
        return;
    if (oldModel != null)
        oldModel.removeStyleListener(this);
    if (model != null)
        model.addStyleListener(this);
}
Also used : ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)

Example 28 with ICSSModel

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSModel in project webtools.sourceediting by eclipse.

the class CleanupProcessorCSS method cleanupModel.

public void cleanupModel(IStructuredModel structuredModel, int start, int length) {
    CSSFormatUtil formatUtil = CSSFormatUtil.getInstance();
    if (structuredModel instanceof ICSSModel) {
        ICSSDocument doc = ((ICSSModel) structuredModel).getDocument();
        CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) doc);
        StringBuffer buf = formatter.cleanup(doc);
        if (buf != null) {
            int startOffset = ((IndexedRegion) doc).getStartOffset();
            int endOffset = ((IndexedRegion) doc).getEndOffset();
            formatUtil.replaceSource(doc.getModel(), startOffset, endOffset - startOffset, buf.toString());
        }
    } else if (structuredModel instanceof IDOMModel) {
        List cssnodes = formatUtil.collectCSSNodes(structuredModel, start, length);
        if (cssnodes != null && !cssnodes.isEmpty()) {
            ICSSModel model = null;
            for (int i = 0; i < cssnodes.size(); i++) {
                ICSSNode node = (ICSSNode) cssnodes.get(i);
                CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) node);
                StringBuffer buf = formatter.cleanup(node);
                if (buf != null) {
                    int startOffset = ((IndexedRegion) node).getStartOffset();
                    int endOffset = ((IndexedRegion) node).getEndOffset();
                    if (model == null) {
                        model = node.getOwnerDocument().getModel();
                    }
                    formatUtil.replaceSource(model, startOffset, endOffset - startOffset, buf.toString());
                }
            }
        }
    }
}
Also used : CSSSourceFormatter(org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter) CSSFormatUtil(org.eclipse.wst.css.core.internal.formatter.CSSFormatUtil) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) List(java.util.List) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 29 with ICSSModel

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSModel in project webtools.sourceediting by eclipse.

the class ElementNodeCleanupHandler method getCSSValue.

/**
 */
private String getCSSValue(Attr attr) {
    ICSSModel model = getCSSModel(attr);
    if (model == null)
        return null;
    ICSSNode document = model.getDocument();
    if (document == null)
        return null;
    INodeNotifier notifier = (INodeNotifier) document;
    CSSSourceFormatter formatter = (CSSSourceFormatter) notifier.getAdapterFor(CSSSourceFormatter.class);
    // try another way to get formatter
    if (formatter == null)
        formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter(notifier);
    if (formatter == null)
        return null;
    StringBuffer buffer = formatter.cleanup(document);
    if (buffer == null)
        return null;
    return buffer.toString();
}
Also used : CSSSourceFormatter(org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 30 with ICSSModel

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSModel in project webtools.sourceediting by eclipse.

the class JFaceNodeContentProviderCSS method addElements.

/**
 * @deprecated
 */
protected void addElements(Object element, ArrayList v) {
    ICSSNode node;
    if (element instanceof ICSSModel) {
        ICSSModel model = (ICSSModel) element;
        ICSSDocument doc = model.getDocument();
        node = doc.getFirstChild();
    } else if (element instanceof ICSSNode) {
        node = ((ICSSNode) element).getFirstChild();
    } else
        return;
    while (node != null) {
        if (node instanceof CSSRule) {
            v.add(node);
        }
        node = node.getNextSibling();
    }
}
Also used : CSSRule(org.w3c.dom.css.CSSRule) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)

Aggregations

ICSSModel (org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)82 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)41 CSSRule (org.w3c.dom.css.CSSRule)21 CSSRuleList (org.w3c.dom.css.CSSRuleList)20 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)15 CSSStyleDeclaration (org.w3c.dom.css.CSSStyleDeclaration)14 CSSStyleSheet (org.w3c.dom.css.CSSStyleSheet)14 CSSValue (org.w3c.dom.css.CSSValue)14 ICSSDocument (org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)12 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)12 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)11 IModelProvideAdapter (org.eclipse.wst.css.core.internal.provisional.adapters.IModelProvideAdapter)8 CSSSourceParser (org.eclipse.wst.css.core.internal.parser.CSSSourceParser)7 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)7 CSSStyleRule (org.w3c.dom.css.CSSStyleRule)7 IFile (org.eclipse.core.resources.IFile)6 Path (org.eclipse.core.runtime.Path)6 CSSSourceFormatter (org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter)6 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)6 Element (org.w3c.dom.Element)6