Merge from the pain train
[official-gcc.git] / libjava / javax / security / auth / callback / ChoiceCallback.java
blob8fd038edf707dcb70d55fff78de5cd3a7fb162cb
1 /* ChoiceCallback.java -- callback for a choice of values.
2 Copyright (C) 2003, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.security.auth.callback;
41 import java.io.Serializable;
43 /**
44 * Underlying security services instantiate and pass a
45 * <code>ChoiceCallback</code> to the <code>handle()</code> method of a
46 * {@link CallbackHandler} to display a list of choices and to retrieve the
47 * selected choice(s).
49 * @see CallbackHandler
51 public class ChoiceCallback implements Callback, Serializable
54 // Constants and variables
55 // -------------------------------------------------------------------------
57 /**
58 * @serial
59 * @since 1.4
61 private String prompt;
63 /**
64 * @serial the list of choices.
65 * @since 1.4
67 private String[] choices;
69 /**
70 * @serial the choice to be used as the default choice.
71 * @since 1.4
73 private int defaultChoice;
75 /**
76 * @serial whether multiple selections are allowed from the list of choices.
77 * @since 1.4
79 private boolean multipleSelectionsAllowed;
81 /**
82 * @serial the selected choices, represented as indexes into the choices list.
83 * @since 1.4
85 private int[] selections;
87 // Constructor(s)
88 //--------------------------------------------------------------------------
90 /**
91 * Construct a <code>ChoiceCallback</code> with a prompt, a list of choices,
92 * a default choice, and a boolean specifying whether or not multiple
93 * selections from the list of choices are allowed.
95 * @param prompt the prompt used to describe the list of choices.
96 * @param choices the list of choices.
97 * @param defaultChoice the choice to be used as the default choice when the
98 * list of choices are displayed. This value is represented as an index into
99 * the <code>choices</code> array.
100 * @param multipleSelectionsAllowed boolean specifying whether or not
101 * multiple selections can be made from the list of choices.
102 * @throws IllegalArgumentException if <code>prompt</code> is <code>null</code>,
103 * if <code>prompt</code> has a length of <code>0</code>, if <code>choices</code>
104 * is <code>null</code>, if <code>choices</code> has a length of <code>0</code>,
105 * if any element from <code>choices</code> is <code>null</code>, if any
106 * element from <code>choices</code> has a length of <code>0</code> or if
107 * <code>defaultChoice</code> does not fall within the array boundaries of
108 * <code>choices</code>.
110 public ChoiceCallback(String prompt, String[] choices, int defaultChoice,
111 boolean multipleSelectionsAllowed)
113 super();
115 setPrompt(prompt);
116 setChoices(choices);
117 if (defaultChoice < 0 || defaultChoice >= this.choices.length)
119 throw new IllegalArgumentException("default choice is out of bounds");
121 this.defaultChoice = defaultChoice;
122 this.multipleSelectionsAllowed = multipleSelectionsAllowed;
125 // Instance methods
126 // -------------------------------------------------------------------------
129 * Get the prompt.
131 * @return the prompt.
133 public String getPrompt()
135 return prompt;
139 * Get the list of choices.
141 * @return the list of choices.
143 public String[] getChoices()
145 return choices;
149 * Get the defaultChoice.
151 * @return the defaultChoice, represented as an index into the choices list.
153 public int getDefaultChoice()
155 return defaultChoice;
159 * Get the boolean determining whether multiple selections from the choices
160 * list are allowed.
162 * @return whether multiple selections are allowed.
164 public boolean allowMultipleSelections()
166 return multipleSelectionsAllowed;
170 * Set the selected choice.
172 * @param selection the selection represented as an index into the choices
173 * list.
174 * @see #getSelectedIndexes()
176 public void setSelectedIndex(int selection)
178 this.selections = new int[1];
179 this.selections[0] = selection;
183 * Set the selected choices.
185 * @param selections the selections represented as indexes into the choices
186 * list.
187 * @throws UnsupportedOperationException if multiple selections are not
188 * allowed, as determined by <code>allowMultipleSelections</code>.
189 * @see #getSelectedIndexes()
191 public void setSelectedIndexes(int[] selections)
193 if (!multipleSelectionsAllowed)
195 throw new UnsupportedOperationException("not allowed");
198 this.selections = selections;
202 * Get the selected choices.
204 * @return the selected choices, represented as indexes into the choices list.
205 * @see #setSelectedIndexes(int[])
207 public int[] getSelectedIndexes()
209 return selections;
212 private void setPrompt(String prompt) throws IllegalArgumentException
214 if ((prompt == null) || (prompt.length() == 0))
216 throw new IllegalArgumentException("invalid prompt");
218 this.prompt = prompt;
221 private void setChoices(String[] choices) throws IllegalArgumentException
223 if (choices == null || choices.length == 0)
225 throw new IllegalArgumentException("invalid choices");
227 for (int i = 0; i < choices.length; i++)
229 if (choices[i] == null || choices[i].length() == 0)
231 throw new IllegalArgumentException("invalid choice at index #"+i);
234 this.choices = choices;