Example usage for com.intellij.openapi.ui DialogBuilder showAndGet

List of usage examples for com.intellij.openapi.ui DialogBuilder showAndGet

Introduction

In this page you can find the example usage for com.intellij.openapi.ui DialogBuilder showAndGet.

Prototype

public boolean showAndGet() 

Source Link

Usage

From source file:consulo.devkit.module.library.ConsuloPluginLibraryType.java

License:Apache License

@Nullable
@Override/*from  w w  w  .  java2s . co m*/
public NewLibraryConfiguration createNewLibrary(@NotNull JComponent jComponent,
        @Nullable VirtualFile virtualFile, @NotNull Project project) {
    DialogBuilder builder = new DialogBuilder();
    builder.title("Enter Plugin ID");
    ChoosePluginPanel centerPanel = new ChoosePluginPanel(project);
    builder.centerPanel(centerPanel);
    builder.setPreferredFocusComponent(centerPanel.getTextField());
    builder.dimensionKey("ConsuloPluginLibraryType#ChoosePluginPanel");

    if (!builder.showAndGet()) {
        return null;
    }

    IdeaPluginDescriptor pluginDescriptor = centerPanel.getPluginDescriptor();
    if (pluginDescriptor == null) {
        Messages.showErrorDialog(project, "Plugin is not found",
                ApplicationInfo.getInstance().getFullVersion());
        return null;
    }

    Ref<VirtualFile> libRef = Ref.create();

    String pluginId = pluginDescriptor.getPluginId().getIdString();

    new Task.Modal(project, "Downloading plugin: " + pluginDescriptor.getPluginId().getIdString(), false) {
        @Override
        public void run(@NotNull ProgressIndicator progressIndicator) {
            String url = RepositoryHelper.buildUrlForDownload(UpdateSettings.getInstance().getChannel(),
                    pluginId, null, true, false);

            try {
                File tempFile = FileUtil.createTempFile("download", "zip");

                HttpRequests.request(url).saveToFile(tempFile, progressIndicator);

                File depDir = new File(project.getBasePath(), DEP_LIBRARY);
                depDir.mkdir();

                ZipUtil.extract(tempFile, depDir, null);

                tempFile.delete();

                libRef.set(LocalFileSystem.getInstance()
                        .refreshAndFindFileByIoFile(new File(depDir, pluginId + "/lib")));
            } catch (IOException e) {
                //
            }
        }
    }.queue();

    VirtualFile libDirectory = libRef.get();
    if (libDirectory == null) {
        Messages.showErrorDialog(project, "Plugin directory is not found",
                ApplicationInfo.getInstance().getFullVersion());
        return null;
    }

    return new NewLibraryConfiguration(LIBRARY_PREFIX + pluginId, this, DummyLibraryProperties.INSTANCE) {
        @Override
        public void addRoots(@NotNull LibraryEditor libraryEditor) {
            for (VirtualFile file : libDirectory.getChildren()) {
                if (file.isDirectory()) {
                    continue;
                }
                VirtualFile localVirtualFileByPath = ArchiveVfsUtil.getArchiveRootForLocalFile(file);
                if (localVirtualFileByPath == null) {
                    continue;
                }
                libraryEditor.addRoot(localVirtualFileByPath, BinariesOrderRootType.getInstance());
            }
        }
    };
}