Search in sources :

Example 1 with AndroidDomElement

use of org.jetbrains.android.dom.AndroidDomElement in project android by JetBrains.

the class AndroidResourceUtil method updateStateList.

public static void updateStateList(@NotNull Project project, @NotNull final ResourceHelper.StateList stateList, @NotNull List<VirtualFile> files) {
    if (!ensureFilesWritable(project, files)) {
        return;
    }
    List<PsiFile> psiFiles = Lists.newArrayListWithCapacity(files.size());
    PsiManager manager = PsiManager.getInstance(project);
    for (VirtualFile file : files) {
        PsiFile psiFile = manager.findFile(file);
        if (psiFile != null) {
            psiFiles.add(psiFile);
        }
    }
    final List<AndroidDomElement> selectors = Lists.newArrayListWithCapacity(files.size());
    Class<? extends AndroidDomElement> selectorClass;
    if (stateList.getFolderType() == ResourceFolderType.COLOR) {
        selectorClass = ColorSelector.class;
    } else {
        selectorClass = DrawableSelector.class;
    }
    for (VirtualFile file : files) {
        final AndroidDomElement selector = AndroidUtils.loadDomElement(project, file, selectorClass);
        if (selector == null) {
            AndroidUtils.reportError(project, file.getName() + " is not a statelist file");
            return;
        }
        selectors.add(selector);
    }
    new WriteCommandAction.Simple(project, "Change State List", psiFiles.toArray(new PsiFile[psiFiles.size()])) {

        @Override
        protected void run() {
            for (AndroidDomElement selector : selectors) {
                XmlTag tag = selector.getXmlTag();
                for (XmlTag subtag : tag.getSubTags()) {
                    subtag.delete();
                }
                for (ResourceHelper.StateListState state : stateList.getStates()) {
                    XmlTag child = tag.createChildTag(TAG_ITEM, tag.getNamespace(), null, false);
                    child = tag.addSubTag(child, false);
                    Map<String, Boolean> attributes = state.getAttributes();
                    for (String attributeName : attributes.keySet()) {
                        child.setAttribute(attributeName, ANDROID_URI, attributes.get(attributeName).toString());
                    }
                    if (!StringUtil.isEmpty(state.getAlpha())) {
                        child.setAttribute("alpha", ANDROID_URI, state.getAlpha());
                    }
                    if (selector instanceof ColorSelector) {
                        child.setAttribute(ATTR_COLOR, ANDROID_URI, state.getValue());
                    } else if (selector instanceof DrawableSelector) {
                        child.setAttribute(ATTR_DRAWABLE, ANDROID_URI, state.getValue());
                    }
                }
            }
            // The following is necessary since layoutlib will look on disk for the color state list file.
            // So as soon as a color state list is modified, the change needs to be saved on disk
            // for the correct values to be used in the theme editor preview.
            // TODO: Remove this once layoutlib can get color state lists from PSI instead of disk
            FileDocumentManager.getInstance().saveAllDocuments();
        }
    }.execute();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) DrawableSelector(org.jetbrains.android.dom.drawable.DrawableSelector) ColorSelector(org.jetbrains.android.dom.color.ColorSelector) AndroidDomElement(org.jetbrains.android.dom.AndroidDomElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with AndroidDomElement

use of org.jetbrains.android.dom.AndroidDomElement in project android by JetBrains.

the class AndroidXmlSpellcheckingStrategy method isAttributeValueContext.

private static boolean isAttributeValueContext(@NotNull PsiElement element) {
    if (!(element instanceof XmlAttributeValue)) {
        return false;
    }
    PsiElement parent = element.getParent();
    parent = parent != null ? parent.getParent() : null;
    if (!(parent instanceof XmlTag)) {
        return false;
    }
    DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement((XmlTag) parent);
    if (domElement instanceof AndroidDomElement) {
        return inEnglish(element);
    }
    return false;
}
Also used : DomElement(com.intellij.util.xml.DomElement) AndroidDomElement(org.jetbrains.android.dom.AndroidDomElement) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidDomElement(org.jetbrains.android.dom.AndroidDomElement) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 3 with AndroidDomElement

use of org.jetbrains.android.dom.AndroidDomElement in project android by JetBrains.

the class DbLanguageInjector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull PsiLanguageInjectionHost host, @NotNull InjectedLanguagePlaces injectionPlacesRegistrar) {
    if (!(host instanceof XmlAttributeValue)) {
        return;
    }
    String valueText = ((XmlAttributeValue) host).getValue();
    if (!DataBindingUtil.isBindingExpression(valueText)) {
        return;
    }
    String prefix = valueText.startsWith(PREFIX_TWOWAY_BINDING_EXPR) ? PREFIX_TWOWAY_BINDING_EXPR : PREFIX_BINDING_EXPR;
    PsiElement parent = host.getParent();
    if (!(parent instanceof XmlAttribute))
        return;
    GenericAttributeValue element = DomManager.getDomManager(host.getProject()).getDomElement((XmlAttribute) parent);
    if (element == null || !(element.getParent() instanceof AndroidDomElement))
        return;
    // Parser only parses the expression, not the prefix '@{' or the suffix '}'. Extract the start/end index of the expression.
    String unescapedValue = host.getText();
    int startIndex = unescapedValue.indexOf(prefix.charAt(0)) + prefix.length();
    int endIndex;
    if (valueText.endsWith("}")) {
        endIndex = unescapedValue.lastIndexOf('}');
    } else {
        if (host.getNode().getLastChildNode().getElementType() == XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER) {
            endIndex = host.getLastChild().getStartOffsetInParent();
        } else {
            endIndex = unescapedValue.length();
        }
    }
    if (endIndex == startIndex) {
        // No expression found.
        return;
    }
    injectionPlacesRegistrar.addPlace(DbLanguage.INSTANCE, TextRange.from(startIndex, endIndex - startIndex), null, null);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidDomElement(org.jetbrains.android.dom.AndroidDomElement) PsiElement(com.intellij.psi.PsiElement) GenericAttributeValue(com.intellij.util.xml.GenericAttributeValue)

Aggregations

AndroidDomElement (org.jetbrains.android.dom.AndroidDomElement)3 PsiElement (com.intellij.psi.PsiElement)2 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)2 XmlTag (com.intellij.psi.xml.XmlTag)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 DomElement (com.intellij.util.xml.DomElement)1 GenericAttributeValue (com.intellij.util.xml.GenericAttributeValue)1 ColorSelector (org.jetbrains.android.dom.color.ColorSelector)1 DrawableSelector (org.jetbrains.android.dom.drawable.DrawableSelector)1