From ef2f55d0b70edcddeeeac7ad7cab0c8f115c0800 Mon Sep 17 00:00:00 2001 From: Xavier Ducrohet Date: Thu, 18 Jun 2009 16:01:05 -0700 Subject: [PATCH] Add support to delete AVD in the new AvdSelector. Also: change button order in the update chooser dialog. --- .../avdmanager/AvdManagerListPage.java | 29 ----- .../sdklib/internal/avd/AvdManager.java | 8 +- .../repository/UpdateChooserDialog.java | 10 +- .../internal/widgets/AvdSelector.java | 117 +++++++++++++++++- 4 files changed, 129 insertions(+), 35 deletions(-) diff --git a/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/avdmanager/AvdManagerListPage.java b/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/avdmanager/AvdManagerListPage.java index 9708a0f39..5867f7e95 100755 --- a/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/avdmanager/AvdManagerListPage.java +++ b/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/avdmanager/AvdManagerListPage.java @@ -433,35 +433,6 @@ class AvdManagerListPage extends WizardPage { } } - /** - * Triggered when the user selects the "delete" button (the extra action in the selector) - * Deletes the currently selected AVD, if any. - * - * This is obsolete. Kept around to reuse the code later in the AvdSelector itself. - */ - @Deprecated - private void onDelete() { - AvdInfo avdInfo = mAvdSelector.getSelected(); - AvdManager avdm = getAvdManager(); - if (avdInfo == null || avdm == null) { - return; - } - - // Confirm you want to delete this AVD - if (!AdtPlugin.displayPrompt("Delete Android Virtual Device", - String.format("Please confirm that you want to delete the Android Virtual Device named '%s'. This operation cannot be reverted.", - avdInfo.getName()))) { - return; - } - - SdkLog log = new SdkLog(String.format("Result of deleting AVD '%s':", avdInfo.getName())); - - boolean success = avdm.deleteAvd(avdInfo, log); - - log.display(success); - reloadAvdList(); - } - /** * Triggered when the user selects the "create" button. */ diff --git a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java index b211a7ae9..5179a0d94 100644 --- a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java +++ b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java @@ -783,7 +783,7 @@ public final class AvdManager { File f = avdInfo.getIniFile(); if (f != null && f.exists()) { - log.warning("Deleting file %1$s", f.getCanonicalPath()); + log.printf("Deleting file %1$s", f.getCanonicalPath()); if (!f.delete()) { log.error(null, "Failed to delete %1$s", f.getCanonicalPath()); error = true; @@ -794,7 +794,7 @@ public final class AvdManager { if (path != null) { f = new File(path); if (f.exists()) { - log.warning("Deleting folder %1$s", f.getCanonicalPath()); + log.printf("Deleting folder %1$s", f.getCanonicalPath()); recursiveDelete(f); if (!f.delete()) { log.error(null, "Failed to delete %1$s", f.getCanonicalPath()); @@ -806,10 +806,10 @@ public final class AvdManager { removeAvd(avdInfo); if (error) { - log.printf("AVD '%1$s' deleted with errors. See warnings above.\n", + log.printf("\nAVD '%1$s' deleted with errors. See errors above.", avdInfo.getName()); } else { - log.printf("AVD '%1$s' deleted.\n", avdInfo.getName()); + log.printf("\nAVD '%1$s' deleted.", avdInfo.getName()); return true; } diff --git a/tools/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/UpdateChooserDialog.java b/tools/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/UpdateChooserDialog.java index 8b9a417c1..14c8bbca8 100755 --- a/tools/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/UpdateChooserDialog.java +++ b/tools/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/UpdateChooserDialog.java @@ -245,6 +245,11 @@ final class UpdateChooserDialog extends Dialog { placeholder = new Label(mDialogShell, SWT.NONE); placeholder.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1)); + // for MacOS, the Cancel button should be left. + if (SdkConstants.currentPlatform() == SdkConstants.PLATFORM_DARWIN) { + mCancelButton = new Button(mDialogShell, SWT.PUSH); + } + mInstallButton = new Button(mDialogShell, SWT.PUSH); mInstallButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); mInstallButton.setText("Install Accepted"); @@ -255,7 +260,10 @@ final class UpdateChooserDialog extends Dialog { } }); - mCancelButton = new Button(mDialogShell, SWT.PUSH); + // if we haven't created the cancel button yet (macos?), create it now. + if (mCancelButton == null) { + mCancelButton = new Button(mDialogShell, SWT.PUSH); + } mCancelButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); mCancelButton.setText("Cancel"); mCancelButton.addSelectionListener(new SelectionAdapter() { diff --git a/tools/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdSelector.java b/tools/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdSelector.java index 80fdb1ea2..37dada9a0 100644 --- a/tools/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdSelector.java +++ b/tools/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdSelector.java @@ -18,11 +18,13 @@ package com.android.sdkuilib.internal.widgets; import com.android.prefs.AndroidLocation.AndroidLocationException; import com.android.sdklib.IAndroidTarget; +import com.android.sdklib.ISdkLog; import com.android.sdklib.internal.avd.AvdManager; import com.android.sdklib.internal.avd.AvdManager.AvdInfo; import com.android.sdklib.internal.avd.AvdManager.AvdInfo.AvdStatus; import com.android.sdkuilib.internal.repository.icons.ImageFactory; +import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ControlAdapter; import org.eclipse.swt.events.ControlEvent; @@ -37,11 +39,15 @@ import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; +import java.util.ArrayList; + /** * The AVD selector is a table that is added to the given parent composite. @@ -195,7 +201,12 @@ public final class AvdSelector { Button deleteButton = new Button(buttons, SWT.PUSH | SWT.FLAT); deleteButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); deleteButton.setText("Delete"); - // TODO: callback for button + deleteButton.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent arg0) { + onDelete(); + } + }); Label l = new Label(buttons, SWT.SEPARATOR | SWT.HORIZONTAL); l.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); @@ -610,4 +621,108 @@ public final class AvdSelector { item.setText(3, "--"); } } + + private void onDelete() { + final AvdInfo avdInfo = getSelected(); + + // get the current Display + final Display display = mTable.getDisplay(); + + // Confirm you want to delete this AVD + final boolean[] result = new boolean[1]; + display.syncExec(new Runnable() { + public void run() { + Shell shell = display.getActiveShell(); + result[0] = MessageDialog.openQuestion(shell, + "Delete Android Virtual Device", + String.format( + "Please confirm that you want to delete the Android Virtual Device named '%s'. This operation cannot be reverted.", + avdInfo.getName())); + } + }); + + if (result[0] == false) { + return; + } + + // log for this action. + SdkLog log = new SdkLog( + String.format("Result of deleting AVD '%s':", avdInfo.getName()), + display); + + // delete the AVD + boolean success = mManager.deleteAvd(avdInfo, log); + + // display the result + log.displayResult(success); + + if (success) { + refresh(false); + } + } + + + /** + * Collects all log from the AVD action and displays it in a dialog. + */ + private class SdkLog implements ISdkLog { + + final ArrayList logMessages = new ArrayList(); + private final String mMessage; + private final Display mDisplay; + + public SdkLog(String message, Display display) { + mMessage = message; + mDisplay = display; + } + + public void error(Throwable throwable, String errorFormat, Object... arg) { + if (errorFormat != null) { + logMessages.add(String.format("Error: " + errorFormat, arg)); + } + + if (throwable != null) { + logMessages.add(throwable.getMessage()); + } + } + + public void warning(String warningFormat, Object... arg) { + logMessages.add(String.format("Warning: " + warningFormat, arg)); + } + + public void printf(String msgFormat, Object... arg) { + logMessages.add(String.format(msgFormat, arg)); + } + + /** + * Displays the log if anything was captured. + */ + public void displayResult(final boolean success) { + if (logMessages.size() > 0) { + final StringBuilder sb = new StringBuilder(mMessage + "\n"); + for (String msg : logMessages) { + sb.append('\n'); + sb.append(msg); + } + + // display the message + // dialog box only run in ui thread.. + mDisplay.asyncExec(new Runnable() { + public void run() { + Shell shell = mDisplay.getActiveShell(); + if (success) { + MessageDialog.openInformation(shell, "Android Virtual Devices Manager", + sb.toString()); + } else { + MessageDialog.openError(shell, "Android Virtual Devices Manager", + sb.toString()); + + } + } + }); + } + } + } + + }