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);
}
}));
}
}
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();
}
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);
}
}));
}
}
}
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);
}
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();
}
Aggregations