summaryrefslogtreecommitdiff
path: root/src/plugins/emulator/src/com/motorola/studio/android/emulator/device/ui/StartupOptionsComposite.java
blob: b6f3f9cfc4b22dd710cbfc54c156d050b23d285f (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* 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.device.ui;

import java.util.List;

import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

import com.motorola.studio.android.emulator.core.skin.IAndroidSkin;
import com.motorola.studio.android.emulator.device.IAndroidDeviceConstants;
import com.motorola.studio.android.emulator.device.instance.options.IStartupOptionsConstants;
import com.motorola.studio.android.emulator.device.instance.options.StartupOption;
import com.motorola.studio.android.emulator.device.instance.options.StartupOptionsGroup;
import com.motorola.studio.android.emulator.device.instance.options.StartupOptionsMgt;
import com.motorola.studio.android.emulator.i18n.EmulatorNLS;

/**
 * DESCRIPTION:
 * <br>
 * This class implements the UI for showing all Android Emulator Device Instance startup options information.
 * <br>
 * It extends the AbstractPropertiesComposite so as to use its common functionalities.
 * <br>
 * RESPONSIBILITY:
 * <br>
 * - Show Android Emulator Device Instance main information on the UI
 * <br>
 * COLABORATORS:
 * <br>
 * AbstractPropertiesComposite: extends this class
 * <br>
 * USAGE:
 * <br>
 * This class should be added as a regular composite whenever startup options information on Android Emulator
 * Device Instance is necessary to be shown and edited on the UI. 
 */
public class StartupOptionsComposite extends AbstractPropertiesComposite implements
        IStartupOptionsConstants
{
    // The widget which displays the current command line used to pass the startup options
    private Text commandLine;

    private final IAndroidSkin skin;

    private final int TABFOLDER_HEIGHT_HINT = 350;

    private boolean canCalculateScale = true;

    /**
     * Creates a StartupOptionsComposite object.
     * 
     * @param parent the parent composite
     * @param canCalculateScale
     */
    public StartupOptionsComposite(Composite parent, String startupOptions, IAndroidSkin skin,
            boolean canCalculateScale)
    {
        super(parent);

        this.skin = skin;
        this.canCalculateScale = canCalculateScale;
        StartupOptionsMgt.loadFromCommandLine(startupOptions);
        createUI();

        // Set context Help 
        PlatformUI.getWorkbench().getHelpSystem()
                .setHelp(parent, IAndroidDeviceConstants.STARTUP_OPTIONS_HELP);
    }

    /**
     * Create widgets for startup options
     */
    private void createUI()
    {

        Composite mainComposite = this;
        Layout mainLayout = new GridLayout();
        mainComposite.setLayout(mainLayout);

        // list of startup options groups
        List<StartupOptionsGroup> startupOptionsGroupsList =
                StartupOptionsMgt.getStartupOptionsGroupsList();

        // list of startup options in each group
        List<StartupOption> startupOptions = null;

        // Create Tab Folder
        final TabFolder tabFolder = new TabFolder(mainComposite, SWT.NULL);
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
        data.heightHint = TABFOLDER_HEIGHT_HINT;
        tabFolder.setLayoutData(data);

        /*
         * Iterate through Startup Groups 
         */
        for (StartupOptionsGroup startupOptionGroup : startupOptionsGroupsList)
        {

            // Create Tab Item
            TabItem tabItem = new TabItem(tabFolder, SWT.NULL);
            tabItem.setText(startupOptionGroup.getTitle());
            Composite group = new Composite(tabFolder, SWT.NULL);
            group.setLayout(new GridLayout(3, false));
            group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
            tabItem.setControl(group);

            // get startup options in this group
            startupOptions = startupOptionGroup.getStartupOptions();

            /*
             * Iterate through Startup Options in this group
             */
            for (final StartupOption startupOption : startupOptions)
            {

                // create a checkbox for each startup option
                Button checkbox = new Button(group, SWT.CHECK);
                checkbox.setSelection(startupOption.isChecked());
                checkbox.setText(startupOption.getUserFriendlyName());
                checkbox.setToolTipText(startupOption.getName() + ": " //$NON-NLS-1$
                        + startupOption.getDescription());
                startupOption.setCheckedWidget(checkbox);
                checkbox.addSelectionListener(new SelectionAdapter()
                {
                    @Override
                    public void widgetSelected(SelectionEvent e)
                    {
                        boolean checkedStatus = ((Button) e.widget).getSelection();
                        startupOption.setChecked(checkedStatus);
                        notifyCompositeChangeListeners();
                    }
                });
                GridData checkboxData = new GridData(SWT.NULL, SWT.FILL, false, false);
                checkbox.setLayoutData(checkboxData);

                // Create input fields depending on the startup option type
                switch (startupOption.getType())
                {
                    case TYPE_NONE:
                        // extend checkbox area along the line
                        checkboxData.widthHint = SWT.DEFAULT;
                        checkboxData.horizontalSpan = 3;
                        checkbox.setLayoutData(checkboxData);
                        break;

                    case TYPE_TEXT:
                    case TYPE_NUMBER:
                        createWidgetsForTextOrNumberType(group, startupOption);
                        break;

                    case TYPE_PATH:
                        createWidgetsForPathType(group, startupOption);
                        break;

                    default:
                        // none

                }
            }
        }

        /* 
         * Command Line area
         */
        Composite commandLineArea = new Composite(mainComposite, SWT.NONE); // composite
        commandLineArea.setLayout(new GridLayout(2, false));
        data = new GridData(SWT.FILL, SWT.FILL, true, true);
        commandLineArea.setLayoutData(data);

        Label commandLineLabel = new Label(commandLineArea, SWT.NONE); // label
        commandLineLabel.setText(""); //$NON-NLS-1$
        data = new GridData(SWT.FILL, SWT.FILL, false, true);
        commandLineLabel.setLayoutData(data);

        commandLine = new Text(commandLineArea, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.V_SCROLL); // text
        data = new GridData(SWT.FILL, SWT.FILL, true, true);
        commandLineArea.pack();
        data.widthHint = commandLineArea.getBounds().width - commandLineLabel.getBounds().width;
        data.heightHint = commandLineArea.getBounds().height;
        commandLine.setLayoutData(data);
        commandLine.setText(StartupOptionsMgt.getParamList());
        commandLine.setEditable(false);
    }

    /**
     * Create widgets to enable user to input data for fields of text or number type
     * 
     * @param parent composite where the widgets will be attached to
     * @param startupOption the corresponding startup option
     */
    private void createWidgetsForTextOrNumberType(final Composite parent,
            final StartupOption startupOption)
    {
        // create input text with calc button
        if (startupOption.getName().equals(SCALE))
        {
            final Text inputText = new Text(parent, SWT.SINGLE | SWT.BORDER);
            inputText.setText(startupOption.getValue());
            startupOption.setValueWidget(inputText);
            inputText.setLayoutData(new GridData(SWT.FILL, SWT.NULL, true, false));
            inputText.addModifyListener(new ModifyListener()
            {
                public void modifyText(ModifyEvent e)
                {
                    startupOption.setValue(inputText.getText());
                    notifyCompositeChangeListeners();
                }
            });

            Button calc = new Button(parent, SWT.PUSH);
            calc.setText(EmulatorNLS.UI_DpiScale_Calculator);
            GridData calcData = new GridData(SWT.NULL, SWT.NULL, false, false);
            calc.setLayoutData(calcData);
            calc.addSelectionListener(new SelectionAdapter()
            {
                @Override
                public void widgetSelected(SelectionEvent e)
                {
                    DpiScaleCalculatorDialog dialog =
                            new DpiScaleCalculatorDialog(new Shell(parent.getShell()), skin);
                    if (dialog.open() == Dialog.OK)
                    {
                        for (StartupOptionsGroup group : StartupOptionsMgt
                                .getStartupOptionsGroupsList())
                        {
                            for (StartupOption startupOption : group.getStartupOptions())
                            {
                                if (startupOption.getName().equals(SCALE))
                                {
                                    startupOption.setChecked(true);
                                    startupOption.setValue(dialog.getResultScaleValue());
                                    startupOption.updateUI();
                                }
                            }
                        }
                    }
                }
            });
            calc.setEnabled(canCalculateScale);
            if (!canCalculateScale)
            {
                ControlDecoration controlDecoration =
                        new ControlDecoration(inputText, SWT.LEFT | SWT.TOP);
                controlDecoration
                        .setDescriptionText(EmulatorNLS.StartupOptionsComposite_Error_Loading_Skin_Cant_Calculate_Scale);
                FieldDecoration fieldDecoration =
                        FieldDecorationRegistry.getDefault().getFieldDecoration(
                                FieldDecorationRegistry.DEC_WARNING);
                controlDecoration.setImage(fieldDecoration.getImage());
            }
        }
        // create input text
        else if ((startupOption.getPreDefinedValues() == null)
                || (startupOption.getPreDefinedValues().size() == 0))
        {
            final Text inputText = new Text(parent, SWT.SINGLE | SWT.BORDER);
            inputText.setText(startupOption.getValue());
            startupOption.setValueWidget(inputText);
            inputText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
            inputText.addModifyListener(new ModifyListener()
            {
                public void modifyText(ModifyEvent e)
                {
                    startupOption.setValue(inputText.getText());
                    notifyCompositeChangeListeners();
                }
            });
        }
        // create combobox
        else
        {
            final Combo combo = new Combo(parent, SWT.READ_ONLY);
            startupOption.setValueWidget(combo);
            int selectedIndex = 0;
            for (String preDefinedValue : startupOption.getPreDefinedValues())
            {
                combo.add(preDefinedValue);
                if (startupOption.getValue().equals(preDefinedValue))
                {
                    combo.select(selectedIndex);
                }
                else
                {
                    selectedIndex++;
                }
            }
            combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
            combo.addModifyListener(new ModifyListener()
            {
                public void modifyText(ModifyEvent e)
                {
                    startupOption.setValue(combo.getText());
                    notifyCompositeChangeListeners();
                }
            });
        }
    }

    /**
     * Create widgets to enable user to input data for fields of path type
     * 
     * @param parent composite where the widgets will be attached to
     * @param startupOption the corresponding startup option
     */
    private void createWidgetsForPathType(final Composite parent, final StartupOption startupOption)
    {
        // create input text
        final Text pathText = new Text(parent, SWT.SINGLE | SWT.BORDER);
        pathText.setText(startupOption.getValue());
        startupOption.setValueWidget(pathText);
        pathText.setLayoutData(new GridData(SWT.FILL, SWT.NULL, true, false));
        pathText.addModifyListener(new ModifyListener()
        {
            public void modifyText(ModifyEvent e)
            {
                startupOption.setValue(pathText.getText());
                notifyCompositeChangeListeners();
            }
        });
        // create browse button
        Button pathBrowseButton = new Button(parent, SWT.PUSH);
        pathBrowseButton.setText(EmulatorNLS.UI_General_BrowseButtonLabel);
        GridData data = new GridData(SWT.NULL, SWT.NULL, false, false);
        pathBrowseButton.setLayoutData(data);
        pathBrowseButton.addSelectionListener(new SelectionAdapter()
        {
            @Override
            public void widgetSelected(SelectionEvent e)
            {
                String selectedPath = null;

                if (startupOption.getTypeDetails().equalsIgnoreCase(TYPE_PATH_DIR))
                {
                    DirectoryDialog directoryDialog = new DirectoryDialog(getShell(), SWT.OPEN);
                    selectedPath = directoryDialog.open();
                }
                else
                {
                    FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN);
                    String[] filterExtensions =
                    {
                        "*" + startupOption.getTypeDetails() //$NON-NLS-1$
                    };
                    fileDialog.setFilterExtensions(filterExtensions);
                    selectedPath = fileDialog.open();
                }

                if (selectedPath != null)
                {
                    pathText.setText(selectedPath);
                }
            }
        });
    }

    /**
     * Update command line value
     * 
     * @see com.motorola.studio.android.emulator.device.ui.AbstractPropertiesComposite#notifyCompositeChangeListeners()
     */
    @Override
    protected void notifyCompositeChangeListeners()
    {
        commandLine.setText(StartupOptionsMgt.getParamList());
        super.notifyCompositeChangeListeners();
    }

    /**
     * Retrieves the error message associated to this composites current state.
     * The order of precedence of error is the same as the fields displayed on the
     * UI, which means errors on fields drawn first are shown with a higher precedence
     * than the errors of fields drawn last.
     * The instance description field is the only non required field. 
     * 
     * @return the error message, or <code>null</code> if there are no errors
     */
    @Override
    public String getErrorMessage()
    {
        String errMsg = null;
        Status status = StartupOptionsMgt.validate();
        if (status.getSeverity() == Status.ERROR)
        {
            errMsg = status.getMessage();
        }
        return errMsg;
    }

    /**
     * Reload the values being displayed in the UI as well as the ones 
     * in the model.
     * 
     * @param startupOptions commandLine the command line used to start the emulator
     */
    public void reloadValues(String commandLine)
    {
        StartupOptionsMgt.loadFromCommandLine(commandLine);
    }
}