ConfirmationCallback.java.svn-base 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package javax.security.auth.callback;
  18. import java.io.Serializable;
  19. import org.apache.harmony.auth.internal.nls.Messages;
  20. public class ConfirmationCallback implements Callback, Serializable {
  21. private static final long serialVersionUID = -9095656433782481624L;
  22. public static final int YES = 0; // default options
  23. public static final int NO = 1;
  24. public static final int CANCEL = 2;
  25. public static final int OK = 3;
  26. public static final int YES_NO_OPTION = 0; // options type
  27. public static final int YES_NO_CANCEL_OPTION = 1;
  28. public static final int OK_CANCEL_OPTION = 2;
  29. public static final int UNSPECIFIED_OPTION = -1;
  30. public static final int INFORMATION = 0; // messages type
  31. public static final int WARNING = 1;
  32. public static final int ERROR = 2;
  33. private String prompt;
  34. private int messageType;
  35. private int optionType = UNSPECIFIED_OPTION;
  36. private int defaultOption;
  37. private String[] options;
  38. private int selection;
  39. public ConfirmationCallback(int messageType, int optionType, int defaultOption) {
  40. super();
  41. if (messageType > ERROR || messageType < INFORMATION) {
  42. throw new IllegalArgumentException(Messages.getString("auth.16")); //$NON-NLS-1$
  43. }
  44. switch (optionType) {
  45. case YES_NO_OPTION:
  46. if (defaultOption != YES && defaultOption != NO) {
  47. throw new IllegalArgumentException(Messages.getString("auth.17")); //$NON-NLS-1$
  48. }
  49. break;
  50. case YES_NO_CANCEL_OPTION:
  51. if (defaultOption != YES && defaultOption != NO && defaultOption != CANCEL) {
  52. throw new IllegalArgumentException(Messages.getString("auth.17")); //$NON-NLS-1$
  53. }
  54. break;
  55. case OK_CANCEL_OPTION:
  56. if (defaultOption != OK && defaultOption != CANCEL) {
  57. throw new IllegalArgumentException(Messages.getString("auth.17")); //$NON-NLS-1$
  58. }
  59. break;
  60. default:
  61. throw new IllegalArgumentException(Messages.getString("auth.18")); //$NON-NLS-1$
  62. }
  63. this.messageType = messageType;
  64. this.optionType = optionType;
  65. this.defaultOption = defaultOption;
  66. }
  67. public ConfirmationCallback(int messageType, String[] options, int defaultOption) {
  68. super();
  69. if (messageType > ERROR || messageType < INFORMATION) {
  70. throw new IllegalArgumentException(Messages.getString("auth.16")); //$NON-NLS-1$
  71. }
  72. if (options == null || options.length == 0) {
  73. throw new IllegalArgumentException(Messages.getString("auth.1A")); //$NON-NLS-1$
  74. }
  75. for (int i = 0; i < options.length; i++) {
  76. if (options[i] == null || options[i].length() == 0) {
  77. throw new IllegalArgumentException(Messages.getString("auth.1A")); //$NON-NLS-1$
  78. }
  79. }
  80. if (0 > defaultOption || defaultOption >= options.length) {
  81. throw new IllegalArgumentException(Messages.getString("auth.17")); //$NON-NLS-1$
  82. }
  83. // FIXME:System.arraycopy(options, 0 , new String[this.options.length],
  84. // 0, this.options.length);
  85. this.options = options;
  86. this.defaultOption = defaultOption;
  87. this.messageType = messageType;
  88. }
  89. public ConfirmationCallback(String prompt, int messageType, int optionType,
  90. int defaultOption) {
  91. super();
  92. if (prompt == null || prompt.length() == 0) {
  93. throw new IllegalArgumentException(Messages.getString("auth.14")); //$NON-NLS-1$
  94. }
  95. if (messageType > ERROR || messageType < INFORMATION) {
  96. throw new IllegalArgumentException(Messages.getString("auth.16")); //$NON-NLS-1$
  97. }
  98. switch (optionType) {
  99. case YES_NO_OPTION:
  100. if (defaultOption != YES && defaultOption != NO) {
  101. throw new IllegalArgumentException(Messages.getString("auth.17")); //$NON-NLS-1$
  102. }
  103. break;
  104. case YES_NO_CANCEL_OPTION:
  105. if (defaultOption != YES && defaultOption != NO && defaultOption != CANCEL) {
  106. throw new IllegalArgumentException(Messages.getString("auth.17")); //$NON-NLS-1$
  107. }
  108. break;
  109. case OK_CANCEL_OPTION:
  110. if (defaultOption != OK && defaultOption != CANCEL) {
  111. throw new IllegalArgumentException(Messages.getString("auth.17")); //$NON-NLS-1$
  112. }
  113. break;
  114. default:
  115. throw new IllegalArgumentException(Messages.getString("auth.18")); //$NON-NLS-1$
  116. }
  117. this.prompt = prompt;
  118. this.messageType = messageType;
  119. this.optionType = optionType;
  120. this.defaultOption = defaultOption;
  121. }
  122. public ConfirmationCallback(String prompt, int messageType, String[] options,
  123. int defaultOption) {
  124. super();
  125. if (prompt == null || prompt.length() == 0) {
  126. throw new IllegalArgumentException(Messages.getString("auth.14")); //$NON-NLS-1$
  127. }
  128. if (messageType > ERROR || messageType < INFORMATION) {
  129. throw new IllegalArgumentException(Messages.getString("auth.16")); //$NON-NLS-1$
  130. }
  131. if (options == null || options.length == 0) {
  132. throw new IllegalArgumentException(Messages.getString("auth.1A")); //$NON-NLS-1$
  133. }
  134. for (int i = 0; i < options.length; i++) {
  135. if (options[i] == null || options[i].length() == 0) {
  136. throw new IllegalArgumentException(Messages.getString("auth.1A")); //$NON-NLS-1$
  137. }
  138. }
  139. if (0 > defaultOption || defaultOption >= options.length) {
  140. throw new IllegalArgumentException(Messages.getString("auth.17")); //$NON-NLS-1$
  141. }
  142. // FIXME:System.arraycopy(options, 0 , new String[this.options.length],
  143. // 0, this.options.length);
  144. this.options = options;
  145. this.defaultOption = defaultOption;
  146. this.messageType = messageType;
  147. this.prompt = prompt;
  148. }
  149. public String getPrompt() {
  150. return prompt;
  151. }
  152. public int getMessageType() {
  153. return messageType;
  154. }
  155. public int getDefaultOption() {
  156. return defaultOption;
  157. }
  158. public String[] getOptions() {
  159. return options;
  160. }
  161. public int getOptionType() {
  162. return optionType;
  163. }
  164. public int getSelectedIndex() {
  165. return selection;
  166. }
  167. public void setSelectedIndex(int selection) {
  168. if (options != null) {
  169. if (0 <= selection && selection <= options.length) {
  170. this.selection = selection;
  171. } else {
  172. throw new ArrayIndexOutOfBoundsException(Messages.getString("auth.1B")); //$NON-NLS-1$
  173. }
  174. } else {
  175. switch (optionType) {
  176. case YES_NO_OPTION:
  177. if (selection != YES && selection != NO) {
  178. throw new IllegalArgumentException(Messages.getString("auth.19")); //$NON-NLS-1$
  179. }
  180. break;
  181. case YES_NO_CANCEL_OPTION:
  182. if (selection != YES && selection != NO && selection != CANCEL) {
  183. throw new IllegalArgumentException(Messages.getString("auth.19")); //$NON-NLS-1$
  184. }
  185. break;
  186. case OK_CANCEL_OPTION:
  187. if (selection != OK && selection != CANCEL) {
  188. throw new IllegalArgumentException(Messages.getString("auth.19")); //$NON-NLS-1$
  189. }
  190. break;
  191. }
  192. this.selection = selection;
  193. }
  194. }
  195. }