KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > security > auth > callback > ChoiceCallback


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
8 package javax.security.auth.callback;
9
10 /**
11  * <p> Underlying security services instantiate and pass a
12  * <code>ChoiceCallback</code> to the <code>handle</code>
13  * method of a <code>CallbackHandler</code> to display a list of choices
14  * and to retrieve the selected choice(s).
15  *
16  * @version 1.17, 12/19/03
17  * @see javax.security.auth.callback.CallbackHandler
18  */

19 public class ChoiceCallback implements Callback JavaDoc, java.io.Serializable JavaDoc {
20
21     private static final long serialVersionUID = -3975664071579892167L;
22
23     /**
24      * @serial
25      * @since 1.4
26      */

27     private String JavaDoc prompt;
28     /**
29      * @serial the list of choices
30      * @since 1.4
31      */

32     private String JavaDoc[] choices;
33     /**
34      * @serial the choice to be used as the default choice
35      * @since 1.4
36      */

37     private int defaultChoice;
38     /**
39      * @serial whether multiple selections are allowed from the list of
40      * choices
41      * @since 1.4
42      */

43     private boolean multipleSelectionsAllowed;
44     /**
45      * @serial the selected choices, represented as indexes into the
46      * <code>choices</code> list.
47      * @since 1.4
48      */

49     private int[] selections;
50
51     /**
52      * Construct a <code>ChoiceCallback</code> with a prompt,
53      * a list of choices, a default choice, and a boolean specifying
54      * whether or not multiple selections from the list of choices are allowed.
55      *
56      * <p>
57      *
58      * @param prompt the prompt used to describe the list of choices. <p>
59      *
60      * @param choices the list of choices. <p>
61      *
62      * @param defaultChoice the choice to be used as the default choice
63      * when the list of choices are displayed. This value
64      * is represented as an index into the
65      * <code>choices</code> array. <p>
66      *
67      * @param multipleSelectionsAllowed boolean specifying whether or
68      * not multiple selections can be made from the
69      * list of choices.
70      *
71      * @exception IllegalArgumentException if <code>prompt</code> is null,
72      * if <code>prompt</code> has a length of 0,
73      * if <code>choices</code> is null,
74      * if <code>choices</code> has a length of 0,
75      * if any element from <code>choices</code> is null,
76      * if any element from <code>choices</code>
77      * has a length of 0 or if <code>defaultChoice</code>
78      * does not fall within the array boundaries of
79      * <code>choices</code>.
80      */

81     public ChoiceCallback(String JavaDoc prompt, String JavaDoc[] choices,
82                 int defaultChoice, boolean multipleSelectionsAllowed) {
83
84     if (prompt == null || prompt.length() == 0 ||
85         choices == null || choices.length == 0 ||
86         defaultChoice < 0 || defaultChoice >= choices.length)
87         throw new IllegalArgumentException JavaDoc();
88
89     for (int i = 0; i < choices.length; i++) {
90         if (choices[i] == null || choices[i].length() == 0)
91         throw new IllegalArgumentException JavaDoc();
92     }
93
94     this.prompt = prompt;
95     this.choices = choices;
96     this.defaultChoice = defaultChoice;
97     this.multipleSelectionsAllowed = multipleSelectionsAllowed;
98     }
99
100     /**
101      * Get the prompt.
102      *
103      * <p>
104      *
105      * @return the prompt.
106      */

107     public String JavaDoc getPrompt() {
108     return prompt;
109     }
110
111     /**
112      * Get the list of choices.
113      *
114      * <p>
115      *
116      * @return the list of choices.
117      */

118     public String JavaDoc[] getChoices() {
119     return choices;
120     }
121
122     /**
123      * Get the defaultChoice.
124      *
125      * <p>
126      *
127      * @return the defaultChoice, represented as an index into
128      * the <code>choices</code> list.
129      */

130     public int getDefaultChoice() {
131     return defaultChoice;
132     }
133
134     /**
135      * Get the boolean determining whether multiple selections from
136      * the <code>choices</code> list are allowed.
137      *
138      * <p>
139      *
140      * @return whether multiple selections are allowed.
141      */

142     public boolean allowMultipleSelections() {
143     return multipleSelectionsAllowed;
144     }
145
146     /**
147      * Set the selected choice.
148      *
149      * <p>
150      *
151      * @param selection the selection represented as an index into the
152      * <code>choices</code> list.
153      *
154      * @see #getSelectedIndexes
155      */

156     public void setSelectedIndex(int selection) {
157     this.selections = new int[1];
158     this.selections[0] = selection;
159     }
160
161     /**
162      * Set the selected choices.
163      *
164      * <p>
165      *
166      * @param selections the selections represented as indexes into the
167      * <code>choices</code> list.
168      *
169      * @exception UnsupportedOperationException if multiple selections are
170      * not allowed, as determined by
171      * <code>allowMultipleSelections</code>.
172      *
173      * @see #getSelectedIndexes
174      */

175     public void setSelectedIndexes(int[] selections) {
176     if (!multipleSelectionsAllowed)
177         throw new UnsupportedOperationException JavaDoc();
178     this.selections = selections;
179     }
180
181     /**
182      * Get the selected choices.
183      *
184      * <p>
185      *
186      * @return the selected choices, represented as indexes into the
187      * <code>choices</code> list.
188      *
189      * @see #setSelectedIndexes
190      */

191     public int[] getSelectedIndexes() {
192     return selections;
193     }
194 }
195
Popular Tags