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

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PartInitException;

import com.motorola.studio.android.codeutils.CodeUtilsActivator;
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.EclipseUtils;
import com.motorola.studio.android.model.BuildingBlockModel;
import com.motorola.studio.android.model.ContentProvider;

/**
 * Class that implements the Content Provider Wizard.
 */
public class NewProviderWizard extends NewBuildingBlocksWizard
{
    private static final String WIZBAN_ICON = "icons/wizban/new_provider_wiz.png";

    private ContentProvider contentProvider = new ContentProvider();

    /* (non-Javadoc)
     * @see org.eclipse.jface.wizard.Wizard#performFinish()
     */
    @Override
    public boolean performFinish()
    {
        boolean saved = false;

        try
        {
            DoSave doSave = new DoSave();

            getContainer().run(false, false, doSave);

            if (doSave.exception != null)
            {
                throw doSave.exception;
            }
            else
            {
                saved = doSave.saved;
            }
        }
        catch (AndroidException e)
        {
            IStatus status =
                    new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage());
            EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle,
                    CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status);
        }
        catch (InvocationTargetException e)
        {
            IStatus status =
                    new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage());
            EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle,
                    CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status);
        }
        catch (InterruptedException e)
        {
            IStatus status =
                    new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage());
            EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle,
                    CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status);
        }

        if (saved)
        {
            ICompilationUnit javaFile =
                    getBuildingBlock().getPackageFragment().getCompilationUnit(
                            getBuildingBlock().getName() + ".java");

            if ((javaFile != null) && javaFile.exists())
            {
                try
                {
                    JavaUI.openInEditor(javaFile);
                }
                catch (PartInitException e)
                {
                    // Do nothing
                    StudioLogger.error(NewProviderWizard.class,
                            "Could not open the content provider " + getBuildingBlock().getName()
                                    + " on an editor.", e);
                }
                catch (JavaModelException e)
                {
                    // Do nothing
                    StudioLogger.error(NewProviderWizard.class,
                            "Could not open the content provider " + getBuildingBlock().getName()
                                    + " on an editor.", e);
                }
            }
        }

        if (saved)
        {
            // Collecting usage data for statistical purposes
            try
            {
                StudioLogger.collectUsageData(StudioLogger.WHAT_BUILDINGBLOCK_PROVIDER,
                        StudioLogger.KIND_BUILDINGBLOCK, StudioLogger.DESCRIPTION_DEFAULT,
                        CodeUtilsActivator.PLUGIN_ID, CodeUtilsActivator.getDefault().getBundle()
                                .getVersion().toString());
            }
            catch (Throwable e)
            {
                //Do nothing, but error on the log should never prevent app from working
            }
        }
        return saved;
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
     */
    public void init(IWorkbench arg0, IStructuredSelection selection)
    {
        setWindowTitle(CodeUtilsNLS.UI_NewProviderWizard_WizardTitle);
        setNeedsProgressMonitor(true);
        setDefaultPageImageDescriptor(CodeUtilsActivator.getImageDescriptor(WIZBAN_ICON));
        contentProvider.configure(selection);
    }

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

    /* (non-Javadoc)
     * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizard#getBuildingBlock()
     */
    @Override
    protected BuildingBlockModel getBuildingBlock()
    {
        return contentProvider;
    }

    /*
     * IRunnableWithProgress object to create the content provider
     */
    private class DoSave implements IRunnableWithProgress
    {
        AndroidException exception = null;

        boolean saved = false;

        public void run(IProgressMonitor monitor) throws InvocationTargetException,
                InterruptedException
        {
            try
            {
                saved = getBuildingBlock().save(getContainer(), monitor);
            }
            catch (AndroidException e)
            {
                exception = e;
            }
        }
    }
}