summaryrefslogtreecommitdiff
path: root/src/plugins/android.codeutils/src/com/motorola/studio/android/codeutils/wizards/DatabaseManagementClassesCreationWizard.java
blob: 3baf292415800f77748bba54cc1badba0de781ea (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
/*
 * 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.wizards;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;

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.log.StudioLogger;
import com.motorola.studio.android.common.log.UsageDataConstants;
import com.motorola.studio.android.common.preferences.DialogWithToggleUtils;
import com.motorola.studio.android.common.utilities.EclipseUtils;

/**
 * 
 * Wizard to create classes which aids the database management. 
 *
 */
public class DatabaseManagementClassesCreationWizard extends Wizard
{

    private static final String SDK_SWITCH_PERSPECTIVE_TO_MOTODEV_STUDIO_ANDROID_KEY =
            "switch.perspective.to.motodevstudioandroid"; //$NON-NLS-1$

    private static final String SDK_SWITCH_PERSPECTIVE_TO_JAVA_KEY = "switch.perspective.to.java"; //$NON-NLS-1$

    /**
     * This listener generates Database Management classes. It has the
     * characteristic of displaying a progress bar.
     */
    private final class GenerateDatabaseManagementClassesRunnableWithProgress implements
            IRunnableWithProgress
    {
        private final IProject project;

        private final IPath databaseFilePath;

        private final boolean isCreateSQLOpenHelper;

        private final boolean isCreateContentProviders;

        private final String sqlOpenHelperPackageName;

        private final String contentProvidersPackageName;

        private final String sqlOpenHelperClassName;

        private final boolean isOverrideContentProviders;

        /**
         * Constructor for assigning values
         * 
         * @param project Target Project
         * @param databaseFilePath Database file path
         * @param isCreateSQLOpenHelper Creates or not SQL Open Helper class
         * @param isCreateContentProviders Creates or not Content Provider classes
         * @param sqlOpenHelperPackageName SQL Open Helper Package Name
         * @param contentProvidersPackageName Content Provider classes package name
         * @param sqlOpenHelperClassName SQL Open Helper class Name
         * @param isOverrideContentProviders Overrides or not existing Content Provider classes
         */
        public GenerateDatabaseManagementClassesRunnableWithProgress(IProject project,
                IPath databaseFilePath, boolean isCreateSQLOpenHelper,
                boolean isCreateContentProviders, String sqlOpenHelperPackageName,
                String contentProvidersPackageName, String sqlOpenHelperClassName,
                boolean isOverrideContentProviders)
        {
            this.project = project;
            this.databaseFilePath = databaseFilePath;
            this.isCreateSQLOpenHelper = isCreateSQLOpenHelper;
            this.isCreateContentProviders = isCreateContentProviders;
            this.sqlOpenHelperPackageName = sqlOpenHelperPackageName;
            this.contentProvidersPackageName = contentProvidersPackageName;
            this.sqlOpenHelperClassName = sqlOpenHelperClassName;
            this.isOverrideContentProviders = isOverrideContentProviders;
        }

        /*
         * (non-Javadoc)
         * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
         */
        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException,
                InterruptedException
        {
            // create Database Management classes
            try
            {
                // sub monitor
                SubMonitor subMonitor = SubMonitor.convert(monitor, 10);

                // copy Database file to assets folder, in case it is not already there
                DatabaseUtils.copyDatabaseFileToAssetsFolder(databaseFilePath, project,
                        subMonitor.newChild(4));

                DatabaseUtils.createDatabaseManagementClasses(project, databaseFilePath.toFile()
                        .getName(), isCreateSQLOpenHelper, isCreateContentProviders,
                        sqlOpenHelperPackageName, contentProvidersPackageName,
                        sqlOpenHelperClassName, isOverrideContentProviders, false, subMonitor
                                .newChild(6), true);
            }
            catch (Exception e)
            {
                StudioLogger.error(DatabaseManagementClassesCreationWizard.class,
                        CodeUtilsNLS.DATABASE_DEPLOY_ERROR_DEPLOYING_DATABASE, e);
                IStatus status =
                        new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID,
                                e.getLocalizedMessage());
                EclipseUtils.showErrorDialog(CodeUtilsNLS.DATABASE_DEPLOY_ERROR_DEPLOYING_DATABASE,
                        CodeUtilsNLS.DATABASE_DEPLOY_ERROR_DEPLOYING_DATABASE, status);
            }

        }
    }

    // deployment page
    private DatabaseManagementClassesCreationMainPage page = null;

    public DatabaseManagementClassesCreationWizard(IProject selectedProject,
            IResource selectedDatabase)
    {
        // set title
        setWindowTitle(CodeUtilsNLS.UI_PersistenceWizardPageTitleDeploy);
        // set properties
        setNeedsProgressMonitor(true);
        setDefaultPageImageDescriptor(CodeUtilsActivator.imageDescriptorFromPlugin(
                CodeUtilsActivator.PLUGIN_ID, "icons/wizban/create_management_classes.png")); //$NON-NLS-1$
        // set page
        this.page = new DatabaseManagementClassesCreationMainPage("Main Page", selectedProject, //$NON-NLS-1$
                selectedDatabase);

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.wizard.Wizard#addPages()
     */
    @Override
    public void addPages()
    {
        addPage(this.page);
    }

    /**
     * Generate the requested classes and finishes this wizard.
     */
    @Override
    public boolean performFinish()
    {
        // get project
        IProject project = page.getSelectedProject();
        // get the file name
        IPath databaseFilePath = page.getDatabaseFilePath();
        // get package of content providers classes
        String contentProvidersPackageName = page.getContentProviderPackage();
        // get the package of SQL Open Helper
        String sqlOpenHelperPackageName = page.getDatabaseOpenHelperPackage();
        // get the SQL Open Helper class name
        String sqlOpenHelperClassName = page.getSQLOpenHelperClassName();
        // create or no open helper
        boolean isCreateSQLOpenHelper = page.isCreateSQLOpenHelperClass();
        // create or not content providers
        boolean isCreateContentProviders = page.isCreateContentProviders();
        // override or not content providers
        boolean isOverrideContentProviders = page.isOverrideExistingContentProviderClasses();

        try
        {
            // create database management classes
            getContainer().run(
                    true,
                    true,
                    new GenerateDatabaseManagementClassesRunnableWithProgress(project,
                            databaseFilePath, isCreateSQLOpenHelper, isCreateContentProviders,
                            sqlOpenHelperPackageName, contentProvidersPackageName,
                            sqlOpenHelperClassName, isOverrideContentProviders));
        }
        catch (Exception e)
        {
            StudioLogger.error(DatabaseManagementClassesCreationWizard.class,
                    CodeUtilsNLS.DATABASE_DEPLOY_ERROR_DEPLOYING_DATABASE, e);
            IStatus status =
                    new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage());
            EclipseUtils.showErrorDialog(CodeUtilsNLS.DATABASE_DEPLOY_ERROR_DEPLOYING_DATABASE,
                    CodeUtilsNLS.DATABASE_DEPLOY_ERROR_DEPLOYING_DATABASE, status);
        }

        try
        {

            if ((PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null)
                    && (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() != null))
            {
                if (PlatformUI.getWorkbench().getPerspectiveRegistry()
                        .findPerspectiveWithId(CodeUtilsActivator.PERSPECTIVE_ID) != null)
                {
                    if (!PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                            .getPerspective().getId().equals(CodeUtilsActivator.PERSPECTIVE_ID))
                    {

                        //ask users if they want to change perspective to check files created
                        if (DialogWithToggleUtils
                                .showQuestion(
                                        SDK_SWITCH_PERSPECTIVE_TO_MOTODEV_STUDIO_ANDROID_KEY,
                                        CodeUtilsNLS.UI_PersistenceWizard_ChangePerspectiveToMOTODEVStudioAndroid_DialogTitle,
                                        CodeUtilsNLS.UI_PersistenceWizard_ChangePerspectiveToMOTODEVStudioAndroid_DialogMessage))
                        {
                            PlatformUI.getWorkbench().showPerspective(
                                    CodeUtilsActivator.PERSPECTIVE_ID,
                                    PlatformUI.getWorkbench().getActiveWorkbenchWindow());
                        }
                    }
                }
                else
                {
                    if (DialogWithToggleUtils
                            .showQuestion(
                                    SDK_SWITCH_PERSPECTIVE_TO_JAVA_KEY,
                                    CodeUtilsNLS.UI_PersistenceWizard_ChangePerspectiveToJava_DialogTitle,
                                    CodeUtilsNLS.UI_PersistenceWizard_ChangePerspectiveToJava_DialogMessage))
                    {
                        PlatformUI.getWorkbench().showPerspective(JavaUI.ID_PERSPECTIVE,
                                PlatformUI.getWorkbench().getActiveWorkbenchWindow());
                    }
                }
            }
        }
        catch (WorkbenchException e)
        {
            StudioLogger.warn("It was not possible to change perspective to " //$NON-NLS-1$
                    + CodeUtilsActivator.PERSPECTIVE_ID);
        }

        // create UDC log for db file size
        StudioLogger
                .collectUsageData(
                        UsageDataConstants.WHAT_DATABASE_MANAGMT_CLASSES, //$NON-NLS-1$
                        UsageDataConstants.KIND_DATABASE_MANAGMT_CLASSES,
                        "Database classes created. Database filesize " + getDatabaseFileSize(databaseFilePath) + ".", //$NON-NLS-1$ //$NON-NLS-2$
                        CodeUtilsActivator.PLUGIN_ID, CodeUtilsActivator.getDefault().getBundle()
                                .getVersion().toString());

        return true;
    }

    /**
     * Returns a database file size string in Kb, Mb or Gb. Example: '10 Kb'.
     * @param databaseFilePath The database file path.
     * @return String containing the database file size.
     */
    private String getDatabaseFileSize(IPath databaseFilePath)
    {

        String fileSizeStr = null;

        long fileSize = databaseFilePath.toFile().length();

        long KBsize = fileSize / 1024;

        if (KBsize > 1024)
        {

            long MBSize = KBsize / 1024;

            if (MBSize > 1024)
            {

                long GBSize = MBSize / 1024;

                fileSizeStr = GBSize + " Gb"; //$NON-NLS-1$

            }
            else
            {
                fileSizeStr = MBSize + " Mb"; //$NON-NLS-1$

            }
        }
        else
        {
            fileSizeStr = KBsize + " Kb"; //$NON-NLS-1$

        }

        return fileSizeStr;
    }

}