@Test public void testVerifyText_NoContacts() throws Exception { List<Contact> allContacts = Lists.newArrayList(); when(contactService.allContacts()).thenReturn(allContacts); when(contactService.filterContactsForSingleMatch(anyString(),anyBoolean())).thenReturn(Optional.<Contact>absent()); JComboBox<Recipient> comboBox = ComboBoxes.newRecipientComboBox(contactService, BitcoinNetwork.current().get()); ThemeAwareRecipientInputVerifier testObject = new ThemeAwareRecipientInputVerifier(contactService); RecipientComboBoxEditor.RecipientComboBoxTextField comboEditor = ((RecipientComboBoxEditor.RecipientComboBoxTextField) comboBox.getEditor().getEditorComponent()); comboBox.setSelectedItem(""); assertThat(testObject.verify(comboEditor)).isFalse(); comboBox.setSelectedItem(" "); assertThat(testObject.verify(comboEditor)).isFalse(); comboBox.setSelectedItem("AhN6rPdrMuKBGFDKR1k9A8SCLYaNgXhty"); assertThat(testObject.verify(comboEditor)).isFalse(); comboBox.setSelectedItem("1AhN6rPdrMuKBGFDKR1k9A8SCLYaNgdfjkt"); assertThat(testObject.verify(comboEditor)).isFalse(); comboBox.setSelectedItem("1AhN6rPdrMuKBGFDKR1k9A8SCLYa"); assertThat(testObject.verify(comboEditor)).isFalse(); comboBox.setSelectedItem("1AhN6rPdrMuKBGFDKR1k9A8SCLYaNgXhty"); assertThat(testObject.verify(comboEditor)).isTrue(); comboBox.setSelectedItem("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1t"); assertThat(testObject.verify(comboEditor)).isFalse(); comboBox.setSelectedItem("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tUB"); assertThat(testObject.verify(comboEditor)).isFalse(); comboBox.setSelectedItem("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU"); assertThat(testObject.verify(comboEditor)).isTrue(); }
@Override public boolean verify(JComponent component) { if (component instanceof RecipientComboBoxEditor.RecipientComboBoxTextField) { boolean isValid = false; RecipientComboBoxEditor.RecipientComboBoxTextField textField = (RecipientComboBoxEditor.RecipientComboBoxTextField) component; String text = textField.getText(); if (!Strings.isNullOrEmpty(text)) { final Optional<Address> enteredAddress = Addresses.parse(text); if (enteredAddress.isPresent()) { isValid = true; Recipient recipient = new Recipient(enteredAddress.get()); textField.setRecipient(Optional.of(recipient)); } else { Optional<Contact> contactOptional = contactService.filterContactsForSingleMatch(text, true); if (contactOptional.isPresent()) { Contact contact = contactOptional.get(); Optional<Address> bitcoinAddress = contact.getBitcoinAddress(); if (bitcoinAddress.isPresent()) { Recipient recipient = new Recipient(bitcoinAddress.get()); recipient.setContact(contact); textField.setText(contact.getName()); textField.setRecipient(Optional.of(recipient)); isValid = true; } } } } component.setBackground(isValid ? validColor : invalidColor); return isValid; } else { throw new IllegalArgumentException("'component' must be a JTextField. Actual: " + component.getClass().getCanonicalName()); } }
ThemeAwareRecipientInputVerifier extends InputVerifier { @Override public boolean verify(JComponent component) { if (component instanceof RecipientComboBoxEditor.RecipientComboBoxTextField) { boolean isValid = false; RecipientComboBoxEditor.RecipientComboBoxTextField textField = (RecipientComboBoxEditor.RecipientComboBoxTextField) component; String text = textField.getText(); if (!Strings.isNullOrEmpty(text)) { final Optional<Address> enteredAddress = Addresses.parse(text); if (enteredAddress.isPresent()) { isValid = true; Recipient recipient = new Recipient(enteredAddress.get()); textField.setRecipient(Optional.of(recipient)); } else { Optional<Contact> contactOptional = contactService.filterContactsForSingleMatch(text, true); if (contactOptional.isPresent()) { Contact contact = contactOptional.get(); Optional<Address> bitcoinAddress = contact.getBitcoinAddress(); if (bitcoinAddress.isPresent()) { Recipient recipient = new Recipient(bitcoinAddress.get()); recipient.setContact(contact); textField.setText(contact.getName()); textField.setRecipient(Optional.of(recipient)); isValid = true; } } } } component.setBackground(isValid ? validColor : invalidColor); return isValid; } else { throw new IllegalArgumentException("'component' must be a JTextField. Actual: " + component.getClass().getCanonicalName()); } } }
ThemeAwareRecipientInputVerifier extends InputVerifier { @Override public boolean verify(JComponent component) { if (component instanceof RecipientComboBoxEditor.RecipientComboBoxTextField) { boolean isValid = false; RecipientComboBoxEditor.RecipientComboBoxTextField textField = (RecipientComboBoxEditor.RecipientComboBoxTextField) component; String text = textField.getText(); if (!Strings.isNullOrEmpty(text)) { final Optional<Address> enteredAddress = Addresses.parse(text); if (enteredAddress.isPresent()) { isValid = true; Recipient recipient = new Recipient(enteredAddress.get()); textField.setRecipient(Optional.of(recipient)); } else { Optional<Contact> contactOptional = contactService.filterContactsForSingleMatch(text, true); if (contactOptional.isPresent()) { Contact contact = contactOptional.get(); Optional<Address> bitcoinAddress = contact.getBitcoinAddress(); if (bitcoinAddress.isPresent()) { Recipient recipient = new Recipient(bitcoinAddress.get()); recipient.setContact(contact); textField.setText(contact.getName()); textField.setRecipient(Optional.of(recipient)); isValid = true; } } } } component.setBackground(isValid ? validColor : invalidColor); return isValid; } else { throw new IllegalArgumentException("'component' must be a JTextField. Actual: " + component.getClass().getCanonicalName()); } } ThemeAwareRecipientInputVerifier(ContactService contactService); }
ThemeAwareRecipientInputVerifier extends InputVerifier { @Override public boolean verify(JComponent component) { if (component instanceof RecipientComboBoxEditor.RecipientComboBoxTextField) { boolean isValid = false; RecipientComboBoxEditor.RecipientComboBoxTextField textField = (RecipientComboBoxEditor.RecipientComboBoxTextField) component; String text = textField.getText(); if (!Strings.isNullOrEmpty(text)) { final Optional<Address> enteredAddress = Addresses.parse(text); if (enteredAddress.isPresent()) { isValid = true; Recipient recipient = new Recipient(enteredAddress.get()); textField.setRecipient(Optional.of(recipient)); } else { Optional<Contact> contactOptional = contactService.filterContactsForSingleMatch(text, true); if (contactOptional.isPresent()) { Contact contact = contactOptional.get(); Optional<Address> bitcoinAddress = contact.getBitcoinAddress(); if (bitcoinAddress.isPresent()) { Recipient recipient = new Recipient(bitcoinAddress.get()); recipient.setContact(contact); textField.setText(contact.getName()); textField.setRecipient(Optional.of(recipient)); isValid = true; } } } } component.setBackground(isValid ? validColor : invalidColor); return isValid; } else { throw new IllegalArgumentException("'component' must be a JTextField. Actual: " + component.getClass().getCanonicalName()); } } ThemeAwareRecipientInputVerifier(ContactService contactService); @Override boolean verify(JComponent component); @Override boolean shouldYieldFocus(JComponent input); }
ThemeAwareRecipientInputVerifier extends InputVerifier { @Override public boolean verify(JComponent component) { if (component instanceof RecipientComboBoxEditor.RecipientComboBoxTextField) { boolean isValid = false; RecipientComboBoxEditor.RecipientComboBoxTextField textField = (RecipientComboBoxEditor.RecipientComboBoxTextField) component; String text = textField.getText(); if (!Strings.isNullOrEmpty(text)) { final Optional<Address> enteredAddress = Addresses.parse(text); if (enteredAddress.isPresent()) { isValid = true; Recipient recipient = new Recipient(enteredAddress.get()); textField.setRecipient(Optional.of(recipient)); } else { Optional<Contact> contactOptional = contactService.filterContactsForSingleMatch(text, true); if (contactOptional.isPresent()) { Contact contact = contactOptional.get(); Optional<Address> bitcoinAddress = contact.getBitcoinAddress(); if (bitcoinAddress.isPresent()) { Recipient recipient = new Recipient(bitcoinAddress.get()); recipient.setContact(contact); textField.setText(contact.getName()); textField.setRecipient(Optional.of(recipient)); isValid = true; } } } } component.setBackground(isValid ? validColor : invalidColor); return isValid; } else { throw new IllegalArgumentException("'component' must be a JTextField. Actual: " + component.getClass().getCanonicalName()); } } ThemeAwareRecipientInputVerifier(ContactService contactService); @Override boolean verify(JComponent component); @Override boolean shouldYieldFocus(JComponent input);  }