Search in sources :

Example 1 with Resources

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

the class LocalResourceManager method findStyleableAttributesByFieldName.

public List<Attr> findStyleableAttributesByFieldName(@NotNull String fieldName) {
    int index = fieldName.lastIndexOf('_');
    // is "miny_moe".
    while (index != -1) {
        int prev = fieldName.lastIndexOf('_', index - 1);
        if (prev == -1 || Character.isUpperCase(fieldName.charAt(prev + 1))) {
            break;
        }
        index = prev;
    }
    if (index == -1) {
        return Collections.emptyList();
    }
    String styleableName = fieldName.substring(0, index);
    String attrName = fieldName.substring(index + 1);
    List<Attr> list = new ArrayList<Attr>();
    for (Pair<Resources, VirtualFile> pair : getResourceElements()) {
        final Resources res = pair.getFirst();
        for (DeclareStyleable styleable : res.getDeclareStyleables()) {
            if (styleableName.equals(styleable.getName().getValue())) {
                for (Attr attr : styleable.getAttrs()) {
                    if (attrName.equals(attr.getName().getValue())) {
                        list.add(attr);
                    }
                }
            }
        }
    }
    return list;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Resources(org.jetbrains.android.dom.resources.Resources) Attr(org.jetbrains.android.dom.resources.Attr) DeclareStyleable(org.jetbrains.android.dom.resources.DeclareStyleable)

Example 2 with Resources

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

the class AndroidResourceUtil method changeValueResource.

/**
   * Sets a new value for a resource.
   * @param project the project containing the resource
   * @param resDir the res/ directory containing the resource
   * @param name the name of the resource to be modified
   * @param newValue the new resource value
   * @param fileName the resource values file name
   * @param dirNames list of values directories where the resource should be changed
   * @param useGlobalCommand if true, the undo will be registered globally. This allows the command to be undone from anywhere in the IDE
   *                         and not only the XML editor
   * @return true if the resource value was changed
   */
public static boolean changeValueResource(@NotNull final Project project, @NotNull VirtualFile resDir, @NotNull final String name, @NotNull final ResourceType resourceType, @NotNull final String newValue, @NotNull String fileName, @NotNull List<String> dirNames, final boolean useGlobalCommand) {
    if (dirNames.isEmpty()) {
        return false;
    }
    ArrayList<VirtualFile> resFiles = Lists.newArrayListWithExpectedSize(dirNames.size());
    for (String dirName : dirNames) {
        final VirtualFile resFile = findResourceFile(resDir, fileName, dirName);
        if (resFile != null) {
            resFiles.add(resFile);
        }
    }
    if (!ensureFilesWritable(project, resFiles)) {
        return false;
    }
    final Resources[] resourcesElements = new Resources[resFiles.size()];
    for (int i = 0; i < resFiles.size(); i++) {
        final Resources resources = AndroidUtils.loadDomElement(project, resFiles.get(i), Resources.class);
        if (resources == null) {
            AndroidUtils.reportError(project, AndroidBundle.message("not.resource.file.error", fileName));
            return false;
        }
        resourcesElements[i] = resources;
    }
    List<PsiFile> psiFiles = Lists.newArrayListWithExpectedSize(resFiles.size());
    PsiManager manager = PsiManager.getInstance(project);
    for (VirtualFile file : resFiles) {
        PsiFile psiFile = manager.findFile(file);
        if (psiFile != null) {
            psiFiles.add(psiFile);
        }
    }
    PsiFile[] files = psiFiles.toArray(new PsiFile[psiFiles.size()]);
    WriteCommandAction<Boolean> action = new WriteCommandAction<Boolean>(project, "Change " + resourceType.getName() + " Resource", files) {

        @Override
        protected void run(@NotNull Result<Boolean> result) throws Throwable {
            if (useGlobalCommand) {
                CommandProcessor.getInstance().markCurrentCommandAsGlobal(project);
            }
            result.setResult(false);
            for (Resources resources : resourcesElements) {
                for (ResourceElement element : getValueResourcesFromElement(resourceType, resources)) {
                    String value = element.getName().getStringValue();
                    if (name.equals(value)) {
                        element.setStringValue(newValue);
                        result.setResult(true);
                    }
                }
            }
        }
    };
    return action.execute().getResultObject();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) Resources(org.jetbrains.android.dom.resources.Resources)

Example 3 with Resources

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

the class AndroidResourceUtil method addValueResource.

private static boolean addValueResource(@NotNull Project project, @NotNull VirtualFile resDir, @NotNull final String resourceName, @NotNull final ResourceType resourceType, @NotNull String fileName, @NotNull List<String> dirNames, @Nullable final String resourceValue, @NotNull final Processor<ResourceElement> afterAddedProcessor) throws Exception {
    if (dirNames.size() == 0) {
        return false;
    }
    final VirtualFile[] resFiles = new VirtualFile[dirNames.size()];
    for (int i = 0, n = dirNames.size(); i < n; i++) {
        String dirName = dirNames.get(i);
        resFiles[i] = WriteAction.compute(() -> findOrCreateResourceFile(project, resDir, fileName, dirName));
        if (resFiles[i] == null) {
            return false;
        }
    }
    if (!ReadonlyStatusHandler.ensureFilesWritable(project, resFiles)) {
        return false;
    }
    final Resources[] resourcesElements = new Resources[resFiles.length];
    for (int i = 0; i < resFiles.length; i++) {
        final Resources resources = AndroidUtils.loadDomElement(project, resFiles[i], Resources.class);
        if (resources == null) {
            AndroidUtils.reportError(project, AndroidBundle.message("not.resource.file.error", fileName));
            return false;
        }
        resourcesElements[i] = resources;
    }
    List<PsiFile> psiFiles = Lists.newArrayListWithExpectedSize(resFiles.length);
    PsiManager manager = PsiManager.getInstance(project);
    for (VirtualFile file : resFiles) {
        PsiFile psiFile = manager.findFile(file);
        if (psiFile != null) {
            psiFiles.add(psiFile);
        }
    }
    PsiFile[] files = psiFiles.toArray(new PsiFile[psiFiles.size()]);
    WriteCommandAction<Void> action = new WriteCommandAction<Void>(project, "Add Resource", files) {

        @Override
        protected void run(@NotNull Result<Void> result) {
            for (Resources resources : resourcesElements) {
                final ResourceElement element = addValueResource(resourceType, resources, resourceValue);
                element.getName().setValue(resourceName);
                afterAddedProcessor.process(element);
            }
        }
    };
    action.execute();
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) Resources(org.jetbrains.android.dom.resources.Resources)

Example 4 with Resources

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

the class ResourceManager method getValueResources.

protected List<ResourceElement> getValueResources(@NotNull final ResourceType resourceType, @Nullable Set<VirtualFile> files) {
    final List<ResourceElement> result = new ArrayList<ResourceElement>();
    List<Pair<Resources, VirtualFile>> resourceFiles = getResourceElements(files);
    for (final Pair<Resources, VirtualFile> pair : resourceFiles) {
        final Resources resources = pair.getFirst();
        ApplicationManager.getApplication().runReadAction(new Runnable() {

            @Override
            public void run() {
                if (!resources.isValid() || myProject.isDisposed()) {
                    return;
                }
                final List<ResourceElement> valueResources = AndroidResourceUtil.getValueResourcesFromElement(resourceType, resources);
                for (ResourceElement valueResource : valueResources) {
                    final String resName = valueResource.getName().getValue();
                    if (resName != null && isResourcePublic(resourceType.getName(), resName)) {
                        result.add(valueResource);
                    }
                }
            }
        });
    }
    return result;
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Resources(org.jetbrains.android.dom.resources.Resources) Pair(com.intellij.openapi.util.Pair)

Example 5 with Resources

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

the class CreateXmlResourceDialog method checkIfResourceAlreadyExists.

@Nullable
public static ValidationInfo checkIfResourceAlreadyExists(@NotNull Project project, @NotNull VirtualFile resourceDir, @NotNull String resourceName, @NotNull ResourceType resourceType, @NotNull List<String> dirNames, @NotNull String fileName) {
    if (resourceName.length() == 0 || dirNames.size() == 0 || fileName.length() == 0) {
        return null;
    }
    for (String directoryName : dirNames) {
        final VirtualFile resourceSubdir = resourceDir.findChild(directoryName);
        if (resourceSubdir == null) {
            continue;
        }
        final VirtualFile resFile = resourceSubdir.findChild(fileName);
        if (resFile == null) {
            continue;
        }
        if (resFile.getFileType() != StdFileTypes.XML) {
            return new ValidationInfo("File " + FileUtil.toSystemDependentName(resFile.getPath()) + " is not XML file");
        }
        final Resources resources = AndroidUtils.loadDomElement(project, resFile, Resources.class);
        if (resources == null) {
            return new ValidationInfo(AndroidBundle.message("not.resource.file.error", FileUtil.toSystemDependentName(resFile.getPath())));
        }
        for (ResourceElement element : AndroidResourceUtil.getValueResourcesFromElement(resourceType, resources)) {
            if (resourceName.equals(element.getName().getValue())) {
                return new ValidationInfo("resource '" + resourceName + "' already exists in " + FileUtil.toSystemDependentName(resFile.getPath()));
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) ValidationInfo(com.intellij.openapi.ui.ValidationInfo) Resources(org.jetbrains.android.dom.resources.Resources) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 Resources (org.jetbrains.android.dom.resources.Resources)6 ResourceElement (org.jetbrains.android.dom.resources.ResourceElement)4 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 NotNull (org.jetbrains.annotations.NotNull)2 StructureViewModel (com.intellij.ide.structureView.StructureViewModel)1 ValidationInfo (com.intellij.openapi.ui.ValidationInfo)1 Pair (com.intellij.openapi.util.Pair)1 PsiFile (com.intellij.psi.PsiFile)1 XmlFile (com.intellij.psi.xml.XmlFile)1 Attr (org.jetbrains.android.dom.resources.Attr)1 DeclareStyleable (org.jetbrains.android.dom.resources.DeclareStyleable)1 ResourceStructureViewBuilder (org.jetbrains.android.dom.structure.resources.ResourceStructureViewBuilder)1 Nullable (org.jetbrains.annotations.Nullable)1