aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewActivityWizard.java
blob: b33d65bb707f8be9a1e20e167da044a5cfc0831d (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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
 *
 * 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.android.ide.eclipse.adt.internal.wizards.templates;

import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_BUILD_API;
import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_MIN_API;
import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_MIN_API_LEVEL;
import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_PACKAGE_NAME;
import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.ATTR_TARGET_API;
import static org.eclipse.core.resources.IResource.DEPTH_INFINITE;

import com.android.annotations.NonNull;
import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.AdtUtils;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ui.IWorkbench;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Set;

/**
 * Wizard for creating new activities. This is a hybrid between a New Project
 * Wizard and a New Template Wizard: it has the "Activity selector" page from
 * the New Project Wizard, which is used to dynamically select a wizard for the
 * second page, but beyond that it runs the normal template wizard when it comes
 * time to create the template.
 */
public class NewActivityWizard extends TemplateWizard {
    private NewTemplatePage mTemplatePage;
    private ActivityPage mActivityPage;
    private NewProjectWizardState mValues;
    private NewTemplateWizardState mActivityValues;
    protected boolean mOnlyActivities;

    /** Creates a new {@link NewActivityWizard} */
    public NewActivityWizard() {
        mOnlyActivities = true;
    }

    @Override
    protected boolean shouldAddIconPage() {
        return mActivityValues.getIconState() != null;
    }

    @Override
    public void init(IWorkbench workbench, IStructuredSelection selection) {
        super.init(workbench, selection);

        setWindowTitle(mOnlyActivities ? "New Activity" : "New Android Object");

        mValues = new NewProjectWizardState();
        mActivityPage = new ActivityPage(mValues, mOnlyActivities, false);

        mActivityValues = mValues.activityValues;
        List<IProject> projects = AdtUtils.getSelectedProjects(selection);
        if (projects.size() == 1) {
            mActivityValues.project = projects.get(0);
        }
    }

    @Override
    public void addPages() {
        super.addPages();
        addPage(mActivityPage);
    }

    @Override
    public IWizardPage getNextPage(IWizardPage page) {
        if (page == mActivityPage) {
            if (mTemplatePage == null) {
                Set<String> hidden = mActivityValues.hidden;
                hidden.add(ATTR_PACKAGE_NAME);
                hidden.add(ATTR_MIN_API);
                hidden.add(ATTR_MIN_API_LEVEL);
                hidden.add(ATTR_TARGET_API);
                hidden.add(ATTR_BUILD_API);

                mTemplatePage = new NewTemplatePage(mActivityValues, true);
                addPage(mTemplatePage);
            }
            return mTemplatePage;
        } else if (page == mTemplatePage && shouldAddIconPage()) {
            WizardPage iconPage = getIconPage(mActivityValues.getIconState());
            mActivityValues.updateIconState(mTemplatePage.getEvaluator());
            return iconPage;
        } else if (page == mTemplatePage
                || shouldAddIconPage() && page == getIconPage(mActivityValues.getIconState())) {
            TemplateMetadata template = mActivityValues.getTemplateHandler().getTemplate();
            if (template != null) {
                if (InstallDependencyPage.isInstalled(template.getDependencies())) {
                    return getPreviewPage(mActivityValues);
                } else {
                    return getDependencyPage(template, true);
                }
            }
        } else {
            TemplateMetadata template = mActivityValues.getTemplateHandler().getTemplate();
            if (template != null && page == getDependencyPage(template, false)) {
                return getPreviewPage(mActivityValues);
            }
        }

        return super.getNextPage(page);
    }

    @Override
    public boolean canFinish() {
        // Deal with lazy creation of some pages: these may not be in the page-list yet
        // since they are constructed lazily, so consider that option here.
        if (mTemplatePage == null || !mTemplatePage.isPageComplete()) {
            return false;
        }

        return super.canFinish();
    }

    @Override
    public boolean performFinish(IProgressMonitor monitor) throws InvocationTargetException {
        boolean success = super.performFinish(monitor);

        if (success) {
            List<Runnable> finalizingTasks = getFinalizingActions();
            for (Runnable r : finalizingTasks) {
                r.run();
            }
            return true;
        }
        return false;
    }

    @Override
    @NonNull
    protected IProject getProject() {
        return mActivityValues.project;
    }

    @Override
    @NonNull
    protected List<String> getFilesToOpen() {
        TemplateHandler activityTemplate = mActivityValues.getTemplateHandler();
        return activityTemplate.getFilesToOpen();
    }

    @Override
    @NonNull
    protected List<Runnable> getFinalizingActions() {
        TemplateHandler activityTemplate = mActivityValues.getTemplateHandler();
        return activityTemplate.getFinalizingActions();
    }

    @Override
    protected List<Change> computeChanges() {
        return mActivityValues.computeChanges();
    }

    /** Wizard for creating other Android components */
    public static class OtherWizard extends NewActivityWizard {
        /** Create new {@link OtherWizard} */
        public OtherWizard() {
            mOnlyActivities = false;
        }
    }
}