/*******************************************************************************
* Copyright (c) 2010 Oak Ridge National Laboratory.
* 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
******************************************************************************/
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.csstudio.swt.widgets.figures.ProgressBarFigure;
import org.csstudio.swt.xygraph.util.XYGraphMediaFactory;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* A live updated Gauge Example.
* @author Xihui Chen
*
*/
public class ProgressBarExample {
private static int counter = 0;
public static void main(String[] args) {
final Shell shell = new Shell();
shell.setSize(300, 120);
shell.open();
//use LightweightSystem to create the bridge between SWT and draw2D
final LightweightSystem lws = new LightweightSystem(shell);
//Create Gauge
final ProgressBarFigure progressBarFigure = new ProgressBarFigure();
//Init gauge
progressBarFigure.setFillColor(
XYGraphMediaFactory.getInstance().getColor(0, 255, 0));
progressBarFigure.setRange(-100, 100);
progressBarFigure.setLoLevel(-50);
progressBarFigure.setLoloLevel(-80);
progressBarFigure.setHiLevel(60);
progressBarFigure.setHihiLevel(80);
progressBarFigure.setMajorTickMarkStepHint(50);
progressBarFigure.setHorizontal(true);
progressBarFigure.setOriginIgnored(true);
lws.setContents(progressBarFigure);
//Update the gauge in another thread.
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
progressBarFigure.setValue(Math.sin(counter++/10.0)*100);
}
});
}
}, 100, 100, TimeUnit.MILLISECONDS);
Display display = Display.getDefault();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
future.cancel(true);
scheduler.shutdown();
}
}