Search in sources :

Example 11 with ICSSModel

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

the class LinkElementAdapter method createModel.

protected ICSSModel createModel() {
    // create phantom(broken link) external CSS model
    if (getElement() == null)
        return null;
    IStructuredModel baseModel = ((IDOMNode) getElement()).getModel();
    ICSSModel newModel = (ICSSModel) baseModel.getModelManager().createUnManagedStructuredModelFor(CSS_ID);
    // calculate base location and set
    // get resolver in Model
    URIResolver resolver = baseModel.getResolver();
    // resolve to absolute url : this need not exact location of css file. It is important that absurl is not null.
    String ref = getElement().getAttribute(HTML40Namespace.ATTR_NAME_HREF);
    String absurl = (resolver != null && ref != null && ref.length() > 0) ? resolver.getLocationByURI(ref, true) : null;
    if ((absurl == null) || (absurl.length() == 0)) {
        IPath basePath = new Path(baseModel.getBaseLocation());
        URLHelper helper = new URLHelper(basePath.removeLastSegments(1).toString());
        // $NON-NLS-1$
        absurl = helper.toAbsolute(ref == null ? "" : ref);
    }
    if ((absurl == null) || (absurl.length() == 0)) {
        absurl = ref;
    }
    if (absurl == null) {
        // $NON-NLS-1$
        absurl = "";
    }
    newModel.setBaseLocation(absurl);
    // set style listener
    newModel.addStyleListener(this);
    return newModel;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 12 with ICSSModel

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

the class StyleAttrAdapter method setValue.

/**
 */
private void setValue() {
    Element element = getElement();
    if (element == null)
        return;
    ICSSModel model = getExistingModel();
    if (model == null)
        return;
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    if (structuredDocument == null)
        return;
    String value = null;
    IStructuredDocumentRegionList flatNodes = structuredDocument.getRegionList();
    if (flatNodes != null) {
        int count = flatNodes.getLength();
        if (count > 0) {
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < count; i++) {
                IStructuredDocumentRegion flatNode = flatNodes.item(i);
                if (flatNode == null)
                    continue;
                buffer.append(flatNode.getText());
            }
            value = buffer.toString();
        }
    }
    this.ignoreNotification = true;
    if (value == null || value.length() == 0) {
        element.removeAttribute(STYLE);
    } else {
        Attr attr = element.getAttributeNode(STYLE);
        if (attr != null) {
            ((IDOMNode) attr).setValueSource(value);
        } else {
            Document document = element.getOwnerDocument();
            attr = document.createAttribute(STYLE);
            ((IDOMNode) attr).setValueSource(value);
            element.setAttributeNode(attr);
        }
    }
    this.ignoreNotification = false;
    notifyStyleChanged(element);
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) IStructuredDocumentRegionList(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Element(org.w3c.dom.Element) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Document(org.w3c.dom.Document) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Attr(org.w3c.dom.Attr)

Example 13 with ICSSModel

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

the class StyleAttrAdapter method valueChanged.

/**
 */
private void valueChanged() {
    Element element = getElement();
    if (element == null)
        return;
    if (!isModelNecessary()) {
        // removed
        setModel(null);
        notifyStyleChanged(element);
        return;
    }
    ICSSModel model = getExistingModel();
    if (model == null)
        // defer
        return;
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    if (structuredDocument == null)
        // error
        return;
    String value = null;
    Attr attr = element.getAttributeNode(org.eclipse.wst.html.core.internal.provisional.HTML40Namespace.ATTR_NAME_STYLE);
    if (attr != null)
        value = ((IDOMNode) attr).getValueSource();
    structuredDocument.setText(this, value);
    notifyStyleChanged(element);
}
Also used : ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Element(org.w3c.dom.Element) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Attr(org.w3c.dom.Attr)

Example 14 with ICSSModel

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

the class CSSCompletionProposalComputer method computeCompletionProposals.

/**
 * @see org.eclipse.wst.sse.ui.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
 */
public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
    ITextViewer viewer = context.getViewer();
    int documentPosition = context.getInvocationOffset();
    IndexedRegion indexedNode = ContentAssistUtils.getNodeAt(viewer, documentPosition);
    IDOMNode xNode = null;
    IDOMNode parent = null;
    CSSProposalArranger arranger = null;
    // If there is a selected region, we'll need to replace the text
    ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
    // if(indexedNode == null) return new ICompletionProposal[0];
    if (indexedNode instanceof IDOMNode) {
        xNode = (IDOMNode) indexedNode;
        parent = (IDOMNode) xNode.getParentNode();
    }
    // case
    if ((xNode != null) && xNode.getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.STYLE)) {
        // now we know the cursor is in a <style> tag w/out region
        IStructuredModel cssModel = getCSSModel(xNode);
        if (cssModel != null) {
            // adjust offsets for embedded style
            int offset = documentPosition;
            int pos = 0;
            IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(pos);
            if (keyIndexedNode == null) {
                keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
            }
            arranger = new CSSProposalArranger(pos, (ICSSNode) keyIndexedNode, offset, selection.getLength(), (char) 0);
        }
    } else if ((parent != null) && parent.getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.STYLE)) {
        // now we know the cursor is in a <style> tag with a region
        // use the parent because that will be the <style> tag
        IStructuredModel cssModel = getCSSModel(parent);
        if (cssModel != null) {
            // adjust offsets for embedded style
            int offset = indexedNode.getStartOffset();
            int pos = documentPosition - offset;
            IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(pos);
            if (keyIndexedNode == null) {
                keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
            }
            arranger = new CSSProposalArranger(pos, (ICSSNode) keyIndexedNode, offset, selection.getLength(), (char) 0);
        }
    } else if (indexedNode instanceof IDOMNode) {
        IDOMNode domNode = ((IDOMNode) indexedNode);
        // get model for node w/ style attribute
        IStructuredModel cssModel = getCSSModel(domNode);
        if (cssModel != null) {
            // adjust offsets for embedded style
            int textRegionStartOffset = getTextRegionStartOffset(domNode, documentPosition);
            int pos = documentPosition - textRegionStartOffset;
            char quote = (char) 0;
            try {
                quote = context.getDocument().get(textRegionStartOffset, 1).charAt(0);
            } catch (BadLocationException e) {
                Logger.logException("error getting quote character", e);
            }
            // get css indexed region
            IndexedRegion cssIndexedNode = cssModel.getIndexedRegion(pos);
            if (cssIndexedNode == null) {
                cssIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
            }
            if (cssIndexedNode instanceof ICSSNode) {
                // inline style for a tag, not embedded
                arranger = new CSSProposalArranger(pos, (ICSSNode) cssIndexedNode, textRegionStartOffset, selection.getLength(), quote);
            }
        }
    } else if (indexedNode instanceof ICSSNode) {
        // when editing external CSS using CSS Designer, ICSSNode is passed.
        ICSSDocument cssdoc = ((ICSSNode) indexedNode).getOwnerDocument();
        if (cssdoc != null) {
            IStructuredModel cssModel = cssdoc.getModel();
            if (cssModel != null) {
                IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition);
                if (keyIndexedNode == null) {
                    keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
                }
                if (keyIndexedNode instanceof ICSSNode) {
                    // inline style for a tag, not embedded
                    arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, 0, selection.getLength(), (char) 0);
                }
            }
        }
    } else if ((indexedNode == null) && ContentAssistUtils.isViewerEmpty(viewer)) {
        // the top of empty CSS Document
        IStructuredModel cssModel = null;
        try {
            cssModel = StructuredModelManager.getModelManager().getExistingModelForRead(viewer.getDocument());
            if (cssModel instanceof ICSSModel) {
                IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition);
                if (keyIndexedNode == null) {
                    keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
                }
                if (keyIndexedNode instanceof ICSSNode) {
                    // inline style for a tag, not embedded
                    arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, 0, (char) 0);
                }
            }
        } finally {
            if (cssModel != null)
                cssModel.releaseFromRead();
        }
    }
    ICompletionProposal[] proposals = new ICompletionProposal[0];
    if (arranger != null) {
        proposals = arranger.getProposals();
        ICompletionProposal[] newfileproposals = new ICompletionProposal[0];
        ICompletionProposal[] anyproposals = new ICompletionProposal[0];
        // add end tag if parent is not closed
        ICompletionProposal endTag = XMLContentAssistUtilities.computeXMLEndTagProposal(viewer, documentPosition, indexedNode, HTML40Namespace.ElementName.STYLE, SharedXMLEditorPluginImageHelper.IMG_OBJ_TAG_GENERIC);
        // add the additional proposals
        int additionalLength = newfileproposals.length + anyproposals.length;
        additionalLength = (endTag != null) ? ++additionalLength : additionalLength;
        if (additionalLength > 0) {
            ICompletionProposal[] plusOnes = new ICompletionProposal[proposals.length + additionalLength];
            int appendPos = proposals.length;
            // add end tag proposal
            if (endTag != null) {
                System.arraycopy(proposals, 0, plusOnes, 1, proposals.length);
                plusOnes[0] = endTag;
                ++appendPos;
            } else {
                System.arraycopy(proposals, 0, plusOnes, 0, proposals.length);
            }
            // add items in newfileproposals
            for (int i = 0; i < newfileproposals.length; ++i) {
                plusOnes[appendPos + i] = newfileproposals[i];
            }
            // add items in anyproposals
            appendPos = appendPos + newfileproposals.length;
            for (int i = 0; i < anyproposals.length; ++i) {
                plusOnes[appendPos + i] = anyproposals[i];
            }
            proposals = plusOnes;
        }
    }
    return Arrays.asList(proposals);
}
Also used : 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) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) ITextSelection(org.eclipse.jface.text.ITextSelection) ITextViewer(org.eclipse.jface.text.ITextViewer) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 15 with ICSSModel

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

the class CSSContentAssistProcessor method computeCompletionProposals.

/**
 * Return a list of proposed code completions based on the specified
 * location within the document that corresponds to the current cursor
 * position within the text-editor control.
 *
 * @param documentPosition
 *            a location within the document
 * @return an array of code-assist items
 */
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentPosition) {
    IndexedRegion indexedNode = ContentAssistUtils.getNodeAt(viewer, documentPosition + fDocumentOffset);
    IDOMNode xNode = null;
    IDOMNode parent = null;
    CSSProposalArranger arranger = null;
    boolean isEmptyDocument = false;
    // If there is a selected region, we'll need to replace the text
    ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
    boolean selected = (selection != null && selection.getText() != null && selection.getText().trim().length() > 0);
    // if(indexedNode == null) return new ICompletionProposal[0];
    if (indexedNode instanceof IDOMNode) {
        xNode = (IDOMNode) indexedNode;
        parent = (IDOMNode) xNode.getParentNode();
    }
    // case
    if ((xNode != null) && xNode.getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.STYLE)) {
        // now we know the cursor is in a <style> tag w/out region
        IStructuredModel cssModel = getCSSModel(xNode);
        if (cssModel != null) {
            // adjust offsets for embedded style
            int offset = documentPosition + fDocumentOffset;
            int pos = 0;
            IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(pos);
            if (keyIndexedNode == null) {
                keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
            }
            arranger = new CSSProposalArranger(pos, (ICSSNode) keyIndexedNode, offset, (char) 0, selected);
        }
    } else if ((parent != null) && parent.getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.STYLE)) {
        // now we know the cursor is in a <style> tag with a region
        // use the parent because that will be the <style> tag
        IStructuredModel cssModel = getCSSModel(parent);
        if (cssModel != null) {
            // adjust offsets for embedded style
            int offset = indexedNode.getStartOffset();
            int pos = documentPosition - offset;
            IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(pos);
            if (keyIndexedNode == null) {
                keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
            }
            arranger = new CSSProposalArranger(pos, (ICSSNode) keyIndexedNode, offset, (char) 0, selected);
        }
    } else if (indexedNode instanceof IDOMNode) {
        // get model for node w/ style attribute
        IStructuredModel cssModel = getCSSModel((IDOMNode) indexedNode);
        if (cssModel != null) {
            IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition - fDocumentOffset);
            if (keyIndexedNode == null) {
                keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
            }
            if (keyIndexedNode instanceof ICSSNode) {
                // inline style for a tag, not embedded
                arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, fDocumentOffset, fQuote, selected);
            }
        }
    } else if (indexedNode instanceof ICSSNode) {
        // when editing external CSS using CSS Designer, ICSSNode is
        // passed.
        ICSSDocument cssdoc = ((ICSSNode) indexedNode).getOwnerDocument();
        if (cssdoc != null) {
            IStructuredModel cssModel = cssdoc.getModel();
            if (cssModel != null) {
                IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition - fDocumentOffset);
                if (keyIndexedNode == null) {
                    keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
                }
                if (keyIndexedNode instanceof ICSSNode) {
                    // inline style for a tag, not embedded
                    arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, fDocumentOffset, fQuote, selected);
                }
            }
        }
    } else if ((indexedNode == null) && isViewerEmpty(viewer)) {
        isEmptyDocument = true;
        // the top of empty CSS Document
        IStructuredModel cssModel = null;
        try {
            cssModel = StructuredModelManager.getModelManager().getExistingModelForRead(viewer.getDocument());
            if (cssModel instanceof ICSSModel) {
                IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition - fDocumentOffset);
                if (keyIndexedNode == null) {
                    keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
                }
                if (keyIndexedNode instanceof ICSSNode) {
                    // inline style for a tag, not embedded
                    arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, fDocumentOffset, fQuote);
                }
            }
        } finally {
            if (cssModel != null)
                cssModel.releaseFromRead();
        }
    }
    ICompletionProposal[] proposals = new ICompletionProposal[0];
    if (arranger != null) {
        fDocumentOffset = 0;
        proposals = arranger.getProposals();
        ICompletionProposal[] newfileproposals = new ICompletionProposal[0];
        ICompletionProposal[] anyproposals = new ICompletionProposal[0];
        // add template proposals
        if (getTemplateCompletionProcessor() != null) {
            if (isEmptyDocument) {
                getTemplateCompletionProcessor().setContextType(TemplateContextTypeIdsCSS.NEW);
                newfileproposals = getTemplateCompletionProcessor().computeCompletionProposals(viewer, documentPosition);
            }
            getTemplateCompletionProcessor().setContextType(TemplateContextTypeIdsCSS.ALL);
            anyproposals = getTemplateCompletionProcessor().computeCompletionProposals(viewer, documentPosition);
        }
        // add end tag if parent is not closed
        ICompletionProposal endTag = XMLContentAssistUtilities.computeXMLEndTagProposal(viewer, documentPosition, indexedNode, HTML40Namespace.ElementName.STYLE, SharedXMLEditorPluginImageHelper.IMG_OBJ_TAG_GENERIC);
        // add the additional proposals
        int additionalLength = newfileproposals.length + anyproposals.length;
        additionalLength = (endTag != null) ? ++additionalLength : additionalLength;
        if (additionalLength > 0) {
            ICompletionProposal[] plusOnes = new ICompletionProposal[proposals.length + additionalLength];
            int appendPos = proposals.length;
            // add end tag proposal
            if (endTag != null) {
                System.arraycopy(proposals, 0, plusOnes, 1, proposals.length);
                plusOnes[0] = endTag;
                ++appendPos;
            } else {
                System.arraycopy(proposals, 0, plusOnes, 0, proposals.length);
            }
            // add items in newfileproposals
            for (int i = 0; i < newfileproposals.length; ++i) {
                plusOnes[appendPos + i] = newfileproposals[i];
            }
            // add items in anyproposals
            appendPos = appendPos + newfileproposals.length;
            for (int i = 0; i < anyproposals.length; ++i) {
                plusOnes[appendPos + i] = anyproposals[i];
            }
            proposals = plusOnes;
        }
    }
    return proposals;
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) ITextSelection(org.eclipse.jface.text.ITextSelection)

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