Search in sources :

Example 26 with AzureTask

use of com.microsoft.azure.toolkit.lib.common.task.AzureTask in project azure-tools-for-java by Microsoft.

the class BlobExplorerFileEditor method downloadSelectedFile.

private void downloadSelectedFile(final File targetFile, final boolean open) {
    final BlobFile fileSelection = getFileSelection();
    if (fileSelection != null) {
        final AzureString title = AzureOperationBundle.title("blob.download", targetFile, blobContainer.getName());
        AzureTaskManager.getInstance().runInBackground(new AzureTask(project, title, false, () -> {
            final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
            try {
                progressIndicator.setIndeterminate(false);
                if (!targetFile.exists()) {
                    if (!targetFile.createNewFile()) {
                        throw new IOException("File not created");
                    }
                }
                final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetFile), 65536) {

                    private long runningCount = 0;

                    @Override
                    public synchronized void write(@NotNull byte[] bytes, int i, int i1) throws IOException {
                        super.write(bytes, i, i1);
                        runningCount += i1;
                        double progress = (double) runningCount / fileSelection.getSize();
                        progressIndicator.setFraction(progress);
                        progressIndicator.setText2(String.format("%s%% downloaded", (int) (progress * 100)));
                    }
                };
                try {
                    Future<?> future = ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                StorageClientSDKManager.getManager().downloadBlobFileContent(connectionString, fileSelection, bufferedOutputStream);
                                if (open && targetFile.exists()) {
                                    Desktop.getDesktop().open(targetFile);
                                }
                            } catch (AzureCmdException e) {
                                Throwable connectionFault = e.getCause().getCause();
                                progressIndicator.setText("Error downloading Blob");
                                progressIndicator.setText2((connectionFault instanceof SocketTimeoutException) ? "Connection timed out" : connectionFault.getMessage());
                            } catch (IOException ex) {
                                try {
                                    final Process p;
                                    Runtime runtime = Runtime.getRuntime();
                                    p = runtime.exec(new String[] { "open", "-R", targetFile.getName() }, null, targetFile.getParentFile());
                                    InputStream errorStream = p.getErrorStream();
                                    String errResponse = new String(IOUtils.readFully(errorStream, -1));
                                    if (p.waitFor() != 0) {
                                        throw new Exception(errResponse);
                                    }
                                } catch (Exception e) {
                                    progressIndicator.setText("Error openning file");
                                    progressIndicator.setText2(ex.getMessage());
                                }
                            }
                        }
                    });
                    while (!future.isDone()) {
                        progressIndicator.checkCanceled();
                        if (progressIndicator.isCanceled()) {
                            future.cancel(true);
                        }
                    }
                } finally {
                    bufferedOutputStream.close();
                }
            } catch (IOException e) {
                PluginUtil.displayErrorDialogAndLog(message("errTtl"), "An error occurred while attempting to download Blob.", e);
            }
        }));
    }
}
Also used : BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) SocketTimeoutException(java.net.SocketTimeoutException) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) SocketTimeoutException(java.net.SocketTimeoutException) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) Future(java.util.concurrent.Future) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask)

Example 27 with AzureTask

use of com.microsoft.azure.toolkit.lib.common.task.AzureTask in project azure-tools-for-java by Microsoft.

the class SubscriptionsDialog method createUIComponents.

private void createUIComponents() {
    contentPane = new JPanel();
    contentPane.setPreferredSize(new Dimension(350, 200));
    DefaultTableModel model = new SubscriptionTableModel();
    // Set the text read by JAWS
    model.addColumn("Selected");
    model.addColumn("Subscription name");
    model.addColumn("Subscription ID");
    table = new JBTable();
    table.setModel(model);
    TableColumn column = table.getColumnModel().getColumn(CHECKBOX_COLUMN);
    // Don't show title text
    column.setHeaderValue("");
    column.setMinWidth(23);
    column.setMaxWidth(23);
    JTableUtils.enableBatchSelection(table, CHECKBOX_COLUMN);
    table.getTableHeader().setReorderingAllowed(false);
    new TableSpeedSearch(table);
    AnActionButton refreshAction = new AnActionButton("Refresh", AllIcons.Actions.Refresh) {

        @Override
        public void actionPerformed(AnActionEvent anActionEvent) {
            this.setEnabled(false);
            model.setRowCount(0);
            model.fireTableDataChanged();
            table.getEmptyText().setText("Refreshing");
            AppInsightsClient.createByType(AppInsightsClient.EventType.Subscription, "", "Refresh", null);
            final AzureString title = AzureOperationBundle.title("account|subscription.refresh");
            final AzureTask task = new AzureTask(project, title, true, () -> {
                try {
                    SubscriptionsDialog.this.refreshSubscriptions();
                } finally {
                    this.setEnabled(true);
                }
            }, AzureTask.Modality.ANY);
            AzureTaskManager.getInstance().runInBackground(task);
        }
    };
    refreshAction.registerCustomShortcutSet(KeyEvent.VK_R, InputEvent.ALT_DOWN_MASK, contentPane);
    ToolbarDecorator tableToolbarDecorator = ToolbarDecorator.createDecorator(table).disableUpDownActions().addExtraActions(refreshAction);
    panelTable = tableToolbarDecorator.createPanel();
}
Also used : TableSpeedSearch(com.intellij.ui.TableSpeedSearch) DefaultTableModel(javax.swing.table.DefaultTableModel) ToolbarDecorator(com.intellij.ui.ToolbarDecorator) JBTable(com.intellij.ui.table.JBTable) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) TableColumn(javax.swing.table.TableColumn) AnActionButton(com.intellij.ui.AnActionButton) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString)

Example 28 with AzureTask

use of com.microsoft.azure.toolkit.lib.common.task.AzureTask in project azure-tools-for-java by Microsoft.

the class BlobExplorerFileEditor method deleteSelectedFile.

private void deleteSelectedFile() {
    final BlobFile blobItem = getFileSelection();
    if (blobItem != null) {
        boolean isConfirm = DefaultLoader.getUIHelper().showYesNoDialog(mainPanel, "Are you sure you want to " + "delete this blob?", "Delete Blob", null);
        if (isConfirm) {
            setUIState(true);
            final AzureString title = AzureOperationBundle.title("blob.delete", blobItem.getName());
            AzureTaskManager.getInstance().runInBackground(new AzureTask(project, title, false, () -> {
                final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
                progressIndicator.setIndeterminate(true);
                try {
                    StorageClientSDKManager.getManager().deleteBlobFile(connectionString, blobItem);
                    if (blobItems.size() <= 1) {
                        directoryQueue.clear();
                        directoryQueue.addLast(StorageClientSDKManager.getManager().getRootDirectory(connectionString, blobContainer));
                        queryTextField.setText("");
                    }
                    AzureTaskManager.getInstance().runLater(this::fillGrid);
                } catch (AzureCmdException ex) {
                    String msg = "An error occurred while attempting to delete blob." + "\n" + String.format(message("webappExpMsg"), ex.getMessage());
                    PluginUtil.displayErrorDialogAndLog(message("errTtl"), msg, ex);
                }
            }));
        }
    }
}
Also used : ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString)

Example 29 with AzureTask

use of com.microsoft.azure.toolkit.lib.common.task.AzureTask in project azure-tools-for-java by Microsoft.

the class IntellijAzureTaskManager method doRunInBackgroundableModal.

protected void doRunInBackgroundableModal(final Runnable runnable, final AzureTask<?> task) {
    final PerformInBackgroundOption foreground = PerformInBackgroundOption.DEAF;
    // refer https://jetbrains.org/intellij/sdk/docs/basics/disposers.html
    final Disposable disposable = Disposer.newDisposable();
    // refer https://github.com/JetBrains/intellij-community/commit/077c5558993b97cfb6f68ccc3cbe13065ba3cba8
    Registry.get("ide.background.tasks").setValue(false, disposable);
    final String title = StringUtils.capitalize(Objects.requireNonNull(task.getTitle()).toString());
    final Task.Backgroundable modalTask = new Task.Backgroundable((Project) task.getProject(), title, task.isCancellable(), foreground) {

        @Override
        public void run(@Nonnull final ProgressIndicator progressIndicator) {
            task.setMonitor(new IntellijTaskMonitor(progressIndicator));
            task.setBackgrounded(false);
            runnable.run();
            Disposer.dispose(disposable);
        }

        @Override
        public void processSentToBackground() {
            task.setBackgrounded(true);
        }
    };
    ProgressManager.getInstance().run(modalTask);
}
Also used : Disposable(com.intellij.openapi.Disposable) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) Task(com.intellij.openapi.progress.Task) Nonnull(javax.annotation.Nonnull) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) PerformInBackgroundOption(com.intellij.openapi.progress.PerformInBackgroundOption)

Example 30 with AzureTask

use of com.microsoft.azure.toolkit.lib.common.task.AzureTask in project azure-tools-for-java by Microsoft.

the class AzureSignInAction method loginNonDeviceCodeSingle.

private static Single<AuthMethodDetails> loginNonDeviceCodeSingle(AuthConfiguration auth) {
    final AzureString title = AzureOperationBundle.title("account.sign_in");
    final AzureTask<AuthMethodDetails> task = new AzureTask<>(null, title, true, () -> {
        final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
        indicator.setIndeterminate(true);
        return doLogin(indicator, auth);
    });
    return AzureTaskManager.getInstance().runInBackgroundAsObservable(task).toSingle();
}
Also used : AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString)

Aggregations

AzureTask (com.microsoft.azure.toolkit.lib.common.task.AzureTask)38 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)36 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)22 Project (com.intellij.openapi.project.Project)7 AzureCmdException (com.microsoft.azuretools.azurecommons.helpers.AzureCmdException)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 AzureOperationBundle (com.microsoft.azure.toolkit.lib.common.operation.AzureOperationBundle)5 AzureTaskManager (com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager)5 AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)4 EventUtil (com.microsoft.azuretools.telemetrywrapper.EventUtil)4 BlobFile (com.microsoft.tooling.msservices.model.storage.BlobFile)4 MessageType (com.intellij.openapi.ui.MessageType)3 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)3 AzureAccount (com.microsoft.azure.toolkit.lib.auth.AzureAccount)3 File (java.io.File)3 List (java.util.List)3 javax.swing (javax.swing)3 FileChooserDescriptorFactory (com.intellij.openapi.fileChooser.FileChooserDescriptorFactory)2 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)2 ProgressManager (com.intellij.openapi.progress.ProgressManager)2