package org.gcube.portlets.user.td.monitorwidget.client; import java.util.ArrayList; import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTSessionExpiredException; import org.gcube.portlets.user.td.gwtservice.shared.monitor.OperationMonitor; import org.gcube.portlets.user.td.gwtservice.shared.monitor.OperationMonitorSession; import org.gcube.portlets.user.td.monitorwidget.client.details.MonitorDetailPanel; import org.gcube.portlets.user.td.widgetcommonevent.client.event.BackgroundRequestEvent; import org.gcube.portlets.user.td.widgetcommonevent.client.event.SessionExpiredEvent; import org.gcube.portlets.user.td.widgetcommonevent.client.type.BackgroundRequestType; import org.gcube.portlets.user.td.widgetcommonevent.client.type.SessionExpiredType; import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId; import com.allen_sauer.gwt.log.client.Log; import com.google.web.bindery.event.shared.EventBus; import com.sencha.gxt.core.client.util.Margins; import com.sencha.gxt.widget.core.client.FramedPanel; import com.sencha.gxt.widget.core.client.ProgressBar; import com.sencha.gxt.widget.core.client.Window; import com.sencha.gxt.widget.core.client.button.TextButton; import com.sencha.gxt.widget.core.client.button.ToolButton; import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutData; import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack; import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer; import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer.HBoxLayoutAlign; 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; /** * AddColumnProgressDialog is a Dialog that show progress of AddColumn * * @author "Giancarlo Panichi" g.panichi@isti.cnr.it * */ public class MonitorDialog extends Window implements MonitorUpdaterListener { private static final int STATUS_POLLING_DELAY = 1000; private static final String WIDTH = "640px"; private static final String HEIGHT = "378px"; private static final String PANELWIDTH = "620px"; private static final String PANELHEIGHT = "308px"; private static final String PROGRESSWIDTH = "618px"; private EventBus eventBus; private String taskId; private MonitorUpdater progressUpdater; private TextButton btnOk; private TextButton btnBackground; private TextButton btnAbort; private TRId trId; private String reason; private String details; protected ToolButton backgroundBtn; protected ArrayList listeners = new ArrayList(); protected ArrayList monitorDialogEventUIListeners = new ArrayList(); private MonitorDetailPanel monitorDetailPanel; public MonitorDialog(String taskId, EventBus eventBus) { this.eventBus = eventBus; this.taskId = taskId; trId = null; initWindow(); create(); } protected void initWindow() { setWidth(WIDTH); setHeight(HEIGHT); setBodyBorder(false); setResizable(true); setModal(true); setClosable(true); setHeadingText("Progress"); } protected void create() { FramedPanel panel = new FramedPanel(); panel.setWidth(PANELWIDTH); panel.setHeight(PANELHEIGHT); panel.setHeaderVisible(false); panel.setBodyBorder(false); VerticalLayoutContainer v = new VerticalLayoutContainer(); /* * FlowLayoutContainer progressDetailContainer = new * FlowLayoutContainer(); * progressDetailContainer.setScrollMode(ScrollMode.AUTO); MarginData * layoutData = new MarginData(new Margins(5, 5, 5, 5)); */ ProgressBar progressBar = new ProgressBar(); progressBar.setWidth(PROGRESSWIDTH); /* * FieldSet detailsSet = new FieldSet(); * detailsSet.setHeadingText("Details"); * detailsSet.setCollapsible(true); */ monitorDetailPanel = new MonitorDetailPanel(eventBus); // detailsSet.add(monitorDetailPanel); /* * TextField * * FlowLayoutContainer progressDetailContainer = new * FlowLayoutContainer(); * progressDetailContainer.setScrollMode(ScrollMode.ALWAYS); MarginData * layoutData = new MarginData(new Margins(0, 5, 0, 0)); c.add(new * TextButton("Button 1"), layoutData); c.add(new * TextButton("Button 2"), layoutData); c.add(new * TextButton("Button 3"), layoutData); Viewport v = new Viewport(); * v.add(c); RootPanel.get().add(v); */ btnOk = new TextButton("Ok"); btnOk.setWidth("70px"); // btnOk.setIcon(ResourceBundle.INSTANCE.ok()); // btnOk.setIconAlign(IconAlign.RIGHT); btnOk.setTitle("Ok"); btnOk.addSelectHandler(new SelectHandler() { public void onSelect(SelectEvent event) { updateInvocation(); } }); btnBackground = new TextButton("Background"); btnBackground.setWidth("70px"); // btnBackground.setIcon(ResourceBundle.INSTANCE.background()); // btnBackground.setIconAlign(IconAlign.RIGHT); btnBackground.setTitle("Background"); btnBackground.addSelectHandler(new SelectHandler() { public void onSelect(SelectEvent event) { startBackground(); } }); btnAbort = new TextButton("Abort"); btnAbort.setWidth("70px"); // btnAbort.setIcon(ResourceBundle.INSTANCE.abort()); // btnAbort.setIconAlign(IconAlign.RIGHT); btnAbort.setTitle("Abort"); btnAbort.addSelectHandler(new SelectHandler() { public void onSelect(SelectEvent event) { startAbort(); } }); HBoxLayoutContainer flowButton = new HBoxLayoutContainer(); flowButton.setHBoxLayoutAlign(HBoxLayoutAlign.MIDDLE); flowButton.setPack(BoxLayoutPack.CENTER); flowButton.add(btnOk, new BoxLayoutData(new Margins(0, 4, 0, 4))); flowButton.add(btnBackground, new BoxLayoutData(new Margins(0, 4, 0, 4))); flowButton.add(btnAbort, new BoxLayoutData(new Margins(0, 4, 0, 4))); v.add(progressBar, new VerticalLayoutData(1, -1, new Margins(5, 5, 5, 5))); v.add(monitorDetailPanel, new VerticalLayoutData(1, -1, new Margins(5, 5, 5, 5))); v.add(flowButton, new VerticalLayoutData(1, 36, new Margins(5, 5, 5, 5))); /* * progressDetailContainer.add(progressBar, layoutData); * progressDetailContainer.add(monitorDetailPanel, layoutData); */ panel.add(v); add(panel); OperationMonitorSession operationMonitorSession = new OperationMonitorSession( taskId); progressUpdater = new MonitorUpdater(operationMonitorSession); progressUpdater.addListener(new MonitorBarUpdater(progressBar)); progressUpdater.addListener(this); progressUpdater.scheduleRepeating(STATUS_POLLING_DELAY); addMonitorDialogEventUIListener(progressUpdater); show(); btnOk.setVisible(false); } protected void startAbort() { btnOk.setEnabled(false); btnBackground.setEnabled(false); btnAbort.setEnabled(false); fireRequestAborted(); } protected void startBackground() { // TODO Auto-generated method stub btnOk.setEnabled(false); btnBackground.setEnabled(false); btnAbort.setEnabled(false); fireRequestPutInBackground(); } public boolean isAbortBtnEnabled() { if (btnAbort != null) { return btnAbort.isEnabled(); } else { return false; } } public void setAbortBtnEnabled(boolean abortBtnEnabled) { if (btnBackground != null) { btnBackground.setEnabled(abortBtnEnabled); btnBackground.setVisible(abortBtnEnabled); } } public boolean isBackgroundBtnEnabled() { if (btnBackground != null) { return btnBackground.isEnabled(); } else { return false; } } public void setBackgroundBtnEnabled(boolean backgroundBtnEnabled) { if (btnBackground != null) { btnBackground.setEnabled(backgroundBtnEnabled); btnBackground.setVisible(backgroundBtnEnabled); } } public void addProgressDialogListener(MonitorDialogListener listener) { listeners.add(listener); } public void removeProgressDialogListener(MonitorDialogListener listener) { listeners.remove(listener); } public void addMonitorDialogEventUIListener( MonitorDialogEventUIListener listener) { monitorDialogEventUIListeners.add(listener); } public void removeMonitorDialogEventUIListener( MonitorDialogEventUIListener listener) { monitorDialogEventUIListeners.remove(listener); } @Override public void monitorInitializing(OperationMonitor operationMonitor) { if (monitorDetailPanel != null) { monitorDetailPanel.update(operationMonitor); } forceLayout(); } @Override public void monitorUpdate(OperationMonitor operationMonitor) { if (monitorDetailPanel != null) { monitorDetailPanel.update(operationMonitor); } forceLayout(); } @Override public void monitorComplete(TRId trId, OperationMonitor operationMonitor) { if (monitorDetailPanel != null) { monitorDetailPanel.update(operationMonitor); } Log.debug("Operation Complete return: " + trId.toString()); this.trId = trId; btnBackground.setVisible(false); btnAbort.setVisible(false); forceLayout(); fireOperationComplete(trId); close(); } @Override public void monitorFailed(Throwable caught, String reason, String details, OperationMonitor operationMonitor) { if (monitorDetailPanel != null) { monitorDetailPanel.update(operationMonitor); } forceLayout(); if (caught instanceof TDGWTSessionExpiredException) { eventBus.fireEvent(new SessionExpiredEvent( SessionExpiredType.EXPIREDONSERVER)); } else { fireOperationFailed(caught, reason, details); close(); } } public void updateInvocation() { if (trId != null) { fireOperationStopped(trId, reason, details); } close(); } @Override public void monitorStopped(TRId trId, String reason, String details, OperationMonitor operationMonitor) { if (monitorDetailPanel != null) { monitorDetailPanel.update(operationMonitor); } Log.debug("Operation Stopped: [" + trId.toString() + ", " + reason + ", " + details + "]"); this.trId = trId; this.reason = reason; this.details = details; btnOk.setVisible(true); btnBackground.setVisible(false); btnAbort.setVisible(false); forceLayout(); } @Override public void monitorGeneratingView(OperationMonitor operationMonitor) { if (monitorDetailPanel != null) { monitorDetailPanel.update(operationMonitor); } forceLayout(); } @Override public void monitorValidate(OperationMonitor operationMonitor) { if (monitorDetailPanel != null) { monitorDetailPanel.update(operationMonitor); } forceLayout(); } @Override public void monitorAborted() { fireOperationAborted(); close(); } @Override public void monitorPutInBackground() { fireOperationPutInBackground(); close(); Log.debug("Request Background"); BackgroundRequestEvent e = new BackgroundRequestEvent( BackgroundRequestType.BACKGROUND); eventBus.fireEvent(e); } protected void fireOperationComplete(TRId trId) { for (MonitorDialogListener listener : listeners) listener.operationComplete(trId); } protected void fireOperationFailed(Throwable caught, String reason, String details) { for (MonitorDialogListener listener : listeners) listener.operationFailed(caught, reason, details); } protected void fireOperationStopped(TRId trId, String reason, String details) { for (MonitorDialogListener listener : listeners) listener.operationStopped(trId, reason, details); } protected void fireOperationAborted() { for (MonitorDialogListener listener : listeners) listener.operationAborted(); } protected void fireOperationPutInBackground() { for (MonitorDialogListener listener : listeners) listener.operationPutInBackground(); } //UI event Fire protected void fireRequestAborted() { for (MonitorDialogEventUIListener listener : monitorDialogEventUIListeners) { listener.requestAborted(); } } protected void fireRequestPutInBackground() { for (MonitorDialogEventUIListener listener : monitorDialogEventUIListeners) { listener.requestPutInBackground(); } } /** * {@inheritDoc} */ @Override protected void initTools() { /* * if (backgroundBtnEnabled) { backgroundBtn = new * ToolButton(ToolButton.CLOSE); * backgroundBtn.setToolTip("Put in the background"); * backgroundBtn.addSelectHandler(new SelectHandler() { * * @Override public void onSelect(SelectEvent event) { hide(); } }); * * header.addTool(backgroundBtn); } */ super.initTools(); closeBtn.setVisible(false); /* * closeBtn.addSelectHandler(new SelectHandler() { * * public void onSelect(SelectEvent event) { close(); } }); */ } protected void close() { hide(); } public void updateOperationMonitor(OperationMonitor operationMonitor) { if (monitorDetailPanel != null) { monitorDetailPanel.update(operationMonitor); } forceLayout(); } }