Package com.monkygames.wox.health.gui

Source Code of com.monkygames.wox.health.gui.FreeScaleGUI

/*
* 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;

/**
* Displays the "primitive" scale gui that doesn't record the 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 --
* @version 1.0
*/
public class FreeScaleGUI extends JFrame implements ScaleInterface, ActionListener{

// ============= Class variables ============== //
    /**
     * The root panel.
     **/
    private JPanel rootPanel;
    /**
     * Contains information to notify the user with.
     **/
    private JLabel infoLabel;
    /**
     * Used for the user to cancel or accept.
     **/
    private JButton actionB;
    /**
     * The progress (indeterminate) of the measurement.
     **/
    private JProgressBar progressBar;
    /**
     * The current state of the measurement.
     **/
    private ScaleThread.State state;
    /**
     * Handles measuring the weight of the user.
     **/
    private Scale scale;
    /**
     * The number of samples recieved from the scale.
     **/
    private int sampleNum;
    private boolean isKG = true;
// ============= Constructors ============== //
    public FreeScaleGUI(){
  int w = 300;
  int h = 200;
  setSize(w,h);
  // moves window to center of screen
  setLocationRelativeTo(null);
  setTitle("Measure Weight");
  setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

  setup();

  getContentPane().add(rootPanel);
    }
// ============= Public Methods ============== //
    /**
     * Sets the scale to be used.
     * @param scale the scale to be used.
     **/
    public void setScale(Scale scale){
  this.scale.removeScaleInterface(this);
  this.scale = scale;
    }
    /**
     * Initialises the gui and pops open.
     * @param isKG true if using KG and false if bls.
     **/
    public void init(boolean isKG){
  this.isKG = isKG;
  actionB.setText("Stop");
  if(scale.balanceBoardFound()){
      // scale is now on
      state = ScaleThread.State.FREE;
      //state = ScaleThread.State.TARE;
      //infoLabel.setText("Calculating Tare");
      infoLabel.setText("Free Scale");

      ScaleThread scaleThread = new ScaleThread(state,scale,this);
      scaleThread.start();
      scale.addScaleInterface(this);

      updateGUI();
      setVisible(true);
  }else{
      //pop open error
            JOptionPane.showMessageDialog(this,"Please use the find button to enable balance board","Warning",JOptionPane.INFORMATION_MESSAGE);
      return;
  }
    }
// ============= Protected Methods ============== //
// ============= Private Methods ============== //
    private void setup(){
  //rootPanel = new JPanel(new MigLayout("","grow","grow"));
  rootPanel = new JPanel(new MigLayout("fill"));
  infoLabel = new JLabel("");
  actionB = new JButton("Stop");
  actionB.addActionListener(this);
  progressBar = new JProgressBar();
  progressBar.setOrientation(SwingConstants.HORIZONTAL);
  progressBar.setStringPainted(true);
  progressBar.setIndeterminate(true);

  rootPanel.add(infoLabel,"dock north");
  rootPanel.add(progressBar,"dock center");
  rootPanel.add(actionB,"dock south");

  scale = new Scale();
  scale.addScaleInterface(this);
    }
 
    /**
     * Handles repating the graphics.
     **/
    private void updateGUI(){
  rootPanel.repaint();
  rootPanel.revalidate();
    }
// ============= Implemented Methods ============== //
    public void actionPerformed(ActionEvent evt){
  Object src = evt.getSource();
  // inform thread to stop
  scale.removeScaleInterface(this);
  scale.setNoTare();
  // need to make sure that we do not record the measurements!
  try{
      // wait for the scale to be dropped
      Thread.sleep(1000);
  }catch(Exception e){}
  setVisible(false);
    }
    /**
     * The scale encountered an error.
     **/
    public void scaleFailed(){
  infoLabel.setText("Unable to connect to Scale");
  updateGUI();
    }
    /**
     * The scale has been initialized.
     **/
    public void initComplete(){}
    /**
     * Handles measuring.
     **/
    public void measuringComplete(Weight weight){}
    /**
     * Notifies that the tare process is complete.
     **/
    public void tareComplete(float tare){
  //ScaleThread scaleThread = new ScaleThread(state,scale,this);
  infoLabel.setText("Scale now Ready for use");
  updateGUI();
    }
    /**
     * The current weight reported from the scale.
     * @param weight the new weight.
     **/
    public void weightUpdate(float weight){
  sampleNum++;
  if(sampleNum == 100){
      // update every n samples
      if(isKG){
    progressBar.setString(weight+"kg");
      }else{
    progressBar.setString(weight*2.2f+"bls");
      }
      sampleNum = 0;
  }
    }


// ============= Extended Methods ============== //
// ============= Internal Classes ============== //
// ============= Static Methods ============== //

}
/*
* Local variables:
*  c-indent-level: 4
*  c-basic-offset: 4
* End:
*
* vim: ts=8 sts=4 sw=4 noexpandtab
*/ 
TOP

Related Classes of com.monkygames.wox.health.gui.FreeScaleGUI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.