aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateWizard.java
blob: 7ca32f91f547499c193b1ffe180bccaa9d009585 (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
/*
 * 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 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.internal.assetstudio.ConfigureAssetSetPage;
import com.android.ide.eclipse.adt.internal.assetstudio.CreateAssetSetWizardState;
import com.android.ide.eclipse.adt.internal.editors.IconFactory;
import com.google.common.collect.Lists;

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.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.swing.SwingUtilities;

abstract class TemplateWizard extends Wizard implements INewWizard {
    private static final String PROJECT_LOGO_LARGE = "android-64"; //$NON-NLS-1$
    protected IWorkbench mWorkbench;
    private UpdateToolsPage mUpdatePage;
    private InstallDependencyPage mDependencyPage;
    private TemplatePreviewPage mPreviewPage;
    protected ConfigureAssetSetPage mIconPage;

    protected TemplateWizard() {
    }

    /** Should this wizard add an icon page? */
    protected boolean shouldAddIconPage() {
        return false;
    }

    @Override
    public void init(IWorkbench workbench, IStructuredSelection selection) {
        mWorkbench = workbench;

        setHelpAvailable(false);
        ImageDescriptor desc = IconFactory.getInstance().getImageDescriptor(PROJECT_LOGO_LARGE);
        setDefaultPageImageDescriptor(desc);

        if (!UpdateToolsPage.isUpToDate()) {
            mUpdatePage = new UpdateToolsPage();
        }

        setNeedsProgressMonitor(true);

        // Trigger a check to see if the SDK needs to be reloaded (which will
        // invoke onSdkLoaded asynchronously as needed).
        AdtPlugin.getDefault().refreshSdk();
    }

    @Override
    public void addPages() {
        super.addPages();
        if (mUpdatePage != null) {
            addPage(mUpdatePage);
        }
    }

    @Override
    public IWizardPage getStartingPage() {
        if (mUpdatePage != null && mUpdatePage.isPageComplete()) {
            return getNextPage(mUpdatePage);
        }

        return super.getStartingPage();
    }

    protected WizardPage getPreviewPage(NewTemplateWizardState values) {
        if (mPreviewPage == null) {
            mPreviewPage = new TemplatePreviewPage(values);
            addPage(mPreviewPage);
        }

        return mPreviewPage;
    }

    protected WizardPage getIconPage(CreateAssetSetWizardState iconState) {
        if (mIconPage == null) {
            mIconPage = new ConfigureAssetSetPage(iconState);
            mIconPage.setTitle("Configure Icon");
            addPage(mIconPage);
        }

        return mIconPage;
    }

    protected WizardPage getDependencyPage(TemplateMetadata template, boolean create) {
        if (!create) {
            return mDependencyPage;
        }

        if (mDependencyPage == null) {
            mDependencyPage = new InstallDependencyPage();
            addPage(mDependencyPage);
        }
        mDependencyPage.setTemplate(template);
        return mDependencyPage;
    }

    /**
     * Returns the project where the template is being inserted
     *
     * @return the project to insert the template into
     */
    @NonNull
    protected abstract IProject getProject();

    /**
     * Returns the list of files to open, which might be empty. This method will
     * only be called <b>after</b> {@link #computeChanges()} has been called.
     *
     * @return a list of files to open
     */
    @NonNull
    protected abstract List<String> getFilesToOpen();

    /**
     * Returns the list of files to open, which might be empty. This method will
     * only be called <b>after</b> {@link #computeChanges()} has been called.
     *
     * @return a list of files to open
     */
    @NonNull
    protected abstract List<Runnable> getFinalizingActions();

    /**
     * Computes the changes to the {@link #getProject()} this template should
     * perform
     *
     * @return the changes to perform
     */
    protected abstract List<Change> computeChanges();

    protected boolean performFinish(IProgressMonitor monitor) throws InvocationTargetException {
        List<Change> changes = computeChanges();
        if (!changes.isEmpty()) {
            monitor.beginTask("Creating template...", changes.size());
            try {
                CompositeChange composite = new CompositeChange("",
                        changes.toArray(new Change[changes.size()]));
                composite.perform(monitor);
            } catch (CoreException e) {
                AdtPlugin.log(e, null);
                throw new InvocationTargetException(e);
            } finally {
                monitor.done();
            }
        }

        // TBD: Is this necessary now that we're using IFile objects?
        try {
            getProject().refreshLocal(DEPTH_INFINITE, new NullProgressMonitor());
        } catch (CoreException e) {
            AdtPlugin.log(e, null);
        }
        return true;
    }

    @Override
    public boolean performFinish() {
        final AtomicBoolean success = new AtomicBoolean();
        try {
            getContainer().run(true, false, new IRunnableWithProgress() {
                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException,
                InterruptedException {
                    boolean ok = performFinish(monitor);
                    success.set(ok);
                }
            });

        } catch (InvocationTargetException e) {
            AdtPlugin.log(e, null);
            return false;
        } catch (InterruptedException e) {
            AdtPlugin.log(e, null);
            return false;
        }

        if (success.get()) {
            // Open the primary file/files
            NewTemplateWizard.openFiles(getProject(), getFilesToOpen(), mWorkbench);
            return true;
        } else {
            return false;
        }
    }
}