Search in sources :

Example 1 with TemplateHandle

use of com.android.tools.idea.npw.template.TemplateHandle in project android by JetBrains.

the class TemplateManager method fillCategory.

private void fillCategory(NonEmptyActionGroup categoryGroup, final String category, ActionManager am) {
    Map<String, File> categoryRow = myCategoryTable.row(category);
    if (CATEGORY_ACTIVITY.equals(category)) {
        AnAction galleryAction = new AnAction() {

            @Override
            public void update(AnActionEvent e) {
                updateAction(e, "Gallery...", true);
            }

            @Override
            public void actionPerformed(AnActionEvent e) {
                // TODO: before submitting this code, change this to only use the new wizard
                if (Boolean.getBoolean("use.npw.modelwizard") && (e.getModifiers() & InputEvent.SHIFT_MASK) == 0) {
                    DataContext dataContext = e.getDataContext();
                    Module module = LangDataKeys.MODULE.getData(dataContext);
                    assert module != null;
                    VirtualFile targetFile = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
                    assert targetFile != null;
                    VirtualFile targetDirectory = targetFile;
                    if (!targetDirectory.isDirectory()) {
                        targetDirectory = targetFile.getParent();
                        assert targetDirectory != null;
                    }
                    AndroidFacet facet = AndroidFacet.getInstance(module);
                    assert facet != null && facet.getAndroidModel() != null;
                    List<TemplateHandle> templateList = getTemplateList(FormFactor.MOBILE);
                    List<AndroidSourceSet> sourceSets = AndroidSourceSet.getSourceSets(facet, targetDirectory);
                    assert (sourceSets.size() > 0);
                    String initialPackageSuggestion = AndroidPackageUtils.getPackageForPath(facet, sourceSets, targetDirectory);
                    Project project = facet.getModule().getProject();
                    // TODO: Missing logic to select the default template
                    RenderTemplateModel renderModel = new RenderTemplateModel(project, templateList.get(0), initialPackageSuggestion, sourceSets.get(0), AndroidBundle.message("android.wizard.activity.add"));
                    NewModuleModel moduleModel = new NewModuleModel(project);
                    ChooseActivityTypeStep chooseActivityTypeStep = new ChooseActivityTypeStep(moduleModel, renderModel, facet, templateList, targetDirectory);
                    ModelWizard wizard = new ModelWizard.Builder().addStep(chooseActivityTypeStep).build();
                    new StudioWizardDialogBuilder(wizard, "New Android Activity").build().show();
                } else {
                    DataContext dataContext = e.getDataContext();
                    final Module module = LangDataKeys.MODULE.getData(dataContext);
                    VirtualFile targetFile = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
                    NewAndroidActivityWizard wizard = new NewAndroidActivityWizard(module, targetFile, null);
                    wizard.init();
                    wizard.show();
                }
            }
        };
        categoryGroup.add(galleryAction);
        categoryGroup.addSeparator();
        setPresentation(category, galleryAction);
    }
    for (String templateName : categoryRow.keySet()) {
        if (EXCLUDED_TEMPLATES.contains(templateName)) {
            continue;
        }
        TemplateMetadata metadata = getTemplateMetadata(myCategoryTable.get(category, templateName));
        NewAndroidComponentAction templateAction = new NewAndroidComponentAction(category, templateName, metadata);
        String actionId = ACTION_ID_PREFIX + category + templateName;
        am.unregisterAction(actionId);
        am.registerAction(actionId, templateAction);
        categoryGroup.add(templateAction);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NewAndroidActivityWizard(com.android.tools.idea.npw.NewAndroidActivityWizard) StudioWizardDialogBuilder(com.android.tools.idea.ui.wizard.StudioWizardDialogBuilder) RenderTemplateModel(com.android.tools.idea.npw.template.RenderTemplateModel) NewAndroidComponentAction(com.android.tools.idea.actions.NewAndroidComponentAction) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) NewModuleModel(com.android.tools.idea.npw.module.NewModuleModel) StudioWizardDialogBuilder(com.android.tools.idea.ui.wizard.StudioWizardDialogBuilder) TemplateHandle(com.android.tools.idea.npw.template.TemplateHandle) ChooseActivityTypeStep(com.android.tools.idea.npw.template.ChooseActivityTypeStep) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) AndroidSourceSet(com.android.tools.idea.npw.project.AndroidSourceSet) ModelWizard(com.android.tools.idea.wizard.model.ModelWizard)

Example 2 with TemplateHandle

use of com.android.tools.idea.npw.template.TemplateHandle in project android by JetBrains.

the class TemplateManager method getTemplateList.

/**
   * Search the given folder for a list of templates and populate the display list.
   */
private List<TemplateHandle> getTemplateList(@NotNull FormFactor formFactor, @NotNull String category, @Nullable Set<String> excluded) {
    List<File> templates = getTemplatesInCategory(category);
    List<TemplateHandle> metadataList = new ArrayList<>(templates.size());
    for (File template : templates) {
        TemplateHandle templateHandle = new TemplateHandle(template);
        TemplateMetadata metadata = templateHandle.getMetadata();
        if (!metadata.isSupported()) {
            continue;
        }
        // Don't include this template if it's been excluded
        if (excluded != null && excluded.contains(metadata.getTitle())) {
            continue;
        }
        // If a form factor has been specified, ensure that requirement is met.
        if (!formFactor.id.equalsIgnoreCase(metadata.getFormFactor())) {
            continue;
        }
        metadataList.add(templateHandle);
    }
    return metadataList;
}
Also used : TemplateHandle(com.android.tools.idea.npw.template.TemplateHandle) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 3 with TemplateHandle

use of com.android.tools.idea.npw.template.TemplateHandle in project android by JetBrains.

the class ConfigureAndroidModuleStep method createDependentSteps.

@NotNull
@Override
protected Collection<? extends ModelWizardStep> createDependentSteps() {
    List<TemplateHandle> templateList = TemplateManager.getInstance().getTemplateList(myFormFactor);
    AndroidSourceSet dummySourceSet = new AndroidSourceSet("main", new AndroidProjectPaths(new File("")));
    RenderTemplateModel renderModel = new RenderTemplateModel(getModel().getProject(), getModel().packageName(), templateList.get(0), dummySourceSet, message("android.wizard.activity.add"));
    // Some changes on this step model trigger changes on the Render Model
    myListeners.listenAll(getModel().getProject(), getModel().moduleName()).withAndFire(() -> {
        String moduleName = getModel().moduleName().get();
        Project project = getModel().getProject().getValue();
        renderModel.getSourceSet().set(new AndroidSourceSet("main", new AndroidProjectPaths(new File(project.getBasePath(), moduleName))));
    });
    myBindings.bind(renderModel.androidSdkInfo(), new SelectedItemProperty<>(mySdkControls));
    myChooseActivityTypeStep = new ChooseActivityTypeStep(getModel(), renderModel, templateList, Lists.newArrayList());
    return Lists.newArrayList(myChooseActivityTypeStep);
}
Also used : Project(com.intellij.openapi.project.Project) TemplateHandle(com.android.tools.idea.npw.template.TemplateHandle) RenderTemplateModel(com.android.tools.idea.npw.template.RenderTemplateModel) ChooseActivityTypeStep(com.android.tools.idea.npw.template.ChooseActivityTypeStep) File(java.io.File) AndroidSourceSet(com.android.tools.idea.npw.project.AndroidSourceSet) AndroidProjectPaths(com.android.tools.idea.npw.project.AndroidProjectPaths) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with TemplateHandle

use of com.android.tools.idea.npw.template.TemplateHandle in project android by JetBrains.

the class NewAndroidComponentAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    DataContext dataContext = e.getDataContext();
    IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (view == null) {
        return;
    }
    Module module = LangDataKeys.MODULE.getData(dataContext);
    if (module == null) {
        return;
    }
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null || facet.getAndroidModel() == null) {
        return;
    }
    VirtualFile targetFile = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
    File file = TemplateManager.getInstance().getTemplateFile(myTemplateCategory, myTemplateName);
    assert targetFile != null;
    VirtualFile targetDirectory = targetFile;
    if (!targetDirectory.isDirectory()) {
        targetDirectory = targetFile.getParent();
        assert targetDirectory != null;
    }
    assert file != null;
    // e.g. "Empty Activity", "Tabbed Activity"
    String activityDescription = e.getPresentation().getText();
    List<AndroidSourceSet> sourceSets = AndroidSourceSet.getSourceSets(facet, targetDirectory);
    assert sourceSets.size() > 0;
    String initialPackageSuggestion = AndroidPackageUtils.getPackageForPath(facet, sourceSets, targetDirectory);
    Project project = module.getProject();
    RenderTemplateModel templateModel = new RenderTemplateModel(project, new TemplateHandle(file), initialPackageSuggestion, sourceSets.get(0), "New " + activityDescription);
    boolean isActivity = isActivityTemplate();
    String dialogTitle = AndroidBundle.message(isActivity ? "android.wizard.new.activity.title" : "android.wizard.new.component.title");
    String stepTitle = AndroidBundle.message(isActivity ? "android.wizard.config.activity.title" : "android.wizard.config.component.title");
    ModelWizard.Builder wizardBuilder = new ModelWizard.Builder();
    wizardBuilder.addStep(new ConfigureTemplateParametersStep(templateModel, stepTitle, sourceSets, facet));
    ModelWizardDialog dialog = new StudioWizardDialogBuilder(wizardBuilder.build(), dialogTitle).setProject(project).build();
    dialog.show();
/*
    // TODO: Implement the getCreatedElements call for the wizard
    final PsiElement[] createdElements = dialog.getCreatedElements();

    for (PsiElement createdElement : createdElements) {
      view.selectElement(createdElement);
    }
    */
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) StudioWizardDialogBuilder(com.android.tools.idea.ui.wizard.StudioWizardDialogBuilder) RenderTemplateModel(com.android.tools.idea.npw.template.RenderTemplateModel) IdeView(com.intellij.ide.IdeView) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) StudioWizardDialogBuilder(com.android.tools.idea.ui.wizard.StudioWizardDialogBuilder) TemplateHandle(com.android.tools.idea.npw.template.TemplateHandle) ConfigureTemplateParametersStep(com.android.tools.idea.npw.template.ConfigureTemplateParametersStep) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) ModelWizardDialog(com.android.tools.idea.wizard.model.ModelWizardDialog) AndroidSourceSet(com.android.tools.idea.npw.project.AndroidSourceSet) ModelWizard(com.android.tools.idea.wizard.model.ModelWizard)

Aggregations

TemplateHandle (com.android.tools.idea.npw.template.TemplateHandle)4 File (java.io.File)4 AndroidSourceSet (com.android.tools.idea.npw.project.AndroidSourceSet)3 RenderTemplateModel (com.android.tools.idea.npw.template.RenderTemplateModel)3 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 ChooseActivityTypeStep (com.android.tools.idea.npw.template.ChooseActivityTypeStep)2 StudioWizardDialogBuilder (com.android.tools.idea.ui.wizard.StudioWizardDialogBuilder)2 ModelWizard (com.android.tools.idea.wizard.model.ModelWizard)2 Module (com.intellij.openapi.module.Module)2 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)2 NewAndroidComponentAction (com.android.tools.idea.actions.NewAndroidComponentAction)1 NewAndroidActivityWizard (com.android.tools.idea.npw.NewAndroidActivityWizard)1 NewModuleModel (com.android.tools.idea.npw.module.NewModuleModel)1 AndroidProjectPaths (com.android.tools.idea.npw.project.AndroidProjectPaths)1 ConfigureTemplateParametersStep (com.android.tools.idea.npw.template.ConfigureTemplateParametersStep)1 ModelWizardDialog (com.android.tools.idea.wizard.model.ModelWizardDialog)1 IdeView (com.intellij.ide.IdeView)1 NotNull (org.jetbrains.annotations.NotNull)1