Search in sources :

Example 1 with DrawableSelector

use of org.jetbrains.android.dom.drawable.DrawableSelector 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)

Aggregations

WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 AndroidDomElement (org.jetbrains.android.dom.AndroidDomElement)1 ColorSelector (org.jetbrains.android.dom.color.ColorSelector)1 DrawableSelector (org.jetbrains.android.dom.drawable.DrawableSelector)1