ChoiceCallback.java.svn-base 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 ChoiceCallback implements Callback, Serializable {
  21. private static final long serialVersionUID = -3975664071579892167L;
  22. private int defaultChoice;
  23. private String prompt;
  24. private boolean multipleSelectionsAllowed;
  25. private String[] choices;
  26. private int[] selections;
  27. private void setChoices(String[] choices) {
  28. if (choices == null || choices.length == 0) {
  29. throw new IllegalArgumentException(Messages.getString("auth.1C")); //$NON-NLS-1$
  30. }
  31. for (int i = 0; i < choices.length; i++) {
  32. if (choices[i] == null || choices[i].length() == 0) {
  33. throw new IllegalArgumentException(Messages.getString("auth.1C")); //$NON-NLS-1$
  34. }
  35. }
  36. //FIXME: System.arraycopy(choices, 0 , new String[choices.length], 0, choices.length);
  37. this.choices = choices;
  38. }
  39. private void setPrompt(String prompt) {
  40. if (prompt == null || prompt.length() == 0) {
  41. throw new IllegalArgumentException(Messages.getString("auth.14")); //$NON-NLS-1$
  42. }
  43. this.prompt = prompt;
  44. }
  45. private void setDefaultChoice(int defaultChoice) {
  46. if (0 > defaultChoice || defaultChoice >= choices.length) {
  47. throw new IllegalArgumentException(Messages.getString("auth.1D")); //$NON-NLS-1$
  48. }
  49. this.defaultChoice = defaultChoice;
  50. }
  51. public ChoiceCallback(String prompt, String[] choices, int defaultChoice,
  52. boolean multipleSelectionsAllowed) {
  53. super();
  54. setPrompt(prompt);
  55. setChoices(choices);
  56. setDefaultChoice(defaultChoice);
  57. this.multipleSelectionsAllowed = multipleSelectionsAllowed;
  58. }
  59. public boolean allowMultipleSelections() {
  60. return multipleSelectionsAllowed;
  61. }
  62. public String[] getChoices() {
  63. return choices;
  64. }
  65. public int getDefaultChoice() {
  66. return defaultChoice;
  67. }
  68. public String getPrompt() {
  69. return prompt;
  70. }
  71. public int[] getSelectedIndexes() {
  72. return selections;
  73. }
  74. public void setSelectedIndex(int selection) {
  75. this.selections = new int[1];
  76. this.selections[0] = selection;
  77. }
  78. public void setSelectedIndexes(int[] selections) {
  79. if (!multipleSelectionsAllowed) {
  80. throw new UnsupportedOperationException();
  81. }
  82. this.selections = selections;
  83. //FIXME:
  84. // this.selections = new int[selections.length]
  85. //System.arraycopy(selections, 0, this.selections, 0, this.selections.length);
  86. }
  87. }