summaryrefslogtreecommitdiff
path: root/src/plugins/android.codeutils/src/com/motorola/studio/android/codeutils/codegeneration/CreateSampleDatabaseActivityPage.java
blob: e9f0af35e45cffe1a070403cfd166767957cc2f5 (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
/*
 * 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.codeutils.codegeneration;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.datatools.connectivity.ConnectionProfileException;
import org.eclipse.datatools.modelbase.sql.schema.Database;
import org.eclipse.datatools.modelbase.sql.tables.Table;
import org.eclipse.jface.dialogs.IPageChangeProvider;
import org.eclipse.jface.dialogs.IPageChangedListener;
import org.eclipse.jface.dialogs.PageChangedEvent;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeNode;
import org.eclipse.jface.viewers.TreeNodeContentProvider;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.motorola.studio.android.codeutils.CodeUtilsActivator;
import com.motorola.studio.android.codeutils.db.utils.DatabaseUtils;
import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS;
import com.motorola.studio.android.common.exception.AndroidException;
import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.common.utilities.AndroidUtils;
import com.motorola.studio.android.model.ActivityBasedOnTemplate;
import com.motorola.studio.android.wizards.buildingblocks.Method;
import com.motorola.studio.android.wizards.buildingblocks.NewLauncherWizardPage;

/**
 * Wizard page to create an activity based on database samples.
 * */
public class CreateSampleDatabaseActivityPage extends NewLauncherWizardPage
{

    private TreeViewer treeViewer;

    //Tree viewer input
    private TreeNode[] treeNodeArray;

    // Page help ID
    public static final String PAGE_HELP_ID = CodeUtilsActivator.PLUGIN_ID + ".selecttablepage";

    // ANDROID_METADATA Table name
    private static final String ANDROID_METADATA_TABLE_NAME = "ANDROID_METADATA";

    // Can flip to next page flag
    private boolean canFlip = false;

    /**
     * Default constructor.
     * </br></br>
     * Creates a new instance using {@link com.motorola.studio.android.wizards.buildingblocks.NewLauncherWizardPage#NewLauncherWizardPage(com.motorola.studio.android.model.BuildingBlockModel,java.lang.String) NewLauncherWizardPage(BuildingBlockModel, String)}
     * and passing {@code null} and {@link CodeUtilsNLS#UI_CreateSampleDatabaseActivityPageName Page name} as arguments.
     */
    public CreateSampleDatabaseActivityPage()
    {
        super(null, CodeUtilsNLS.UI_CreateSampleDatabaseActivityPageName);
    }

    /**
     * Creates a new instance using {@link com.motorola.studio.android.wizards.buildingblocks.NewLauncherWizardPage#NewLauncherWizardPage(com.motorola.studio.android.model.BuildingBlockModel,java.lang.String) NewLauncherWizardPage(BuildingBlockModel, String)}
     * and passing {@code activity} and {@link CodeUtilsNLS#UI_CreateSampleDatabaseActivityPageName Page name} as arguments.
     * 
     * @param activity an {@code com.motorola.studio.android.model.ActivityBasedOnTemplate} to be used as the building block model. 
     */
    public CreateSampleDatabaseActivityPage(ActivityBasedOnTemplate activity)
    {
        super(activity, CodeUtilsNLS.UI_CreateSampleDatabaseActivityPageName);
    }

    /* (non-Javadoc)
     * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#createIntermediateControls(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected void createExtendedControls(Composite parent)
    {
        Composite mainComposite = new Composite(parent, SWT.NONE);
        mainComposite.setLayout(new GridLayout());
        mainComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
        mainComposite.setLayout(new GridLayout());

        Composite composite = new Composite(mainComposite, SWT.NONE);
        composite.setLayout(new GridLayout());
        composite.setLayoutData(new GridData(GridData.FILL_BOTH));

        // Databases and Tables Tree Viewer
        treeViewer = new TreeViewer(composite, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
        treeViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        // Set content and label provider
        treeViewer.setLabelProvider(new SampleDatabaseActivityPageLabelProvider());
        treeViewer.setContentProvider(new TreeNodeContentProvider());

        // Create an array of type TreeNode[] to serve as the input for the tree
        if ((treeNodeArray == null) || (treeNodeArray.length < 1))
        {
            treeNodeArray = generateTreeViewerInput();
        }

        treeViewer.setInput(treeNodeArray);
        treeViewer.addSelectionChangedListener(new DatabaseTreeListener());

        // Check if there were any databases found and update status if not.
        if (treeNodeArray.length < 1)
        {
            // Create a warning status
            IStatus status =
                    new Status(
                            IStatus.WARNING,
                            CodeUtilsActivator.PLUGIN_ID,
                            CodeUtilsNLS.UI_CreateSampleDatabaseActivityPage_No_Database_Found_Information);
            updateStatus(status);
        }

        // Expand all elements
        treeViewer.expandAll();

        // Add a listener to the wizard to listen for page changes
        if (getContainer() instanceof IPageChangeProvider)
        {
            ((IPageChangeProvider) getContainer()).addPageChangedListener(new PageChangeListener());
        }

        setControl(mainComposite);
    }

    /* (non-Javadoc)
     * @see com.motorola.studio.android.wizards.buildingblocks.NewLauncherWizardPage#getIntentFiltersActions()
     */
    @Override
    protected String[] getIntentFiltersActions()
    {
        String[] intentFiltersActions = new String[0];
        try
        {
            intentFiltersActions = AndroidUtils.getActivityActions(getBuildBlock().getProject());
        }
        catch (AndroidException e)
        {
            setErrorMessage(e.getMessage());
        }
        return intentFiltersActions;
    }

    /* (non-Javadoc)
     * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getDefaultMessage()
     */
    @Override
    public String getDefaultMessage()
    {
        return CodeUtilsNLS.UI_CreateSampleDatabaseActivityPage_Default_Message;
    }

    /* (non-Javadoc)
     * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getHelpId()
     */
    @Override
    protected String getHelpId()
    {
        return PAGE_HELP_ID;
    }

    /* (non-Javadoc)
    * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getWizardTitle()
    */
    @Override
    public String getWizardTitle()
    {
        return CodeUtilsNLS.UI_ActivityWizard_Title;

    }

    /* (non-Javadoc)
     * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getMethods()
     */
    @Override
    protected Method[] getMethods()
    {
        return null;
    }

    /*
     * (non-Javadoc)
     * */
    @Override
    public boolean canFlipToNextPage()
    {

        return canFlip;
    }

    /*
     * Method to retrieve the databases and tables from the target project and make a TreeNodeArray
     */
    private TreeNode[] generateTreeViewerInput()
    {
        // Collection of TreeNodes
        HashSet<TreeNode> treeNodeColletion = new HashSet<TreeNode>();

        // The selected project for which the activity will be created
        IProject project = getBuildBlock().getProject();

        if (project != null)
        {
            // Get a collection of existing .db files inside the project
            Set<IFile> dbFilesSet = DatabaseUtils.getDbFilesFromProject(project);

            // Retrieve the database instances
            for (IFile dbFile : dbFilesSet)
            {
                try
                {
                    // For each database retrieved, construct a TreeNode object containing itself and it's tables
                    Database database =
                            DatabaseUtils.getDatabase(project.getName(), dbFile.getName());
                    TreeNode treeNodeDatabase = new TreeNode(database);

                    // Collection to store the table treeNodes from this database. Will be used later to set the children nodes of the database.
                    HashSet<TreeNode> databaseChildren = new HashSet<TreeNode>();

                    // Construct another TreeTable object for each table and set the database tree node as the parent.
                    // Tables don't have children
                    for (Table table : DatabaseUtils.getTables(database))
                    {
                        // Do not add ANDROID_METADATA table
                        if (!table.getName().equalsIgnoreCase(ANDROID_METADATA_TABLE_NAME))
                        {
                            TreeNode treeNodeTable = new TreeNode(table);
                            treeNodeTable.setParent(treeNodeDatabase);

                            // Add this node as a children of the database tree node.
                            databaseChildren.add(treeNodeTable);
                        }

                    }

                    // Add the table nodes as the children of the database node
                    treeNodeDatabase.setChildren(databaseChildren.toArray(new TreeNode[0]));

                    // Add the database tree node to the resulting TreeNode collection that will serve as input
                    treeNodeColletion.add(treeNodeDatabase);
                }
                catch (ConnectionProfileException e)
                {
                    // Log error
                    StudioLogger.error(DatabaseUtils.class,
                            "A error ocurred while retrieving the connection profile.", e);
                }
                catch (IOException e)
                {
                    // Log error
                    StudioLogger.error(DatabaseUtils.class, "An I/O error ocurred.", e);
                }
            }
        }

        // Return a TreeNode array
        return treeNodeColletion.toArray(new TreeNode[0]);

    }

    /**
     * @return Returns true if page has header. Otherwise, returns false.
     */
    @Override
    public boolean hasHeader()
    {
        return false;
    }

    /**
     * Selection listener for the tree viewer.
     */
    class DatabaseTreeListener implements ISelectionChangedListener
    {

        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
         */
        public void selectionChanged(SelectionChangedEvent event)
        {
            ActivityBasedOnTemplate activity = ((ActivityBasedOnTemplate) getBuildBlock());

            if (event.getSelection() instanceof ITreeSelection)
            {
                ITreeSelection treeSelection = (ITreeSelection) event.getSelection();

                if (treeSelection.getFirstElement() instanceof TreeNode)
                {
                    TreeNode selectedNode = (TreeNode) treeSelection.getFirstElement();
                    // Check if it's a database or table that was selected and set verification flags
                    if (selectedNode.getValue() instanceof Table)
                    {
                        canFlip = true;
                        // Set the collector table
                        activity.setCollectorTable((Table) selectedNode.getValue());
                        // Set the collector database
                        TreeNode parentNode = selectedNode.getParent();
                        activity.setCollectorDatabaseName(((Database) parentNode.getValue())
                                .getName());
                        activity.setDatabaseTableSelected(true);

                    }
                    else
                    {
                        canFlip = false;
                        // Set the collector info to null
                        activity.setCollectorTable(null);
                        activity.setCollectorDatabaseName(null);
                        activity.setDatabaseTableSelected(false);

                    }

                    getWizard().getContainer().updateButtons();
                }
            }

        }
    }

    /**
     * Listener to verify when this page is visible
     */
    private class PageChangeListener implements IPageChangedListener
    {
        /*
         * (non-Javadoc)
         * 
         * @see
         * org.eclipse.jface.dialogs.IPageChangedListener#pageChanged(org.eclipse
         * .jface.dialogs.PageChangedEvent)
         */
        public void pageChanged(PageChangedEvent event)
        {
            if ((event.getSelectedPage() == CreateSampleDatabaseActivityPage.this))
            {
                ActivityBasedOnTemplate activity = (ActivityBasedOnTemplate) getBuildBlock();

                treeNodeArray = generateTreeViewerInput();
                treeViewer.setInput(treeNodeArray);

                // Check if there were any databases found and update status if not.
                if (treeNodeArray.length < 1)
                {
                    // Create a warning status
                    IStatus status =
                            new Status(
                                    IStatus.WARNING,
                                    CodeUtilsActivator.PLUGIN_ID,
                                    CodeUtilsNLS.UI_CreateSampleDatabaseActivityPage_No_Database_Found_Information);
                    updateStatus(status);
                }
                else
                {
                    updateStatus(new Status(IStatus.OK, CodeUtilsActivator.PLUGIN_ID, null));
                }

                if ((activity.getCollectorTable() == null)
                        && (!treeViewer.getSelection().isEmpty()))
                {
                    treeViewer.setSelection(TreeSelection.EMPTY);
                }

                if (treeViewer.getSelection().isEmpty())
                {
                    canFlip = false;
                }

                //update buttons
                getWizard().getContainer().updateButtons();

                // Expand all elements
                treeViewer.expandAll();
            }

        }
    }
}