package org.apache.wicket.examples.websocket.progress;
import java.util.concurrent.ScheduledExecutorService;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.examples.websocket.JSR356Application;
import org.apache.wicket.examples.websocket.JSR356Session;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.protocol.ws.api.WebSocketBehavior;
import org.apache.wicket.protocol.ws.api.event.WebSocketPushPayload;
import org.apache.wicket.protocol.ws.api.message.ConnectedMessage;
public class ProgressBarTogglePanel extends Panel
{
private int progress = 0;
private boolean showProgress = true;
public ProgressBarTogglePanel(String id)
{
super(id);
setOutputMarkupId(true);
add(new WebSocketBehavior()
{
});
add(new AjaxLink<Void>("hideShowProgress")
{
@Override
public void onClick(AjaxRequestTarget target)
{
showProgress = !showProgress;
target.add(ProgressBarTogglePanel.this);
}
}.setBody((IModel<String>) () -> showProgress ? "Hide progress" : "Show progress"));
add(new AjaxLink<Void>("cancelRestartTask")
{
@Override
public void onClick(AjaxRequestTarget target)
{
JSR356Session.get().startOrCancelTask();
target.add(ProgressBarTogglePanel.this);
}
}.setBody((IModel<String>) () -> {
ProgressUpdater.ProgressUpdateTask progressUpdateTask = JSR356Session.get().getProgressUpdateTask();
return progressUpdateTask != null && progressUpdateTask.isRunning() && !progressUpdateTask.isCanceled()
? "Cancel task" :
"Restart task";
}));
add(new Label("progressBar", (IModel<String>) () -> {
ProgressUpdater.ProgressUpdateTask progressUpdateTask = JSR356Session.get().getProgressUpdateTask();
return progressUpdateTask != null && progressUpdateTask.isRunning()
? "Background Task is " + progress + "% completed"
: "No task is running";
})
{
@Override
protected void onConfigure()
{
super.onConfigure();
setVisible(showProgress);
}
});
}
@Override
public void onEvent(IEvent<?> event)
{
if (event.getPayload() instanceof WebSocketPushPayload)
{
WebSocketPushPayload wsEvent = (WebSocketPushPayload) event.getPayload();
if (wsEvent.getMessage() instanceof ProgressUpdater.ProgressUpdate)
{
ProgressUpdater.ProgressUpdate progressUpdate = (ProgressUpdater.ProgressUpdate)wsEvent.getMessage();
progress = progressUpdate.getProgress();
wsEvent.getHandler().add(this);
}
else if (wsEvent.getMessage() instanceof ProgressUpdater.TaskCanceled)
{
wsEvent.getHandler().add(this);
}
}
}
}