package com.constellio.app.modules.rm.ui.pages.document;
import static com.constellio.app.ui.i18n.i18n.$;
import com.constellio.app.modules.rm.ui.components.document.DocumentForm;
import com.constellio.app.modules.rm.ui.components.document.DocumentFormImpl;
import com.constellio.app.modules.rm.ui.components.document.fields.CustomDocumentField;
import com.constellio.app.ui.entities.RecordVO;
import com.constellio.app.ui.pages.base.BaseViewImpl;
import com.constellio.model.frameworks.validation.ValidationException;
import com.vaadin.data.Buffered.SourceException;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
public class AddEditDocumentViewImpl extends BaseViewImpl implements AddEditDocumentView {
private final AddEditDocumentPresenter presenter;
private RecordVO recordVO;
private DocumentFormImpl recordForm;
public AddEditDocumentViewImpl() {
presenter = new AddEditDocumentPresenter(this);
}
@Override
protected void initBeforeCreateComponents(ViewChangeEvent event) {
presenter.forParams(event.getParameters());
}
@Override
protected void afterViewAssembled(ViewChangeEvent event) {
presenter.viewAssembled();
}
@Override
public void setRecord(RecordVO recordVO) {
this.recordVO = recordVO;
}
@Override
protected String getTitle() {
String titleKey;
if (presenter.isAddView()) {
titleKey = "AddEditDocumentView.addViewTitle";
} else {
titleKey = "AddEditDocumentView.editViewTitle";
}
return $(titleKey);
}
@Override
protected Component buildMainComponent(ViewChangeEvent event) {
return newForm();
}
private DocumentFormImpl newForm() {
recordForm = new DocumentFormImpl(recordVO) {
@Override
protected void saveButtonClick(RecordVO viewObject)
throws ValidationException {
presenter.saveButtonClicked();
}
@Override
protected void cancelButtonClick(RecordVO viewObject) {
presenter.cancelButtonClicked();
}
@Override
public void reload() {
recordForm = newForm();
AddEditDocumentViewImpl.this.replaceComponent(this, recordForm);
}
@Override
public void commit() {
for (Field<?> field : fieldGroup.getFields()) {
try {
field.commit();
} catch (SourceException | InvalidValueException e) {
}
}
}
};
for (final Field<?> field : recordForm.getFields()) {
if (field instanceof CustomDocumentField) {
field.addValueChangeListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
presenter.customFieldValueChanged((CustomDocumentField<?>) field);
}
});
}
}
return recordForm;
}
@Override
public DocumentForm getForm() {
return recordForm;
}
}