summaryrefslogtreecommitdiff
path: root/src/plugins/android.codeutils/src/com/motorola/studio/android/codeutils/wizards/DatabaseManagementClassesCreationMainPage.java
blob: ac826f6deb6a0836fbb45c55bb79fb08892ad1eb (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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
/*
 * 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.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ui.wizards.NewTypeWizardPage;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.PlatformUI;

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.utilities.EclipseUtils;
import com.motorola.studio.android.manifest.AndroidProjectManifestFile;
import com.motorola.studio.android.model.manifest.AndroidManifestFile;
import com.motorola.studio.android.model.manifest.dom.ManifestNode;
import com.motorola.studio.android.wizards.elements.FileChooser;
import com.motorola.studio.android.wizards.elements.ProjectChooser;

/**
 * 
 * Wizard page to create classes which aids the database management. 
 *
 */
public class DatabaseManagementClassesCreationMainPage extends NewTypeWizardPage
{
    private static final String CONTEXT_HELP_ID = CodeUtilsActivator.PLUGIN_ID
            + ".create_db_classes";

    /**
     * <p>
     * This listener is called when the Project´s name is modified. Here
     * is checked whether the project´s name represents any available project
     * in the current workspace. In case it does, the project of the wizard is updated and
     * the status is also checked. Otherwise, the status is checked and an error
     * message regarding this validation is displayed.
     * </p>
     * <p>
     * This listener also enables/disables components depending on whether the
     * chosen project is valid. Moreover, it populates the fields accordingly if
     * the selected project is valid.
     * </p>
     */
    private final class ProjectChooserModifyListener implements ModifyListener
    {
        public void modifyText(ModifyEvent e)
        {
            boolean isValidationOK = getProjectStatus(projectChooser.getText()) == null;
            // in case there is no status, the project´s name is OK therefore update the project
            try
            {
                if (isValidationOK)
                {
                    //update fileChooser container
                    fileChooser.setContainer(projectChooser.getProject());
                    // update project
                    updateProject(projectChooser.getProject());
                }
                else
                {
                    fileChooser.setContainer(ResourcesPlugin.getWorkspace().getRoot());
                    // null the project
                    updateProject(null);
                }
            }
            catch (JavaModelException jme)
            {
                StudioLogger.error(this.getClass(), CodeUtilsNLS.Db_GenerateManagementClassesError,
                        jme);
                IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, jme.getLocalizedMessage());
                EclipseUtils.showErrorDialog(CodeUtilsNLS.Db_GenerateManagementClassesError,
                        CodeUtilsNLS.Db_GenerateManagementClassesError, status);
            }

            // enable/disable this wizard´s group
            //setCompositeChildremEnabled(databaseFileGroup, isValidationOK);
            //ckbGenerateSQLOpenHelper.setEnabled(isValidationOK);
            //setCompositeChildremEnabled(sqlOpenHelperGroup, isValidationOK);
            //ckbCreateContentProviders.setEnabled(isValidationOK);
            //setCompositeChildremEnabled(contentProviderGroup, isValidationOK);

            // update status and page completion
            doStatusAndPageCompletionUpdate();
        }
    }

    /**
     * <p>
     * This class represents a listener which is called every time the
     * text field in the file chooser is modified. This listener intends
     * to validate whether the entered path is a valid path or not. To do so,
     * the {@link DatabaseManagementClassesCreationMainPage#doStatusAndPageCompletionUpdate()} is called.
     * </p>
     * <p>
     * This listener also enable/disables components depending on whether the
     * database file is validated correctly.
     * </p>
     */
    private final class DBFileChooserModifyListener implements ModifyListener
    {

        /*
         * (non-Javadoc)
         * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
         */
        public void modifyText(ModifyEvent e)
        {
            // validation flag
            boolean isValidationOK =
                    ((getFileStatus(fileChooser.getText()) == null) || (!getFileStatus(
                            fileChooser.getText()).equals(IStatus.ERROR)));

            if (isValidationOK)
            {
                // set the database file
                selectedDatabasePath = getDatabaseFilePath();
            }

            // enable/disable this wizard´s group
            setCompositeChildremEnabled(sqlOpenHelperGroup, isValidationOK);
            ckbCreateContentProviders.setEnabled(isValidationOK);
            setCompositeChildremEnabled(contentProviderGroup, isValidationOK);

            // update status and page completion
            doStatusAndPageCompletionUpdate();
        }
    }

    /**
     * This listener is dispatched, from the inner part of Source/Package component - {@link SourcePackageChooserPartWizard}.
     * when a message inside it is thrown. This listener calls the {@link DatabaseManagementClassesCreationMainPage#doStatusAndPageCompletionUpdate()}
     * which gets the error message from the Source?Package element and displays it.
     */
    private final class ContentProviderPackageElementMessageActionListener implements
            ActionListener
    {
        /*
         * (non-Javadoc)
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
         */
        public void actionPerformed(ActionEvent e)
        {
            // update status and page completion
            doStatusAndPageCompletionUpdate();
        }
    }

    /**
     * Listener which handles when a check box button is pressed.
     * Basically, it enables or disables all components stored in
     * the constructor parameter {@link Group}. It also
     * starts the event for validating this screen. 
     */
    private final class CkbPanelEnablementListener implements Listener
    {
        private final Group panelEnablementGroup;

        /*
         * Constructor
         * 
         * @param panelEnablementGroup SQL Helper group
         */
        private CkbPanelEnablementListener(Group panelEnablementGroup)
        {
            this.panelEnablementGroup = panelEnablementGroup;
        }

        /*
         * (non-Javadoc)
         * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
         */
        public void handleEvent(Event event)
        {
            // get the check box which dispatched the event
            Button checkBox = event.widget != null ? (Button) event.widget : null;
            // proceed in case there is a check box
            if (checkBox != null)
            {
                // flag indicating whether to enable/disable the controls
                boolean enabled = checkBox.getSelection();
                // enable/disable the children of panelEnablementGroup field
                setCompositeChildremEnabled(panelEnablementGroup, enabled);
            }
            // update status and page completion
            doStatusAndPageCompletionUpdate();
        }
    }

    /* Constants */

    // max size of the database file in bytes
    private static final long MAX_FILE_SIZE = 1048576;

    private static final String MAX_FILE_CHAR_SIZE = "1MB"; //$NON-NLS-1$

    private static final String ASSESTS_FOLDER = "assets";

    private final String PLUGIN_ID = "com.motorola.studio.android.db"; //$NON-NLS-1$

    /* Fields */

    private IProject selectedProject;

    private Button ckbCreateContentProviders;

    private Button ckbOverrideContentProviders;

    private SourcePackageChooserPartWizard contentProviderPackageComposite;

    private Group sqlOpenHelperGroup;

    private Group contentProviderGroup;

    private Group databaseFileGroup;

    private FileChooser fileChooser;

    private Group projectGroup;

    private ProjectChooser projectChooser;

    private ScrolledComposite scroll;

    private Composite innerSchollComposite;

    IPath selectedDatabasePath;

    /**
     * Get the SQL Open Helper class name.
     * 
     * @return SQl Open Helper class name
     */
    public String getSQLOpenHelperClassName()
    {
        return getTypeName();
    }

    /**
     * Get the destination package of the Content Providers
     * 
     * @return Destination package of the Content Providers
     */
    public String getContentProviderPackage()
    {
        return contentProviderPackageComposite != null ? contentProviderPackageComposite
                .getPackageText() : ""; //$NON-NLS-1$
    }

    /**
     * Get the chosen project.
     * @return Selected {@link IProject}.
     */
    public IProject getSelectedProject()
    {
        return selectedProject;
    }

    /**
     * @return The {@link IPath} representing the selected Database file.
     */
    public IPath getDatabaseFilePath()
    {
        // file to be returned
        IPath dbFile = null;
        // text representing the file
        String fileText = fileChooser.getText();
        // get the database file in case there is a selected project
        if (selectedProject != null)
        {
            dbFile = (fileText != null) && (fileText.length() > 0) ? new Path(fileText) : null; //$NON-NLS-1$
        }
        return dbFile;
    }

    /**
     * Get the Database Open Helper Package.
     * 
     * @return Database Open Helper Package.
     */
    public String getDatabaseOpenHelperPackage()
    {
        return getPackageText();
    }

    /**
     * Returns <code>true</code> in case is is necessary to create
     * the Content Provider classes, <code>false</code> otherwise.
     * 
     * @return <code>true</code> in order to create Content Provider
     * classes, <code>false</code> otherwise.
     */
    public boolean isCreateContentProviders()
    {
        return ckbCreateContentProviders != null ? ckbCreateContentProviders.getSelection() : false;
    }

    /**
     * Returns <code>true</code> in case it is necessary to create
     * the Open SQL Helper classes, <code>false</code> otherwise.
     * 
     * @return <code>true</code> in case it is necessary to create SQL
     * Open Helper class, <code>false</code> otherwise.
     */
    public boolean isCreateSQLOpenHelperClass()
    {
        return true;
    }

    /**
     * Returns <code>true</code> in case one wishes to override existing
     * Content Provider classes, <code>false</code> otherwise.
     * 
     * @return <code>true</code> in case one wishes to override existing
     * Content Provider classes, <code>false</code> otherwise.
     */
    public boolean isOverrideExistingContentProviderClasses()
    {
        return ckbOverrideContentProviders != null ? ckbOverrideContentProviders.getSelection()
                : false;
    }

    /**
     * Create a new wizard page based on selection
     * 
     * @param pageName
     *            the page name
     * @param isDeployWaizard
     * 			  show deployment fields            
     */
    public DatabaseManagementClassesCreationMainPage(String pageName, IProject project,
            IResource database)
    {
        // call super
        super(true, pageName);
        // set description and title
        setDescription(CodeUtilsNLS.UI_PersistenceWizardPageDescriptionDeploy);
        setTitle(CodeUtilsNLS.UI_PersistenceWizardPageTitleDeploy);

        // update project
        try
        {
            updateProject(project);
        }
        catch (JavaModelException e)
        {
            StudioLogger.error(this.getClass(), CodeUtilsNLS.Db_GenerateManagementClassesError, e);
            IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, e.getLocalizedMessage());
            EclipseUtils.showErrorDialog(CodeUtilsNLS.Db_GenerateManagementClassesError,
                    CodeUtilsNLS.Db_GenerateManagementClassesError, status);
        }
        // update database
        if (database != null)
        {
            selectedDatabasePath = database.getLocation();
        }
        // update status and page completion
        doStatusAndPageCompletionUpdate();
    }

    /*
     * @see NewContainerWizardPage#handleFieldChanged
     */
    @Override
    protected void handleFieldChanged(String fieldName)
    {
        super.handleFieldChanged(fieldName);
        // update status and page completion
        doStatusAndPageCompletionUpdate();
    }

    /**
     * Creates page content.
     * 
     * @param parent wizard composite.
     */
    public void createControl(Composite parent)
    {
        try
        {
            // main control
            Composite mainComposite = new Composite(parent, SWT.FILL);
            PlatformUI.getWorkbench().getHelpSystem().setHelp(mainComposite, CONTEXT_HELP_ID);
            mainComposite.setLayout(new FillLayout(SWT.FILL));

            scroll = new ScrolledComposite(mainComposite, SWT.H_SCROLL | SWT.V_SCROLL);
            innerSchollComposite = new Composite(scroll, SWT.NONE);

            // set a grid layout with 1 column
            GridLayout layout = new GridLayout();
            layout.numColumns = 1;
            innerSchollComposite.setLayout(layout);
            innerSchollComposite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true,
                    true));

            // create Project selection stuff
            createProjectSelectionSession(innerSchollComposite);

            // create Database File stuff
            createDatabaseFileSession(innerSchollComposite);

            // retrieve package from manifest file
            String manifestPackage = null;
            try
            {
                AndroidManifestFile androidManifestFile =
                        AndroidProjectManifestFile.getFromProject(getSelectedProject());
                ManifestNode manifestNode = androidManifestFile.getManifestNode();
                manifestPackage =
                        manifestNode.getNodeProperties().get(AndroidManifestFile.PROP_PACKAGE);
            }
            catch (Exception e)
            {
                // do nothing; default package value will be passed as null
            }

            // add SQL helper stuff
            createOpenHelperSession(innerSchollComposite, manifestPackage);

            // add content provider stuff
            createContentProviderSession(innerSchollComposite, manifestPackage);

            // add listeners
            addListeners();

            // set up scroll
            scroll.setContent(innerSchollComposite);

            scroll.setExpandHorizontal(true);
            scroll.setExpandVertical(true);

            scroll.addControlListener(new ControlAdapter()
            {
                @Override
                public void controlResized(ControlEvent e)
                {
                    scroll.setMinSize(innerSchollComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
                }
            });

            // set control
            setControl(mainComposite);

            // update project text field
            if (selectedProject != null)
            {
                projectChooser.setText(selectedProject.getName());
            }

            // update database text field
            if (selectedDatabasePath != null)
            {
                fileChooser.setText(selectedDatabasePath.toOSString());
            }

            // set focus on the name text field
            setFocus();
        }
        catch (Exception e)
        {
            StudioLogger.error(this.getClass(), CodeUtilsNLS.Db_GenerateManagementClassesError, e);
            IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, e.getLocalizedMessage());
            EclipseUtils.showErrorDialog(CodeUtilsNLS.Db_GenerateManagementClassesError,
                    CodeUtilsNLS.Db_GenerateManagementClassesError, status);
        }
    }

    /**
     * Add Project Selection Section.
     * 
     * @param mainComposite Main Composite.
     */
    private void createProjectSelectionSession(Composite mainComposite)
    {
        // create layout for Content Provider class creation
        projectGroup = new Group(mainComposite, SWT.NONE);
        projectGroup.setText(CodeUtilsNLS.UI_PersistenceWizardPageSelectProjectTitle);

        projectGroup.setLayout(new GridLayout(5, false));
        projectGroup.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));

        // add project chooser
        projectChooser = new ProjectChooser(projectGroup, SWT.FILL);
    }

    /**
     * Create the Database File Session
     * 
     * @param mainComposite Main composite
     */
    private void createDatabaseFileSession(Composite mainComposite)
    {
        // create group for Database file
        databaseFileGroup = new Group(mainComposite, SWT.NONE);
        databaseFileGroup.setText(CodeUtilsNLS.UI_PersistenceWizardPageDatabaseFileGroupTitle);
        databaseFileGroup.setLayout(new GridLayout(3, false));
        databaseFileGroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));

        // add file chooser
        fileChooser = new FileChooser(selectedProject, databaseFileGroup, SWT.NONE, null);
        fileChooser.setFilterExtensions(new String[]
        {

        });
        fileChooser.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
    }

    /**
     * Create the "Generate SQLOpenHelper" session 
     * 
     * @param mainComposite The Composite used as the main screen of
     * the page.
     * @param defaultPackageName The name of the default package to use
     * on the package field.
     */
    private void createOpenHelperSession(Composite mainComposite, String defaultPackageName)
    {
        // check box for generating SQL Open Helper
        sqlOpenHelperGroup = new Group(mainComposite, SWT.NONE);
        sqlOpenHelperGroup.setText(CodeUtilsNLS.UI_PersistenceWizardPageSQLOpenHelperGroupTitle);
        int numColumns = 5;
        sqlOpenHelperGroup.setLayout(new GridLayout(numColumns, false));
        sqlOpenHelperGroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));

        /* Class selection for SQLOpenHelper */

        createTypeNameControls(sqlOpenHelperGroup, numColumns);

        /* Package selection for SQLOpenHelper */
        setPackageFragmentRoot(getPackageFragmentRoot(), true);
        createContainerControls(sqlOpenHelperGroup, numColumns);
        boolean defaultPackageUsed = false;
        if (defaultPackageName != null)
        {
            // try to use the manifest package, but if this fails, use the default getPackageFragment() logic
            try
            {
                setPackageFragment(getPackageFragmentRoot().getPackageFragment(defaultPackageName),
                        true);
                defaultPackageUsed = true;
            }
            catch (Exception e)
            {
                // do nothing
            }
        }
        if (!defaultPackageUsed)
        {
            setPackageFragment(getPackageFragment(), true);
        }
        // create the controls for the package
        createPackageControls(sqlOpenHelperGroup, numColumns);
    }

    /**
     * Create the "Content Provider" session
     * 
     * @param mainComposite The Composite used as the main screen of
     * the page. 
     * @param defaultPackageName The name of the default package to use
     * on the package field.
     */
    private void createContentProviderSession(Composite mainComposite, String defaultPackageName)
    {
        // create the check for generating content providers
        ckbCreateContentProviders = new Button(mainComposite, SWT.CHECK);
        ckbCreateContentProviders
                .setText(CodeUtilsNLS.UI_PersistenceWizardGenerateContentProvidersForEachTable);
        ckbCreateContentProviders.setSelection(true);

        contentProviderGroup = new Group(mainComposite, SWT.NONE);
        contentProviderGroup
                .setText(CodeUtilsNLS.UI_PersistenceWizardPageContentProviderGroupTitle);
        int numColumns = 5;
        contentProviderGroup.setLayout(new GridLayout(numColumns, false));
        contentProviderGroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));

        // add the Source/Package part
        contentProviderPackageComposite =
                new SourcePackageChooserPartWizard(getName(), selectedProject, defaultPackageName,
                        contentProviderGroup, numColumns);

        // create the check for overriding content providers
        ckbOverrideContentProviders = new Button(contentProviderGroup, SWT.CHECK);
        ckbOverrideContentProviders
                .setText(CodeUtilsNLS.UI_PersistenceWizardOverrideContentProvidersIfAlreadyExists);
        ckbOverrideContentProviders.setSelection(true);
        ckbOverrideContentProviders.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true,
                false, numColumns, 1));
    }

    /**
     * Add listeners to the components
     */
    private void addListeners()
    {
        // add message listener
        contentProviderPackageComposite
                .addMessageNotificationActionListener(new ContentProviderPackageElementMessageActionListener());

        // add listener for content provider panel enablement
        ckbCreateContentProviders.addListener(SWT.Selection, new CkbPanelEnablementListener(
                contentProviderGroup));

        // add listener for DB file chooser
        fileChooser.addModifyListener(new DBFileChooserModifyListener());

        // add listener for the Project chooser
        projectChooser.addModifyListener(new ProjectChooserModifyListener());
    }

    /**
     * Update the status, related to this page´s components,
     * which interferes with this wizard. Moreover, validate the page
     * and launch the {@link NewTypeWizardPage}{@link #setPageComplete(boolean)} method.
     */
    private void doStatusAndPageCompletionUpdate()
    {
        // variable which indicates whether there is any status with an error level
        boolean isAnyStatusWithError = false;
        // variable which indicates whether the wizard validation is OK
        boolean isPageValidated = false;
        // list to hold all status to validate
        List<IStatus> statusList = new ArrayList<IStatus>();
        // array list to be validated
        IStatus[] statusArray = new IStatus[0];

        // get the status from the project chooser
        if (projectChooser != null)
        {
            // get the project Name
            String projectName = projectChooser.getText();

            // validate project´s name
            IStatus status = getProjectStatus(projectName);
            // add status to the list and check for error status, in case there is any
            if (status != null)
            {
                // add status to the list
                statusList.add(status);
                // verify status error
                isAnyStatusWithError = status.getSeverity() == IStatus.ERROR;
            }
        }

        // get the status from the database file chooser path, in case there is no error status
        if (!isAnyStatusWithError && (fileChooser != null))
        {
            // get path
            String path = fileChooser.getText();

            // get the status
            IStatus status = getFileStatus(path);
            // in case it is not null, add it to the list and check error status
            if (status != null)
            {
                statusList.add(status);
                // check error status
                isAnyStatusWithError = status.getSeverity() == IStatus.ERROR;
            }
        }

        // get the status from Database helper name and package selection, in case it is required and there is no error status
        if (!isAnyStatusWithError)
        {
            for (IStatus status : getThisPageStatusList())
            {
                // add the status to its list
                statusList.add(status);
                // verify error status
                isAnyStatusWithError = status.getSeverity() == IStatus.ERROR;
                // in case there is error status, quit the loop
                if (isAnyStatusWithError)
                {
                    break;
                }
            }
            // in case there are still no errors, check for the package name status
            if (!isAnyStatusWithError)
            {
                IStatus status = getDatabasePackageStatus();
                if (status != null)
                {
                    // add status to its list
                    statusList.add(status);
                    // verify whether there is no error status
                    isAnyStatusWithError = status.getSeverity() == IStatus.ERROR;
                }
            }
        }

        // get the status of the content provider part, in case it is required and there is no error status
        if (!isAnyStatusWithError && (ckbCreateContentProviders != null)
                && ckbCreateContentProviders.getSelection())
        {
            if (this.contentProviderPackageComposite != null)
            {
                IStatus status = this.contentProviderPackageComposite.getMostSevereStatus();
                if (status != null)
                {
                    // add status to its list
                    statusList.add(status);
                    // verify whether there is no error status
                    isAnyStatusWithError = status.getSeverity() == IStatus.ERROR;
                }
            }
            // in case there are still no errors, check for the package name status
            if (!isAnyStatusWithError)
            {
                IStatus status = getContentProvidersPackageStatus();
                if (status != null)
                {
                    // add status to its list
                    statusList.add(status);
                    // verify whether there is no error status
                    isAnyStatusWithError = status.getSeverity() == IStatus.ERROR;
                }
            }
        }

        // get the status for minimum requirements of classes generation, if there is no error status
        if (!isAnyStatusWithError)
        {
            IStatus status = getMinimumClassesGenerationRequirementsStatus();
            if (status != null)
            {
                // add status to its list
                statusList.add(status);
                // verify whether there is no error status
                isAnyStatusWithError = status.getSeverity() == IStatus.ERROR;
            }
        }

        // convert to an array
        if (statusList.size() > 0)
        {
            statusArray = new IStatus[statusList.size()];
            for (int index = 0; index < statusList.size(); index++)
            {
                statusArray[index] = statusList.get(index);
            }
        }
        else
        {
            // in case there is no error, status, add an OK status with the default message
            statusArray =
                    new IStatus[]
                    {
                        new Status(IStatus.OK, PLUGIN_ID,
                                CodeUtilsNLS.UI_PersistenceWizardPageDescriptionDeploy)
                    };
        }

        // the most severe status will be displayed and the OK button enabled/disabled.
        updateStatus(statusArray);

        /*
         * The page is validated if:
         * 1 - There are no status errors
         * 2 - The SQL Helper creation and Content Provider creation check boxes exists 
         */
        isPageValidated = (!isAnyStatusWithError) && ((ckbCreateContentProviders != null));

        // set page completion
        setPageComplete(isPageValidated);
    }

    /**
     * Get the SQL Open Helper package name status. Basically, it points an error
     * in case there is no package.
     * 
     * @return SQL Open Helper package name status
     */
    private IStatus getDatabasePackageStatus()
    {
        IStatus status = null;

        // get the package name
        String packageName = getPackageText();
        // it must have a value
        if ((packageName == null) || (packageName.length() == 0))
        {
            status =
                    new Status(
                            IStatus.ERROR,
                            PLUGIN_ID,
                            CodeUtilsNLS.DatabaseManagementClassesCreationMainPage_UI_OpenHelperPackageNameMustNotBeEmpty);
        }
        else
        {
            status = new Status(IStatus.OK, PLUGIN_ID, ""); //$NON-NLS-1$
        }

        return status;
    }

    /**
     * Get the Content Providers package name status. Basically, it points an error
     * in case there is no package.
     * 
     * @return SQL Open Helper package name status
     */
    private IStatus getContentProvidersPackageStatus()
    {
        IStatus status = null;

        // get the package name
        String packageName =
                contentProviderPackageComposite != null ? contentProviderPackageComposite
                        .getPackageText() : ""; //$NON-NLS-1$
        // it must have a value
        if ((packageName == null) || (packageName.length() == 0))
        {
            status =
                    new Status(
                            IStatus.ERROR,
                            PLUGIN_ID,
                            CodeUtilsNLS.DatabaseManagementClassesCreationMainPage_UI_ContentProvidersPackageNameMustNotBeEmpty);
        }
        else
        {
            status = new Status(IStatus.OK, PLUGIN_ID, ""); //$NON-NLS-1$
        }

        return status;
    }

    /**
     * Return the status regarding the mininum requirements
     * for the generation of classes. 
     * 
     * These requirements are simple:
     * there must be either the creation of SQL Open Helper classes.
     * 
     * @return Status regarding minimum requirements for the classes
     * generation. 
     */
    private IStatus getMinimumClassesGenerationRequirementsStatus()
    {
        IStatus status = null;
        status = new Status(IStatus.OK, PLUGIN_ID, ""); //$NON-NLS-1$        
        return status;
    }

    /**
     * Get this page Status list considering only
     * the {@link NewTypeWizardPage} inheritance. It means
     * that this method return status only related to elements
     * from {@link NewTypeWizardPage}. Other things associated, for instance,
     * with {@link FileChooser} or {@link ProjectChooser} are treated
     * some place else.
     * 
     * @return {@link NewTypeWizardPage} related status list
     */
    private IStatus[] getThisPageStatusList()
    {
        return new IStatus[]
        {
                fContainerStatus,
                isEnclosingTypeSelected() ? fEnclosingTypeStatus : fPackageStatus, fTypeNameStatus,
        };
    }

    /**
     * <p>
     * Check whether a Project Name refers to a Project. In case it does,
     * <code>null</code> is returned, otherwise, an {@link IStatus} is returned
     * indicating the error.
     * </p>
     * <p>
     * Note the the Project must belong to the current workspace.
     * </p>
     * 
     * @param projectName Project Name to be validated.
     * @return
     */
    private IStatus getProjectStatus(String projectName)
    {
        IStatus status = null;

        if ((projectName == null) || (projectName.length() == 0))
        {
            // there must be a selected project
            status =
                    new Status(IStatus.ERROR, PLUGIN_ID,
                            CodeUtilsNLS.UI_PersistenceWizardPageThereMustBeASelectedProject);
        }
        else
        {

            // get root workspace
            IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();

            IProject[] projects = workspaceRoot.getProjects();

            // iterate through the projects
            if ((projects != null) && (projects.length > 0))
            {
                // flag indicating whether the project was found
                boolean isProjectFound = false;

                for (IProject project : projects)
                {
                    if (project.getName().equals(projectName))
                    {
                        // the validation is OK, set the flag and quit the loop
                        isProjectFound = true;
                        break;
                    }
                }
                // in case the project was not found, set the flag
                if (!isProjectFound)
                {
                    status =
                            new Status(IStatus.ERROR, PLUGIN_ID,
                                    CodeUtilsNLS.UI_PersistenceWizardPageTheEnteredProjectIsInvalid);
                }
            }
            else
            {
                // there must be a selected project
                status =
                        new Status(IStatus.ERROR, PLUGIN_ID,
                                CodeUtilsNLS.UI_PersistenceWizardPageThereMustBeASelectedProject);
            }
        }

        return status;
    }

    /**
     * Get a file validation status. In case the file is OK, <code>null</code>
     * is returned otherwise an error status is returned.
     * 
     * @param filePath The file path (including the file name) to be
     * validated
     * 
     * @return <code>null</code> in case the validation is OK, an error
     * status otherwise.
     */
    private IStatus getFileStatus(String filePath)
    {
        // get status
        IStatus status = null;
        // there must be a file path and it must not be empty
        if ((filePath == null) || filePath.equals("")) //$NON-NLS-1$
        {
            status =
                    new Status(IStatus.ERROR, PLUGIN_ID,
                            CodeUtilsNLS.UI_PersistenceWizardPageThereMustBeASelectedDatabaseFile);
        }
        else
        {
            // validation result
            boolean isFileOK = false;
            // get path object
            Path path = new Path(filePath);

            // testing if the entered path is a folder
            isFileOK = path.toFile().isFile();
            if (!isFileOK)
            {
                status =
                        new Status(IStatus.ERROR, PLUGIN_ID,
                                CodeUtilsNLS.UI_PersistenceWizardPageTheEnteredPathIsInvalid);
            }
            else
            {
                isFileOK = path.isValidPath(path.toString());

                if (!isFileOK)
                {
                    status =
                            new Status(IStatus.ERROR, PLUGIN_ID,
                                    CodeUtilsNLS.UI_PersistenceWizardPageTheEnteredPathIsInvalid);
                }
                else
                {
                    // Test if file exists
                    isFileOK = path.toFile().exists();
                    if (!isFileOK)
                    {

                        status =
                                new Status(IStatus.ERROR, PLUGIN_ID,
                                        CodeUtilsNLS.UI_PersistenceWizardPageFileDoesNotExist);
                    }
                    else
                    {
                        try
                        {
                            if (!DatabaseUtils.isValidSQLiteDatabase(path.toFile()))
                            {
                                status =
                                        new Status(IStatus.ERROR, PLUGIN_ID,
                                                CodeUtilsNLS.UI_PersistenceWizardPageFileNotValid);

                            }
                        }
                        catch (IOException e)
                        {
                            status =
                                    new Status(IStatus.WARNING, PLUGIN_ID,
                                            CodeUtilsNLS.UI_PersistenceWizardPageFileNotEvaluated);
                        }

                        if (path.toFile().length() > MAX_FILE_SIZE)
                        {
                            status =
                                    new Status(IStatus.WARNING, PLUGIN_ID, NLS.bind(
                                            CodeUtilsNLS.UI_PersistenceWizardPageFileTooLarge,
                                            MAX_FILE_CHAR_SIZE));
                        }
                    }

                }
            }
        }

        // in case there is no status, the validation went OK, or if the status is not an ERROR 
        // verify whether the file belongs to the project assets
        if ((status == null) || (status.getSeverity() != IStatus.ERROR))
        {
            // verify whether the file is within asset´s folder
            if (!isFileWithinProjectAssets(new Path(filePath)))
            {
                // set the status
                status =
                        new Status(
                                IStatus.WARNING,
                                PLUGIN_ID,
                                CodeUtilsNLS.UI_PersistenceWizardPageTheDatabaseFileWillBeCopiedToProjectsAssetsFolder);
            }
        }

        return status;
    }

    /**
     * Verifies whether a given file path belongs to the Project´s assets
     * folder. <code>true</code> is returned in case it does, <code>false</code> otherwise.
     * 
     * @param path File path which will be verified whether it is in the Project´s assets directory.
     * 
     * @return <code>true</code> in case the file path belongs to the Project´s assets, <code>false</code>
     * otherwise.
     */
    private boolean isFileWithinProjectAssets(IPath path)
    {
        boolean isPathWithin = false;

        // proceed in case there is a project and a path
        if ((selectedProject != null) && (path != null))
        {
            // make the assets path = project path + assets + file name
            IPath databaseInAssetsPath =
                    selectedProject.getLocation().addTrailingSeparator().append(ASSESTS_FOLDER)
                            .addTrailingSeparator().append(path.toFile().getName());

            // the database path and the entered path must match
            isPathWithin = databaseInAssetsPath.toOSString().equals(path.toOSString());
        }

        return isPathWithin;
    }

    /**
     * Enable/disable children of the entered {@link Composite}.
     * 
     * @param composite Composite to have its children enabled/disabled
     * @param enabled <code>true</code> for enabling the elements, <code>false</code>
     * for disabling the elements.
     */
    private void setCompositeChildremEnabled(Composite composite, boolean enabled)
    {
        Control[] controls = composite.getChildren();
        if ((controls != null) && (controls.length > 0))
        {
            for (Control control : controls)
            {
                control.setEnabled(enabled);
            }
        }
    }

    /**
     * Update project information within this wizard. Besides
     * the basic project field input update, the package fragment
     * root is determined and correctly updated in this wizard. Therefore,
     * use this method preferably to update the project so everything in the
     * wizard is updated accordingly.
     * 
     * @param project {@link IProject} information to update
     * @throws JavaModelException Exception thrown when there are problems retrieving
     * the project´s fragment root.
     */
    private void updateProject(IProject project) throws JavaModelException
    {
        if (project != null)
        {
            // set selected project
            this.selectedProject = project;

            // update project text in case it is not already set correctly
            if ((projectChooser != null) && !projectChooser.getText().equals(project.getName()))
            {
                projectChooser.setText(project.getName());
            }

            // get the java project
            IJavaProject javaProject = JavaCore.create(project);
            IPackageFragmentRoot[] possibleRoots = null;
            // continue in case it does exist
            if (javaProject != null)
            {
                // get all possible roots
                possibleRoots = javaProject.getPackageFragmentRoots();
                // select the first one, in case it does exist
                if ((possibleRoots != null) && (possibleRoots.length > 0))
                {
                    // set the first one
                    setPackageFragmentRoot(possibleRoots[0], true);
                    if (contentProviderPackageComposite != null)
                    {
                        contentProviderPackageComposite.setPackageFragmentRoot(possibleRoots[0],
                                true);
                    }
                }
            }
        }
        else
        {
            // update null information
            selectedProject = null;
            setPackageFragmentRoot(null, true);
            if (contentProviderPackageComposite != null)
            {
                contentProviderPackageComposite.setPackageFragmentRoot(null, true);
            }
        }
    }
}