import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class RC8Sample{
	private ControlFrame m_frm;
	private CaoJNI m_cao;
	
	public static void main(String[] args){
		new RC8Sample();
	}
	
	public RC8Sample(){
		m_frm = new ControlFrame(this);
		m_cao = new CaoJNI();

		m_frm.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent ev)
			{

				// Delete CaoEngine
				m_cao.CaoRelease();
			}
		});
	}

	public void btnInit_Clicked()
	{
		// Create Engine
		m_cao.CaoInit();

		m_frm.ButtonController(false);
	}

	public void btnConnect_Clicked()
	{
		int hr;

		// Create object and connect with RC8
		hr = m_cao.CaoConnect();

		if (hr >= 0)
		{
			// Enable command buttons
			m_frm.ButtonController(true);
		}
		else
		{
			btnDisconnect_Clicked();
			m_frm.ShowErrorMessage(hr);
		}
	}

	public void btnDisconnect_Clicked()
	{
		// Disconnect from the controller
		m_cao.CaoDisconnect();

		m_frm.ButtonController(false);
	}

	public void btnMove_Clicked()
	{
		int hresult = m_cao.CaoMove();

		if(hresult < 0){
			m_frm.ShowErrorMessage(hresult);
		}
	}

	public void btnApproach_Clicked(){
		int hresult = m_cao.CaoApproach();

		if(hresult < 0){
			m_frm.ShowErrorMessage(hresult);
		}
	}
	
	public void btnDepart_Clicked(){
		int hresult = m_cao.CaoDepart();

		if(hresult < 0){
			m_frm.ShowErrorMessage(hresult);
		}
	}
		
	public void btnDraw_Clicked(){
		int hresult = m_cao.CaoDraw();

		if(hresult < 0){
			m_frm.ShowErrorMessage(hresult);
		}
	}
	
	public void btnDriveAEx_Clicked(){
		int hresult = m_cao.CaoDriveAEx();

		if(hresult < 0){
			m_frm.ShowErrorMessage(hresult);
		}
	}
	
	public void btnDriveEx_Clicked(){
		int hresult = m_cao.CaoDriveEx();

		if(hresult < 0){
			m_frm.ShowErrorMessage(hresult);
		}
	}
	
	public void btnSpeed_Clicked(){
		int hresult = m_cao.CaoSpeed();

		if(hresult < 0){
			m_frm.ShowErrorMessage(hresult);
		}
	}
	
	public void btnTool_Clicked(){
		int hresult = m_cao.CaoTool();

		if(hresult < 0){
			m_frm.ShowErrorMessage(hresult);
		}
	}
	
	public void btnWork_Clicked(){
		int hresult = m_cao.CaoWork();

		if(hresult < 0){
			m_frm.ShowErrorMessage(hresult);
		}
	}
}
