001 /*
002 * Copyright 1999-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.security.auth.callback;
027
028 /**
029 * <p> Underlying security services instantiate and pass a
030 * <code>ChoiceCallback</code> to the <code>handle</code>
031 * method of a <code>CallbackHandler</code> to display a list of choices
032 * and to retrieve the selected choice(s).
033 *
034 * @version 1.24, 05/05/07
035 * @see javax.security.auth.callback.CallbackHandler
036 */
037 public class ChoiceCallback implements Callback, java.io.Serializable {
038
039 private static final long serialVersionUID = -3975664071579892167L;
040
041 /**
042 * @serial
043 * @since 1.4
044 */
045 private String prompt;
046 /**
047 * @serial the list of choices
048 * @since 1.4
049 */
050 private String[] choices;
051 /**
052 * @serial the choice to be used as the default choice
053 * @since 1.4
054 */
055 private int defaultChoice;
056 /**
057 * @serial whether multiple selections are allowed from the list of
058 * choices
059 * @since 1.4
060 */
061 private boolean multipleSelectionsAllowed;
062 /**
063 * @serial the selected choices, represented as indexes into the
064 * <code>choices</code> list.
065 * @since 1.4
066 */
067 private int[] selections;
068
069 /**
070 * Construct a <code>ChoiceCallback</code> with a prompt,
071 * a list of choices, a default choice, and a boolean specifying
072 * whether or not multiple selections from the list of choices are allowed.
073 *
074 * <p>
075 *
076 * @param prompt the prompt used to describe the list of choices. <p>
077 *
078 * @param choices the list of choices. <p>
079 *
080 * @param defaultChoice the choice to be used as the default choice
081 * when the list of choices are displayed. This value
082 * is represented as an index into the
083 * <code>choices</code> array. <p>
084 *
085 * @param multipleSelectionsAllowed boolean specifying whether or
086 * not multiple selections can be made from the
087 * list of choices.
088 *
089 * @exception IllegalArgumentException if <code>prompt</code> is null,
090 * if <code>prompt</code> has a length of 0,
091 * if <code>choices</code> is null,
092 * if <code>choices</code> has a length of 0,
093 * if any element from <code>choices</code> is null,
094 * if any element from <code>choices</code>
095 * has a length of 0 or if <code>defaultChoice</code>
096 * does not fall within the array boundaries of
097 * <code>choices</code>.
098 */
099 public ChoiceCallback(String prompt, String[] choices,
100 int defaultChoice, boolean multipleSelectionsAllowed) {
101
102 if (prompt == null || prompt.length() == 0 || choices == null
103 || choices.length == 0 || defaultChoice < 0
104 || defaultChoice >= choices.length)
105 throw new IllegalArgumentException();
106
107 for (int i = 0; i < choices.length; i++) {
108 if (choices[i] == null || choices[i].length() == 0)
109 throw new IllegalArgumentException();
110 }
111
112 this .prompt = prompt;
113 this .choices = choices;
114 this .defaultChoice = defaultChoice;
115 this .multipleSelectionsAllowed = multipleSelectionsAllowed;
116 }
117
118 /**
119 * Get the prompt.
120 *
121 * <p>
122 *
123 * @return the prompt.
124 */
125 public String getPrompt() {
126 return prompt;
127 }
128
129 /**
130 * Get the list of choices.
131 *
132 * <p>
133 *
134 * @return the list of choices.
135 */
136 public String[] getChoices() {
137 return choices;
138 }
139
140 /**
141 * Get the defaultChoice.
142 *
143 * <p>
144 *
145 * @return the defaultChoice, represented as an index into
146 * the <code>choices</code> list.
147 */
148 public int getDefaultChoice() {
149 return defaultChoice;
150 }
151
152 /**
153 * Get the boolean determining whether multiple selections from
154 * the <code>choices</code> list are allowed.
155 *
156 * <p>
157 *
158 * @return whether multiple selections are allowed.
159 */
160 public boolean allowMultipleSelections() {
161 return multipleSelectionsAllowed;
162 }
163
164 /**
165 * Set the selected choice.
166 *
167 * <p>
168 *
169 * @param selection the selection represented as an index into the
170 * <code>choices</code> list.
171 *
172 * @see #getSelectedIndexes
173 */
174 public void setSelectedIndex(int selection) {
175 this .selections = new int[1];
176 this .selections[0] = selection;
177 }
178
179 /**
180 * Set the selected choices.
181 *
182 * <p>
183 *
184 * @param selections the selections represented as indexes into the
185 * <code>choices</code> list.
186 *
187 * @exception UnsupportedOperationException if multiple selections are
188 * not allowed, as determined by
189 * <code>allowMultipleSelections</code>.
190 *
191 * @see #getSelectedIndexes
192 */
193 public void setSelectedIndexes(int[] selections) {
194 if (!multipleSelectionsAllowed)
195 throw new UnsupportedOperationException();
196 this .selections = selections;
197 }
198
199 /**
200 * Get the selected choices.
201 *
202 * <p>
203 *
204 * @return the selected choices, represented as indexes into the
205 * <code>choices</code> list.
206 *
207 * @see #setSelectedIndexes
208 */
209 public int[] getSelectedIndexes() {
210 return selections;
211 }
212 }
|