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