• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      */
MessageBoxLog(String message, Display display, boolean logErrorsOnly)59     public MessageBoxLog(String message, Display display, boolean logErrorsOnly) {
60         mMessage = message;
61         mDisplay = display;
62         mLogErrorsOnly = logErrorsOnly;
63     }
64 
error(Throwable throwable, String errorFormat, Object... arg)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 
warning(String warningFormat, Object... arg)75     public void warning(String warningFormat, Object... arg) {
76         if (!mLogErrorsOnly) {
77             logMessages.add(String.format("Warning: " + warningFormat, arg));
78         }
79     }
80 
printf(String msgFormat, Object... arg)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      */
displayResult(final boolean success)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                 sb.append(msg);
99             }
100 
101             // display the message
102             // dialog box only run in ui thread..
103             mDisplay.asyncExec(new Runnable() {
104                 public void run() {
105                     Shell shell = mDisplay.getActiveShell();
106                     // Use the success icon if the call indicates success.
107                     // However just use the error icon if the logger was only recording errors.
108                     if (success && !mLogErrorsOnly) {
109                         MessageDialog.openInformation(shell, "Android Virtual Devices Manager",
110                                 sb.toString());
111                     } else {
112                         MessageDialog.openError(shell, "Android Virtual Devices Manager",
113                                     sb.toString());
114 
115                     }
116                 }
117             });
118         }
119     }
120 }
121