view src/alice/topology/manager/keeparive/SchedulerViewer.java @ 32:e580cd6b225a

bug fixed (about totalTime)
author sugi
date Mon, 11 Nov 2013 16:25:55 +0900
parents 7306b4ea110e
children
line wrap: on
line source

package alice.topology.manager.keeparive;

/**
 * Copyright (c) 2008, 2012 Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 */
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.VBox;
import javafx.util.Duration;

/**
 * A simulated stock line chart.
 *
 * @see javafx.scene.chart.Chart
 * @see javafx.scene.chart.LineChart
 * @see javafx.scene.chart.NumberAxis
 * @see javafx.scene.chart.XYChart
 */
public class SchedulerViewer extends Application {
	private PingScheduler ps;
	private XYChart.Series<Number,Number> dataSeries;
	private ChoiceBox<String> cb;
	private NumberAxis xAxis;
	private Timeline animation;
	private int count;


	private void init(Stage primaryStage) {
		Group root = new Group();
		primaryStage.setScene(new Scene(root));
		root.getChildren().addAll(createChart(), createButton(), createChoiceBox());
		// create timeline to add new data every 60th of second
		animation = new Timeline();
		animation.getKeyFrames().add(new KeyFrame(Duration.millis(1000/60), new EventHandler<ActionEvent>() {
			@Override public void handle(ActionEvent actionEvent) {
				// 6 minutes data per frame
				for(int count=0; count < 6; count++) {
					plotGraph();
				}
			}
		}));
		animation.setCycleCount(Animation.INDEFINITE);
	}

	protected LineChart<Number, Number> createChart() {
		xAxis = new NumberAxis(0,100,5);
		final NumberAxis yAxis = new NumberAxis(0,100,10);
		final LineChart<Number,Number> lc = new LineChart<Number,Number>(xAxis,yAxis);
		// setup chart
		lc.setId("lineStockDemo");
		lc.setCreateSymbols(false);
		lc.setAnimated(false);
		lc.setLegendVisible(false);
		lc.setTitle("Task Viewer");
		xAxis.setLabel("Time");
		xAxis.setForceZeroInRange(false);
		yAxis.setLabel("Task Name");
		yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis,"Task ",null));
		// add starting data
		dataSeries = new XYChart.Series<Number,Number>();
		lc.getData().add(dataSeries);
		return lc;
	}

	private void plotGraph() {
		if (ps.updateFlag){
			if(!dataSeries.getData().isEmpty())
				dataSeries.getData().clear();
			count = (int) (ps.getTotalTime()/1000);
			System.out.println("totalTime "+count+" s");
			dataSeries.getData().add(new XYChart.Data<Number, Number>(count, ps.nowTask.getTaskNum()));
			count +=ps.nowTask.getTime()/1000;
			dataSeries.getData().add(new XYChart.Data<Number, Number>(count, ps.nowTask.getTaskNum()));

			for (TaskInfo info : ps.getTaskList()){
				dataSeries.getData().add(new XYChart.Data<Number, Number>(count, info.getTaskNum()));
				count += info.getTime()/1000;
				dataSeries.getData().add(new XYChart.Data<Number, Number>(count, info.getTaskNum()));
			}
			ps.updateFlag = false;
			//plotTimeLine();
		}
	}

	public void plotTimeLine() {		
		xAxis.setLowerBound(xAxis.getLowerBound()+ 10);
		xAxis.setUpperBound(xAxis.getUpperBound()+ 10);
	}

	private VBox createButton(){
		VBox vbox = new VBox();
		vbox.setId("vbox");
		Button button = new Button("update");
		button.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent t) {
                ps.postpone(cb.getValue());
            }
        });
		vbox.getChildren().add(button);
		return vbox;
	}

	private ChoiceBox<String> createChoiceBox(){
		cb = new ChoiceBox<String>();
		cb.getItems().add(ps.nowTask.getName());
		for (TaskInfo info : ps.getTaskList())
			cb.getItems().add(info.getName());
		cb.getSelectionModel().selectFirst();
		cb.setLayoutX(100);
		return cb;
	}
	
	public void StartScheduler(){
		ps = new PingScheduler();
		TaskInfo a = new TaskInfo("a", 10 * 1000);
		TaskInfo b = new TaskInfo("b", 20 * 1000);


		TaskInfo c = new TaskInfo("c", 60 * 1000);
		TaskInfo d = new TaskInfo("d", 40 * 1000);
		TaskInfo e = new TaskInfo("e", 30 * 1000);


		ps.addTask(a);
		ps.addTask(b);
		ps.addTask(c);
		ps.addTask(d);
		ps.addTask(e);
		ps.execute();
	}

	public void play() {
		animation.play();
	}

	@Override public void stop() {
		animation.pause();
	}    

	@Override public void start(Stage primaryStage) throws Exception {
		StartScheduler();
		init(primaryStage);
		primaryStage.show();
		play();
	}

}