Search in sources :

Example 6 with TemplateMetadata

use of com.android.tools.idea.templates.TemplateMetadata in project android by JetBrains.

the class Template method doRender.

/**
   * Render the template.
   * Warnings are only generated during a dry run i.e. no files are changed yet.
   * The user may select to proceed anyway in which case we expect another call
   * to render with dry run set to false.
   * Errors may be shown regardless of the dry run flag.
   */
private boolean doRender(@NotNull RenderingContext context) {
    TemplateMetadata metadata = getMetadata();
    assert metadata != null;
    enforceParameterTypes(metadata, context.getParamMap());
    try {
        processFile(context, new File(TEMPLATE_XML_NAME));
        if (!context.showWarnings() || context.getWarnings().isEmpty()) {
            return true;
        }
        if (!context.getProject().isInitialized() && myTemplateRoot.getPath().contains(GOOGLE_GLASS_PATH_19)) {
            // there are files that are overwritten during project creation by the Glass activity templates.
            return true;
        }
        // @formatter:off
        int result = Messages.showOkCancelDialog(context.getProject(), formatWarningMessage(context), String.format("%1$s %2$s", context.getCommandName(), StringUtil.pluralize("Warning")), "Proceed Anyway", "Cancel", Messages.getWarningIcon());
        // @formatter:on
        return result == Messages.OK;
    } catch (TemplateUserVisibleException e) {
        if (context.showErrors()) {
            // @formatter:off
            Messages.showErrorDialog(context.getProject(), formatErrorMessage(context, e), String.format("%1$s Failed", context.getCommandName()));
        // @formatter:on
        } else {
            throw new RuntimeException(e);
        }
        return false;
    } catch (TemplateProcessingException e) {
        throw new RuntimeException(e);
    }
}
Also used : TemplateUserVisibleException(com.android.tools.idea.templates.FreemarkerUtils.TemplateUserVisibleException) TemplateProcessingException(com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException) File(java.io.File) TemplateMetadata(com.android.tools.idea.templates.TemplateMetadata) Constraint(com.android.tools.idea.templates.Parameter.Constraint)

Example 7 with TemplateMetadata

use of com.android.tools.idea.templates.TemplateMetadata in project android by JetBrains.

the class AndroidModuleTemplatesProvider method getModuleTemplates.

@NotNull
@Override
public Iterable<ModuleTemplate> getModuleTemplates() {
    TemplateManager manager = TemplateManager.getInstance();
    List<File> applicationTemplates = manager.getTemplatesInCategory(Template.CATEGORY_APPLICATION);
    List<ModuleTemplate> moduleTemplates = Lists.newArrayList();
    for (File templateFile : applicationTemplates) {
        TemplateMetadata metadata = manager.getTemplateMetadata(templateFile);
        if (metadata == null) {
            continue;
        }
        if (metadata.getFormFactor() != null) {
            moduleTemplates.addAll(getModuleTemplates(metadata, FormFactor.get(metadata.getFormFactor())));
        }
    }
    Collections.sort(moduleTemplates, new Comparator<ModuleTemplate>() {

        @Override
        public int compare(ModuleTemplate t1, ModuleTemplate t2) {
            FormFactor f1 = t1.getFormFactor();
            FormFactor f2 = t2.getFormFactor();
            // because of null check before we added ot moduleTemplates list
            assert f1 != null : t1;
            assert f2 != null : t2;
            return f1.compareTo(f2);
        }
    });
    return moduleTemplates;
}
Also used : TemplateManager(com.android.tools.idea.templates.TemplateManager) File(java.io.File) TemplateMetadata(com.android.tools.idea.templates.TemplateMetadata) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with TemplateMetadata

use of com.android.tools.idea.templates.TemplateMetadata in project android by JetBrains.

the class AddAndroidActivityPath method init.

@Override
protected void init() {
    Module module = getModule();
    assert module != null;
    AndroidFacet facet = AndroidFacet.getInstance(module);
    assert facet != null;
    AndroidPlatform platform = AndroidPlatform.getInstance(module);
    if (platform != null) {
        myState.put(KEY_BUILD_SDK, platform.getTarget().getVersion().getFeatureLevel());
    }
    AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(facet);
    AndroidVersion minSdkVersion = moduleInfo.getMinSdkVersion();
    myState.put(KEY_MIN_SDK, minSdkVersion);
    myState.put(KEY_TARGET_API, moduleInfo.getTargetSdkVersion());
    myState.put(KEY_PACKAGE_NAME, getInitialPackageName(module, facet));
    myState.put(KEY_OPEN_EDITORS, true);
    if (myTemplate == null) {
        FormFactor formFactor = getFormFactor(myTargetFolder);
        myState.put(FormFactorUtils.getMinApiLevelKey(formFactor), minSdkVersion.getApiLevel());
        myState.put(FormFactorUtils.getBuildApiLevelKey(formFactor), moduleInfo.getTargetSdkVersion().getApiLevel());
        ActivityGalleryStep galleryStep = new ActivityGalleryStep(formFactor, false, KEY_SELECTED_TEMPLATE, module, myParentDisposable);
        addStep(galleryStep);
    } else {
        TemplateMetadata templateMetadata = TemplateManager.getInstance().getTemplateMetadata(myTemplate);
        assert templateMetadata != null;
        myState.put(KEY_SELECTED_TEMPLATE, new TemplateEntry(myTemplate, templateMetadata));
    }
    SourceProvider[] sourceProviders = getSourceProviders(module, myTargetFolder);
    boolean isInstantAppModule = facet.getProjectType() == PROJECT_TYPE_ATOM;
    myState.put(IS_INSTANT_APP_KEY, isInstantAppModule);
    myParameterStep = new TemplateParameterStep2(getFormFactor(myTargetFolder), ImmutableMap.of(), myParentDisposable, KEY_PACKAGE_NAME, sourceProviders, CUSTOMIZE_ACTIVITY_TITLE);
    myAssetStudioStep = new IconStep(KEY_SELECTED_TEMPLATE, KEY_SOURCE_PROVIDER, myParentDisposable);
    addStep(myParameterStep);
    addStep(myAssetStudioStep);
}
Also used : IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) SourceProvider(com.android.builder.model.SourceProvider) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) AndroidVersion(com.android.sdklib.AndroidVersion) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) Module(com.intellij.openapi.module.Module) TemplateMetadata(com.android.tools.idea.templates.TemplateMetadata)

Example 9 with TemplateMetadata

use of com.android.tools.idea.templates.TemplateMetadata in project android by JetBrains.

the class ChooseTemplateStep method getTemplateList.

/**
   * Retrieve the metadata for the given list of template files, excluding the files from the excluded set.
   */
protected static List<MetadataListItem> getTemplateList(TemplateWizardState state, List<File> templateFiles, @Nullable Set<String> excluded) {
    TemplateManager manager = TemplateManager.getInstance();
    List<MetadataListItem> metadataList = new ArrayList<>(templateFiles.size());
    for (File template : templateFiles) {
        TemplateMetadata metadata = manager.getTemplateMetadata(template);
        if (metadata == null || !metadata.isSupported()) {
            continue;
        }
        // If we're trying to create a launchable activity, don't include templates that
        // lack the isLauncher parameter.
        Boolean isLauncher = (Boolean) state.get(ATTR_IS_LAUNCHER);
        if (isLauncher != null && isLauncher && metadata.getParameter(TemplateMetadata.ATTR_IS_LAUNCHER) == null) {
            continue;
        }
        // Don't include this template if it's been excluded
        if (excluded != null && excluded.contains(metadata.getTitle())) {
            continue;
        }
        metadataList.add(new MetadataListItem(template, metadata));
    }
    Collections.sort(metadataList);
    return metadataList;
}
Also used : TemplateManager(com.android.tools.idea.templates.TemplateManager) ArrayList(java.util.ArrayList) File(java.io.File) TemplateMetadata(com.android.tools.idea.templates.TemplateMetadata)

Example 10 with TemplateMetadata

use of com.android.tools.idea.templates.TemplateMetadata in project android by JetBrains.

the class ConfigureFormFactorStep method populateAdditionalFormFactors.

private void populateAdditionalFormFactors() {
    TemplateManager manager = TemplateManager.getInstance();
    List<File> applicationTemplates = manager.getTemplatesInCategory(Template.CATEGORY_APPLICATION);
    myFormFactors.clear();
    myFormFactorPanel.removeAll();
    int row = 0;
    for (File templateFile : applicationTemplates) {
        TemplateMetadata metadata = manager.getTemplateMetadata(templateFile);
        if (metadata == null || metadata.getFormFactor() == null) {
            continue;
        }
        FormFactor formFactor = FormFactor.get(metadata.getFormFactor());
        if (formFactor == FormFactor.GLASS && !AndroidSdkUtils.isGlassInstalled()) {
            // Only show Glass if you've already installed the SDK
            continue;
        }
        Integer prevMinSdk = myFormFactors.get(formFactor);
        int templateMinSdk = metadata.getMinSdk();
        if (prevMinSdk == null) {
            myFormFactors.put(formFactor, Math.max(templateMinSdk, formFactor.getMinOfflineApiLevel()));
        } else if (templateMinSdk > prevMinSdk) {
            myFormFactors.put(formFactor, templateMinSdk);
        }
    }
    // One row for each form factor
    GridLayoutManager gridLayoutManager = new GridLayoutManager(myFormFactors.size(), 1);
    gridLayoutManager.setVGap(5);
    gridLayoutManager.setHGap(10);
    myFormFactorPanel.setLayout(gridLayoutManager);
    for (final FormFactor formFactor : myFormFactors.keySet()) {
        GridConstraints c = new GridConstraints();
        c.setRow(row);
        c.setColumn(0);
        c.setFill(GridConstraints.FILL_HORIZONTAL);
        c.setAnchor(GridConstraints.ANCHOR_WEST);
        FormFactorSdkControls controls = new FormFactorSdkControls(formFactor, myFormFactors.get(formFactor), myDisposable, this);
        myControls.add(controls);
        myFormFactorPanel.add(controls.getComponent(), c);
        myFormFactorApiSelectors.put(formFactor, controls);
        row++;
    }
}
Also used : GridLayoutManager(com.intellij.uiDesigner.core.GridLayoutManager) GridConstraints(com.intellij.uiDesigner.core.GridConstraints) TemplateManager(com.android.tools.idea.templates.TemplateManager) File(java.io.File) TemplateMetadata(com.android.tools.idea.templates.TemplateMetadata)

Aggregations

TemplateMetadata (com.android.tools.idea.templates.TemplateMetadata)13 File (java.io.File)9 TemplateManager (com.android.tools.idea.templates.TemplateManager)7 TemplateWizardState (com.android.tools.idea.wizard.template.TemplateWizardState)2 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 Element (org.w3c.dom.Element)2 SourceProvider (com.android.builder.model.SourceProvider)1 AndroidVersion (com.android.sdklib.AndroidVersion)1 IAndroidTarget (com.android.sdklib.IAndroidTarget)1 AndroidModuleInfo (com.android.tools.idea.model.AndroidModuleInfo)1 MetadataListItem (com.android.tools.idea.npw.ChooseTemplateStep.MetadataListItem)1 FormFactor (com.android.tools.idea.npw.FormFactor)1 NewProjectWizardState (com.android.tools.idea.npw.NewProjectWizardState)1 TemplateProcessingException (com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException)1 TemplateUserVisibleException (com.android.tools.idea.templates.FreemarkerUtils.TemplateUserVisibleException)1 Parameter (com.android.tools.idea.templates.Parameter)1 Constraint (com.android.tools.idea.templates.Parameter.Constraint)1 Module (com.intellij.openapi.module.Module)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1