aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/NewTemplateWizardState.java
blob: 2c97003f26591aed4ae57a9cd372ae37e93e762d (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
/*
 * 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_COPY_ICONS;
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 com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.IS_LIBRARY_PROJECT;
import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.IS_NEW_PROJECT;
import static com.android.ide.eclipse.adt.internal.wizards.templates.NewTemplateWizard.BLANK_ACTIVITY;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
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.manifest.ManifestInfo;
import com.android.ide.eclipse.adt.internal.sdk.ProjectState;
import com.android.ide.eclipse.adt.internal.sdk.Sdk;
import com.android.sdklib.IAndroidTarget;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.NullChange;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Value object which holds the current state of the wizard pages for the
 * {@link NewTemplateWizard}
 */
public class NewTemplateWizardState {
    /** Template handler responsible for instantiating templates and reading resources */
    private TemplateHandler mTemplateHandler;

    /** Configured parameters, by id */
    public final Map<String, Object> parameters = new HashMap<String, Object>();

    /** Configured defaults for the parameters, by id */
    public final Map<String, String> defaults = new HashMap<String, String>();

    /** Ids for parameters which should be hidden (because the client wizard already
     * has information for these parameters) */
    public final Set<String> hidden = new HashSet<String>();

    /**
     * The chosen project (which may be null if the wizard page is being
     * embedded in the new project wizard)
     */
    public IProject project;

    /** The minimum API level to use for this template */
    public int minSdkLevel;

    /** Location of the template being created */
    private File mTemplateLocation;

    /**
     * State for the asset studio wizard, used to create custom icons provided
     * the icon requests it with an {@code <icons>} element
     */
    private CreateAssetSetWizardState mIconState;

    /**
     * Create a new state object for use by the {@link NewTemplatePage}
     */
    public NewTemplateWizardState() {
        parameters.put(IS_NEW_PROJECT, false);
    }

    @NonNull
    TemplateHandler getTemplateHandler() {
        if (mTemplateHandler == null) {
            File inputPath;
            if (mTemplateLocation != null) {
                inputPath = mTemplateLocation;
            } else {
                // Default
                inputPath = TemplateManager.getTemplateLocation(BLANK_ACTIVITY);
            }
            mTemplateHandler = TemplateHandler.createFromPath(inputPath);
        }

        return mTemplateHandler;
    }

    /** Sets the current template */
    void setTemplateLocation(File file) {
        if (!file.equals(mTemplateLocation)) {
            mTemplateLocation = file;
            mTemplateHandler = null;
        }
    }

    /** Returns the current template */
    File getTemplateLocation() {
        return mTemplateLocation;
    }

    /** Returns the min SDK version to use */
    int getMinSdk() {
        if (project == null) {
            return -1;
        }
        ManifestInfo manifest = ManifestInfo.get(project);
        return manifest.getMinSdkVersion();
    }

    /** Returns the build API version to use */
    int getBuildApi() {
        if (project == null) {
            return -1;
        }
        IAndroidTarget target = Sdk.getCurrent().getTarget(project);
        if (target != null) {
            return target.getVersion().getApiLevel();
        }

        return getMinSdk();
    }

    /** Computes the changes this wizard will make */
    @NonNull
    List<Change> computeChanges() {
        if (project == null) {
            return Collections.emptyList();
        }

        ManifestInfo manifest = ManifestInfo.get(project);
        parameters.put(ATTR_PACKAGE_NAME, manifest.getPackage());
        parameters.put(ATTR_MIN_API, manifest.getMinSdkName());
        parameters.put(ATTR_MIN_API_LEVEL, manifest.getMinSdkVersion());
        parameters.put(ATTR_TARGET_API, manifest.getTargetSdkVersion());
        parameters.put(ATTR_BUILD_API, getBuildApi());
        parameters.put(ATTR_COPY_ICONS, mIconState == null);
        ProjectState projectState = Sdk.getProjectState(project);
        parameters.put(IS_LIBRARY_PROJECT,
                projectState != null ? projectState.isLibrary() : false);

        TemplateHandler.addDirectoryParameters(parameters, project);

        List<Change> changes = getTemplateHandler().render(project, parameters);

        if (mIconState != null) {
            String title = String.format("Generate icons (res/drawable-<density>/%1$s.png)",
                    mIconState.outputName);
            changes.add(new NullChange(title) {
                @Override
                public Change perform(IProgressMonitor pm) throws CoreException {
                    ConfigureAssetSetPage.generateIcons(mIconState.project,
                            mIconState, false, null);

                    // Not undoable: just return null instead of an undo-change.
                    return null;
                }
            });

        }

        return changes;
    }

    @NonNull
    CreateAssetSetWizardState getIconState() {
        if (mIconState == null) {
            TemplateHandler handler = getTemplateHandler();
            if (handler != null) {
                TemplateMetadata template = handler.getTemplate();
                if (template != null) {
                    mIconState = template.getIconState(project);
                }
            }
        }

        return mIconState;
    }

    /**
     * Updates the icon state, such as the output name, based on other parameter settings
     * @param evaluator the string evaluator, or null if none exists
     */
    public void updateIconState(@Nullable StringEvaluator evaluator) {
        TemplateMetadata template = getTemplateHandler().getTemplate();
        if (template != null) {
            if (evaluator == null) {
                evaluator = new StringEvaluator();
            }
            template.updateIconName(template.getParameters(), evaluator);
        }
    }
}