Source for javax.security.auth.callback.ChoiceCallback

   1: /* ChoiceCallback.java -- callback for a choice of values.
   2:    Copyright (C) 2003, Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package javax.security.auth.callback;
  40: 
  41: import java.io.Serializable;
  42: 
  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).
  48:  *
  49:  * @see CallbackHandler
  50:  */
  51: public class ChoiceCallback implements Callback, Serializable
  52: {
  53: 
  54:   // Constants and variables
  55:   // -------------------------------------------------------------------------
  56: 
  57:   /**
  58:    * @serial
  59:    * @since 1.4
  60:    */
  61:   private String prompt;
  62: 
  63:   /**
  64:    * @serial the list of choices.
  65:    * @since 1.4
  66:    */
  67:   private String[] choices;
  68: 
  69:   /**
  70:    * @serial the choice to be used as the default choice.
  71:    * @since 1.4
  72:    */
  73:   private int defaultChoice;
  74: 
  75:   /**
  76:    * @serial whether multiple selections are allowed from the list of choices.
  77:    * @since 1.4
  78:    */
  79:   private boolean multipleSelectionsAllowed;
  80: 
  81:   /**
  82:    * @serial the selected choices, represented as indexes into the choices list.
  83:    * @since 1.4
  84:    */
  85:   private int[] selections;
  86: 
  87:   // Constructor(s)
  88:   //--------------------------------------------------------------------------
  89: 
  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.
  94:    *
  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>.
 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: }