summaryrefslogtreecommitdiff
path: root/src/plugins/emulator/src/com/motorola/studio/android/emulator/ui/view/AndroidView.java
blob: 2a05611ad84d95b0927262bad63be8762fd51283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.motorola.studio.android.emulator.ui.view;

import static com.motorola.studio.android.common.log.StudioLogger.error;

import org.eclipse.sequoyah.vnc.protocol.lib.ProtocolHandle;
import org.eclipse.sequoyah.vnc.vncviewer.config.IPropertiesFileHandler;
import org.eclipse.sequoyah.vnc.vncviewer.config.VNCConfiguration;
import org.eclipse.sequoyah.vnc.vncviewer.graphics.swt.ISWTPainter;
import org.eclipse.sequoyah.vnc.vncviewer.graphics.swt.SWTRemoteDisplay;
import org.eclipse.sequoyah.vnc.vncviewer.graphics.swt.img.SWTRemoteDisplayImg;
import org.eclipse.sequoyah.vnc.vncviewer.network.VNCProtocolData;
import org.eclipse.sequoyah.vnc.vncviewer.registry.VNCProtocolRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.commands.ICommandService;

import com.motorola.studio.android.common.utilities.EclipseUtils;
import com.motorola.studio.android.emulator.EmulatorPlugin;
import com.motorola.studio.android.emulator.core.exception.InstanceStopException;
import com.motorola.studio.android.emulator.core.exception.SkinException;
import com.motorola.studio.android.emulator.core.model.IAndroidEmulatorInstance;
import com.motorola.studio.android.emulator.core.skin.IAndroidSkin;
import com.motorola.studio.android.emulator.core.skin.ISkinKeyXmlTags;
import com.motorola.studio.android.emulator.i18n.EmulatorNLS;
import com.motorola.studio.android.emulator.ui.controls.IAndroidComposite;
import com.motorola.studio.android.emulator.ui.controls.maindisplay.MainDisplayComposite;
import com.motorola.studio.android.emulator.ui.controls.nativewindow.NativeWindowComposite;
import com.motorola.studio.android.emulator.ui.controls.skin.SkinComposite;
import com.motorola.studio.android.emulator.ui.handlers.IHandlerConstants;
import com.motorola.studio.android.nativeos.IDevicePropertiesOSConstants;
import com.motorola.studio.android.nativeos.NativeUIUtils;

/**
 * DESCRIPTION: This class represents the Android Emulator view
 * 
 * RESPONSIBILITY: - Show the skin to the end user
 * 
 * COLABORATORS: None.
 * 
 * USAGE: All the public interface is extended from
 * <code>AbstractAndroidView</code>.
 */
public class AndroidView extends AbstractAndroidView
{
    /**
     * The Android Emulator view ID
     */
    public static final String ANDROID_VIEW_ID = EmulatorPlugin.PLUGIN_ID + ".androidView";

    /**
     * @see com.motorola.studio.android.emulator.ui.view.AbstractAndroidView#getViewId()
     */
    @Override
    protected String getViewId()
    {
        return ANDROID_VIEW_ID;
    }

    /**
     * Creates the skin composite, main display and CLI display related to
     * emulator.
     * 
     * @param tab
     *            the tab item that will hold the graphical elements that
     *            represents the emulator
     * @param instance
     *            the emulator instance
     * @param emulatorData
     *            the object to be defined with the elements created.
     */
    @Override
    protected void createWidgets(TabItem tab, final IAndroidEmulatorInstance instance,
            final AndroidViewData tabData) throws SkinException
    {
        tabData.loadSkin(instance);
        IAndroidSkin skin = tabData.getSkin();

        ProtocolHandle handle = instance.getProtocolHandle();
        VNCProtocolData data = VNCProtocolRegistry.getInstance().get(handle);

        final SWTRemoteDisplay mainDisplay;
        Composite parentComposite;
        if (instance.getProperties().getProperty(IDevicePropertiesOSConstants.useVnc,
                NativeUIUtils.getDefaultUseVnc()).toString().equals("true"))
        {
            if (data != null)
            {
                if (instance.getProperties().getProperty("Command_Line").contains("-no-skin"))
                {
                    int baseWidth =
                            skin.getSkinBean(instance.getCurrentLayout()).getSkinPropertyValue(
                                    ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_WIDTH);
                    int baseHeight =
                            skin.getSkinBean(instance.getCurrentLayout()).getSkinPropertyValue(
                                    ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_HEIGHT);

                    ScrolledComposite scrolledComposite =
                            new ScrolledComposite(tab.getParent(), SWT.H_SCROLL | SWT.V_SCROLL
                                    | SWT.BACKGROUND);

                    final MainDisplayComposite composite =
                            new MainDisplayComposite(scrolledComposite, SWT.BACKGROUND, baseWidth,
                                    baseHeight, instance);
                    composite.setLayout(new FillLayout());

                    mainDisplay =
                            createMainDisplay(composite, skin, instance, (ISWTPainter) data
                                    .getVncPainter());
                    composite.setSize(baseWidth, baseHeight);

                    scrolledComposite.setContent(composite);
                    tab.setControl(scrolledComposite);

                    scrolledComposite.addDisposeListener(new DisposeListener()
                    {
                        public void widgetDisposed(DisposeEvent e)
                        {
                            composite.dispose();
                        }
                    });

                    tabData.setCliDisplay(null);
                    tabData.setComposite(composite);
                    tabData.setMainDisplay(mainDisplay);
                }
                else
                {
                    parentComposite = createSkinComposite(tab.getParent(), skin, instance);
                    mainDisplay =
                            createMainDisplay(parentComposite, skin, instance, (ISWTPainter) data
                                    .getVncPainter());

                    tabData.setComposite((IAndroidComposite) parentComposite);
                    tabData.setMainDisplay(mainDisplay);
                    tab.setControl(parentComposite);
                }
            }
            else
            {
                error("The protocol object set in the device instance is not supported. Stopping the emulator instance...");
                EclipseUtils.showErrorDialog(EmulatorNLS.GEN_Error,
                        EmulatorNLS.ERR_AndroidView_ProtocolImplementerNotSupported);

                try
                {
                    instance.stop(true);
                }
                catch (InstanceStopException e)
                {
                    error("Error while running service for stopping virtual machine");
                    EclipseUtils.showErrorDialog(EmulatorNLS.GEN_Error,
                            EmulatorNLS.EXC_General_CannotRunStopService);
                }
            }

        }
        else
        {
            parentComposite = createNativeWindowComposite(tab.getParent(), skin, instance);
            tabData.setComposite((IAndroidComposite) parentComposite);
            tab.setControl(parentComposite);
        }
    }

    /**
     * Creates the skin composite used by this instance
     * 
     * @param parent
     *            The parent composite where to place the widgets
     * @param skin
     *            The object containing skin information used to draw the
     *            widgets
     */
    private SkinComposite createSkinComposite(Composite parent, IAndroidSkin skin,
            IAndroidEmulatorInstance instance)
    {

        SkinComposite skinComposite = new SkinComposite(parent, skin, instance);
        skinComposite.setBackground(new Color(skinComposite.getDisplay(), 255, 255, 255));

        return skinComposite;
    }

    private NativeWindowComposite createNativeWindowComposite(Composite parent, IAndroidSkin skin,
            IAndroidEmulatorInstance instance)
    {
        NativeWindowComposite nativeWindowComposite =
                new NativeWindowComposite(parent, skin, instance);
        //        nativeWindowComposite.setBackground(new Color(nativeWindowComposite.getDisplay(), 255, 255,
        //                255));

        return nativeWindowComposite;
    }

    /**
     * Creates the main display object used by this instance.
     * 
     * @param parent
     *            The parent composite where to place the widgets
     * @param skin
     *            The object containing skin information used to draw the
     *            widgets
     * @param instance
     *            The emulator instance
     * @param painter
     *            the painter used to draw the images
     */
    private SWTRemoteDisplay createMainDisplay(Composite parent, IAndroidSkin skin,
            IAndroidEmulatorInstance instance, ISWTPainter painter)

    {
        // gets and reads the configuration files of the VNC session
        IPropertiesFileHandler handler = IAndroidSkin.DEFAULT_PROPS_HANDLER;
        String configFile = IAndroidSkin.DEFAULT_VNC_CONFIG_FILE;

        VNCConfiguration config = new VNCConfiguration(configFile, handler);
        final SWTRemoteDisplay mainDisplay;
        mainDisplay =
                new SWTRemoteDisplayImg(parent, config.getConfigurationProperties(), handler,
                        painter);

        mainDisplay.setBackground(new Color(mainDisplay.getDisplay(), 0, 0, 0));
        mainDisplay.setLayout(new FillLayout());

        return mainDisplay;
    }

    /**
     * Forces the refreshing of the menu elements.
     */
    @Override
    protected void refreshMenuElements()
    {
        IViewSite viewSite = getViewSite();

        // Update the radio button selection in the zoom menu
        ICommandService service = (ICommandService) viewSite.getService(ICommandService.class);
        service.refreshElements(IHandlerConstants.CHANGE_EMULATOR_ORIENTATION_COMMAND, null);
        service.refreshElements(IHandlerConstants.CHANGE_EMULATOR_ZOOM_COMMAND, null);

    }

}