001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.security.auth.callback;
019:
020: import java.io.Serializable;
021:
022: import org.apache.harmony.auth.internal.nls.Messages;
023:
024: public class ConfirmationCallback implements Callback, Serializable {
025:
026: private static final long serialVersionUID = -9095656433782481624L;
027:
028: public static final int YES = 0; // default options
029:
030: public static final int NO = 1;
031:
032: public static final int CANCEL = 2;
033:
034: public static final int OK = 3;
035:
036: public static final int YES_NO_OPTION = 0; // options type
037:
038: public static final int YES_NO_CANCEL_OPTION = 1;
039:
040: public static final int OK_CANCEL_OPTION = 2;
041:
042: public static final int UNSPECIFIED_OPTION = -1;
043:
044: public static final int INFORMATION = 0; // messages type
045:
046: public static final int WARNING = 1;
047:
048: public static final int ERROR = 2;
049:
050: private String prompt;
051:
052: private int messageType;
053:
054: private int optionType = UNSPECIFIED_OPTION;
055:
056: private int defaultOption;
057:
058: private String[] options;
059:
060: private int selection;
061:
062: public ConfirmationCallback(int messageType, int optionType,
063: int defaultOption) {
064: super ();
065: if (messageType > ERROR || messageType < INFORMATION) {
066: throw new IllegalArgumentException(Messages
067: .getString("auth.16")); //$NON-NLS-1$
068: }
069:
070: switch (optionType) {
071: case YES_NO_OPTION:
072: if (defaultOption != YES && defaultOption != NO) {
073: throw new IllegalArgumentException(Messages
074: .getString("auth.17")); //$NON-NLS-1$
075: }
076: break;
077: case YES_NO_CANCEL_OPTION:
078: if (defaultOption != YES && defaultOption != NO
079: && defaultOption != CANCEL) {
080: throw new IllegalArgumentException(Messages
081: .getString("auth.17")); //$NON-NLS-1$
082: }
083: break;
084: case OK_CANCEL_OPTION:
085: if (defaultOption != OK && defaultOption != CANCEL) {
086: throw new IllegalArgumentException(Messages
087: .getString("auth.17")); //$NON-NLS-1$
088: }
089: break;
090: default:
091: throw new IllegalArgumentException(Messages
092: .getString("auth.18")); //$NON-NLS-1$
093: }
094: this .messageType = messageType;
095: this .optionType = optionType;
096: this .defaultOption = defaultOption;
097: }
098:
099: public ConfirmationCallback(int messageType, String[] options,
100: int defaultOption) {
101: super ();
102: if (messageType > ERROR || messageType < INFORMATION) {
103: throw new IllegalArgumentException(Messages
104: .getString("auth.16")); //$NON-NLS-1$
105: }
106:
107: if (options == null || options.length == 0) {
108: throw new IllegalArgumentException(Messages
109: .getString("auth.1A")); //$NON-NLS-1$
110: }
111: for (int i = 0; i < options.length; i++) {
112: if (options[i] == null || options[i].length() == 0) {
113: throw new IllegalArgumentException(Messages
114: .getString("auth.1A")); //$NON-NLS-1$
115: }
116: }
117: if (0 > defaultOption || defaultOption >= options.length) {
118: throw new IllegalArgumentException(Messages
119: .getString("auth.17")); //$NON-NLS-1$
120: }
121: // FIXME:System.arraycopy(options, 0 , new String[this.options.length],
122: // 0, this.options.length);
123: this .options = options;
124: this .defaultOption = defaultOption;
125: this .messageType = messageType;
126: }
127:
128: public ConfirmationCallback(String prompt, int messageType,
129: int optionType, int defaultOption) {
130: super ();
131: if (prompt == null || prompt.length() == 0) {
132: throw new IllegalArgumentException(Messages
133: .getString("auth.14")); //$NON-NLS-1$
134: }
135:
136: if (messageType > ERROR || messageType < INFORMATION) {
137: throw new IllegalArgumentException(Messages
138: .getString("auth.16")); //$NON-NLS-1$
139: }
140:
141: switch (optionType) {
142: case YES_NO_OPTION:
143: if (defaultOption != YES && defaultOption != NO) {
144: throw new IllegalArgumentException(Messages
145: .getString("auth.17")); //$NON-NLS-1$
146: }
147: break;
148: case YES_NO_CANCEL_OPTION:
149: if (defaultOption != YES && defaultOption != NO
150: && defaultOption != CANCEL) {
151: throw new IllegalArgumentException(Messages
152: .getString("auth.17")); //$NON-NLS-1$
153: }
154: break;
155: case OK_CANCEL_OPTION:
156: if (defaultOption != OK && defaultOption != CANCEL) {
157: throw new IllegalArgumentException(Messages
158: .getString("auth.17")); //$NON-NLS-1$
159: }
160: break;
161: default:
162: throw new IllegalArgumentException(Messages
163: .getString("auth.18")); //$NON-NLS-1$
164: }
165: this .prompt = prompt;
166: this .messageType = messageType;
167: this .optionType = optionType;
168: this .defaultOption = defaultOption;
169: }
170:
171: public ConfirmationCallback(String prompt, int messageType,
172: String[] options, int defaultOption) {
173: super ();
174: if (prompt == null || prompt.length() == 0) {
175: throw new IllegalArgumentException(Messages
176: .getString("auth.14")); //$NON-NLS-1$
177: }
178:
179: if (messageType > ERROR || messageType < INFORMATION) {
180: throw new IllegalArgumentException(Messages
181: .getString("auth.16")); //$NON-NLS-1$
182: }
183:
184: if (options == null || options.length == 0) {
185: throw new IllegalArgumentException(Messages
186: .getString("auth.1A")); //$NON-NLS-1$
187: }
188: for (int i = 0; i < options.length; i++) {
189: if (options[i] == null || options[i].length() == 0) {
190: throw new IllegalArgumentException(Messages
191: .getString("auth.1A")); //$NON-NLS-1$
192: }
193: }
194: if (0 > defaultOption || defaultOption >= options.length) {
195: throw new IllegalArgumentException(Messages
196: .getString("auth.17")); //$NON-NLS-1$
197: }
198: // FIXME:System.arraycopy(options, 0 , new String[this.options.length],
199: // 0, this.options.length);
200: this .options = options;
201: this .defaultOption = defaultOption;
202: this .messageType = messageType;
203: this .prompt = prompt;
204: }
205:
206: public String getPrompt() {
207: return prompt;
208: }
209:
210: public int getMessageType() {
211: return messageType;
212: }
213:
214: public int getDefaultOption() {
215: return defaultOption;
216: }
217:
218: public String[] getOptions() {
219: return options;
220: }
221:
222: public int getOptionType() {
223: return optionType;
224: }
225:
226: public int getSelectedIndex() {
227: return selection;
228: }
229:
230: public void setSelectedIndex(int selection) {
231: if (options != null) {
232: if (0 <= selection && selection <= options.length) {
233: this .selection = selection;
234: } else {
235: throw new ArrayIndexOutOfBoundsException(Messages
236: .getString("auth.1B")); //$NON-NLS-1$
237: }
238: } else {
239: switch (optionType) {
240: case YES_NO_OPTION:
241: if (selection != YES && selection != NO) {
242: throw new IllegalArgumentException(Messages
243: .getString("auth.19")); //$NON-NLS-1$
244: }
245: break;
246: case YES_NO_CANCEL_OPTION:
247: if (selection != YES && selection != NO
248: && selection != CANCEL) {
249: throw new IllegalArgumentException(Messages
250: .getString("auth.19")); //$NON-NLS-1$
251: }
252: break;
253: case OK_CANCEL_OPTION:
254: if (selection != OK && selection != CANCEL) {
255: throw new IllegalArgumentException(Messages
256: .getString("auth.19")); //$NON-NLS-1$
257: }
258: break;
259: }
260: this.selection = selection;
261: }
262: }
263: }
|