/plugins/Templates/tags/Templates-4.1.2/velocity/directives/YesNoPrompt.java

# · Java · 115 lines · 55 code · 11 blank · 49 comment · 8 complexity · 847286c2c79c7baf03525fcaf7bf951e MD5 · raw file

  1. /*
  2. * YesNoPrompt.java
  3. * Copyright (c) 2006 Steve Jakob
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package velocity.directives;
  20. import java.io.Writer;
  21. import javax.swing.JOptionPane;
  22. import org.apache.velocity.context.InternalContextAdapter;
  23. import org.apache.velocity.exception.MethodInvocationException;
  24. import org.apache.velocity.runtime.parser.node.Node;
  25. import org.gjt.sp.jedit.textarea.JEditTextArea;
  26. import velocity.VelocityConstants;
  27. /**
  28. * A directive to prompt the user to select one of two values. <p>
  29. *
  30. * When using the directive, the following are the arguments accepted:
  31. * <ul>
  32. * <li> variable name (required)</li>
  33. * <li> prompt string (required)</li>
  34. * <li> the text to display for the "Yes" button (optional, default = "Yes")
  35. * </li>
  36. * <li> the text to display for the "No" button (optional, default = "No")
  37. * </li>
  38. * </ul>
  39. * </p>
  40. *
  41. * @author Steve Jakob
  42. */
  43. public class YesNoPrompt extends SimpleDirective
  44. implements VelocityConstants
  45. {
  46. /**
  47. * Return name of this directive.
  48. */
  49. public String getName()
  50. {
  51. return "yes_no";
  52. }
  53. /**
  54. * Return type of this directive.
  55. */
  56. public int getType()
  57. {
  58. return LINE;
  59. }
  60. /**
  61. * Prompt the user for a value.
  62. */
  63. public boolean render(InternalContextAdapter context,
  64. Writer writer, Node node)
  65. throws MethodInvocationException
  66. {
  67. // Retrieve prompt
  68. Object prompt = getRequiredValue(node, 0, "label", context);
  69. if (prompt == null)
  70. {
  71. return false;
  72. }
  73. // Retrieve variable name
  74. String key = getRequiredVariable(node, 1, "key");
  75. // Retrieve optional "yes" text
  76. Object yesString = getOptionalValue(node, 2, context);
  77. if (yesString == null)
  78. {
  79. yesString = "Yes";
  80. }
  81. // Retrieve optional "no" text
  82. Object noString = getOptionalValue(node, 3, context);
  83. if (noString == null)
  84. {
  85. noString = "No";
  86. }
  87. // Prompt the user
  88. JEditTextArea textArea = (JEditTextArea) context.get(TEXT_AREA);
  89. Object[] options = {yesString, noString};
  90. int n = JOptionPane.showOptionDialog(textArea, prompt,
  91. "Select desired option",
  92. JOptionPane.YES_NO_OPTION,
  93. JOptionPane.QUESTION_MESSAGE,
  94. null,
  95. options,
  96. null);
  97. // Set the variable's value in the context
  98. if (n == JOptionPane.YES_OPTION)
  99. context.getInternalUserContext().put(key, new Boolean(true));
  100. else
  101. context.getInternalUserContext().put(key, new Boolean(false));
  102. return true;
  103. }
  104. }