1
2
3
4 package ca.spaz.cron.user;
5
6 import java.awt.*;
7 import java.text.*;
8 import java.util.Date;
9
10 import javax.swing.*;
11 import javax.swing.border.Border;
12
13 import ca.spaz.wizard.WizardPanel;
14
15 public class UserInfoWizardPanel extends WizardPanel {
16 private static final String CM_UNITS = "Centimeters";
17 private static final String INCHES_UNITS = "Inches";
18 private static final String[] LENGTH_MEASURES = { CM_UNITS, INCHES_UNITS };
19 private static final double CM_PER_INCH = 2.53999996;
20
21 private JPanel namePanel;
22 private JPanel genderPanel;
23 private JPanel birthPanel;
24 private JPanel heightPanel;
25 private JRadioButton male, female;
26 private JTextField birthField, nameField;
27 private JSpinner heightField;
28 private JComboBox heightUnits;
29 private User user;
30
31 public UserInfoWizardPanel(User user) {
32 this.user = user;
33
34 JLabel info = new JLabel("<html><div align=\"center\">"
35 + "The following information is needed to define "
36 + "your profile and to calculate appropriate "
37 + "nutritional tagets for your body type.</div>" + "</html>",
38 JLabel.CENTER);
39 info.setPreferredSize(new Dimension(200, 80));
40
41 JPanel lp = new JPanel(new GridLayout(4, 1, 8, 8));
42 lp.add(new JLabel("Name:", JLabel.RIGHT));
43 lp.add(new JLabel("Gender:", JLabel.RIGHT));
44 lp.add(new JLabel("Height:", JLabel.RIGHT));
45 lp.add(new JLabel("Birthdate:", JLabel.RIGHT));
46
47 JPanel cp = new JPanel(new GridLayout(4, 1, 8, 8));
48 cp.add(getNamePanel());
49 cp.add(getGenderPanel());
50 cp.add(getHeightPanel());
51 cp.add(getBirthPanel());
52
53
54 JPanel jp = new JPanel(new BorderLayout(4, 4));
55 jp.add(info, BorderLayout.NORTH);
56 jp.add(cp, BorderLayout.CENTER);
57
58 this.setLayout(new BorderLayout(4, 4));
59 this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
60 this.add(jp, BorderLayout.NORTH);
61 this.add(Box.createVerticalGlue(), BorderLayout.CENTER);
62 }
63
64 private static Border makeTitle(String str) {
65 return BorderFactory.createCompoundBorder(
66 BorderFactory.createTitledBorder(
67 BorderFactory.createEtchedBorder(), str),
68 BorderFactory.createEmptyBorder(6,6,6,6));
69 }
70
71 private JPanel getNamePanel() {
72 if (namePanel == null) {
73 namePanel = new JPanel(new BorderLayout(4, 4));
74 namePanel.setBorder(makeTitle("Name:"));
75 namePanel.add(getNameField(), BorderLayout.CENTER);
76 }
77 return namePanel;
78 }
79
80 private JTextField getNameField() {
81 if (nameField == null) {
82 nameField = new JTextField(30);
83 nameField.setText(user.getName());
84 }
85 return nameField;
86 }
87
88
89 private JPanel getGenderPanel() {
90 if (genderPanel == null) {
91 ButtonGroup bg = new ButtonGroup();
92 male = new JRadioButton("Male", user.isMale());
93 female = new JRadioButton("Female", user.isFemale());
94 bg.add(male);
95 bg.add(female);
96 genderPanel = new JPanel(new GridLayout(1, 2, 4, 4));
97 genderPanel.setBorder(makeTitle("Gender:"));
98 genderPanel.add(male);
99 genderPanel.add(female);
100 }
101 return genderPanel;
102 }
103
104 private JPanel getBirthPanel() {
105 if (birthPanel == null) {
106 birthPanel = new JPanel(new GridLayout(1, 2, 4, 4));
107 birthPanel.setBorder(makeTitle("Birthdate:"));
108 birthPanel.add(getBirthField());
109 birthPanel.add(new JLabel("(ex: 26/11/1978)"));
110 }
111 return birthPanel;
112 }
113
114 private JTextField getBirthField() {
115 if (birthField == null) {
116 birthField = new JTextField(10);
117 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
118 birthField.setText(df.format(user.getBirthDate()));
119 }
120 return birthField;
121 }
122
123 private JPanel getHeightPanel() {
124 if (heightPanel == null) {
125 heightPanel = new JPanel(new GridLayout(1, 2, 4, 4));
126 heightPanel.setBorder(makeTitle("Height:"));
127 heightPanel.add(getHeightField());
128 heightPanel.add(getHeightUnits());
129 }
130 return heightPanel;
131 }
132
133 private JSpinner getHeightField() {
134 if (heightField == null) {
135 heightField = new JSpinner();
136 Double height = user.getHeightInCM();
137 if (height != null) {
138 heightField.setValue(height);
139 }
140 }
141 return heightField;
142 }
143
144 private JComboBox getHeightUnits() {
145 if (heightUnits == null) {
146 heightUnits = new JComboBox(LENGTH_MEASURES);
147 }
148 return heightUnits;
149 }
150
151
152 public String getWizardPanelTitle() {
153 return "User Information";
154 }
155
156 private Date getBirthDate() {
157 Date birth = null;
158 try {
159 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
160 birth = df.parse(getBirthField().getText());
161 } catch (ParseException e) {
162 e.printStackTrace();
163 }
164 return birth;
165 }
166
167 private double getUserHeight() {
168 double height = ((Number)getHeightField().getValue()).intValue();
169 if (getHeightUnits().getSelectedItem().equals(INCHES_UNITS)) {
170 height = (height * CM_PER_INCH);
171 }
172 return height;
173 }
174
175 public void commitChanges() {
176 user.setGender(male.isSelected());
177 user.setName(getNameField().getText());
178 user.setHeightInCM(new Double(getUserHeight()));
179 user.setBirthDate(getBirthDate());
180 }
181
182 public boolean isValid() {
183 String name = getNameField().getText();
184 if (name == null || name.length() <= 0) {
185 JOptionPane.showMessageDialog(this, "Please enter a name first.",
186 "Error", JOptionPane.ERROR_MESSAGE);
187 return false;
188 }
189
190 if (getUserHeight() <= 1) {
191 JOptionPane.showMessageDialog(this, "Please enter a valid height.",
192 "Error", JOptionPane.ERROR_MESSAGE);
193 return false;
194 }
195
196
197 if (getBirthDate() == null) {
198 JOptionPane.showMessageDialog(this, "Please enter a valid birth date.",
199 "Error", JOptionPane.ERROR_MESSAGE);
200 return false;
201 }
202
203 return true;
204 }
205 }