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