Example usage for com.intellij.openapi.ui Messages showErrorDialog

List of usage examples for com.intellij.openapi.ui Messages showErrorDialog

Introduction

In this page you can find the example usage for com.intellij.openapi.ui Messages showErrorDialog.

Prototype

public static void showErrorDialog(@Nullable Component component, String message,
            @NotNull @Nls(capitalization = Nls.Capitalization.Title) String title) 

Source Link

Usage

From source file:com.android.tools.idea.gradle.service.notification.hyperlink.FixGradleVersionInWrapperHyperlink.java

License:Apache License

static boolean updateGradleVersion(@NotNull Project project, @NotNull File wrapperPropertiesFile,
        @NotNull String gradleVersion) {
    try {//from   w  w w .j a v  a2 s.  co  m
        boolean updated = GradleUtil.updateGradleDistributionUrl(gradleVersion, wrapperPropertiesFile);
        if (updated) {
            VirtualFile virtualFile = VfsUtil.findFileByIoFile(wrapperPropertiesFile, true);
            if (virtualFile != null) {
                virtualFile.refresh(false, false);
            }
            return true;
        }
    } catch (IOException e) {
        String msg = String.format("Unable to update Gradle wrapper to use Gradle %1$s\n", gradleVersion);
        msg += e.getMessage();
        Messages.showErrorDialog(project, msg, ERROR_MSG_TITLE);
    }
    return false;
}

From source file:com.android.tools.idea.gradle.service.notification.SyncProjectHyperlink.java

License:Apache License

@Override
protected void execute(@NotNull Project project) {
    try {//  w ww  .jav a 2 s  .  c  o  m
        GradleProjectImporter.getInstance().reImportProject(project, null);
    } catch (ConfigurationException e) {
        Messages.showErrorDialog(project, e.getMessage(), e.getTitle());
    }
}

From source file:com.android.tools.idea.gradle.structure.IdeSdksConfigurable.java

License:Apache License

private void saveAndroidNdkPath() {
    if (myProject == null || myProject.isDefault()) {
        return;//from  www . j  a  v  a2 s  .  c  o m
    }

    try {
        LocalProperties localProperties = new LocalProperties(myProject);
        localProperties.setAndroidNdkPath(getNdkLocation());
        localProperties.save();
    } catch (IOException e) {
        LOG.info(
                String.format("Unable to update local.properties file in project '%1$s'.", myProject.getName()),
                e);
        String cause = e.getMessage();
        if (isNullOrEmpty(cause)) {
            cause = "[Unknown]";
        }
        String msg = String.format(
                "Unable to update local.properties file in project '%1$s'.\n\n" + "Cause: %2$s\n\n"
                        + "Please manually update the file's '%3$s' property value to \n" + "'%4$s'\n"
                        + "and sync the project with Gradle files.",
                myProject.getName(), cause, NDK_DIR_PROPERTY, getNdkLocation().getPath());
        Messages.showErrorDialog(myProject, msg, "Android Ndk Update");
    }
}

From source file:com.android.tools.idea.gradle.util.GradleWrapper.java

License:Apache License

/**
 * Updates the 'distributionUrl' in the Gradle wrapper properties file. An unexpected errors that occur while updating the file will be
 * displayed in an error dialog.//  w w  w .  ja v  a  2  s .co  m
 *
 * @param gradleVersion  the Gradle version to update the property to.
 * @return {@code true} if the property was updated, or {@code false} if no update was necessary because the property already had the
 * correct value.
 */
public boolean updateDistributionUrlAndDisplayFailure(@NotNull String gradleVersion) {
    try {
        boolean updated = updateDistributionUrl(gradleVersion);
        if (updated) {
            VirtualFile virtualFile = findFileByIoFile(myPropertiesFilePath, true);
            if (virtualFile != null) {
                virtualFile.refresh(false, false);
            }
            return true;
        }
    } catch (IOException e) {
        String msg = String.format("Unable to update Gradle wrapper to use Gradle %1$s\n", gradleVersion);
        msg += e.getMessage();
        Messages.showErrorDialog(myProject, msg, "Unexpected Error");
    }
    return false;
}

From source file:com.android.tools.idea.lint.GenerateBackupDescriptorFix.java

License:Apache License

@Override
public void apply(@NotNull final PsiElement startElement, @NotNull PsiElement endElement,
        @NotNull AndroidQuickfixContexts.Context context) {
    final Project project = startElement.getProject();
    final AndroidFacet facet = AndroidFacet.getInstance(startElement);
    if (facet == null) {
        return;// www .  ja va 2 s  .c o  m
    }
    // Find all classes that extend the SQLiteOpenHelper
    GlobalSearchScope allScope = GlobalSearchScope.allScope(project);
    GlobalSearchScope useScope = GlobalSearchScope.projectScope(project);

    // All necessary PsiClassType's
    PsiClassType stringType = PsiType.getJavaLangString(PsiManager.getInstance(project), allScope);
    JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
    PsiClass psiOpenHelperClass = javaPsiFacade.findClass("android.database.sqlite.SQLiteOpenHelper", allScope);
    assert psiOpenHelperClass != null;
    PsiClass psiContext = javaPsiFacade.findClass(CLASS_CONTEXT, allScope);
    assert psiContext != null;

    final Set<String> databaseNames = findDatabasesInProject(useScope, psiOpenHelperClass, stringType,
            javaPsiFacade);
    final Set<String> sharedPreferenceFiles = findSharedPrefsInProject(useScope, psiContext, facet, stringType,
            javaPsiFacade);

    WriteCommandAction.runWriteCommandAction(project, "Create Backup Descriptor", null, () -> {
        try {
            @SuppressWarnings("deprecation")
            VirtualFile primaryResourceDir = facet.getPrimaryResourceDir();
            assert primaryResourceDir != null;
            VirtualFile xmlDir = createChildDirectoryIfNotExist(project, primaryResourceDir, FD_RES_XML);
            VirtualFile resFile = xmlDir.createChildData(project, myUrl.name + DOT_XML);
            VfsUtil.saveText(resFile, generateBackupDescriptorContents(databaseNames, sharedPreferenceFiles));
            TemplateUtils.reformatAndRearrange(project, resFile);
            TemplateUtils.openEditor(project, resFile);
            TemplateUtils.selectEditor(project, resFile);
        } catch (IOException e) {
            String error = String.format("Failed to create file: %1$s", e.getMessage());
            Messages.showErrorDialog(project, error, "Create Backup Resource");
        }
    });
}

From source file:com.android.tools.idea.lint.MoveToDebugManifestQuickFix.java

License:Apache License

@Override
public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement,
        @NotNull AndroidQuickfixContexts.Context context) {
    final XmlAttribute attribute = PsiTreeUtil.getParentOfType(startElement, XmlAttribute.class);
    if (attribute != null) {
        XmlTag parent = attribute.getParent();
        if (parent != null && parent.getName().equals(TAG_USES_PERMISSION)) {
            Module module = getModule(parent);

            assert MOCK_LOCATION_PERMISSION.equals(parent.getAttributeValue(ATTR_NAME, ANDROID_URI));
            parent.delete();//  w ww  . j  ava2 s  .  c o m

            if (module != null) {
                AndroidFacet facet = AndroidFacet.getInstance(module);
                if (facet != null) {
                    VirtualFile mainManifest = facet.getMainIdeaSourceProvider().getManifestFile();
                    // TODO: b/22928250
                    AndroidModuleModel androidModel = AndroidModuleModel.get(facet);
                    if (androidModel != null && mainManifest != null && mainManifest.getParent() != null
                            && mainManifest.getParent().getParent() != null) {
                        final VirtualFile src = mainManifest.getParent().getParent();
                        for (BuildTypeContainer container : androidModel.getAndroidProject().getBuildTypes()) {
                            BuildType buildType = container.getBuildType();
                            if (buildType.isDebuggable()) {
                                addManifest(module, src, buildType.getName());
                                return;
                            }
                        }
                        Messages.showErrorDialog(module.getProject(), "Did not find a debug build type",
                                "Move Permission");
                    }
                }
            }
        }
    }
}

From source file:com.android.tools.idea.lint.MoveToDebugManifestQuickFix.java

License:Apache License

private void addManifest(@NotNull final Module module, @NotNull final VirtualFile src,
        @NotNull final String buildTypeName) {
    final Project project = module.getProject();
    final VirtualFile manifest = src.findFileByRelativePath(buildTypeName + '/' + ANDROID_MANIFEST_XML);
    Pair<String, VirtualFile> result = ApplicationManager.getApplication()
            .runWriteAction(new Computable<Pair<String, VirtualFile>>() {
                @Override/*from   ww w .  j a  va  2  s.  co m*/
                public Pair<String, VirtualFile> compute() {
                    if (manifest == null) {
                        try {
                            VirtualFile newParentFolder = src.findChild(buildTypeName);
                            if (newParentFolder == null) {
                                newParentFolder = src.createChildDirectory(this, buildTypeName);
                                if (newParentFolder == null) {
                                    String message = String.format("Could not create folder %1$s in %2$s",
                                            buildTypeName, src.getPath());
                                    return Pair.of(message, null);
                                }
                            }

                            VirtualFile newFile = newParentFolder.createChildData(this, ANDROID_MANIFEST_XML);
                            // TODO: \r\n on Windows?
                            String text = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                                    + "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\">\n"
                                    + "    <uses-permission android:name=\"android.permission.ACCESS_MOCK_LOCATION\" />\n"
                                    + "</manifest>\n";
                            VfsUtil.saveText(newFile, text);
                            return Pair.of(null, newFile);
                        } catch (IOException e) {
                            String message = String.format("Failed to create file: %1$s", e.getMessage());
                            return Pair.of(message, null);
                        }
                    } else {
                        Document document = FileDocumentManager.getInstance().getDocument(manifest);
                        if (document != null) {
                            String text = document.getText();
                            int index = text.lastIndexOf("</manifest>");
                            if (index != -1) {
                                document.insertString(index,
                                        "    <uses-permission android:name=\"android.permission.ACCESS_MOCK_LOCATION\" />\n");
                                return Pair.of(null, manifest);
                            }
                        }

                        return Pair.of("Could not add to " + VfsUtilCore.virtualToIoFile(manifest), null);
                    }
                }
            });

    String error = result.getFirst();
    VirtualFile newFile = result.getSecond();
    if (error != null) {
        Messages.showErrorDialog(project, error, "Move Permission");
    } else {
        TemplateUtils.openEditor(project, newFile);
        TemplateUtils.selectEditor(project, newFile);
    }
}

From source file:com.android.tools.idea.logcat.AndroidLogcatService.java

License:Apache License

/**
 * Clears logs for the current device./*from   w w w.  jav  a2s  .  c  om*/
 */
public void clearLogcat(@NotNull IDevice device, @NotNull Project project) {
    // In theory, we only need to clear the buffer. However, due to issues in the platform, clearing logcat via "logcat -c" could
    // end up blocking the current logcat readers. As a result, we need to issue a restart of the logging to work around the platform bug.
    // See https://code.google.com/p/android/issues/detail?id=81164 and https://android-review.googlesource.com/#/c/119673
    // NOTE: We can avoid this and just clear the console if we ever decide to stop issuing a "logcat -c" to the device or if we are
    // confident that https://android-review.googlesource.com/#/c/119673 doesn't happen anymore.
    synchronized (myLock) {
        ExecutorService executor = myExecutors.get(device);
        // If someone keeps a reference to a device that is disconnected, executor will be null.
        if (executor != null) {
            stopReceiving(device);

            executor.submit(() -> {
                try {
                    AndroidUtils.executeCommandOnDevice(device, "logcat -c", new LoggingReceiver(getLog()),
                            false);
                } catch (final Exception e) {
                    getLog().info(e);
                    ApplicationManager.getApplication()
                            .invokeLater(() -> Messages.showErrorDialog(project, "Error: " + e.getMessage(),
                                    AndroidBundle.message("android.logcat.error.dialog.title")));
                }

                synchronized (myLock) {
                    if (myListeners.containsKey(device)) {
                        for (LogcatListener listener : myListeners.get(device)) {
                            listener.onCleared();
                        }
                    }
                }
            });

            startReceiving(device);
        }
    }
}

From source file:com.android.tools.idea.rendering.SaveScreenshotAction.java

License:Apache License

@Override
public void actionPerformed(AnActionEvent e) {
    Project project = e.getProject();//w w  w.j a v  a 2  s  .c om
    try {
        BufferedImage image = myContext.getRenderedImage();
        assert image != null && project != null; // enforced by update() above

        // We need to create a temp file since the image preview editor requires a real file
        File backingFile = FileUtil.createTempFile("screenshot", SdkConstants.DOT_PNG, true);
        ImageIO.write(image, SdkConstants.EXT_PNG, backingFile);

        ScreenshotViewer viewer = new ScreenshotViewer(project, image, backingFile, null);
        if (viewer.showAndGet()) {
            File screenshot = viewer.getScreenshot();
            VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(screenshot);
            if (vf != null) {
                FileEditorManager.getInstance(project).openFile(vf, true);
            }
        }
    } catch (Exception ex) {
        Messages.showErrorDialog(project, AndroidBundle.message("android.ddms.screenshot.generic.error", e),
                AndroidBundle.message("android.ddms.actions.screenshot"));
    }
}

From source file:com.android.tools.idea.run.editor.DeepLinkConfigurable.java

License:Apache License

public DeepLinkConfigurable(@NotNull final Project project,
        @NotNull final LaunchOptionConfigurableContext context) {
    myDeepLinkField.addActionListener(new ActionListener() {
        @Override//  w  w  w .j a  v a  2s . c o m
        public void actionPerformed(ActionEvent e) {
            if (!project.isInitialized()) {
                return;
            }
            Module module = context.getModule();
            if (module == null) {
                Messages.showErrorDialog(project, ExecutionBundle.message("module.not.specified.error.text"),
                        "Deep Link Launcher");
                return;
            }
            DeepLinkChooserDialog dialog = new DeepLinkChooserDialog(project, module);
            dialog.setTitle("Select URL");
            dialog.show();

            String deepLinkSelected = dialog.getSelectedDeepLink();
            if (deepLinkSelected != null && !deepLinkSelected.isEmpty()) {
                myDeepLinkField.getChildComponent().setText(deepLinkSelected);
            }
        }
    });
    myDeepLinkField.getChildComponent().getEmptyText().setText("Specify URL declared in the manifest");
}