package com.ht.ui.util;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;

import com.ht.util.WorkQueue;
import com.ht.web.remoting.HessianResponse;

public abstract class HessianResponseTask extends Task<HessianResponse> {
	
	public HessianResponseTask() {
		valueProperty().addListener(new ChangeListener<HessianResponse>() {
			@Override
			public void changed(
					ObservableValue<? extends HessianResponse> observable,
					HessianResponse oldValue, HessianResponse newValue) {
				if (newValue == null) {
					nullResponse();
				} else {
					if (newValue.isSuccessful()) {
						successResponse(newValue);
					} else {
						failureResponse(newValue);
					}
				}
			}
		});
	}
	
	/**
	 * 启动新的线程并运行
	 */
	public void start() {
		WorkQueue.INSTANCE.execute(this);
	}
	
	/**
	 * 当返回对象为null时在JavaFx UI线程调用该方法
	 */
	public abstract void nullResponse();
	
	/**
	 * 当返回对象状态为成功时在JavaFx UI线程调用该方法
	 */
	public abstract void successResponse(HessianResponse resp);
	
	/**
	 * 当返回对象状态为失败时在JavaFx UI线程调用该方法
	 */
	public abstract void failureResponse(HessianResponse resp);
}
