package sk.vrto.web.views;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.*;
import sk.vrto.domain.Contact;
import sk.vrto.web.presenters.ContactPresenter;
public class ContactViewPopup extends VerticalLayout {
transient private final ContactPresenter presenter;
// state
private boolean editMode;
private Contact contact;
// ui
private TextField nameField;
protected Button editButton;
public ContactViewPopup(Contact contact, ContactPresenter presenter) {
this.contact = contact;
this.presenter = presenter;
setWidth("280px");
setSpacing(true);
addComponent(createContactInfoBar());
addComponent(createButtonBar());
}
public TextField getNameField() {
return nameField;
}
public boolean isEditMode() {
return editMode;
}
public void enableEditMode() {
editMode = true;
editButton.setCaption("Save");
nameField.setReadOnly(false);
}
public void disableEditMode() {
editMode = false;
editButton.setCaption("Edit");
nameField.setReadOnly(true);
}
private Component createButtonBar() {
HorizontalLayout buttons = new HorizontalLayout();
buttons.setSpacing(true);
buttons.setStyleName("padded");
buttons.addComponent(newEmailButton());
buttons.addComponent(editButton());
buttons.addComponent(deleteButton());
return buttons;
}
private Component newEmailButton() {
Button newEmailButton = new Button("Message");
newEmailButton.setWidth("80px");
newEmailButton.addListener(new Button.ClickListener() {
@Override
public void buttonClick(Button.ClickEvent event) {
presenter.newEmail(contact);
ContactViewPopup.this.setVisible(false);
}
});
return newEmailButton;
}
private Component editButton() {
editButton = new Button("Edit");
editButton.setWidth("80px");
editButton.addListener(new Button.ClickListener() {
@Override
public void buttonClick(Button.ClickEvent event) {
presenter.editContact(contact);
}
});
return editButton;
}
private Component deleteButton() {
Button deleteButton = new Button("Delete");
deleteButton.setWidth("80px");
deleteButton.addListener(new Button.ClickListener() {
@Override
public void buttonClick(Button.ClickEvent event) {
try {
presenter.removeContact(contact);
ContactViewPopup.this.setVisible(false);
} catch (Throwable throwable) {
getWindow().showNotification("Cannot delete " + contact.getName()
+ " because there is one or more emails received from this contact");
}
}
});
return deleteButton;
}
private Component createContactInfoBar() {
VerticalLayout layout = new VerticalLayout();
layout.setStyleName("padded");
layout.addComponent(contactFields());
Embedded pic = new Embedded("", new ThemeResource("../email/images/contact.png"));
pic.setStyleName("contact-pic");
layout.addComponent(pic);
layout.setComponentAlignment(pic, Alignment.MIDDLE_RIGHT);
return layout;
}
private Component contactFields() {
VerticalLayout fields = new VerticalLayout();
fields.setSpacing(true);
nameField = new TextField();
nameField.setValue(contact.getName());
nameField.setStyleName("contact-name");
nameField.setReadOnly(true);
fields.addComponent(nameField);
TextField emailField = new TextField();
emailField.setValue(contact.getEmail());
emailField.setStyleName("contact-email");
emailField.setReadOnly(true);
fields.addComponent(emailField);
return fields;
}
}