View Javadoc

1   /*
2    * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.ant.input;
18  
19  import java.util.Iterator;
20  import java.util.Vector;
21  
22  import org.apache.tools.ant.input.InputRequest;
23  
24  /**
25   * 
26   */
27  public class XMultipleChoiceInputRequest extends InputRequest {
28  
29      private Vector options = null;
30  
31      public XMultipleChoiceInputRequest(String prompt, Vector options) {
32  
33          super(prompt);
34          if (options == null) {
35              throw new IllegalArgumentException("choices must not be null");
36          }
37          this.options = options;
38      }
39  
40      public Vector getOptions() {
41  
42          return options;
43      }
44  
45      /**
46       * @return The possible values.
47       */
48      public Vector getChoices() {
49  
50          Vector choices = new Vector();
51  
52          Iterator i = options.iterator();
53          while (i.hasNext()) {
54              XInputOption o = (XInputOption) i.next();
55              choices.add(o.displayName());
56          }
57  
58          return choices;
59      }
60  
61      /**
62       * @return true if the input is one of the allowed values.
63       */
64      public boolean isInputValid() {
65  
66          // first check if any XInputOptions will accept the input
67          Iterator i = options.iterator();
68          while (i.hasNext()) {
69              XInputOption o = (XInputOption) i.next();
70              if (o.acceptsInput(getInput())) {
71                  setInput(o.getValue());
72                  return true;
73              }
74          }
75  
76          // next check if they tried to input a menu item number
77          try {
78              Integer input = new Integer(getInput());
79              if (input.intValue() > 0 && input.intValue() <= options.size()) {
80                  XInputOption o = (XInputOption) options.get(input.intValue() - 1);
81                  setInput(o.getValue());
82                  return true;
83              }
84          } catch (NumberFormatException nfe) {
85              // input was not a number
86          }
87  
88          return false;
89      }
90  
91  }