/plugin-module-codegen-maven-client/src/main/java/com/atlassian/maven/plugins/amps/codegen/prompter/common/web/WebResourcePrompter.java

https://bitbucket.org/mmeinhold/amps · Java · 151 lines · 112 code · 36 blank · 3 comment · 5 complexity · 72b5466eab680a00e55002d4844de5df MD5 · raw file

  1. package com.atlassian.maven.plugins.amps.codegen.prompter.common.web;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.atlassian.maven.plugins.amps.codegen.annotations.ModuleCreatorClass;
  5. import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
  6. import com.atlassian.plugins.codegen.modules.common.Resource;
  7. import com.atlassian.plugins.codegen.modules.common.web.WebResourceModuleCreator;
  8. import com.atlassian.plugins.codegen.modules.common.web.WebResourceProperties;
  9. import com.atlassian.plugins.codegen.modules.common.web.WebResourceTransformation;
  10. import org.codehaus.plexus.components.interactivity.Prompter;
  11. import org.codehaus.plexus.components.interactivity.PrompterException;
  12. /**
  13. * @since 3.6
  14. */
  15. @ModuleCreatorClass(WebResourceModuleCreator.class)
  16. public class WebResourcePrompter extends AbstractWebFragmentPrompter<WebResourceProperties>
  17. {
  18. public static final String CUSTOM_CONTEXT = "Custom Context";
  19. public WebResourcePrompter(Prompter prompter)
  20. {
  21. super(prompter);
  22. }
  23. @Override
  24. public WebResourceProperties promptForBasicProperties(PluginModuleLocation moduleLocation) throws PrompterException
  25. {
  26. String moduleName = promptNotBlank("Enter Plugin Module Name", "My Web Resource");
  27. WebResourceProperties props = new WebResourceProperties(moduleName);
  28. List<Resource> resourceList = new ArrayList<Resource>();
  29. resourceList.add(promptForResource());
  30. promptForResources(resourceList);
  31. props.setResources(resourceList);
  32. suppressAdvancedNamePrompt();
  33. return props;
  34. }
  35. @Override
  36. public void promptForAdvancedProperties(WebResourceProperties props, PluginModuleLocation moduleLocation) throws PrompterException
  37. {
  38. props.setDependencies(promptForList("Add Dependency?", "Enter Dependency"));
  39. props.setContexts(promptForContexts(props.knownContexts()));
  40. props.setTransformations(promptForTransformations());
  41. props.setConditions(promptForConditions());
  42. }
  43. private List<String> promptForContexts(List<String> knownContexts) throws PrompterException
  44. {
  45. List<String> contexts = new ArrayList<String>();
  46. List<String> mutableValues = new ArrayList<String>(knownContexts);
  47. promptForContext(contexts, mutableValues);
  48. return contexts;
  49. }
  50. private void promptForContext(List<String> contexts, List<String> knownContexts) throws PrompterException
  51. {
  52. if (promptForBoolean("Add Web Resource Context?", "N"))
  53. {
  54. StringBuilder contextQuery = new StringBuilder("Choose A Context\n");
  55. List<String> indexChoices = new ArrayList<String>(knownContexts.size() + 1);
  56. int index = 1;
  57. String strIndex;
  58. for (String context : knownContexts)
  59. {
  60. strIndex = Integer.toString(index);
  61. contextQuery.append(strIndex + ": " + context + "\n");
  62. indexChoices.add(strIndex);
  63. index++;
  64. }
  65. strIndex = Integer.toString(index);
  66. contextQuery.append(strIndex + ": " + CUSTOM_CONTEXT + "\n");
  67. indexChoices.add(strIndex);
  68. contextQuery.append("Choose a number: ");
  69. String contextAnswer = prompt(contextQuery.toString(), indexChoices, "1");
  70. int selectedIndex = Integer.parseInt(contextAnswer) - 1;
  71. String selectedContext;
  72. if (selectedIndex < (indexChoices.size() - 1))
  73. {
  74. selectedContext = knownContexts.get(selectedIndex);
  75. knownContexts.remove(selectedIndex);
  76. } else
  77. {
  78. selectedContext = promptNotBlank("Enter Context");
  79. }
  80. contexts.add(selectedContext);
  81. promptForContext(contexts, knownContexts);
  82. }
  83. }
  84. private List<WebResourceTransformation> promptForTransformations() throws PrompterException
  85. {
  86. List<WebResourceTransformation> transformations = new ArrayList<WebResourceTransformation>();
  87. promptForTransformation(transformations);
  88. return transformations;
  89. }
  90. private void promptForTransformation(List<WebResourceTransformation> transformations) throws PrompterException
  91. {
  92. if (promptForBoolean("Add Web Resource Transformation?", "N"))
  93. {
  94. String extension = promptNotBlank("File Extension");
  95. WebResourceTransformation transformation = new WebResourceTransformation(extension);
  96. List<String> transformers = new ArrayList<String>();
  97. transformers.add(promptForTransformerKey());
  98. promptForTransformers(transformers);
  99. transformation.setTransformerKeys(transformers);
  100. transformations.add(transformation);
  101. promptForTransformation(transformations);
  102. }
  103. }
  104. private void promptForTransformers(List<String> transformers) throws PrompterException
  105. {
  106. if (promptForBoolean("Add Transformer Key?", "N"))
  107. {
  108. transformers.add(promptForTransformerKey());
  109. }
  110. }
  111. private String promptForTransformerKey() throws PrompterException
  112. {
  113. return promptNotBlank("Transformer Key");
  114. }
  115. }