blob: a1893f3bd3d8dd0261998cca9fe782f5b5261c8b [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/*
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
18package org.apache.harmony.javax.security.auth.callback;
19
20import java.io.Serializable;
21
22
23
24public class ConfirmationCallback implements Callback, Serializable {
25
26 private static final long serialVersionUID = -9095656433782481624L;
27
28 public static final int YES = 0; // default options
29
30 public static final int NO = 1;
31
32 public static final int CANCEL = 2;
33
34 public static final int OK = 3;
35
36 public static final int YES_NO_OPTION = 0; // options type
37
38 public static final int YES_NO_CANCEL_OPTION = 1;
39
40 public static final int OK_CANCEL_OPTION = 2;
41
42 public static final int UNSPECIFIED_OPTION = -1;
43
44 public static final int INFORMATION = 0; // messages type
45
46 public static final int WARNING = 1;
47
48 public static final int ERROR = 2;
49
50 private String prompt;
51
52 private int messageType;
53
54 private int optionType = UNSPECIFIED_OPTION;
55
56 private int defaultOption;
57
58 private String[] options;
59
60 private int selection;
61
62 public ConfirmationCallback(int messageType, int optionType, int defaultOption) {
63 super();
64 if (messageType > ERROR || messageType < INFORMATION) {
65 throw new IllegalArgumentException("auth.16"); //$NON-NLS-1$
66 }
67
68 switch (optionType) {
69 case YES_NO_OPTION:
70 if (defaultOption != YES && defaultOption != NO) {
71 throw new IllegalArgumentException("auth.17"); //$NON-NLS-1$
72 }
73 break;
74 case YES_NO_CANCEL_OPTION:
75 if (defaultOption != YES && defaultOption != NO && defaultOption != CANCEL) {
76 throw new IllegalArgumentException("auth.17"); //$NON-NLS-1$
77 }
78 break;
79 case OK_CANCEL_OPTION:
80 if (defaultOption != OK && defaultOption != CANCEL) {
81 throw new IllegalArgumentException("auth.17"); //$NON-NLS-1$
82 }
83 break;
84 default:
85 throw new IllegalArgumentException("auth.18"); //$NON-NLS-1$
86 }
87 this.messageType = messageType;
88 this.optionType = optionType;
89 this.defaultOption = defaultOption;
90 }
91
92 public ConfirmationCallback(int messageType, String[] options, int defaultOption) {
93 super();
94 if (messageType > ERROR || messageType < INFORMATION) {
95 throw new IllegalArgumentException("auth.16"); //$NON-NLS-1$
96 }
97
98 if (options == null || options.length == 0) {
99 throw new IllegalArgumentException("auth.1A"); //$NON-NLS-1$
100 }
101 for (int i = 0; i < options.length; i++) {
102 if (options[i] == null || options[i].length() == 0) {
103 throw new IllegalArgumentException("auth.1A"); //$NON-NLS-1$
104 }
105 }
106 if (0 > defaultOption || defaultOption >= options.length) {
107 throw new IllegalArgumentException("auth.17"); //$NON-NLS-1$
108 }
109 // FIXME:System.arraycopy(options, 0 , new String[this.options.length],
110 // 0, this.options.length);
111 this.options = options;
112 this.defaultOption = defaultOption;
113 this.messageType = messageType;
114 }
115
116 public ConfirmationCallback(String prompt, int messageType, int optionType,
117 int defaultOption) {
118 super();
119 if (prompt == null || prompt.length() == 0) {
120 throw new IllegalArgumentException("auth.14"); //$NON-NLS-1$
121 }
122
123 if (messageType > ERROR || messageType < INFORMATION) {
124 throw new IllegalArgumentException("auth.16"); //$NON-NLS-1$
125 }
126
127 switch (optionType) {
128 case YES_NO_OPTION:
129 if (defaultOption != YES && defaultOption != NO) {
130 throw new IllegalArgumentException("auth.17"); //$NON-NLS-1$
131 }
132 break;
133 case YES_NO_CANCEL_OPTION:
134 if (defaultOption != YES && defaultOption != NO && defaultOption != CANCEL) {
135 throw new IllegalArgumentException("auth.17"); //$NON-NLS-1$
136 }
137 break;
138 case OK_CANCEL_OPTION:
139 if (defaultOption != OK && defaultOption != CANCEL) {
140 throw new IllegalArgumentException("auth.17"); //$NON-NLS-1$
141 }
142 break;
143 default:
144 throw new IllegalArgumentException("auth.18"); //$NON-NLS-1$
145 }
146 this.prompt = prompt;
147 this.messageType = messageType;
148 this.optionType = optionType;
149 this.defaultOption = defaultOption;
150 }
151
152 public ConfirmationCallback(String prompt, int messageType, String[] options,
153 int defaultOption) {
154 super();
155 if (prompt == null || prompt.length() == 0) {
156 throw new IllegalArgumentException("auth.14"); //$NON-NLS-1$
157 }
158
159 if (messageType > ERROR || messageType < INFORMATION) {
160 throw new IllegalArgumentException("auth.16"); //$NON-NLS-1$
161 }
162
163 if (options == null || options.length == 0) {
164 throw new IllegalArgumentException("auth.1A"); //$NON-NLS-1$
165 }
166 for (int i = 0; i < options.length; i++) {
167 if (options[i] == null || options[i].length() == 0) {
168 throw new IllegalArgumentException("auth.1A"); //$NON-NLS-1$
169 }
170 }
171 if (0 > defaultOption || defaultOption >= options.length) {
172 throw new IllegalArgumentException("auth.17"); //$NON-NLS-1$
173 }
174 // FIXME:System.arraycopy(options, 0 , new String[this.options.length],
175 // 0, this.options.length);
176 this.options = options;
177 this.defaultOption = defaultOption;
178 this.messageType = messageType;
179 this.prompt = prompt;
180 }
181
182 public String getPrompt() {
183 return prompt;
184 }
185
186 public int getMessageType() {
187 return messageType;
188 }
189
190 public int getDefaultOption() {
191 return defaultOption;
192 }
193
194 public String[] getOptions() {
195 return options;
196 }
197
198 public int getOptionType() {
199 return optionType;
200 }
201
202 public int getSelectedIndex() {
203 return selection;
204 }
205
206 public void setSelectedIndex(int selection) {
207 if (options != null) {
208 if (0 <= selection && selection <= options.length) {
209 this.selection = selection;
210 } else {
211 throw new ArrayIndexOutOfBoundsException("auth.1B"); //$NON-NLS-1$
212 }
213 } else {
214 switch (optionType) {
215 case YES_NO_OPTION:
216 if (selection != YES && selection != NO) {
217 throw new IllegalArgumentException("auth.19"); //$NON-NLS-1$
218 }
219 break;
220 case YES_NO_CANCEL_OPTION:
221 if (selection != YES && selection != NO && selection != CANCEL) {
222 throw new IllegalArgumentException("auth.19"); //$NON-NLS-1$
223 }
224 break;
225 case OK_CANCEL_OPTION:
226 if (selection != OK && selection != CANCEL) {
227 throw new IllegalArgumentException("auth.19"); //$NON-NLS-1$
228 }
229 break;
230 }
231 this.selection = selection;
232 }
233 }
234}