/** * */ package org.gcube.portlets.user.dataminermanager.client.experimentArea; import java.util.Date; import org.gcube.portlets.user.dataminermanager.client.DataMinerManager; 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.events.SessionExpiredEvent; import org.gcube.portlets.user.dataminermanager.client.util.UtilsGXT3; import org.gcube.portlets.user.dataminermanager.client.widgets.ComputationOutputPanel; import org.gcube.portlets.user.dataminermanager.shared.Constants; import org.gcube.portlets.user.dataminermanager.shared.data.ComputationId; import org.gcube.portlets.user.dataminermanager.shared.exception.ExpiredSessionServiceException; import com.allen_sauer.gwt.log.client.Log; import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.i18n.client.NumberFormat; import com.google.gwt.resources.client.ClientBundle; import com.google.gwt.resources.client.ImageResource; import com.google.gwt.resources.client.ClientBundle.Source; import com.google.gwt.resources.client.ImageResource.ImageOptions; import com.google.gwt.resources.client.ImageResource.RepeatStyle; import com.google.gwt.safehtml.shared.SafeHtmlBuilder; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.HTML; import com.sencha.gxt.cell.core.client.ProgressBarCell; import com.sencha.gxt.cell.core.client.ProgressBarCell.ProgressBarAppearanceOptions; import com.sencha.gxt.core.client.util.Margins; import com.sencha.gxt.theme.base.client.progress.ProgressBarDefaultAppearance; import com.sencha.gxt.theme.base.client.progress.ProgressBarDefaultAppearance.ProgressBarResources; import com.sencha.gxt.theme.base.client.progress.ProgressBarDefaultAppearance.ProgressBarStyle; import com.sencha.gxt.theme.base.client.progress.ProgressBarDefaultAppearance.ProgressBarTemplate; import com.sencha.gxt.theme.blue.client.progress.BlueProgressBarAppearance.BlueProgressBarResources; 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 class ProgressBarColoredAppearance extends ProgressBarDefaultAppearance { public ProgressBarColoredAppearance(ProgressBarResources resources, ProgressBarTemplate template) { super(resources, template); // TODO Auto-generated constructor stub } }*/ 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(); } /* private void custom(){ ProgressBarCell cell=new ProgressBarCell(new GreenProgressBarAppearance()); } public class GreenProgressBarAppearance extends ProgressBarDefaultAppearance { public interface GreenProgressBarResources extends ProgressBarResources, ClientBundle { @Source({"com/sencha/gxt/theme/base/client/progress/ProgressBar.css", "ProgressBar.css"}) @Override ProgressBarStyle style(); @Source("progress-bg.gif") @Override @ImageOptions(repeatStyle = RepeatStyle.Horizontal) ImageResource bar(); @Source("bg.gif") @ImageOptions(repeatStyle = RepeatStyle.Horizontal) ImageResource innerBar(); } public GreenProgressBarAppearance() { super(GWT. create(GreenProgressBarResources.class), GWT. create(ProgressBarTemplate.class)); } } */ /** * @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.getElement().getStyle().setMargin(20, Unit.PX); 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(){ DataMinerManager.getService().cancelComputation(computationId, new AsyncCallback() { @Override public void onFailure(Throwable caught) { if (caught instanceof ExpiredSessionServiceException) { UtilsGXT3.alert("Error", "Expired Session"); EventBusProvider.INSTANCE.fireEvent(new SessionExpiredEvent()); } else { UtilsGXT3.alert( "Error", "Error in cancel computation: ComputationId="+computationId.getId()+", Error=" + caught.getLocalizedMessage()); } } @Override public void onSuccess(String result) { Log.debug(result); } }); } /** * @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."); progressBar.updateProgress(1, "Computation Complete"); progressBar.addStyleName("progressBar-complete"); showOutput(); } else if (computationStatus.isFailed()) { Log.debug("Computation is Failed"); // Info.display("Failed", "The computation of "+operator.getName()+" has failed."); // MessageBox.alert("Failed", "The computation "+computationId+" of "+operator.getName()+" has failed.
" // +"Message: "+computationStatus.getMessage(), null); 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, null); progressBar.updateProgress(1, "Computation Fail"); progressBar.getElement().getStyle().setMarginBottom(36, Unit.PX); progressBar.addStyleName("progressBar-failed"); // } } 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 { /* (non-Javadoc) * @see com.google.gwt.user.client.Timer#run() */ @Override public void run() { Log.debug("Timer run ....."); // final ComputationStatusPanel cp = ComputationStatusPanel.this; DataMinerManager.getService().getComputationStatus(computationId, new AsyncCallback() { @Override public void onFailure(Throwable caught) { progressBar.updateProgress(1, "Failed to get the status"); progressBar.getElement().getStyle().setMarginBottom(36, Unit.PX); progressBar.addStyleName("progressBar-failed"); // MessageBox.alert("Error", ""+caught.getLocalizedMessage()+"\nCause: "+caught.getCause()+"\nStackTrace: "+caught.getStackTrace().toString(), null); // computationTerminated(computationId, operator, TerminationMode.FAILED2); // ComputationTimer.this.cancel(); } @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(); } }