Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.eloquentix.soundpure.web.page.migration;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- import org.apache.wicket.AttributeModifier;
- import org.apache.wicket.Component;
- import org.apache.wicket.ajax.AjaxRequestTarget;
- import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
- import org.apache.wicket.ajax.IAjaxIndicatorAware;
- import org.apache.wicket.ajax.markup.html.AjaxLink;
- import org.apache.wicket.extensions.ajax.markup.html.AjaxIndicatorAppender;
- import org.apache.wicket.markup.html.basic.Label;
- import org.apache.wicket.markup.html.panel.Panel;
- import org.apache.wicket.model.AbstractReadOnlyModel;
- import org.apache.wicket.model.IModel;
- import org.apache.wicket.model.Model;
- import org.apache.wicket.util.time.Duration;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * A kind of button used to execute long running process & notifying about the progress.
- *
- * @author Alex Objelean
- */
- @SuppressWarnings("serial")
- public abstract class ProcessExecutorPanel
- extends Panel implements IAjaxIndicatorAware {
- private static final Logger LOG = LoggerFactory.getLogger(ProcessExecutorPanel.class);
- /**
- * Indicates if the process is still running.
- */
- private boolean everStarted = false;
- private Component status;
- private transient Future<?> future;
- private AjaxIndicatorAppender ajaxIndicator;
- public ProcessExecutorPanel(final String id) {
- super(id);
- addComponents();
- }
- private void addComponents() {
- add(getStatus());
- add(getButton());
- add(ajaxIndicator = new AjaxIndicatorAppender());
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public String getAjaxIndicatorMarkupId() {
- return ajaxIndicator.getMarkupId();
- }
- private Component getButton() {
- final Component button = new AjaxLink<Void>("button") {
- @Override
- public void onClick(final AjaxRequestTarget target) {
- target.addComponent(getStatus());
- if (isProcessRunning()) {
- target.appendJavascript("alert('still in progress')");
- } else {
- status.add(getSelfUpdatingBehavior());
- everStarted = true;
- future = start();
- }
- }
- };
- button.add(new AttributeModifier("value", getButtonText()));
- return button;
- }
- protected IModel<String> getButtonText() {
- return Model.of("Execute");
- }
- /**
- * Start execution, immediately returning a future.
- */
- protected abstract Future<?> start();
- /**
- * @return if the process is still running.
- */
- private boolean isProcessRunning() {
- return future != null && !future.isDone();
- }
- /**
- * @return component displaying the current status of the process.
- */
- private Component getStatus() {
- if (status == null) {
- final IModel<String> model = new AbstractReadOnlyModel<String>() {
- @Override
- public String getObject() {
- return isProcessRunning() ? "in progress" : "complete";
- }
- };
- status = new Label("status", model) {
- @Override
- public boolean isVisible() {
- return everStarted;
- }
- };
- status.setOutputMarkupPlaceholderTag(true);
- status.setOutputMarkupId(true);
- }
- return status;
- }
- private AjaxSelfUpdatingTimerBehavior getSelfUpdatingBehavior() {
- return new AjaxSelfUpdatingTimerBehavior(Duration.seconds(2)) {
- @Override
- protected void onPostProcessTarget(final AjaxRequestTarget target) {
- if (!isProcessRunning()) {
- stop();
- status.remove(this);
- }
- }
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement