1. /*
  2. * @(#)ChoiceCallback.java 1.17 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.security.auth.callback;
  8. /**
  9. * <p> Underlying security services instantiate and pass a
  10. * <code>ChoiceCallback</code> to the <code>handle</code>
  11. * method of a <code>CallbackHandler</code> to display a list of choices
  12. * and to retrieve the selected choice(s).
  13. *
  14. * @version 1.17, 12/19/03
  15. * @see javax.security.auth.callback.CallbackHandler
  16. */
  17. public class ChoiceCallback implements Callback, java.io.Serializable {
  18. private static final long serialVersionUID = -3975664071579892167L;
  19. /**
  20. * @serial
  21. * @since 1.4
  22. */
  23. private String prompt;
  24. /**
  25. * @serial the list of choices
  26. * @since 1.4
  27. */
  28. private String[] choices;
  29. /**
  30. * @serial the choice to be used as the default choice
  31. * @since 1.4
  32. */
  33. private int defaultChoice;
  34. /**
  35. * @serial whether multiple selections are allowed from the list of
  36. * choices
  37. * @since 1.4
  38. */
  39. private boolean multipleSelectionsAllowed;
  40. /**
  41. * @serial the selected choices, represented as indexes into the
  42. * <code>choices</code> list.
  43. * @since 1.4
  44. */
  45. private int[] selections;
  46. /**
  47. * Construct a <code>ChoiceCallback</code> with a prompt,
  48. * a list of choices, a default choice, and a boolean specifying
  49. * whether or not multiple selections from the list of choices are allowed.
  50. *
  51. * <p>
  52. *
  53. * @param prompt the prompt used to describe the list of choices. <p>
  54. *
  55. * @param choices the list of choices. <p>
  56. *
  57. * @param defaultChoice the choice to be used as the default choice
  58. * when the list of choices are displayed. This value
  59. * is represented as an index into the
  60. * <code>choices</code> array. <p>
  61. *
  62. * @param multipleSelectionsAllowed boolean specifying whether or
  63. * not multiple selections can be made from the
  64. * list of choices.
  65. *
  66. * @exception IllegalArgumentException if <code>prompt</code> is null,
  67. * if <code>prompt</code> has a length of 0,
  68. * if <code>choices</code> is null,
  69. * if <code>choices</code> has a length of 0,
  70. * if any element from <code>choices</code> is null,
  71. * if any element from <code>choices</code>
  72. * has a length of 0 or if <code>defaultChoice</code>
  73. * does not fall within the array boundaries of
  74. * <code>choices</code>.
  75. */
  76. public ChoiceCallback(String prompt, String[] choices,
  77. int defaultChoice, boolean multipleSelectionsAllowed) {
  78. if (prompt == null || prompt.length() == 0 ||
  79. choices == null || choices.length == 0 ||
  80. defaultChoice < 0 || defaultChoice >= choices.length)
  81. throw new IllegalArgumentException();
  82. for (int i = 0; i < choices.length; i++) {
  83. if (choices[i] == null || choices[i].length() == 0)
  84. throw new IllegalArgumentException();
  85. }
  86. this.prompt = prompt;
  87. this.choices = choices;
  88. this.defaultChoice = defaultChoice;
  89. this.multipleSelectionsAllowed = multipleSelectionsAllowed;
  90. }
  91. /**
  92. * Get the prompt.
  93. *
  94. * <p>
  95. *
  96. * @return the prompt.
  97. */
  98. public String getPrompt() {
  99. return prompt;
  100. }
  101. /**
  102. * Get the list of choices.
  103. *
  104. * <p>
  105. *
  106. * @return the list of choices.
  107. */
  108. public String[] getChoices() {
  109. return choices;
  110. }
  111. /**
  112. * Get the defaultChoice.
  113. *
  114. * <p>
  115. *
  116. * @return the defaultChoice, represented as an index into
  117. * the <code>choices</code> list.
  118. */
  119. public int getDefaultChoice() {
  120. return defaultChoice;
  121. }
  122. /**
  123. * Get the boolean determining whether multiple selections from
  124. * the <code>choices</code> list are allowed.
  125. *
  126. * <p>
  127. *
  128. * @return whether multiple selections are allowed.
  129. */
  130. public boolean allowMultipleSelections() {
  131. return multipleSelectionsAllowed;
  132. }
  133. /**
  134. * Set the selected choice.
  135. *
  136. * <p>
  137. *
  138. * @param selection the selection represented as an index into the
  139. * <code>choices</code> list.
  140. *
  141. * @see #getSelectedIndexes
  142. */
  143. public void setSelectedIndex(int selection) {
  144. this.selections = new int[1];
  145. this.selections[0] = selection;
  146. }
  147. /**
  148. * Set the selected choices.
  149. *
  150. * <p>
  151. *
  152. * @param selections the selections represented as indexes into the
  153. * <code>choices</code> list.
  154. *
  155. * @exception UnsupportedOperationException if multiple selections are
  156. * not allowed, as determined by
  157. * <code>allowMultipleSelections</code>.
  158. *
  159. * @see #getSelectedIndexes
  160. */
  161. public void setSelectedIndexes(int[] selections) {
  162. if (!multipleSelectionsAllowed)
  163. throw new UnsupportedOperationException();
  164. this.selections = selections;
  165. }
  166. /**
  167. * Get the selected choices.
  168. *
  169. * <p>
  170. *
  171. * @return the selected choices, represented as indexes into the
  172. * <code>choices</code> list.
  173. *
  174. * @see #setSelectedIndexes
  175. */
  176. public int[] getSelectedIndexes() {
  177. return selections;
  178. }
  179. }