1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.sdkuilib.internal.widgets; 18 19 import com.android.sdklib.ISdkLog; 20 21 import org.eclipse.jface.dialogs.MessageDialog; 22 import org.eclipse.swt.widgets.Display; 23 import org.eclipse.swt.widgets.Shell; 24 25 import java.util.ArrayList; 26 27 28 /** 29 * Collects all log and displays it in a message box dialog. 30 * <p/> 31 * This is good if only a few lines of log are expected. 32 * If you pass <var>logErrorsOnly</var> to the constructor, the message box 33 * will be shown only if errors were generated, which is the typical use-case. 34 * <p/> 35 * To use this: </br> 36 * - Construct a new {@link MessageBoxLog}. </br> 37 * - Pass the logger to the action. </br> 38 * - Once the action is completed, call {@link #displayResult(boolean)} 39 * indicating whether the operation was successful or not. 40 * 41 * When <var>logErrorsOnly</var> is true, if the operation was not successful or errors 42 * were generated, this will display the message box. 43 */ 44 public final class MessageBoxLog implements ISdkLog { 45 46 final ArrayList<String> logMessages = new ArrayList<String>(); 47 private final String mMessage; 48 private final Display mDisplay; 49 private final boolean mLogErrorsOnly; 50 51 /** 52 * Creates a logger that will capture all logs and eventually display them 53 * in a simple message box. 54 * 55 * @param message 56 * @param display 57 * @param logErrorsOnly 58 */ 59 public MessageBoxLog(String message, Display display, boolean logErrorsOnly) { 60 mMessage = message; 61 mDisplay = display; 62 mLogErrorsOnly = logErrorsOnly; 63 } 64 65 public void error(Throwable throwable, String errorFormat, Object... arg) { 66 if (errorFormat != null) { 67 logMessages.add(String.format("Error: " + errorFormat, arg)); 68 } 69 70 if (throwable != null) { 71 logMessages.add(throwable.getMessage()); 72 } 73 } 74 75 public void warning(String warningFormat, Object... arg) { 76 if (!mLogErrorsOnly) { 77 logMessages.add(String.format("Warning: " + warningFormat, arg)); 78 } 79 } 80 81 public void printf(String msgFormat, Object... arg) { 82 if (!mLogErrorsOnly) { 83 logMessages.add(String.format(msgFormat, arg)); 84 } 85 } 86 87 /** 88 * Displays the log if anything was captured. 89 * <p/> 90 * @param success Used only when the logger was constructed with <var>logErrorsOnly</var>==true. 91 * In this case the dialog will only be shown either if success if false or some errors 92 * where captured. 93 */ 94 public void displayResult(final boolean success) { 95 if (logMessages.size() > 0) { 96 final StringBuilder sb = new StringBuilder(mMessage + "\n\n"); 97 for (String msg : logMessages) { 98 if (msg.length() > 0) { 99 if (msg.charAt(0) != '\n') { 100 int n = sb.length(); 101 if (n > 0 && sb.charAt(n-1) != '\n') { 102 sb.append('\n'); 103 } 104 } 105 sb.append(msg); 106 } 107 } 108 109 // display the message 110 // dialog box only run in ui thread.. 111 if (mDisplay != null && !mDisplay.isDisposed()) { 112 mDisplay.asyncExec(new Runnable() { 113 public void run() { 114 // This is typically displayed at the end, so make sure the UI 115 // instances are not disposed. 116 Shell shell = null; 117 if (mDisplay != null && !mDisplay.isDisposed()) { 118 shell = mDisplay.getActiveShell(); 119 } 120 if (shell == null || shell.isDisposed()) { 121 return; 122 } 123 // Use the success icon if the call indicates success. 124 // However just use the error icon if the logger was only recording errors. 125 if (success && !mLogErrorsOnly) { 126 MessageDialog.openInformation(shell, "Android Virtual Devices Manager", 127 sb.toString()); 128 } else { 129 MessageDialog.openError(shell, "Android Virtual Devices Manager", 130 sb.toString()); 131 132 } 133 } 134 }); 135 } 136 } 137 } 138 } 139