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