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
18package org.apache.harmony.javax.security.auth.callback;
19
20import java.io.Serializable;
21
22
23
24public class ChoiceCallback implements Callback, Serializable {
25
26    private static final long serialVersionUID = -3975664071579892167L;
27
28    private int defaultChoice;
29
30    private String prompt;
31
32    private boolean multipleSelectionsAllowed;
33
34    private String[] choices;
35
36    private int[] selections;
37
38    private void setChoices(String[] choices) {
39        if (choices == null || choices.length == 0) {
40            throw new IllegalArgumentException("auth.1C"); //$NON-NLS-1$
41        }
42        for (int i = 0; i < choices.length; i++) {
43            if (choices[i] == null || choices[i].length() == 0) {
44                throw new IllegalArgumentException("auth.1C"); //$NON-NLS-1$
45            }
46        }
47        //FIXME: System.arraycopy(choices, 0 , new String[choices.length], 0, choices.length);
48        this.choices = choices;
49
50    }
51
52    private void setPrompt(String prompt) {
53        if (prompt == null || prompt.length() == 0) {
54            throw new IllegalArgumentException("auth.14"); //$NON-NLS-1$
55        }
56        this.prompt = prompt;
57    }
58
59    private void setDefaultChoice(int defaultChoice) {
60        if (0 > defaultChoice || defaultChoice >= choices.length) {
61            throw new IllegalArgumentException("auth.1D"); //$NON-NLS-1$
62        }
63        this.defaultChoice = defaultChoice;
64    }
65
66    public ChoiceCallback(String prompt, String[] choices, int defaultChoice,
67            boolean multipleSelectionsAllowed) {
68        super();
69        setPrompt(prompt);
70        setChoices(choices);
71        setDefaultChoice(defaultChoice);
72        this.multipleSelectionsAllowed = multipleSelectionsAllowed;
73    }
74
75    public boolean allowMultipleSelections() {
76        return multipleSelectionsAllowed;
77    }
78
79    public String[] getChoices() {
80        return choices;
81    }
82
83    public int getDefaultChoice() {
84        return defaultChoice;
85    }
86
87    public String getPrompt() {
88        return prompt;
89    }
90
91    public int[] getSelectedIndexes() {
92        return selections;
93    }
94
95    public void setSelectedIndex(int selection) {
96        this.selections = new int[1];
97        this.selections[0] = selection;
98    }
99
100    public void setSelectedIndexes(int[] selections) {
101        if (!multipleSelectionsAllowed) {
102            throw new UnsupportedOperationException();
103        }
104        this.selections = selections;
105        //FIXME:
106        // this.selections = new int[selections.length]
107        //System.arraycopy(selections, 0, this.selections, 0, this.selections.length);
108    }
109}
110