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

246 lines
6.3 KiB
Java
Raw Normal View History

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