Search in sources :

Example 1 with ResourceElement

use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.

the class ThemeEditorUtils method createNewStyle.

/**
   * Creates a new style
   * @param project the project where the new style is being created
   * @param resourceDir the res/ directory where the new style is being created
   * @param newStyleName the new style name
   * @param parentStyleName the name of the new style parent
   * @param fileName name of the xml file where the style will be added (usually "styles.xml")
   * @param folderNames folder names where the style will be added
   * @return true if the style was created or false otherwise
   */
public static boolean createNewStyle(@NotNull final Project project, @NotNull final VirtualFile resourceDir, @NotNull final String newStyleName, @Nullable final String parentStyleName, @NotNull final String fileName, @NotNull final List<String> folderNames) {
    return new WriteCommandAction<Boolean>(project, "Create new style " + newStyleName) {

        @Override
        protected void run(@NotNull Result<Boolean> result) {
            CommandProcessor.getInstance().markCurrentCommandAsGlobal(project);
            result.setResult(AndroidResourceUtil.createValueResource(project, resourceDir, newStyleName, null, ResourceType.STYLE, fileName, folderNames, new Processor<ResourceElement>() {

                @Override
                public boolean process(ResourceElement element) {
                    assert element instanceof Style;
                    final Style style = (Style) element;
                    if (parentStyleName != null) {
                        style.getParentStyle().setStringValue(parentStyleName);
                    }
                    return true;
                }
            }));
        }
    }.execute().getResultObject();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) Processor(com.intellij.util.Processor) CommandProcessor(com.intellij.openapi.command.CommandProcessor) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) Style(org.jetbrains.android.dom.resources.Style) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result)

Example 2 with ResourceElement

use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.

the class AndroidFindUsagesHandlerFactory method correctResourceElement.

@Nullable
private static PsiElement correctResourceElement(PsiElement element) {
    if (element instanceof XmlElement && !(element instanceof XmlFile)) {
        XmlTag tag = element instanceof XmlTag ? (XmlTag) element : PsiTreeUtil.getParentOfType(element, XmlTag.class);
        DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement(tag);
        if (domElement instanceof ResourceElement) {
            return tag;
        }
        return null;
    }
    return element;
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) DomElement(com.intellij.util.xml.DomElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ResourceElement

use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.

the class ValueResourceElementWrapper method setName.

@Override
@Nullable
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
    if (AndroidResourceUtil.isIdDeclaration(myWrappee)) {
        XmlAttribute attribute = (XmlAttribute) myWrappee.getParent();
        attribute.setValue(name);
    } else {
        // then it is a value resource
        XmlTag tag = PsiTreeUtil.getParentOfType(myWrappee, XmlTag.class);
        DomElement domElement = DomManager.getDomManager(getProject()).getDomElement(tag);
        assert domElement instanceof ResourceElement;
        ResourceElement resElement = (ResourceElement) domElement;
        resElement.getName().setValue(name);
    }
    return null;
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) DomElement(com.intellij.util.xml.DomElement) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ResourceElement

use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.

the class ValueResourceInfoImpl method computeDomElement.

@Nullable
public ResourceElement computeDomElement() {
    final PsiFile file = PsiManager.getInstance(myProject).findFile(myFile);
    if (!(file instanceof XmlFile)) {
        return null;
    }
    final XmlTag tag = PsiTreeUtil.findElementOfClassAtOffset(file, myOffset, XmlTag.class, true);
    if (tag == null) {
        return null;
    }
    final DomElement domElement = DomManager.getDomManager(myProject).getDomElement(tag);
    if (!(domElement instanceof ResourceElement)) {
        return null;
    }
    final String resType = domElement instanceof Item ? ((Item) domElement).getType().getStringValue() : AndroidCommonUtils.getResourceTypeByTagName(tag.getName());
    if (!myType.getName().equals(resType)) {
        return null;
    }
    final ResourceElement resDomElement = (ResourceElement) domElement;
    final String resName = ((ResourceElement) domElement).getName().getStringValue();
    return myName.equals(resName) ? resDomElement : null;
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) Item(org.jetbrains.android.dom.resources.Item) DomElement(com.intellij.util.xml.DomElement) XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with ResourceElement

use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.

the class OverrideResourceAction method createValueResource.

private static void createValueResource(@NotNull final Project project, @NotNull final PsiDirectory resDir, @NotNull PsiFile file, @NotNull PsiDirectory resourceSubdir, @NotNull final String resName, @NotNull final String value, @NotNull final ResourceType type, @NotNull final String oldTagText, boolean open) {
    final String filename = file.getName();
    final List<String> dirNames = Collections.singletonList(resourceSubdir.getName());
    final AtomicReference<PsiElement> openAfter = new AtomicReference<PsiElement>();
    final WriteCommandAction<Void> action = new WriteCommandAction<Void>(project, "Override Resource " + resName, file) {

        @Override
        protected void run(@NotNull Result<Void> result) {
            List<ResourceElement> elements = Lists.newArrayListWithExpectedSize(1);
            // AndroidResourceUtil.createValueResource will create a new resource value in the given resource
            // folder (and record the corresponding tags added in the elements list passed into it).
            // However, it only creates a new element and sets the name attribute on it; it does not
            // transfer attributes, child content etc. Therefore, we use this utility method first to
            // create the corresponding tag, and then *afterwards* we will replace the tag with a text copy
            // from the resource tag we are overriding. We do this all under a single write lock such
            // that it becomes a single atomic operation.
            AndroidResourceUtil.createValueResource(project, resDir.getVirtualFile(), resName, type, filename, dirNames, value, elements);
            if (elements.size() == 1) {
                final XmlTag tag = elements.get(0).getXmlTag();
                if (tag != null && tag.isValid()) {
                    try {
                        XmlTag tagFromText = XmlElementFactory.getInstance(tag.getProject()).createTagFromText(oldTagText);
                        PsiElement replaced = tag.replace(tagFromText);
                        openAfter.set(replaced);
                    } catch (IncorrectOperationException e) {
                        // The user tried to override an invalid XML fragment: don't attempt to do a replacement in that case
                        openAfter.set(tag);
                    }
                }
            }
        }
    };
    action.execute();
    PsiElement tag = openAfter.get();
    if (open && tag != null) {
        NavigationUtil.openFileWithPsiElement(tag, true, true);
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) AtomicReference(java.util.concurrent.atomic.AtomicReference) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

ResourceElement (org.jetbrains.android.dom.resources.ResourceElement)16 XmlTag (com.intellij.psi.xml.XmlTag)7 NotNull (org.jetbrains.annotations.NotNull)7 Result (com.intellij.openapi.application.Result)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)6 DomElement (com.intellij.util.xml.DomElement)6 Nullable (org.jetbrains.annotations.Nullable)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Resources (org.jetbrains.android.dom.resources.Resources)4 Style (org.jetbrains.android.dom.resources.Style)4 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)3 ResourceType (com.android.resources.ResourceType)2 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)2 Project (com.intellij.openapi.project.Project)2 PsiFile (com.intellij.psi.PsiFile)2 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2 Processor (com.intellij.util.Processor)2 Item (org.jetbrains.android.dom.resources.Item)2 StyleItem (org.jetbrains.android.dom.resources.StyleItem)2 LocalResourceManager (org.jetbrains.android.resourceManagers.LocalResourceManager)2