1 /*
2 * Copyright 1999-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package javax.security.auth.callback;
27
28 /**
29 * <p> Underlying security services instantiate and pass a
30 * <code>ChoiceCallback</code> to the <code>handle</code>
31 * method of a <code>CallbackHandler</code> to display a list of choices
32 * and to retrieve the selected choice(s).
33 *
34 * @see javax.security.auth.callback.CallbackHandler
35 */
36 public class ChoiceCallback implements Callback, java.io.Serializable {
37
38 private static final long serialVersionUID = -3975664071579892167L;
39
40 /**
41 * @serial
42 * @since 1.4
43 */
44 private String prompt;
45 /**
46 * @serial the list of choices
47 * @since 1.4
48 */
49 private String[] choices;
50 /**
51 * @serial the choice to be used as the default choice
52 * @since 1.4
53 */
54 private int defaultChoice;
55 /**
56 * @serial whether multiple selections are allowed from the list of
57 * choices
58 * @since 1.4
59 */
60 private boolean multipleSelectionsAllowed;
61 /**
62 * @serial the selected choices, represented as indexes into the
63 * <code>choices</code> list.
64 * @since 1.4
65 */
66 private int[] selections;
67
68 /**
69 * Construct a <code>ChoiceCallback</code> with a prompt,
70 * a list of choices, a default choice, and a boolean specifying
71 * whether or not multiple selections from the list of choices are allowed.
72 *
73 * <p>
74 *
75 * @param prompt the prompt used to describe the list of choices. <p>
76 *
77 * @param choices the list of choices. <p>
78 *
79 * @param defaultChoice the choice to be used as the default choice
80 * when the list of choices are displayed. This value
81 * is represented as an index into the
82 * <code>choices</code> array. <p>
83 *
84 * @param multipleSelectionsAllowed boolean specifying whether or
85 * not multiple selections can be made from the
86 * list of choices.
87 *
88 * @exception IllegalArgumentException if <code>prompt</code> is null,
89 * if <code>prompt</code> has a length of 0,
90 * if <code>choices</code> is null,
91 * if <code>choices</code> has a length of 0,
92 * if any element from <code>choices</code> is null,
93 * if any element from <code>choices</code>
94 * has a length of 0 or if <code>defaultChoice</code>
95 * does not fall within the array boundaries of
96 * <code>choices</code>.
97 */
98 public ChoiceCallback(String prompt, String[] choices,
99 int defaultChoice, boolean multipleSelectionsAllowed) {
100
101 if (prompt == null || prompt.length() == 0 ||
102 choices == null || choices.length == 0 ||
103 defaultChoice < 0 || defaultChoice >= choices.length)
104 throw new IllegalArgumentException();
105
106 for (int i = 0; i < choices.length; i++) {
107 if (choices[i] == null || choices[i].length() == 0)
108 throw new IllegalArgumentException();
109 }
110
111 this.prompt = prompt;
112 this.choices = choices;
113 this.defaultChoice = defaultChoice;
114 this.multipleSelectionsAllowed = multipleSelectionsAllowed;
115 }
116
117 /**
118 * Get the prompt.
119 *
120 * <p>
121 *
122 * @return the prompt.
123 */
124 public String getPrompt() {
125 return prompt;
126 }
127
128 /**
129 * Get the list of choices.
130 *
131 * <p>
132 *
133 * @return the list of choices.
134 */
135 public String[] getChoices() {
136 return choices;
137 }
138
139 /**
140 * Get the defaultChoice.
141 *
142 * <p>
143 *
144 * @return the defaultChoice, represented as an index into
145 * the <code>choices</code> list.
146 */
147 public int getDefaultChoice() {
148 return defaultChoice;
149 }
150
151 /**
152 * Get the boolean determining whether multiple selections from
153 * the <code>choices</code> list are allowed.
154 *
155 * <p>
156 *
157 * @return whether multiple selections are allowed.
158 */
159 public boolean allowMultipleSelections() {
160 return multipleSelectionsAllowed;
161 }
162
163 /**
164 * Set the selected choice.
165 *
166 * <p>
167 *
168 * @param selection the selection represented as an index into the
169 * <code>choices</code> list.
170 *
171 * @see #getSelectedIndexes
172 */
173 public void setSelectedIndex(int selection) {
174 this.selections = new int[1];
175 this.selections[0] = selection;
176 }
177
178 /**
179 * Set the selected choices.
180 *
181 * <p>
182 *
183 * @param selections the selections represented as indexes into the
184 * <code>choices</code> list.
185 *
186 * @exception UnsupportedOperationException if multiple selections are
187 * not allowed, as determined by
188 * <code>allowMultipleSelections</code>.
189 *
190 * @see #getSelectedIndexes
191 */
192 public void setSelectedIndexes(int[] selections) {
193 if (!multipleSelectionsAllowed)
194 throw new UnsupportedOperationException();
195 this.selections = selections;
196 }
197
198 /**
199 * Get the selected choices.
200 *
201 * <p>
202 *
203 * @return the selected choices, represented as indexes into the
204 * <code>choices</code> list.
205 *
206 * @see #setSelectedIndexes
207 */
208 public int[] getSelectedIndexes() {
209 return selections;
210 }
211 }