001    /* ChoiceCallback.java -- callback for a choice of values.
002       Copyright (C) 2003, Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package javax.security.auth.callback;
040    
041    import java.io.Serializable;
042    
043    /**
044     * Underlying security services instantiate and pass a
045     * <code>ChoiceCallback</code> to the <code>handle()</code> method of a
046     * {@link CallbackHandler} to display a list of choices and to retrieve the
047     * selected choice(s).
048     *
049     * @see CallbackHandler
050     */
051    public class ChoiceCallback implements Callback, Serializable
052    {
053    
054      // Constants and variables
055      // -------------------------------------------------------------------------
056    
057      /**
058       * @serial
059       * @since 1.4
060       */
061      private String prompt;
062    
063      /**
064       * @serial the list of choices.
065       * @since 1.4
066       */
067      private String[] choices;
068    
069      /**
070       * @serial the choice to be used as the default choice.
071       * @since 1.4
072       */
073      private int defaultChoice;
074    
075      /**
076       * @serial whether multiple selections are allowed from the list of choices.
077       * @since 1.4
078       */
079      private boolean multipleSelectionsAllowed;
080    
081      /**
082       * @serial the selected choices, represented as indexes into the choices list.
083       * @since 1.4
084       */
085      private int[] selections;
086    
087      // Constructor(s)
088      //--------------------------------------------------------------------------
089    
090      /**
091       * Construct a <code>ChoiceCallback</code> with a prompt, a list of choices,
092       * a default choice, and a boolean specifying whether or not multiple
093       * selections from the list of choices are allowed.
094       *
095       * @param prompt the prompt used to describe the list of choices.
096       * @param choices the list of choices.
097       * @param defaultChoice the choice to be used as the default choice when the
098       * list of choices are displayed. This value is represented as an index into
099       * 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>.
109       */
110      public ChoiceCallback(String prompt, String[] choices, int defaultChoice,
111                            boolean multipleSelectionsAllowed)
112      {
113        super();
114    
115        setPrompt(prompt);
116        setChoices(choices);
117        if (defaultChoice < 0 || defaultChoice >= this.choices.length)
118          {
119            throw new IllegalArgumentException("default choice is out of bounds");
120          }
121        this.defaultChoice = defaultChoice;
122        this.multipleSelectionsAllowed = multipleSelectionsAllowed;
123      }
124    
125      // Instance methods
126      // -------------------------------------------------------------------------
127    
128      /**
129       * Get the prompt.
130       *
131       * @return the prompt.
132       */
133      public String getPrompt()
134      {
135        return prompt;
136      }
137    
138      /**
139       * Get the list of choices.
140       *
141       * @return the list of choices.
142       */
143      public String[] getChoices()
144      {
145        return choices;
146      }
147    
148      /**
149       * Get the defaultChoice.
150       *
151       * @return the defaultChoice, represented as an index into the choices list.
152       */
153      public int getDefaultChoice()
154      {
155        return defaultChoice;
156      }
157    
158      /**
159       * Get the boolean determining whether multiple selections from the choices
160       * list are allowed.
161       *
162       * @return whether multiple selections are allowed.
163       */
164      public boolean allowMultipleSelections()
165      {
166        return multipleSelectionsAllowed;
167      }
168    
169      /**
170       * Set the selected choice.
171       *
172       * @param selection the selection represented as an index into the choices
173       * list.
174       * @see #getSelectedIndexes()
175       */
176      public void setSelectedIndex(int selection)
177      {
178        this.selections = new int[1];
179        this.selections[0] = selection;
180      }
181    
182      /**
183       * Set the selected choices.
184       *
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()
190       */
191      public void setSelectedIndexes(int[] selections)
192      {
193        if (!multipleSelectionsAllowed)
194          {
195            throw new UnsupportedOperationException("not allowed");
196          }
197    
198        this.selections = selections;
199      }
200    
201      /**
202       * Get the selected choices.
203       *
204       * @return the selected choices, represented as indexes into the choices list.
205       * @see #setSelectedIndexes(int[])
206       */
207      public int[] getSelectedIndexes()
208      {
209        return selections;
210      }
211    
212      private void setPrompt(String prompt) throws IllegalArgumentException
213      {
214        if ((prompt == null) || (prompt.length() == 0))
215          {
216            throw new IllegalArgumentException("invalid prompt");
217          }
218        this.prompt = prompt;
219      }
220    
221      private void setChoices(String[] choices) throws IllegalArgumentException
222      {
223        if (choices == null || choices.length == 0)
224          {
225            throw new IllegalArgumentException("invalid choices");
226          }
227        for (int i = 0; i < choices.length; i++)
228          {
229            if (choices[i] == null || choices[i].length() == 0)
230              {
231                throw new IllegalArgumentException("invalid choice at index #"+i);
232              }
233          }
234        this.choices = choices;
235      }
236    }