geoportal-data-entry-app/src/main/java/org/gcube/portlets/user/geoportaldataentry/client/ui/form/GeonaDataEntryMainForm.java

246 lines
6.3 KiB
Java

package org.gcube.portlets.user.geoportaldataentry.client.ui.form;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.gcube.portlets.user.geoportaldataentry.client.events.SaveGeonaDataFormsEvent;
import org.gcube.portlets.user.geoportaldataentry.client.ui.card.GeoNaFormCardModel;
import org.gcube.portlets.user.geoportaldataentry.client.ui.card.MetadataFormCard;
import org.gcube.portlets.user.geoportaldataentry.shared.GeoNaFormDataObject;
import org.gcube.portlets.widgets.mpformbuilder.client.form.generic.CreateMetadataForm;
import org.gcube.portlets.widgets.mpformbuilder.shared.GenericDatasetBean;
import com.github.gwtbootstrap.client.ui.AlertBlock;
import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.FormActions;
import com.github.gwtbootstrap.client.ui.Tab;
import com.github.gwtbootstrap.client.ui.TabPanel;
import com.github.gwtbootstrap.client.ui.constants.AlertType;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
/**
* The Class GeonaDataEntryMainForm.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Mar 4, 2022
*/
public class GeonaDataEntryMainForm extends Composite {
private static GeonaDataEntryMainFormUiBinder uiBinder = GWT.create(GeonaDataEntryMainFormUiBinder.class);
/**
* The Interface GeonaDataEntryMainFormUiBinder.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Mar 4, 2022
*/
interface GeonaDataEntryMainFormUiBinder extends UiBinder<Widget, GeonaDataEntryMainForm> {
}
private List<GeoNaFormCardModel> listCards = new ArrayList<GeoNaFormCardModel>();
@UiField
HTMLPanel mainHTMLPanel;
@UiField
TabPanel mainTabPanel;
@UiField
AlertBlock alertFormAction;
@UiField
Button buttonSave;
@UiField
FormActions formActions;
private LinkedHashMap<String, MetadataFormCard> mapForms = new LinkedHashMap<String, MetadataFormCard>();
private List<Tab> listTabs = new ArrayList<Tab>();
private HandlerManager appManagerBus;
/**
* Instantiates a new geona data entry main form.
*
* @param appManagerBus the app manager bus
*/
public GeonaDataEntryMainForm(HandlerManager appManagerBus) {
initWidget(uiBinder.createAndBindUi(this));
this.appManagerBus = appManagerBus;
}
/**
* Adds the form.
*
* @param tabHeading the tab heading
* @param geonFormModel the geon form model
*/
public void addForm(String tabHeading, GeoNaFormCardModel geonFormModel) {
listCards.add(geonFormModel);
createNewCard(tabHeading, geonFormModel);
}
/**
* Creates the new card.
*
* @param tabHeading the tab heading
* @param geonFormModel the geon form model
*/
private void createNewCard(String tabHeading, final GeoNaFormCardModel geonFormModel) {
final Tab tab = new Tab();
tab.setHeading(tabHeading);
// MetadataFormCard mfc = new MetadataFormCard(tab, createMetadataForm,
// repeatible);
MetadataFormCard mfc = new MetadataFormCard(tab, geonFormModel, appManagerBus);
listTabs.add(tab);
mainTabPanel.add(tab);
if (listTabs.size() == 1) {
tab.setActive(true);
mainTabPanel.selectTab(0);
}
mapForms.put(tabHeading, mfc);
}
/**
* Reset.
*/
public void reset() {
mapForms.clear();
mainTabPanel.clear();
listTabs.clear();
}
/**
* Select tab.
*
* @param index the index
*/
public void selectTab(int index) {
GWT.log("Selecting tab index: " + index + " of " + listTabs.size());
if (listTabs.size() < index) {
mainTabPanel.selectTab(index);
}
}
/**
* Save data.
*
* @param e the e
*/
@UiHandler("buttonSave")
void saveData(ClickEvent e) {
boolean canSave = true;
for (String metadataType : mapForms.keySet()) {
MetadataFormCard card = mapForms.get(metadataType);
boolean isValid = card.validateForm();
card.setValidCard(isValid);
if (isValid == false)
canSave = false;
}
List<GeoNaFormDataObject> listGeonaFormObjects = new ArrayList<GeoNaFormDataObject>();
if (canSave) {
for (String metadataType : mapForms.keySet()) {
MetadataFormCard card = mapForms.get(metadataType);
List<GenericDatasetBean> listGDB = new ArrayList<GenericDatasetBean>(card.getListForms().size());
for (CreateMetadataForm form : card.getListForms()) {
listGDB.add(form.getFormDataBean());
}
listGeonaFormObjects.add(new GeoNaFormDataObject(listGDB, card.getGeonaFormModel().getFormCard()));
}
appManagerBus.fireEvent(new SaveGeonaDataFormsEvent(listGeonaFormObjects));
} else {
showAlertOnSaveAction("Detected errors, please fix it/them", AlertType.ERROR, true);
}
// Window.alert("I can save: "+listGeonaFormObjects);
}
/**
* Show alert on save action.
*
* @param text the text
* @param type the type
* @param hideAfterAWhile the hide after A while
*/
public void showAlertOnSaveAction(String text, AlertType type, boolean hideAfterAWhile) {
// Window.alert("Called alertOnCreate");
alertFormAction.setText(text);
alertFormAction.setType(type);
alertFormAction.setVisible(true);
// goBackButtonSecondStep.setEnabled(true);
if (hideAfterAWhile) {
// hide after some seconds
Timer t = new Timer() {
@Override
public void run() {
alertFormAction.setVisible(false);
}
};
t.schedule(10000);
}
}
/**
* Enable button save.
*
* @param enabled the enabled
*/
public void enableButtonSave(boolean enabled) {
buttonSave.setEnabled(enabled);
}
/**
* Sets the ative after.
*
* @param index the index
* @param bool the bool
*/
public void setAtiveAfter(int index, boolean bool) {
// if(listTabs.size()<index) {
// for (int i = index; i < listTabs.size(); i++) {
// listTabs.get(i).setEnabled(bool);
// }
// }
}
/**
* Sets the visible form actions.
*
* @param bool the new visible form actions
*/
public void setVisibleFormActions(boolean bool) {
formActions.setVisible(bool);
}
}