/*
* See COPYING in top-level directory.
*/
package com.monkygames.wox.health.gui;
// === java imports === //
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
// === miglayout imports === //
import net.miginfocom.swing.MigLayout;
// === jdatepicker === //
import net.sourceforge.jdatepicker.*;
// === wox imports === //
import com.monkygames.wox.health.io.*;
import com.monkygames.wox.health.data.Weight;
/**
* Used to manually input weight for a specified date.
* @version 1.0
*/
public class ManualInputGUI extends JFrame implements ActionListener{
// ============= Class variables ============== //
/**
* The root panel.
**/
private JPanel rootPanel;
private JDatePanel datePanel;
private JComboBox measurementCB;
private JTextField weightTF;
private JButton cancelB,addB,doneB;
/**
* Used for notifying when the weight has been calc.
**/
private HealthGUI healthGUI;
/**
* The hour for the input.
**/
private JSpinner hourSpinner;
/**
* The min for the input.
**/
private JSpinner minSpinner;
private Vector<Weight> weightV;
// ============= Constructors ============== //
public ManualInputGUI(HealthGUI healthGUI){
this.healthGUI = healthGUI;
int w = 345;
int h = 290;
setSize(w,h);
setTitle("Manualy Input Weight");
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setup();
getContentPane().add(rootPanel);
init();
}
// ============= Public Methods ============== //
public void init(){
weightV = new Vector<Weight>();
updateGUI();
setVisible(true);
}
// ============= Protected Methods ============== //
// ============= Private Methods ============== //
private void setup(){
//rootPanel = new JPanel(new MigLayout("","grow","grow"));
rootPanel = new JPanel(new MigLayout());
measurementCB = new JComboBox(new Object[]{"kg","lbs"});
weightTF = new JTextField(20);
JLabel infoL = new JLabel("Weight");
cancelB = new JButton("Cancel");
addB = new JButton("Add");
doneB = new JButton("Done");
cancelB.addActionListener(this);
addB.addActionListener(this);
doneB.addActionListener(this);
datePanel = JDateComponentFactory.createJDatePanel();
datePanel.setShowYearButtons(true);
SpinnerNumberModel hourModel = new SpinnerNumberModel(0,0, 24,1);
hourSpinner = new JSpinner(hourModel);
SpinnerNumberModel minModel = new SpinnerNumberModel(0,0, 59,1);
minSpinner = new JSpinner(minModel);
JPanel timeP = new JPanel(new MigLayout());
timeP.add(new JLabel("Hour"));
timeP.add(new JLabel("Min"),"wrap");
timeP.add(hourSpinner);
timeP.add(minSpinner);
JPanel northP = new JPanel(new MigLayout());
northP.add(infoL);
northP.add(weightTF);
northP.add(measurementCB);
JPanel rightP = new JPanel(new MigLayout());
rightP.add(cancelB);
rightP.add(addB);
rightP.add(doneB);
JPanel southP = new JPanel(new MigLayout("fill"));
southP.add(timeP,"dock east,push,grow");
southP.add(rightP,"dock west,push,growy");
rootPanel.add(northP,"dock north");
rootPanel.add((JComponent)datePanel,"dock center");
rootPanel.add(southP,"dock south");
}
/**
* Handles repating the graphics.
**/
private void updateGUI(){
rootPanel.repaint();
rootPanel.revalidate();
}
private void setWeight(){
String weightS = weightTF.getText();
float weight = 0f;
try{
weight = Float.parseFloat(weightS);
if(measurementCB.getSelectedIndex() == 1){
// convert to kg
weight = weight/2.2f;
}
}catch(Exception e){
JOptionPane.showMessageDialog(this,"Please enter a valid weight","Warning",JOptionPane.INFORMATION_MESSAGE);
return;
}
DateModel dateModel = datePanel.getModel();
int day = dateModel.getDay();
int month = dateModel.getMonth();
int year = dateModel.getYear();
int hour = ((Integer)hourSpinner.getValue()).intValue();
int min = ((Integer)minSpinner.getValue()).intValue();
if(hour < 0 || hour > 24 || min < 0 || min > 59){
JOptionPane.showMessageDialog(this,"Please enter a valid time","Warning",JOptionPane.INFORMATION_MESSAGE);
return;
}
System.out.println("min,hour,day,month,year "+min+","+hour+","+day+","+month+","+year);
Calendar cal = Calendar.getInstance();
cal.set(year,month,day,hour,min);
Weight weightW = new Weight(weight,cal.getTimeInMillis());
weightV.add(weightW);
JOptionPane.showMessageDialog(this,"Weight Added to Queue\nMust select done to commit","Info",JOptionPane.INFORMATION_MESSAGE);
}
// ============= Implemented Methods ============== //
public void actionPerformed(ActionEvent evt){
Object src = evt.getSource();
if(src == cancelB){
JOptionPane.showMessageDialog(this,"All entered weights disregarded","Info",JOptionPane.INFORMATION_MESSAGE);
setVisible(false);
}else if(src == addB){
setWeight();
}else if(src == doneB){
for(int i = 0; i < weightV.size(); i++){
healthGUI.updateWeightSorted(weightV.elementAt(i));
}
JOptionPane.showMessageDialog(this,"Number of weights ("+weightV.size()+") committed to history","Info",JOptionPane.INFORMATION_MESSAGE);
setVisible(false);
}
}
// ============= 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
*/