/** * */ package org.gcube.portlets.user.dataminermanager.client.experiments; import java.util.Date; import org.gcube.portlets.user.dataminermanager.client.bean.ComputationStatus; import org.gcube.portlets.user.dataminermanager.client.bean.ComputationStatus.Status; import org.gcube.portlets.user.dataminermanager.client.bean.Operator; import org.gcube.portlets.user.dataminermanager.client.common.EventBusProvider; import org.gcube.portlets.user.dataminermanager.client.custom.GreenProgressBarAppaearance; import org.gcube.portlets.user.dataminermanager.client.custom.RedProgressBarAppaearance; import org.gcube.portlets.user.dataminermanager.client.events.CancelComputationExecutionRequestEvent; import org.gcube.portlets.user.dataminermanager.client.rpc.DataMinerPortletServiceAsync; import org.gcube.portlets.user.dataminermanager.client.util.UtilsGXT3; import org.gcube.portlets.user.dataminermanager.shared.Constants; import org.gcube.portlets.user.dataminermanager.shared.data.ComputationId; import com.allen_sauer.gwt.log.client.Log; import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.i18n.client.NumberFormat; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.rpc.AsyncCallback; import com.sencha.gxt.cell.core.client.ProgressBarCell; import com.sencha.gxt.core.client.util.Margins; import com.sencha.gxt.widget.core.client.ProgressBar; import com.sencha.gxt.widget.core.client.button.TextButton; import com.sencha.gxt.widget.core.client.container.HtmlLayoutContainer; import com.sencha.gxt.widget.core.client.container.SimpleContainer; import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer; import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData; import com.sencha.gxt.widget.core.client.event.SelectEvent; import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler; import com.sencha.gxt.widget.core.client.info.Info; /** * * @author Giancarlo Panichi * email: g.panichi@isti.cnr.it * */ public class ComputationStatusPanel extends SimpleContainer { private ProgressBar progressBar; private ComputationId computationId; private Operator operator; private boolean terminated=false; private ComputationTimer timer = new ComputationTimer(); private TextButton cancelComputationBtn; private VerticalLayoutContainer vert; /** * */ public ComputationStatusPanel(Operator operator) { super(); this.operator=operator; init(); create(); } private void init(){ setStylePrimaryName("computationStatusPanel"); } private void create(){ vert=new VerticalLayoutContainer(); HtmlLayoutContainer title = new HtmlLayoutContainer("

Computation of " + operator.getName() + "

"); HtmlLayoutContainer date = new HtmlLayoutContainer("

" + new Date().toString() + "

"); vert.add(title, new VerticalLayoutData(-1, -1, new Margins(0))); vert.add(date,new VerticalLayoutData(-1, -1, new Margins(0))); add(vert); forceLayout(); } /** * @param computationId the computationId to set */ public void computationStarted(ComputationId computationId) { this.computationId = computationId; vert.add(new HtmlLayoutContainer("

Created, the id is "+computationId.getId()+" [link]

")); progressBar = new ProgressBar(); progressBar.updateProgress(0, "Starting..."); vert.add(progressBar,new VerticalLayoutData(1, -1, new Margins(20))); cancelComputationBtn = new TextButton("Cancel"); cancelComputationBtn.addSelectHandler(new SelectHandler() { @Override public void onSelect(SelectEvent event) { cancelComputationBtn.disable(); cancelComputation(); } }); cancelComputationBtn.getElement().getStyle().setMarginBottom(36, Unit.PX); vert.add(cancelComputationBtn,new VerticalLayoutData(-1, -1, new Margins(0))); forceLayout(); timer.scheduleRepeating(Constants.TIME_UPDATE_COMPUTATION_STATUS_PANEL); } private void cancelComputation(){ CancelComputationExecutionRequestEvent event=new CancelComputationExecutionRequestEvent(computationId); EventBusProvider.INSTANCE.fireEvent(event); } /** * @param computationId * @param operator * */ protected void computationTerminated(ComputationId computationId, Operator operator, ComputationStatus computationStatus) { Log.debug("Computation Terminated"); Log.debug("Computation Status:"+computationStatus); if (terminated == false) { terminated = true; cancelComputationBtn.setVisible(false); if (computationStatus.isComplete()) { Log.debug("Computation is Complete"); Info.display("Terminated", "The computation "+computationId.getId()+" of "+operator.getName()+" is terminated correctly."); int index=vert.getWidgetIndex(progressBar); vert.remove(index); //TODO ProgressBarCell cell=new ProgressBarCell(new GreenProgressBarAppaearance()); progressBar =new ProgressBar(cell); progressBar.updateProgress(1, "Computation Complete"); progressBar.addStyleName("progressBar-complete"); vert.insert(progressBar, index); showOutput(); } else if (computationStatus.isFailed()) { Log.debug("Computation is Failed"); String errorMessage; if(computationStatus.getErrResource()==null||computationStatus.getErrResource().getDescription()==null ){ errorMessage=new String("Computation Failed!"); } else { errorMessage=computationStatus.getErrResource().getDescription(); } UtilsGXT3.alert("Failed", "The computation "+computationId.getId()+" of "+operator.getName()+" has failed.
" +"Message: "+errorMessage); int index=vert.getWidgetIndex(progressBar); vert.remove(index); //TODO ProgressBarCell cell=new ProgressBarCell(new RedProgressBarAppaearance()); progressBar =new ProgressBar(cell); progressBar.updateProgress(1, "Computation Fail"); progressBar.getElement().getStyle().setMarginBottom(36, Unit.PX); progressBar.addStyleName("progressBar-failed"); vert.insert(progressBar, index); } } forceLayout(); } /** * */ private void showOutput() { HtmlLayoutContainer computationEndMessage=new HtmlLayoutContainer("

The computation "+operator.getName()+" finished.

"); vert.add(computationEndMessage, new VerticalLayoutData(-1, -1, new Margins(0))); ComputationOutputPanel computationOutputPanel = new ComputationOutputPanel(computationId); computationOutputPanel.getElement().getStyle().setMarginBottom(36, Unit.PX); vert.add(computationOutputPanel, new VerticalLayoutData(1, -1, new Margins(0))); } /** * @param computationStatus */ protected void updateStatus(ComputationStatus computationStatus) { Log.debug("Conputation Status Panel ::Update Status "); if (computationStatus.getStatus() == Status.PENDING) progressBar.updateText("Pending..."); else { double percentage = computationStatus.getPercentage(); progressBar.updateProgress(percentage/100, "Running, " + NumberFormat.getFormat("0.00").format(percentage) + "% Complete"); } } private class ComputationTimer extends Timer { @Override public void run() { Log.debug("Timer run ....."); DataMinerPortletServiceAsync.INSTANCE.getComputationStatus(computationId, new AsyncCallback() { @Override public void onFailure(Throwable caught) { int index=vert.getWidgetIndex(progressBar); vert.remove(index); //TODO ProgressBarCell cell=new ProgressBarCell(new RedProgressBarAppaearance()); progressBar =new ProgressBar(cell); progressBar.updateProgress(1, "Failed to get the status"); progressBar.getElement().getStyle().setMarginBottom(36, Unit.PX); progressBar.addStyleName("progressBar-failed"); vert.insert(progressBar, index); } @Override public void onSuccess(ComputationStatus computationStatus) { if (computationStatus.isTerminated()) { ComputationTimer.this.cancel(); computationTerminated(computationId, operator, computationStatus); } else updateStatus(computationStatus); } }); } } /** * */ public void stopTimer() { try { timer.cancel(); } catch (Exception e) { } vert.clear(); } }