| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| */ |
| package org.eclipse.osbp.xtext.datainterchange.common; |
| |
| import org.eclipse.e4.core.services.events.IEventBroker; |
| import org.milyn.event.ExecutionEvent; |
| import org.milyn.event.ExecutionEventListener; |
| import org.milyn.event.types.ElementPresentEvent; |
| import org.milyn.event.types.FilterLifecycleEvent; |
| import org.milyn.event.types.FilterLifecycleEvent.EventType; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import com.vaadin.ui.HorizontalLayout; |
| import com.vaadin.ui.ProgressBar; |
| import com.vaadin.ui.UI; |
| |
| import org.eclipse.osbp.eventbroker.EventBrokerMsg; |
| import org.eclipse.osbp.persistence.IPersistenceService; |
| |
| public class WorkerThreadRunnable implements Runnable, ExecutionEventListener |
| { |
| static final Logger log = LoggerFactory.getLogger(WorkerThreadRunnable.class); |
| private String name = null; |
| private String status = null; |
| private IEventBroker eventBroker; |
| private ProgressBar progressBar; |
| private HorizontalLayout progressBarArea; |
| private IPersistenceService persistenceService; |
| private long counter = 0 ; |
| private long length = 0; |
| private long avgElementSize = 0; |
| private static float previousValue = 0.0f; |
| private static float newValue = 0.0f; |
| private UI ui = null; |
| |
| public enum Direction {IMPORT, EXPORT}; |
| |
| private Direction direction = Direction.IMPORT; |
| |
| public WorkerThreadRunnable() { |
| progressBarArea = new HorizontalLayout(); |
| progressBar = new ProgressBar(); |
| progressBarArea.addComponent(progressBar); |
| } |
| |
| @Override |
| public void run() { |
| } |
| |
| public void setLength(long length) { |
| this.length = length; |
| } |
| |
| public void setAvgElementSize(long avgElementSize) { |
| this.avgElementSize = avgElementSize; |
| } |
| |
| public void setProgressBarStyleName(String styleName) { |
| progressBar.addStyleName(styleName); |
| } |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| progressBar.setDescription(name); |
| } |
| |
| public String getStatus() { |
| return status; |
| } |
| |
| public void setStatus(String status) { |
| this.status = status; |
| } |
| |
| public IEventBroker getEventBroker() { |
| return eventBroker; |
| } |
| |
| public void setEventBroker(IEventBroker eventBroker) { |
| this.eventBroker = eventBroker; |
| } |
| |
| public IPersistenceService getPersistenceService() { |
| return persistenceService; |
| } |
| |
| public void setPersistenceService(IPersistenceService persistenceService) { |
| this.persistenceService = persistenceService; |
| } |
| |
| public ProgressBar getProgressBar() { |
| return progressBar; |
| } |
| |
| public HorizontalLayout getProgressBarArea() { |
| return progressBarArea; |
| } |
| |
| public Direction getDirection() { |
| return direction; |
| } |
| |
| public void setDirection(Direction direction) { |
| this.direction = direction; |
| } |
| |
| public UI getUi() { |
| return ui; |
| } |
| |
| public void setUi(UI ui) { |
| this.ui = ui; |
| } |
| |
| public void setProgressBarEnabled(final boolean enable) { |
| if (ui != null) { |
| ui.access(new Runnable() { |
| @Override |
| public void run() { |
| progressBar.setEnabled(enable); |
| } |
| }); |
| } |
| } |
| |
| public void setProgressIndeterminated(final boolean indeterminate) { |
| if (ui != null) { |
| ui.access(new Runnable() { |
| @Override |
| public void run() { |
| progressBar.setIndeterminate(indeterminate); |
| } |
| }); |
| } |
| } |
| |
| @Override |
| public void onEvent(ExecutionEvent event) { |
| if (event instanceof FilterLifecycleEvent) { |
| if (((FilterLifecycleEvent) event).getEventType() == EventType.STARTED) { |
| log.debug("started"); |
| if (ui != null) { |
| ui.access(new Runnable() { |
| @Override |
| public void run() { |
| progressBar.setValue(0.0f); |
| progressBar.setEnabled(false); |
| } |
| }); |
| } |
| } else if (((FilterLifecycleEvent) event).getEventType() == EventType.FINISHED) { |
| log.debug("finished"); |
| if (ui != null) { |
| ui.access(new Runnable() { |
| @Override |
| public void run() { |
| progressBar.setValue(1.0f); |
| if(getEventBroker()!=null) { |
| // notify view about this worker finished working |
| getEventBroker().send(EventBrokerMsg.WORKER_THREAD_INFO, getName()); |
| } |
| } |
| }); |
| } |
| } |
| } else if (event instanceof ElementPresentEvent) { |
| log.debug("ElementPresentEvent"); |
| counter ++; |
| if (length != 0) { |
| newValue = ((float)counter*avgElementSize)/length; |
| if (Math.abs(newValue-previousValue) > 0.01) { |
| if (ui != null) { |
| ui.access(new Runnable() { |
| @Override |
| public void run() { |
| log.debug("new value:"+newValue); |
| progressBar.setValue(newValue); |
| } |
| }); |
| } |
| } |
| } |
| } |
| } |
| } |