Search in sources :

Example 1 with CreateXmlResourceDialog

use of org.jetbrains.android.actions.CreateXmlResourceDialog in project android by JetBrains.

the class AndroidAddStringResourceAction method doInvoke.

protected static void doInvoke(Project project, Editor editor, PsiFile file, @Nullable String resName, PsiElement element, ResourceType type) {
    String value = getStringLiteralValue(project, element, file, type);
    assert value != null;
    final AndroidFacet facet = AndroidFacet.getInstance(file);
    assert facet != null;
    final String aPackage = getPackage(facet);
    if (aPackage == null) {
        Messages.showErrorDialog(project, AndroidBundle.message("package.not.found.error"), CommonBundle.getErrorTitle());
        return;
    }
    if (resName != null && ApplicationManager.getApplication().isUnitTestMode()) {
        String fileName = AndroidResourceUtil.getDefaultResourceFileName(type);
        assert fileName != null;
        VirtualFile resourceDir = facet.getPrimaryResourceDir();
        assert resourceDir != null;
        AndroidResourceUtil.createValueResource(project, resourceDir, resName, type, fileName, Collections.singletonList(ResourceFolderType.VALUES.getName()), value);
    } else {
        Module facetModule = facet.getModule();
        final CreateXmlResourceDialog dialog = new CreateXmlResourceDialog(facetModule, type, resName, value, true, null, file.getVirtualFile());
        dialog.setTitle("Extract Resource");
        if (!dialog.showAndGet()) {
            return;
        }
        VirtualFile resourceDir = dialog.getResourceDirectory();
        if (resourceDir == null) {
            AndroidUtils.reportError(project, AndroidBundle.message("check.resource.dir.error", facetModule));
            return;
        }
        resName = dialog.getResourceName();
        if (!AndroidResourceUtil.createValueResource(project, resourceDir, resName, type, dialog.getFileName(), dialog.getDirNames(), dialog.getValue())) {
            return;
        }
    }
    doAddStringResource(editor, file, resName, element, type, facet, aPackage);
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    UndoUtil.markPsiFileForUndo(file);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Module(com.intellij.openapi.module.Module) CreateXmlResourceDialog(org.jetbrains.android.actions.CreateXmlResourceDialog) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 2 with CreateXmlResourceDialog

use of org.jetbrains.android.actions.CreateXmlResourceDialog in project android by JetBrains.

the class ChooseResourceDialog method createNewResourceValue.

private void createNewResourceValue(ResourceType resourceType) {
    ensurePickersInitialized();
    if (resourceType == ResourceType.COLOR && myColorPickerPanel != null) {
        getSelectedPanel().showNewResource(myColorPickerPanel);
        return;
    }
    CreateXmlResourceDialog dialog = new CreateXmlResourceDialog(myModule, resourceType, null, null, true, null, null);
    dialog.setTitle("New " + StringUtil.capitalize(resourceType.getDisplayName()) + " Value Resource");
    if (!dialog.showAndGet()) {
        return;
    }
    Project project = myModule.getProject();
    final VirtualFile resDir = dialog.getResourceDirectory();
    if (resDir == null) {
        AndroidUtils.reportError(project, AndroidBundle.message("check.resource.dir.error", myModule));
        return;
    }
    String fileName = dialog.getFileName();
    List<String> dirNames = dialog.getDirNames();
    String resValue = dialog.getValue();
    String resName = dialog.getResourceName();
    if (!AndroidResourceUtil.createValueResource(project, resDir, resName, resourceType, fileName, dirNames, resValue)) {
        return;
    }
    PsiDocumentManager.getInstance(myModule.getProject()).commitAllDocuments();
    myResultResourceName = "@" + resourceType.getName() + "/" + resName;
    close(OK_EXIT_CODE);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) CreateXmlResourceDialog(org.jetbrains.android.actions.CreateXmlResourceDialog)

Example 3 with CreateXmlResourceDialog

use of org.jetbrains.android.actions.CreateXmlResourceDialog in project android by JetBrains.

the class CreateValueResourceQuickFix method doInvoke.

protected boolean doInvoke() {
    Project project = myFile.getProject();
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        final String fileName = AndroidResourceUtil.getDefaultResourceFileName(myResourceType);
        assert fileName != null;
        VirtualFile resourceDir = myFacet.getPrimaryResourceDir();
        assert resourceDir != null;
        if (!AndroidResourceUtil.createValueResource(project, resourceDir, myResourceName, myResourceType, fileName, Collections.singletonList(SdkConstants.FD_RES_VALUES), "a")) {
            return false;
        }
    } else {
        final String value = myResourceType == ResourceType.STYLEABLE || myResourceType == ResourceType.ATTR ? "\n" : null;
        VirtualFile defaultFileToCreate = null;
        if (myFile instanceof XmlFile && myFile.isWritable() && myFile.getManager().isInProject(myFile)) {
            final DomFileDescription<?> description = DomManager.getDomManager(project).getDomFileDescription((XmlFile) myFile);
            if (description instanceof ResourcesDomFileDescription) {
                final VirtualFile defaultFile = myFile.getVirtualFile();
                if (defaultFile != null) {
                    defaultFileToCreate = defaultFile;
                }
            }
        }
        final CreateXmlResourceDialog dialog = new CreateXmlResourceDialog(myFacet.getModule(), myResourceType, myResourceName, value, myChooseName, defaultFileToCreate, myFile.getVirtualFile());
        dialog.setTitle("New " + StringUtil.capitalize(myResourceType.getDisplayName()) + " Value Resource");
        if (!dialog.showAndGet()) {
            return false;
        }
        final VirtualFile resourceDir = dialog.getResourceDirectory();
        if (resourceDir == null) {
            AndroidUtils.reportError(project, AndroidBundle.message("check.resource.dir.error", myFacet.getModule()));
            return false;
        }
        final String fileName = dialog.getFileName();
        final List<String> dirNames = dialog.getDirNames();
        final String resValue = dialog.getValue();
        final String resName = dialog.getResourceName();
        if (!AndroidResourceUtil.createValueResource(project, resourceDir, resName, myResourceType, fileName, dirNames, resValue)) {
            return false;
        }
    }
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    UndoUtil.markPsiFileForUndo(myFile);
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) CreateXmlResourceDialog(org.jetbrains.android.actions.CreateXmlResourceDialog) ResourcesDomFileDescription(org.jetbrains.android.dom.resources.ResourcesDomFileDescription)

Aggregations

VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 CreateXmlResourceDialog (org.jetbrains.android.actions.CreateXmlResourceDialog)3 Project (com.intellij.openapi.project.Project)2 Module (com.intellij.openapi.module.Module)1 XmlFile (com.intellij.psi.xml.XmlFile)1 ResourcesDomFileDescription (org.jetbrains.android.dom.resources.ResourcesDomFileDescription)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1