summaryrefslogtreecommitdiff
path: root/src/plugins/preflighting.ui/src/com/motorolamobility/preflighting/ui/wizards/ApkValidationWizardPage.java
blob: d9fba62f163a083a1d9fd1ef9a64af544c19766b (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.motorolamobility.preflighting.ui.wizards;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.motorolamobility.preflighting.ui.i18n.PreflightingUiNLS;

public class ApkValidationWizardPage extends WizardPage
{
    private Text sourceDirText = null;

    private Button browseDirButton = null;

    private Tree packagesTree = null;

    private Button selectAllButton = null;

    private Button deselectAllButton = null;

    //private WizardSelection selection = null;

    protected Composite mainComposite = null;

    /**
     * Create a new wizard page based on selection
     * 
     * @param pageName
     *            the page name
     * @param selection
     *            the selection
     */
    public ApkValidationWizardPage(String pageName, IStructuredSelection selection)
    {
        super(pageName);
        setDescription(PreflightingUiNLS.ApkValidationWizardPage_description);
        setTitle(PreflightingUiNLS.ApkValidationWizardPage_title);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
     * .Composite)
     */
    public void createControl(Composite parent)
    {
        this.mainComposite = new Composite(parent, SWT.NULL);
        // create new layout with 3 columns of different sizes
        GridLayout layout = new GridLayout(3, false);
        this.mainComposite.setLayout(layout);

        Label sourceDirLabel = new Label(this.mainComposite, SWT.NONE);
        sourceDirLabel.setText(PreflightingUiNLS.ApkValidationWizardPage_folderLabel);

        GridData layoutData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
        sourceDirLabel.setLayoutData(layoutData);

        this.sourceDirText = new Text(this.mainComposite, SWT.BORDER);
        layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
        this.sourceDirText.setLayoutData(layoutData);
        this.sourceDirText.addListener(SWT.Modify, new SourceDirectoryTextListener());

        this.browseDirButton = new Button(this.mainComposite, SWT.PUSH);
        this.browseDirButton.setText(PreflightingUiNLS.ApkValidationWizardPage_browseLabel);
        layoutData = new GridData(SWT.FILL, SWT.CENTER, false, false);
        this.browseDirButton.setLayoutData(layoutData);
        this.browseDirButton.addListener(SWT.Selection, new BrowseButtonListener());

        Label packagesLabel = new Label(this.mainComposite, SWT.NONE);
        packagesLabel.setText(PreflightingUiNLS.ApkValidationWizardPage_packagesLabel);
        packagesLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 3, 1));

        this.packagesTree = new Tree(this.mainComposite, SWT.BORDER | SWT.CHECK | SWT.V_SCROLL);
        layoutData = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 2);
        layoutData.heightHint = 150;
        this.packagesTree.setLayoutData(layoutData);
        this.packagesTree.addListener(SWT.Selection, new TreeSelectionListener());

        Composite selectionButtons = new Composite(this.mainComposite, SWT.FILL);
        layoutData = new GridData(SWT.FILL, SWT.TOP, false, true, 1, 2);
        selectionButtons.setLayoutData(layoutData);
        FillLayout row = new FillLayout(SWT.VERTICAL);
        row.spacing = 3;
        selectionButtons.setLayout(row);

        this.selectAllButton = new Button(selectionButtons, SWT.PUSH);
        this.selectAllButton.setText(PreflightingUiNLS.ApkValidationWizardPage_selectAllLabel);
        SelectionButtonsListener selectionButtonsListener = new SelectionButtonsListener();
        this.selectAllButton.addListener(SWT.Selection, selectionButtonsListener);

        this.deselectAllButton = new Button(selectionButtons, SWT.PUSH);
        this.deselectAllButton.setText(PreflightingUiNLS.ApkValidationWizardPage_deseletAllLabel);
        this.deselectAllButton.addListener(SWT.Selection, selectionButtonsListener);

        updatePageComplete();
        setControl(this.mainComposite);
    }

    /**
     * Populates the tree with the packages of base dir Requires a valid folder
     * set as source dir
     */
    private void populateTree(List<String> selection)
    {
        File sourceDir = getSourcePath().toFile();
        this.packagesTree.removeAll();
        if (sourceDir.isDirectory() && sourceDir.canWrite())
        {
            File[] list = sourceDir.listFiles();
            for (File file : list)
            {
                if (file.canRead() && file.isFile() && file.getName().endsWith(".apk")) //$NON-NLS-1$
                {
                    TreeItem fileItem = new TreeItem(this.packagesTree, SWT.NONE);
                    String text = file.getName();

                    fileItem.setText(text);
                    fileItem.setData(file);
                    if ((selection != null) && selection.contains(file.getName())
                            && file.canWrite())
                    {
                        fileItem.setChecked(true);
                    }
                }
            }
        }
    }

    /**
     * Validates if the source directory is valid one
     * 
     * @return true if the source dir text is valid, false otherwise
     */
    private boolean isSourceDirValid()
    {

        String messageAux = null;
        int severity = IMessageProvider.NONE;

        /*
         * Check if the selected location is valid, even if non existent.
         */
        IPath path = new Path(this.sourceDirText.getText());

        // Test if path is blank, to warn user instead of show an error message
        if (this.sourceDirText.getText().equals("")) //$NON-NLS-1$
        {
            messageAux = PreflightingUiNLS.ApkValidationWizardPage_emptyListMsg;
            severity = IMessageProvider.INFORMATION;
        }

        /*
         * Do Win32 Validation
         */
        if ((messageAux == null) && Platform.getOS().equalsIgnoreCase(Platform.OS_WIN32))
        {
            // test path size
            if (path.toString().length() > 255)
            {
                messageAux = PreflightingUiNLS.ApkValidationWizardPage_tooLongMsg;
                severity = IMessageProvider.WARNING;
            }
            String device = path.getDevice();
            File deviceFile = null;
            if (device != null)
            {
                deviceFile = new File(path.getDevice());
            }

            if ((device != null) && !deviceFile.exists())
            {
                messageAux =
                        PreflightingUiNLS.ApkValidationWizardPage_invalidDeviceMsg
                                + " [" + device + "]"; //$NON-NLS-2$ //$NON-NLS-3$
                severity = IMessageProvider.ERROR;
            }

        }
        // test if path is absolute
        if (messageAux == null)
        {
            if (!path.isAbsolute() || !path.toFile().exists())
            {
                messageAux = PreflightingUiNLS.ApkValidationWizardPage_invalidFolderMsg;
                severity = IMessageProvider.ERROR;
            }
        }

        if (messageAux == null)
        {
            for (String folderName : path.segments())
            {
                if (!ResourcesPlugin.getWorkspace().validateName(folderName, IResource.FOLDER)
                        .isOK())
                {
                    messageAux = PreflightingUiNLS.ApkValidationWizardPage_invalidFolderMsg;
                    severity = IMessageProvider.ERROR;
                }
            }
        }

        if ((messageAux == null) && ((path.toFile().exists() && !path.toFile().isDirectory())))
        {
            messageAux = PreflightingUiNLS.ApkValidationWizardPage_invalidSourceDirectoryMsg;
            severity = IMessageProvider.ERROR;
        }

        /*
         * Setting message
         */
        if (messageAux == null)
        {
            messageAux = PreflightingUiNLS.ApkValidationWizardPage_validateMsg;
            severity = IMessageProvider.NONE;
        }
        setMessage(messageAux, severity);
        return severity == IMessageProvider.NONE;
    }

    /**
     * @return the path of base dir where packages are located
     */
    public IPath getSourcePath()
    {
        return new Path(this.sourceDirText.getText());
    }

    /**
     * 
     * @return the list with selected packages
     */
    public List<File> getSelectedPackages()
    {
        ArrayList<File> selected = new ArrayList<File>();
        for (TreeItem item : this.packagesTree.getItems())
        {
            if (item.getChecked())
            {
                selected.add((File) item.getData());
            }
        }

        return selected;
    }

    /**
     * Update the page status, validating each field of this page Subclasses
     */
    public void updatePageComplete()
    {
        String messageAux = null;
        int severity = IMessageProvider.NONE;

        if (isSourceDirValid())
        {
            if (this.packagesTree.getItemCount() == 0)
            {
                messageAux = PreflightingUiNLS.ApkValidationWizardPage_emptyFolderMsg;
                severity = IMessageProvider.ERROR;
            }
        }
        else
        {
            messageAux = getMessage();
            severity = getMessageType();
        }

        if ((messageAux == null) && (getSelectedPackages().size() == 0))
        {
            messageAux = PreflightingUiNLS.ApkValidationWizardPage_onePackageMsg;
            severity = IMessageProvider.INFORMATION;
        }

        if (messageAux == null)
        {
            messageAux = PreflightingUiNLS.ApkValidationWizardPage_validateMsg;
            severity = IMessageProvider.NONE;
        }

        setMessage(messageAux, severity);
        setPageComplete(severity == IMessageProvider.NONE);
    }

    /**
     * This class implements the listener of browse button, opening the browse
     * window and updating the dir text
     */
    class BrowseButtonListener implements Listener
    {
        /*
         * (non-Javadoc)
         * 
         * @see
         * org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets
         * .Event)
         */
        public void handleEvent(Event event)
        {
            DirectoryDialog dialog =
                    new DirectoryDialog(ApkValidationWizardPage.this.mainComposite.getShell());
            dialog.setFilterPath(!ApkValidationWizardPage.this.sourceDirText.getText().trim()
                    .equals("") ? ApkValidationWizardPage.this.sourceDirText.getText() : null); //$NON-NLS-1$
            String path = dialog.open();
            if (path != null)
            {
                ApkValidationWizardPage.this.sourceDirText.setText(path);
                populateTree(null);
                updatePageComplete();
            }
        }
    }

    /*
     * Listener to validate any SourceDirectory text Change
     */
    class SourceDirectoryTextListener implements Listener
    {
        /*
         * (non-Javadoc)
         * 
         * @see
         * org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets
         * .Event)
         */
        public void handleEvent(Event event)
        {
            ApkValidationWizardPage.this.packagesTree.removeAll();
            if (isSourceDirValid())
            {
                populateTree(null);
            }
            updatePageComplete();
        }
    }

    /*
     * This class handles clicks on select all and deselect all buttons
     */
    class SelectionButtonsListener implements Listener
    {

        /**
         * Check/Uncheck all items
         * 
         * @param check
         *            : true for check, false for unckeck
         */
        private void setCheckedAll(boolean check)
        {
            for (TreeItem item : ApkValidationWizardPage.this.packagesTree.getItems())
            {
                item.setChecked(check);
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see
         * org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets
         * .Event)
         */
        public void handleEvent(Event event)
        {
            if (event.widget == ApkValidationWizardPage.this.selectAllButton)
            {
                setCheckedAll(true);
            }
            else if (event.widget == ApkValidationWizardPage.this.deselectAllButton)
            {
                setCheckedAll(false);
            }
            updatePageComplete();
        }
    }

    /**
     * Listener to update wizard status according tree selection
     * 
     */
    class TreeSelectionListener implements Listener
    {
        /*
         * (non-Javadoc)
         * 
         * @see
         * org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets
         * .Event)
         */
        public void handleEvent(Event event)
        {
            updatePageComplete();
        }
    }
}