private void addLoginCallbackMessage(Callback[] callbacks) throws UnsupportedCallbackException { int i = 0; try { for (i = 0; i < callbacks.length; i++) { if (callbacks[i] instanceof TextOutputCallback) { handleTextOutputCallback((TextOutputCallback)callbacks[i]); } else if (callbacks[i] instanceof NameCallback) { handleNameCallback((NameCallback)callbacks[i]); } else if (callbacks[i] instanceof PasswordCallback) { handlePasswordCallback((PasswordCallback)callbacks[i]); } else if (callbacks[i] instanceof TextInputCallback) { handleTextInputCallback((TextInputCallback)callbacks[i]); } else if (callbacks[i] instanceof ChoiceCallback) { handleChoiceCallback((ChoiceCallback)callbacks[i]); } } } catch (IOException e) { e.printStackTrace(); throw new UnsupportedCallbackException(callbacks[i],e.getMessage()); } }
private void handleChoiceCallback(ChoiceCallback cc) throws IOException { // ignore the provided defaultValue System.out.print(cc.getPrompt()); String[] strChoices = cc.getChoices(); for (int j = 0; j < strChoices.length; j++) { System.out.print("choice[" + j + "] : " + strChoices[j]); } System.out.flush(); cc.setSelectedIndex(Integer.parseInt((new BufferedReader (new InputStreamReader(System.in))).readLine())); }
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { if (callbacks == null) throw new NullPointerException(); for (int i = 0; i < callbacks.length; i++) { if (callbacks[i] == null) continue; if (callbacks[i] instanceof ChoiceCallback) handleChoice((ChoiceCallback) callbacks[i]); else if (callbacks[i] instanceof ConfirmationCallback) handleConfirmation((ConfirmationCallback) callbacks[i]); else if (callbacks[i] instanceof LanguageCallback) handleLanguage((LanguageCallback) callbacks[i]); else if (callbacks[i] instanceof NameCallback) handleName((NameCallback) callbacks[i]); else if (callbacks[i] instanceof PasswordCallback) handlePassword((PasswordCallback) callbacks[i]); else if (callbacks[i] instanceof TextInputCallback) handleTextInput((TextInputCallback) callbacks[i]); else if (callbacks[i] instanceof TextOutputCallback) handleTextOutput((TextOutputCallback) callbacks[i]); else handleOther(callbacks[i]); } }
@Override protected Object[] getData() { String prompt = "prompt"; int defaultChoice = 1; String[] choices = {"AAA", "BBB"}; return new Object[] {new ChoiceCallback(prompt, choices, defaultChoice, true)}; }
public void assertDeserialized(Serializable golden, Serializable test) { assertTrue(golden instanceof ChoiceCallback); assertEquals(((ChoiceCallback) golden).getPrompt(), ((ChoiceCallback) test).getPrompt()); assertEquals(((ChoiceCallback) golden).getDefaultChoice(), ((ChoiceCallback) test).getDefaultChoice()); }
/** * Class under test for Ctor */ public final void testChoiceCallback_01() { cb = new ChoiceCallback(prompt, choices, defaultChoice, true); assertEquals(this.prompt, cb.getPrompt()); assertEquals(this.choices, cb.getChoices()); assertEquals(this.defaultChoice, cb.getDefaultChoice()); assertTrue(cb.allowMultipleSelections()); }
/** * test for the method setSelectedIndexes */ public final void testChoiceCallback_02() { cb = new ChoiceCallback(prompt, choices, defaultChoice, false); try { cb.setSelectedIndexes(index); fail("should be throw UnsupportedOperationException"); } catch (UnsupportedOperationException e) { } cb.setSelectedIndex(1); assertEquals(1, cb.getSelectedIndexes()[0]); }
protected synchronized void handleChoice(ChoiceCallback c) { Frame ownerFrame = new Frame(); Dialog dialog = new Dialog(ownerFrame); String[] choices = c.getChoices(); dialog.setTitle(c.getPrompt()); Label label = new Label(c.getPrompt()); List list = new List(Math.min(5, choices.length), c.allowMultipleSelections()); Panel buttons = new Panel(); Button ok = new Button(messages.getString("callback.ok")); ok.setActionCommand(ACTION_OK); ok.addActionListener(this); Button cancel = new Button(messages.getString("callback.cancel")); cancel.setActionCommand(ACTION_CANCEL); cancel.addActionListener(this); for (int i = 0; i < choices.length; i++) { list.add(choices[i]); } if (c.getDefaultChoice() >= 0 && c.getDefaultChoice() < choices.length) { list.select(c.getDefaultChoice()); } dialog.setLayout(new BorderLayout()); dialog.add(label, BorderLayout.NORTH); dialog.add(list, BorderLayout.CENTER); buttons.setLayout(new FlowLayout(FlowLayout.RIGHT)); buttons.add(cancel); buttons.add(ok); dialog.add(buttons, BorderLayout.SOUTH); dialog.pack(); dialog.show(); try { wait(); } catch (InterruptedException ie) { } if (actionCommand.equals(ACTION_OK)) { if (c.allowMultipleSelections()) { c.setSelectedIndexes(list.getSelectedIndexes()); } else { c.setSelectedIndex(list.getSelectedIndex()); } } dialog.dispose(); ownerFrame.dispose(); }
protected synchronized void handleLanguage(LanguageCallback c) { Locale[] locales = Locale.getAvailableLocales(); String[] languages = new String[locales.length]; Locale def = Locale.getDefault(); int defind = 0; for (int i = 0; i < locales.length; i++) { CPStringBuilder lang = new CPStringBuilder(locales[i].getDisplayLanguage(locales[i])); String country = locales[i].getDisplayCountry(locales[i]); String variant = locales[i].getDisplayVariant(locales[i]); if (country.length() > 0 && variant.length() > 0) { lang.append(" ("); lang.append(country); lang.append(", "); lang.append(variant); lang.append(")"); } else if (country.length() > 0) { lang.append(" ("); lang.append(country); lang.append(")"); } else if (variant.length() > 0) { lang.append(" ("); lang.append(variant); lang.append(")"); } languages[i] = lang.toString(); if (locales[i].equals(def)) defind = i; } ChoiceCallback c2 = new ChoiceCallback(messages.getString("callback.language"), languages, defind, false); handleChoice(c2); c.setLocale(def); if (c2.getSelectedIndexes() != null && c2.getSelectedIndexes().length > 0) { int index = c2.getSelectedIndexes()[0]; if (index >= 0 && index < locales.length) c.setLocale(locales[index]); } }
protected void handleChoice(ChoiceCallback c) { c.setSelectedIndex(c.getDefaultChoice()); }
private int getStateManageDevices(String successMessage) throws AuthLoginException { if (successMessage != null) { replaceCallback(STATE_MANAGE_DEVICES, 0, new TextOutputCallback(TextOutputCallback.INFORMATION, successMessage)); } List<String> devices = new ArrayList<String>(); for (DataStoreElement dataStoreElement : getDataStore().getRegistrationData(getIdentity())) { devices.add(dataStoreElement.getDeviceName()); } ChoiceCallback choiceCallback = new ChoiceCallback(bundle.getString("u2f-register-choice-header"), devices.toArray(new String[] {}), 0, true); replaceCallback(STATE_MANAGE_DEVICES, 1, choiceCallback); return STATE_MANAGE_DEVICES; }
/** * Handles a {@link ChoiceCallback}. * * @param callback The choice callback. * @throws IOException If an I/O error occurs. */ protected abstract void handleChoice(ChoiceCallback callback) throws IOException;