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 ChoiceCallback implements Callback, Serializable {
025:
026: private static final long serialVersionUID = -3975664071579892167L;
027:
028: private int defaultChoice;
029:
030: private String prompt;
031:
032: private boolean multipleSelectionsAllowed;
033:
034: private String[] choices;
035:
036: private int[] selections;
037:
038: private void setChoices(String[] choices) {
039: if (choices == null || choices.length == 0) {
040: throw new IllegalArgumentException(Messages
041: .getString("auth.1C")); //$NON-NLS-1$
042: }
043: for (int i = 0; i < choices.length; i++) {
044: if (choices[i] == null || choices[i].length() == 0) {
045: throw new IllegalArgumentException(Messages
046: .getString("auth.1C")); //$NON-NLS-1$
047: }
048: }
049: //FIXME: System.arraycopy(choices, 0 , new String[choices.length], 0, choices.length);
050: this .choices = choices;
051:
052: }
053:
054: private void setPrompt(String prompt) {
055: if (prompt == null || prompt.length() == 0) {
056: throw new IllegalArgumentException(Messages
057: .getString("auth.14")); //$NON-NLS-1$
058: }
059: this .prompt = prompt;
060: }
061:
062: private void setDefaultChoice(int defaultChoice) {
063: if (0 > defaultChoice || defaultChoice >= choices.length) {
064: throw new IllegalArgumentException(Messages
065: .getString("auth.1D")); //$NON-NLS-1$
066: }
067: this .defaultChoice = defaultChoice;
068: }
069:
070: public ChoiceCallback(String prompt, String[] choices,
071: int defaultChoice, boolean multipleSelectionsAllowed) {
072: super ();
073: setPrompt(prompt);
074: setChoices(choices);
075: setDefaultChoice(defaultChoice);
076: this .multipleSelectionsAllowed = multipleSelectionsAllowed;
077: }
078:
079: public boolean allowMultipleSelections() {
080: return multipleSelectionsAllowed;
081: }
082:
083: public String[] getChoices() {
084: return choices;
085: }
086:
087: public int getDefaultChoice() {
088: return defaultChoice;
089: }
090:
091: public String getPrompt() {
092: return prompt;
093: }
094:
095: public int[] getSelectedIndexes() {
096: return selections;
097: }
098:
099: public void setSelectedIndex(int selection) {
100: this .selections = new int[1];
101: this .selections[0] = selection;
102: }
103:
104: public void setSelectedIndexes(int[] selections) {
105: if (!multipleSelectionsAllowed) {
106: throw new UnsupportedOperationException();
107: }
108: this .selections = selections;
109: //FIXME:
110: // this.selections = new int[selections.length]
111: //System.arraycopy(selections, 0, this.selections, 0, this.selections.length);
112: }
113: }
|