OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / sdk / sdkmanager / libs / sdkuilib / src / com / android / sdkuilib / internal / widgets / AvdDetailsDialog.java
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.AndroidVersion;
20 import com.android.sdklib.IAndroidTarget;
21 import com.android.sdklib.internal.avd.AvdManager;
22 import com.android.sdklib.internal.avd.AvdManager.AvdInfo;
23 import com.android.sdklib.internal.avd.AvdManager.AvdInfo.AvdStatus;
24
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.graphics.Rectangle;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Dialog;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Shell;
35
36 import java.util.HashMap;
37 import java.util.Map;
38 import java.util.Set;
39
40 /**
41  * Dialog displaying the details of an AVD.
42  */
43 final class AvdDetailsDialog extends Dialog {
44
45     /** Last dialog size for this session. */
46     private static Point sLastSize;
47
48     private Shell mDialogShell;
49     private final AvdInfo mAvdInfo;
50
51     private Composite mRootComposite;
52
53     public AvdDetailsDialog(Shell shell, AvdInfo avdInfo) {
54         super(shell, SWT.APPLICATION_MODAL);
55         mAvdInfo = avdInfo;
56
57         setText("AVD details");
58     }
59
60     /**
61      * Open the dialog and blocks till it gets closed
62      */
63     public void open() {
64         createContents();
65         positionShell();            //$hide$ (hide from SWT designer)
66         mDialogShell.open();
67         mDialogShell.layout();
68
69         Display display = getParent().getDisplay();
70         while (!mDialogShell.isDisposed()) {
71             if (!display.readAndDispatch()) {
72                 display.sleep();
73             }
74         }
75
76         if (!mDialogShell.isDisposed()) {
77             sLastSize = mDialogShell.getSize();
78             mDialogShell.close();
79         }
80     }
81
82     /**
83      * Create contents of the dialog.
84      */
85     private void createContents() {
86         mDialogShell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.RESIZE);
87         mDialogShell.setLayout(new GridLayout(1, false));
88         mDialogShell.setSize(450, 300);
89         mDialogShell.setText(getText());
90
91         mRootComposite = new Composite(mDialogShell, SWT.NONE);
92         mRootComposite.setLayout(new GridLayout(2, false));
93         mRootComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
94
95         GridLayout gl;
96
97         Composite c = new Composite(mRootComposite, SWT.NONE);
98         c.setLayout(gl = new GridLayout(2, false));
99         gl.marginHeight = gl.marginWidth = 0;
100         c.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
101
102         if (mAvdInfo != null) {
103             displayValue(c, "Name:", mAvdInfo.getName());
104             displayValue(c, "Path:", mAvdInfo.getPath());
105
106             if (mAvdInfo.getStatus() != AvdStatus.OK) {
107                 displayValue(c, "Error:", mAvdInfo.getErrorMessage());
108             } else {
109                 IAndroidTarget target = mAvdInfo.getTarget();
110                 AndroidVersion version = target.getVersion();
111                 displayValue(c, "Target:", String.format("%s (API level %s)",
112                         target.getName(), version.getApiString()));
113
114                 // display some extra values.
115                 Map<String, String> properties = mAvdInfo.getProperties();
116                 if (properties != null) {
117                     String skin = properties.get(AvdManager.AVD_INI_SKIN_NAME);
118                     if (skin != null) {
119                         displayValue(c, "Skin:", skin);
120                     }
121
122                     String sdcard = properties.get(AvdManager.AVD_INI_SDCARD_SIZE);
123                     if (sdcard == null) {
124                         sdcard = properties.get(AvdManager.AVD_INI_SDCARD_PATH);
125                     }
126                     if (sdcard != null) {
127                         displayValue(c, "SD Card:", sdcard);
128                     }
129
130                     // display other hardware
131                     HashMap<String, String> copy = new HashMap<String, String>(properties);
132                     // remove stuff we already displayed (or that we don't want to display)
133                     copy.remove(AvdManager.AVD_INI_SKIN_NAME);
134                     copy.remove(AvdManager.AVD_INI_SKIN_PATH);
135                     copy.remove(AvdManager.AVD_INI_SDCARD_SIZE);
136                     copy.remove(AvdManager.AVD_INI_SDCARD_PATH);
137                     copy.remove(AvdManager.AVD_INI_IMAGES_1);
138                     copy.remove(AvdManager.AVD_INI_IMAGES_2);
139
140                     if (copy.size() > 0) {
141                         Label l = new Label(mRootComposite, SWT.SEPARATOR | SWT.HORIZONTAL);
142                         l.setLayoutData(new GridData(
143                                 GridData.FILL, GridData.CENTER, false, false, 2, 1));
144
145                         c = new Composite(mRootComposite, SWT.NONE);
146                         c.setLayout(gl = new GridLayout(2, false));
147                         gl.marginHeight = gl.marginWidth = 0;
148                         c.setLayoutData(new GridData(GridData.FILL_BOTH));
149
150                         Set<String> keys = copy.keySet();
151                         for (String key : keys) {
152                             displayValue(c, key + ":", copy.get(key));
153                         }
154                     }
155                 }
156             }
157         }
158     }
159
160     // -- Start of internal part ----------
161     // Hide everything down-below from SWT designer
162     //$hide>>$
163
164     /**
165      * Displays a value with a label.
166      *
167      * @param parent the parent Composite in which to display the value. This Composite must use a
168      * {@link GridLayout} with 2 columns.
169      * @param label the label of the value to display.
170      * @param value the string value to display.
171      */
172     private void displayValue(Composite parent, String label, String value) {
173         Label l = new Label(parent, SWT.NONE);
174         l.setText(label);
175         l.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
176
177         l = new Label(parent, SWT.NONE);
178         l.setText(value);
179         l.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
180     }
181
182     /**
183      * Centers the dialog in its parent shell.
184      */
185     private void positionShell() {
186         // Centers the dialog in its parent shell
187         Shell child = mDialogShell;
188         Shell parent = getParent();
189         if (child != null && parent != null) {
190
191             // get the parent client area with a location relative to the display
192             Rectangle parentArea = parent.getClientArea();
193             Point parentLoc = parent.getLocation();
194             int px = parentLoc.x;
195             int py = parentLoc.y;
196             int pw = parentArea.width;
197             int ph = parentArea.height;
198
199             // Reuse the last size if there's one, otherwise use the default
200             Point childSize = sLastSize != null ? sLastSize : child.getSize();
201             int cw = childSize.x;
202             int ch = childSize.y;
203
204             child.setLocation(px + (pw - cw) / 2, py + (ph - ch) / 2);
205             child.setSize(cw, ch);
206         }
207     }
208
209     // End of hiding from SWT Designer
210     //$hide<<$
211 }