View Javadoc

1   /*
2    * This file is part of hyphenType. hyphenType is free software: you can
3    * redistribute it and/or modify it under the terms of the GNU General Public
4    * License as published by the Free Software Foundation, either version 3 of the
5    * License, or (at your option) any later version. hyphenType is distributed in
6    * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
8    * the GNU General Public License for more details. You should have received a
9    * copy of the GNU General Public License along with hyphenType. If not, see
10   * <http://www.gnu.org/licenses/>.
11   */
12  package org.hyphenType.input;
13  
14  import java.lang.reflect.Array;
15  import java.util.ArrayList;
16  
17  import org.hyphenType.datastructure.annotations.InputChannel;
18  import org.hyphenType.datastructure.parser.StructureArgument;
19  
20  /**
21   * @author Aurelio Akira M. Matsui
22   */
23  public abstract class UserInput {
24  
25      @SuppressWarnings("unchecked")
26      public <T> T[] readArray(final StructureArgument argument, final String singleHyphen, final String doubleHyphen) throws UserInputException {
27  
28          if (!argument.getChannels().contains(InputChannel.TEXT) && !argument.getChannels().contains(InputChannel.GUI)) {
29              throw new IllegalArgumentException(String.format("Input channels should have at least one of %s or %s.", InputChannel.TEXT, InputChannel.GUI));
30          }
31          
32          if (!isGraphicalUIAvailable() && !isTextUIAvailable()) {
33              throw new UserInputException("No input method available. Before invoking a user input, make sure there is a textual or graphical input.");
34          }
35          
36          if (!(argument.getChannels().contains(InputChannel.GUI) && isGraphicalUIAvailable() || argument.getChannels().contains(InputChannel.TEXT) && isTextUIAvailable())) {
37              throw new UserInputException("No argument input channel combination available.");
38          }
39  
40          String regex = argument.getRegex();
41          regex = regex.replace("\\h", singleHyphen);
42          regex = regex.replace("\\H", doubleHyphen);
43  
44          T input = null;
45          ArrayList<T> list = new ArrayList<T>();
46          int i = 0;
47          do {
48              if (argument.getChannels().contains(InputChannel.GUI) && isGraphicalUIAvailable()) {
49                  input = (T) readFromGraphicalUI(argument.method.getReturnType().getComponentType(), String.format("%s[%d]:", argument.getName(), i), regex);
50              } else if (isTextUIAvailable()) {
51                  input = (T) readFromTextUI(argument.method.getReturnType().getComponentType(), String.format("%s[%d]:", argument.getName(), i), regex);
52              }
53              i++;
54              if (input != null) {
55                  list.add(input);
56              }
57          } while (input != null);
58          
59          return list.toArray((T[]) Array.newInstance(argument.method.getReturnType().getComponentType(), list.size()));
60      }
61  
62      @SuppressWarnings("unchecked")
63      public <T> T readString(final StructureArgument argument, final String singleHyphen, final String doubleHyphen) throws UserInputException {
64          
65          if (!argument.getChannels().contains(InputChannel.TEXT) && !argument.getChannels().contains(InputChannel.GUI)) {
66              throw new UserInputException(String.format("Input channels should have at least one of %s or %s.", InputChannel.TEXT, InputChannel.GUI));
67          }
68  
69          String regex = argument.getRegex();
70          regex = regex.replace("\\h", singleHyphen);
71          regex = regex.replace("\\H", doubleHyphen);
72  
73          if (argument.getChannels().contains(InputChannel.GUI) && isGraphicalUIAvailable()) {
74              return (T) readFromGraphicalUI(argument.method.getReturnType(), String.format("%s:", argument.getName()), regex);
75          }
76          else if (isTextUIAvailable()) {
77              return (T) readFromTextUI(argument.method.getReturnType(), String.format("%s:", argument.getName()), regex);
78          }
79          else {
80              throw new UserInputException("No input method available. Before invoking a user input, make sure there is a textual or graphical input.");
81          }
82      }
83  
84      protected abstract boolean isTextUIAvailable();
85  
86      protected abstract <T> T readFromTextUI(Class<T> type, String message, String regex) throws UserInputException;
87  
88      protected abstract boolean isGraphicalUIAvailable();
89  
90      protected abstract <T> T readFromGraphicalUI(Class<T> type, String message, String regex) throws UserInputException;
91  }