/*
* See COPYING in top-level directory.
*/
package com.monkygames.wox.health.gui;
// === java imports === //
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// === miglayout imports === //
import net.miginfocom.swing.MigLayout;
// === wox imports === //
import com.monkygames.wox.health.io.*;
import com.monkygames.wox.health.data.Weight;
/**
* Note, this works by using a thread to initialize the scale
* due to blocking issues as well as getting the weight.
* The callgraph should look like this<br>
* init -- ScaleThread -- turnOn -- weightUpdate -- scaleThread --
* Handles interacting with the scale through a thread.
*/
public class ScaleThread extends Thread{
// ============= Class variables ============== //
/**
* Handles measuring the weight of the user.
**/
private Scale scale;
/**
* The state that the measurement can be in.
**/
public enum State{ TURN_ON,STEP_ON,MEASURING,DONE,FREE,TARE }
private State state;
private ScaleInterface scaleInterface;
// ============= Constructors ============== //
public ScaleThread(State state, Scale scale, ScaleInterface scaleInterface){
this.scale = scale;
this.state = state;
this.scaleInterface = scaleInterface;
}
// ============= Public Methods ============== //
// ============= Protected Methods ============== //
// ============= Private Methods ============== //
/**
* Handles finding the scale.
**/
private void turnOn(){
if(scale.initScale()){
// update scale gui
scaleInterface.initComplete();
return;
}
System.out.println("[ScaleThread:turnOn] turnOn Failed");
scaleInterface.scaleFailed();
}
/**
* Handles measuring.
**/
private void measuring(){
Weight weight = scale.getWeight();
scaleInterface.measuringComplete(weight);
/*
state = State.DONE;
healthGUI.updateWeight(weight);
rootPanel.remove(progressBar);
infoLabel.setText("All Done: "+weight.weightInkg+"kg");
updateGUI();
*/
}
/**
* Handles doing the tare calculations.
**/
private void tare(){
float tare = scale.getTare();
scaleInterface.tareComplete(tare);
}
// ============= Implemented Methods ============== //
// ============= Extended Methods ============== //
public void run(){
switch(state){
case TURN_ON:
turnOn();
break;
//case STEP_ON:
// break;
case MEASURING:
measuring();
break;
case FREE:
scale.calibrate();
break;
case TARE:
tare();
break;
}
}
// ============= Internal Classes ============== //
// ============= Static Methods ============== //
}
/*
* Local variables:
* c-indent-level: 4
* c-basic-offset: 4
* End:
*
* vim: ts=8 sts=4 sw=4 noexpandtab
*/