tabular-data-unionwizard-wi.../src/main/java/org/gcube/portlets/user/td/unionwizardwidget/client/TabResourcesSelectionCard.java

197 lines
5.7 KiB
Java

package org.gcube.portlets.user.td.unionwizardwidget.client;
import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTServiceAsync;
import org.gcube.portlets.user.td.gwtservice.shared.tr.TabResource;
import org.gcube.portlets.user.td.gwtservice.shared.tr.TableData;
import org.gcube.portlets.user.td.gwtservice.shared.tr.union.UnionSession;
import org.gcube.portlets.user.td.wizardwidget.client.WizardCard;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.widget.core.client.Dialog.PredefinedButton;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
public class TabResourcesSelectionCard extends WizardCard {
protected UnionSession unionSession;
protected TabResourcesSelectionCard thisCard;
protected TabResourcesSelectionPanel tabResourcesSelectionPanel;
protected TabResource selectedTabResource = null;
public TabResourcesSelectionCard(final UnionSession unionSession) {
super("Select Tabular Resource for Union", "");
Log.debug("TabResourcesSelectionCard");
this.unionSession = unionSession;
thisCard = this;
tabResourcesSelectionPanel = new TabResourcesSelectionPanel(thisCard,res);
tabResourcesSelectionPanel
.addSelectionHandler(new SelectionHandler<TabResource>() {
public void onSelection(SelectionEvent<TabResource> event) {
unionSession.setUnionTabularResource(tabResourcesSelectionPanel
.getSelectedItem());
getWizardWindow().setEnableNextButton(true);
}
});
setContent(tabResourcesSelectionPanel);
}
@Override
public void setup() {
Log.debug("TabResourcesSelectionCard Call Setup ");
Command sayNextCard = new Command() {
public void execute() {
Log.debug("TabResourcesSelectionCard Call sayNextCard");
retrieveLastTable();
}
};
getWizardWindow().setNextButtonCommand(sayNextCard);
Command sayPreviousCard = new Command() {
public void execute() {
try {
getWizardWindow().previousCard();
getWizardWindow().removeCard(thisCard);
Log.debug("Remove TabResourcesSelectionCard");
} catch (Exception e) {
Log.error("sayPreviousCard :" + e.getLocalizedMessage());
}
}
};
getWizardWindow().setPreviousButtonCommand(sayPreviousCard);
getWizardWindow().setEnableNextButton(false);
getWizardWindow().setEnableBackButton(true);
}
protected void retrieveLastTable() {
getWizardWindow().setEnableNextButton(false);
getWizardWindow().setEnableBackButton(false);
TDGWTServiceAsync.INSTANCE.getLastTable(unionSession
.getUnionTabularResource().getTrId(),
new AsyncCallback<TableData>() {
@Override
public void onFailure(Throwable caught) {
Log.debug("Attention",
"This tabular resource does not have a valid table");
AlertMessageBox d = new AlertMessageBox("Attention",
"This tabular resource does not have a valid table");
d.addHideHandler(new HideHandler() {
public void onHide(HideEvent event) {
deleteTRWithLastTableNull();
}
});
d.show();
}
@Override
public void onSuccess(TableData result) {
Log.debug("Retrieve last table: " + result);
updateConnectedTRInfo(result);
}
});
}
protected void deleteTRWithLastTableNull() {
final ConfirmMessageBox mb = new ConfirmMessageBox("Delete",
"Would you like to delete this tabular resource without table?");
mb.addHideHandler(new HideHandler() {
public void onHide(HideEvent event) {
if (mb.getHideButton() == mb.getButtonById(PredefinedButton.YES
.name())) {
callDeleteLastTable();
} else if (mb.getHideButton() == mb
.getButtonById(PredefinedButton.NO.name())) {
getWizardWindow().setEnableNextButton(true);
getWizardWindow().setEnableBackButton(true);
}
}
});
mb.setWidth(300);
mb.show();
}
protected void callDeleteLastTable(){
Log.debug("Delete TR:" + unionSession.getUnionTabularResource()
.getTrId());
TDGWTServiceAsync.INSTANCE.removeTabularResource(unionSession.getUnionTabularResource()
.getTrId(),
new AsyncCallback<Void>() {
public void onFailure(Throwable caught) {
AlertMessageBox d = new AlertMessageBox("Error",
"Error on delete TabResource: "
+ caught.getLocalizedMessage());
d.addHideHandler(new HideHandler() {
public void onHide(HideEvent event) {
getWizardWindow().setEnableNextButton(true);
getWizardWindow().setEnableBackButton(true);
}
});
d.show();
}
public void onSuccess(Void result) {
tabResourcesSelectionPanel.gridReload();
getWizardWindow().setEnableNextButton(false);
getWizardWindow().setEnableBackButton(true);
}
});
}
protected void updateConnectedTRInfo(TableData table) {
TabResource tabResource = unionSession.getUnionTabularResource();
tabResource.setTrId(table.getTrId());
unionSession.setUnionTabularResource(tabResource);
Log.debug("UnionSession: " + unionSession);
goNext();
}
protected void goNext() {
try {
Log.info("NextCard ColumnMappingCard");
ColumnMappingCard columnSelectionCard = new ColumnMappingCard(
unionSession);
getWizardWindow().addCard(
columnSelectionCard);
getWizardWindow().nextCard();
} catch (Throwable e) {
Log.error("goNext: " + e.getLocalizedMessage());
}
}
}