Search in sources :

Example 1 with Parameter

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

the class TemplateWizardStep method refreshUiFromParameters.

public void refreshUiFromParameters() {
    if (myTemplateState.myTemplate == null) {
        return;
    }
    for (Parameter param : myTemplateState.myTemplate.getMetadata().getParameters()) {
        if (param.initial != null && !myTemplateState.myModified.contains(param.id)) {
            myTemplateState.myParameters.remove(param.id);
        }
    }
    myTemplateState.setParameterDefaults();
    Template.convertApisToInt(myTemplateState.getParameters());
    boolean oldIgnoreUpdates = myIgnoreUpdates;
    try {
        myIgnoreUpdates = true;
        for (String paramName : myParamFields.keySet()) {
            if (myTemplateState.myHidden.contains(paramName)) {
                continue;
            }
            JComponent component = myParamFields.get(paramName);
            Object value = myTemplateState.get(paramName);
            if (value == null) {
                continue;
            }
            if (component instanceof JCheckBox) {
                ((JCheckBox) component).setSelected(Boolean.parseBoolean(value.toString()));
            } else if (component instanceof JComboBox) {
                for (int i = 0; i < ((JComboBox) component).getItemCount(); i++) {
                    if (((ApiComboBoxItem) ((JComboBox) component).getItemAt(i)).getData().equals(value)) {
                        ((JComboBox) component).setSelectedIndex(i);
                        break;
                    }
                }
            } else if (component instanceof JTextField) {
                ((JTextField) component).setText(value.toString());
            } else if (component instanceof TextFieldWithBrowseButton) {
                ((TextFieldWithBrowseButton) component).setText(value.toString());
            } else if (component instanceof JSlider) {
                ((JSlider) component).setValue(Integer.parseInt(value.toString()));
            } else if (component instanceof JSpinner) {
                ((JSpinner) component).setValue(Integer.parseInt(value.toString()));
            } else if (component instanceof ColorPanel) {
                ((ColorPanel) component).setSelectedColor((Color) value);
            }
        }
    } finally {
        myIgnoreUpdates = oldIgnoreUpdates;
    }
}
Also used : ApiComboBoxItem(com.android.tools.idea.ui.ApiComboBoxItem) TextFieldWithBrowseButton(com.intellij.openapi.ui.TextFieldWithBrowseButton) ColorPanel(com.intellij.ui.ColorPanel) Parameter(com.android.tools.idea.templates.Parameter)

Example 2 with Parameter

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

the class TemplateWizardStep method updateParams.

/**
   * Called by update to write the new values of the parameters being edited into the template model.
   */
public void updateParams() {
    if (!myVisible) {
        return;
    }
    Template.convertApisToInt(myTemplateState.getParameters());
    Component focusedComponent = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    setDescriptionHtml("");
    setErrorHtml("");
    for (String paramName : myParamFields.keySet()) {
        if (myTemplateState.myHidden.contains(paramName)) {
            continue;
        }
        Parameter param = myTemplateState.hasTemplate() ? myTemplateState.getTemplateMetadata().getParameter(paramName) : null;
        Object oldValue = myTemplateState.get(paramName);
        JComponent component = myParamFields.get(paramName);
        if (component == focusedComponent || component.isAncestorOf(focusedComponent)) {
            String help = param != null && param.help != null && param.help.length() > 0 ? param.help : getHelpText(paramName);
            setDescriptionHtml(help);
        }
        Object newValue = getComponentValue(param, component);
        if (newValue != null && !newValue.equals(oldValue)) {
            myTemplateState.put(paramName, newValue);
            if (oldValue != null) {
                myTemplateState.myModified.add(paramName);
            }
            if (!myIdsWithNewValues.contains(paramName)) {
                myIdsWithNewValues.add(paramName);
            }
        }
    }
    for (Map.Entry<JRadioButton, Pair<String, Object>> entry : myRadioButtonValues.entrySet()) {
        if (entry.getKey().isSelected()) {
            Pair<String, Object> value = entry.getValue();
            myTemplateState.put(value.getFirst(), value.getSecond());
        }
    }
}
Also used : Parameter(com.android.tools.idea.templates.Parameter) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashBiMap(com.google.common.collect.HashBiMap) Pair(com.android.utils.Pair)

Example 3 with Parameter

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

the class TemplateWizardStep method validate.

@Override
public boolean validate() {
    Template.convertApisToInt(myTemplateState.getParameters());
    if (!myVisible) {
        return true;
    }
    Integer minApi = (Integer) myTemplateState.get(ATTR_MIN_API_LEVEL);
    Integer buildApi = (Integer) myTemplateState.get(ATTR_BUILD_API);
    for (String paramName : myParamFields.keySet()) {
        if (myTemplateState.myHidden.contains(paramName)) {
            continue;
        }
        Parameter param = myTemplateState.hasTemplate() ? myTemplateState.getTemplateMetadata().getParameter(paramName) : null;
        if (param != null) {
            Set<Object> relatedValues = Sets.newHashSet();
            for (Parameter related : myTemplateState.getTemplateMetadata().getRelatedParams(param)) {
                relatedValues.add(myTemplateState.get(related.id));
            }
            String error = param.validate(myProject, myModule, myTemplateState.getSourceProvider(), (String) myTemplateState.get(ATTR_PACKAGE_NAME), myTemplateState.get(paramName), relatedValues);
            if (error != null) {
                setErrorHtml(error);
                return false;
            }
            // Check to see that the selection's constraints are met if this is a combo box
            if (myComboBoxValues.containsKey(param)) {
                ApiComboBoxItem selectedItem = myComboBoxValues.get(param);
                if (selectedItem == null) {
                    return false;
                }
                if (minApi == null || buildApi == null) {
                    return false;
                }
                String message = selectedItem.validate(minApi, buildApi);
                if (message != null) {
                    setErrorHtml(message);
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : Parameter(com.android.tools.idea.templates.Parameter) ApiComboBoxItem(com.android.tools.idea.ui.ApiComboBoxItem)

Example 4 with Parameter

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

the class TemplateWizardState method setTemplateLocation.

/**
   * Sets the current template
   */
public void setTemplateLocation(@NotNull File file) {
    if (myTemplate == null || !myTemplate.getRootPath().getAbsolutePath().equals(file.getAbsolutePath())) {
        // Clear out any parameters from the old template and bring in the defaults for the new template.
        if (myTemplate != null && myTemplate.getMetadata() != null) {
            for (Parameter param : myTemplate.getMetadata().getParameters()) {
                if (!myFinal.contains(param.id)) {
                    myParameters.remove(param.id);
                }
            }
        }
        myTemplate = Template.createFromPath(file);
        setParameterDefaults();
    }
}
Also used : Parameter(com.android.tools.idea.templates.Parameter)

Example 5 with Parameter

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

the class NewFormFactorModulePath method renderActivity.

private boolean renderActivity(boolean dryRun, Map<String, Object> templateState, File projectRoot, File moduleRoot) {
    TemplateEntry templateEntry = myState.get(KEY_SELECTED_TEMPLATE);
    if (templateEntry == null) {
        return true;
    }
    Template activityTemplate = templateEntry.getTemplate();
    for (Parameter parameter : templateEntry.getMetadata().getParameters()) {
        templateState.put(parameter.id, myState.get(myParameterStep.getParameterKey(parameter)));
    }
    RenderingContext activityContext = RenderingContext.Builder.newContext(activityTemplate, myWizard.getProject()).withCommandName("New Module").withDryRun(dryRun).withShowErrors(true).withOutputRoot(projectRoot).withModuleRoot(moduleRoot).withParams(templateState).withGradleSync(myGradleSyncIfNecessary).intoTargetFiles(myState.get(TARGET_FILES_KEY)).intoOpenFiles(myState.get(FILES_TO_OPEN_KEY)).intoDependencies(myState.get(DEPENDENCIES_KEY)).build();
    return activityTemplate.render(activityContext);
}
Also used : RenderingContext(com.android.tools.idea.templates.recipe.RenderingContext) Parameter(com.android.tools.idea.templates.Parameter) Template(com.android.tools.idea.templates.Template)

Aggregations

Parameter (com.android.tools.idea.templates.Parameter)6 ApiComboBoxItem (com.android.tools.idea.ui.ApiComboBoxItem)2 Template (com.android.tools.idea.templates.Template)1 TemplateMetadata (com.android.tools.idea.templates.TemplateMetadata)1 RenderingContext (com.android.tools.idea.templates.recipe.RenderingContext)1 Pair (com.android.utils.Pair)1 BiMap (com.google.common.collect.BiMap)1 HashBiMap (com.google.common.collect.HashBiMap)1 TextFieldWithBrowseButton (com.intellij.openapi.ui.TextFieldWithBrowseButton)1 ColorPanel (com.intellij.ui.ColorPanel)1 Map (java.util.Map)1