Commit | Line | Data |
---|---|---|
6977e614 | 1 | import javax.swing.*; |
bb6c655a | 2 | import java.awt.*; |
c8da8eb7 JB |
3 | import java.awt.event.ActionEvent; |
4 | import java.awt.event.ActionListener; | |
9b3bfcdd | 5 | import java.util.ArrayList; |
6977e614 | 6 | |
c8da8eb7 | 7 | public class PersonView extends JPanel implements ActionListener { |
bb6c655a JB |
8 | private int width = 400; |
9 | private int height = 600; | |
883508ca | 10 | private Person personObj; |
5687dc10 | 11 | private ArrayList<Person> personArrayList; |
6977e614 JB |
12 | private FirstnameView firstnameView; |
13 | private OriginView originView; | |
14 | private SizeView sizeView; | |
15 | private WeightView weightView; | |
16 | private EyeView eyeView; | |
bb6c655a JB |
17 | private JPanel personPanel = new JPanel(); |
18 | private JButton compareButton = new JButton("Compare"); | |
19 | /*private ImageIcon originImage; | |
20 | private ImageIcon personImage;*/ | |
6977e614 | 21 | |
5687dc10 | 22 | PersonView(Person personObj, ArrayList<Person> personArrayList) { |
b974e749 | 23 | setPersonObj(personObj); |
bb6c655a | 24 | setFirstnameView(new FirstnameView(15, this.personObj.getFirstname())); |
b974e749 JB |
25 | setOriginView(new OriginView(this.personObj.getOrigin())); |
26 | setSizeView(new SizeView(this.personObj.getPersonSize())); | |
27 | setWeightView(new WeightView(this.personObj.getWeight())); | |
28 | setEyeView(new EyeView(this.personObj.getEye())); | |
5687dc10 | 29 | setPersonArrayList(personArrayList); |
60971873 | 30 | |
c8da8eb7 | 31 | this.compareButton.addActionListener(this); |
bb6c655a JB |
32 | JLabel personLabel = new JLabel("Closest person found"); |
33 | personPanel.add(personLabel); | |
34 | setPreferredSize(new Dimension(this.width, this.height)); | |
35 | setLayout(new GridLayout(7, 1, 0, 4)); | |
089fcbfc JB |
36 | add(firstnameView); |
37 | add(originView); | |
38 | add(sizeView); | |
39 | add(weightView); | |
40 | add(eyeView); | |
bb6c655a JB |
41 | add(compareButton); |
42 | add(personPanel); | |
60971873 JB |
43 | } |
44 | ||
883508ca JB |
45 | public Person getPersonObj() { |
46 | return personObj; | |
47 | } | |
48 | ||
49 | public void setPersonObj(Person personObj) { | |
50 | this.personObj = personObj; | |
ac6c3ea2 JB |
51 | } |
52 | ||
53 | /** | |
ac6c3ea2 JB |
54 | * @return |
55 | */ | |
6977e614 JB |
56 | public FirstnameView getFirstnameView() { |
57 | return firstnameView; | |
58 | } | |
59 | ||
ac6c3ea2 | 60 | /** |
ac6c3ea2 JB |
61 | * @param firstnameView |
62 | */ | |
6977e614 JB |
63 | public void setFirstnameView(FirstnameView firstnameView) { |
64 | this.firstnameView = firstnameView; | |
65 | } | |
66 | ||
ac6c3ea2 | 67 | /** |
ac6c3ea2 JB |
68 | * @return |
69 | */ | |
6977e614 JB |
70 | public OriginView getOriginView() { |
71 | return originView; | |
72 | } | |
73 | ||
ac6c3ea2 | 74 | /** |
ac6c3ea2 JB |
75 | * @param originView |
76 | */ | |
6977e614 JB |
77 | public void setOriginView(OriginView originView) { |
78 | this.originView = originView; | |
79 | } | |
80 | ||
ac6c3ea2 | 81 | /** |
ac6c3ea2 JB |
82 | * @return |
83 | */ | |
6977e614 JB |
84 | public SizeView getSizeView() { |
85 | return sizeView; | |
86 | } | |
87 | ||
ac6c3ea2 | 88 | /** |
ac6c3ea2 JB |
89 | * @param sizeView |
90 | */ | |
6977e614 JB |
91 | public void setSizeView(SizeView sizeView) { |
92 | this.sizeView = sizeView; | |
93 | } | |
94 | ||
ac6c3ea2 | 95 | /** |
ac6c3ea2 JB |
96 | * @return |
97 | */ | |
6977e614 JB |
98 | public WeightView getWeightView() { |
99 | return weightView; | |
100 | } | |
101 | ||
ac6c3ea2 | 102 | /** |
ac6c3ea2 JB |
103 | * @param weightView |
104 | */ | |
105 | public void setWeightView(WeightView weightView) { | |
106 | this.weightView = weightView; | |
6977e614 JB |
107 | } |
108 | ||
5687dc10 JB |
109 | /** |
110 | * @return | |
111 | */ | |
112 | public EyeView getEyeView() { | |
113 | return eyeView; | |
114 | } | |
115 | ||
ac6c3ea2 | 116 | /** |
ac6c3ea2 JB |
117 | * @param eyeView |
118 | */ | |
6977e614 JB |
119 | public void setEyeView(EyeView eyeView) { |
120 | this.eyeView = eyeView; | |
121 | } | |
122 | ||
5687dc10 JB |
123 | public ArrayList<Person> getPersonArrayList() { |
124 | return personArrayList; | |
125 | } | |
126 | ||
127 | public void setPersonArrayList(ArrayList<Person> personArrayList) { | |
128 | this.personArrayList = personArrayList; | |
6977e614 | 129 | } |
c8da8eb7 JB |
130 | |
131 | @Override | |
132 | public void actionPerformed(ActionEvent actionEvent) { | |
9b3bfcdd JB |
133 | //TODO: one can implement a smarter way of getting all inputs values inside the main panel. |
134 | String firstname = this.getFirstnameView().getTextField().getText(); | |
135 | String country = null; | |
136 | if (this.getOriginView().getComboBox().getSelectedIndex() != -1) | |
137 | country = (String) this.getOriginView().getComboBox().getSelectedObjects()[0]; | |
138 | Integer size = (Integer) this.getSizeView().getSpinner().getValue(); | |
139 | Integer weight = (Integer) this.getWeightView().getComboBox().getSelectedItem(); | |
140 | String eye_color = (String) this.getEyeView().getColorsList().getSelectedValue(); | |
141 | if (firstname.equals("") || country == null || eye_color == null) { | |
142 | JOptionPane.showMessageDialog(this, | |
143 | "Some required fields are missing.", | |
144 | "Error", | |
145 | JOptionPane.ERROR_MESSAGE); | |
146 | } else { | |
147 | this.getPersonObj().setFirstname(new Firstname(firstname)); | |
148 | this.getPersonObj().setOrigin(new Origin(country)); | |
149 | this.getPersonObj().setPersonSize(new Size(size)); | |
150 | this.getPersonObj().setWeight(new Weight(weight)); | |
151 | this.getPersonObj().setEye(new Eye(eye_color)); | |
5687dc10 JB |
152 | ArrayList<Person> personArrayList = this.getPersonArrayList(); |
153 | //Utils.displayArrayList(personArrayList); | |
154 | Utils.computeDistanceFrom(getPersonObj(), personArrayList); | |
9b3bfcdd | 155 | personArrayList.sort(getPersonObj()); |
5687dc10 JB |
156 | //Utils.displayArrayList(personArrayList); |
157 | Person closestPerson = personArrayList.get(personArrayList.indexOf(this.getPersonObj()) + 1); | |
158 | JLabel closestPersonLabel = new JLabel(); | |
159 | closestPersonLabel.setText(closestPerson.toString()); | |
160 | this.personPanel.add(closestPersonLabel); | |
9b3bfcdd | 161 | } |
c8da8eb7 | 162 | } |
6977e614 | 163 | } |