Merge remote-tracking branch 'origin/master'
[Persons_Comparator.git] / src / PersonView.java
CommitLineData
6977e614 1import javax.swing.*;
bb6c655a 2import java.awt.*;
c8da8eb7
JB
3import java.awt.event.ActionEvent;
4import java.awt.event.ActionListener;
9b3bfcdd 5import java.util.ArrayList;
6977e614 6
d252f672 7public class PersonView extends JComponent implements ActionListener {
255d3354
JB
8 private int width = 600;
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;
d252f672
JB
17 private JPanel northPanel = new JPanel();
18 private JPanel southPanel = new JPanel();
19 private JPanel eastPanel = new JPanel();
20 private JPanel westPanel = new JPanel();
255d3354 21 private PersonPanel personPanel = new PersonPanel();
bb6c655a 22 private JButton compareButton = new JButton("Compare");
6977e614 23
5687dc10 24 PersonView(Person personObj, ArrayList<Person> personArrayList) {
b974e749 25 setPersonObj(personObj);
255d3354 26 setFirstnameView(new FirstnameView(14, this.personObj.getFirstname()));
b974e749
JB
27 setOriginView(new OriginView(this.personObj.getOrigin()));
28 setSizeView(new SizeView(this.personObj.getPersonSize()));
29 setWeightView(new WeightView(this.personObj.getWeight()));
30 setEyeView(new EyeView(this.personObj.getEye()));
5687dc10 31 setPersonArrayList(personArrayList);
60971873 32
c8da8eb7 33 this.compareButton.addActionListener(this);
d252f672
JB
34 northPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
35 northPanel.add(firstnameView);
36 southPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
37 southPanel.add(compareButton);
255d3354 38 eastPanel.setLayout(new BoxLayout(this.eastPanel, BoxLayout.PAGE_AXIS));
d252f672
JB
39 eastPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
40 eastPanel.add(sizeView);
41 eastPanel.add(weightView);
42 eastPanel.add(eyeView);
255d3354 43 westPanel.setLayout(new BoxLayout(this.westPanel, BoxLayout.PAGE_AXIS));
d252f672
JB
44 westPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
45 westPanel.add(originView);
60971873
JB
46 }
47
883508ca
JB
48 public Person getPersonObj() {
49 return personObj;
50 }
51
52 public void setPersonObj(Person personObj) {
53 this.personObj = personObj;
ac6c3ea2
JB
54 }
55
56 /**
ac6c3ea2
JB
57 * @return
58 */
6977e614
JB
59 public FirstnameView getFirstnameView() {
60 return firstnameView;
61 }
62
ac6c3ea2 63 /**
ac6c3ea2
JB
64 * @param firstnameView
65 */
6977e614
JB
66 public void setFirstnameView(FirstnameView firstnameView) {
67 this.firstnameView = firstnameView;
68 }
69
ac6c3ea2 70 /**
ac6c3ea2
JB
71 * @return
72 */
6977e614
JB
73 public OriginView getOriginView() {
74 return originView;
75 }
76
ac6c3ea2 77 /**
ac6c3ea2
JB
78 * @param originView
79 */
6977e614
JB
80 public void setOriginView(OriginView originView) {
81 this.originView = originView;
82 }
83
ac6c3ea2 84 /**
ac6c3ea2
JB
85 * @return
86 */
6977e614
JB
87 public SizeView getSizeView() {
88 return sizeView;
89 }
90
ac6c3ea2 91 /**
ac6c3ea2
JB
92 * @param sizeView
93 */
6977e614
JB
94 public void setSizeView(SizeView sizeView) {
95 this.sizeView = sizeView;
96 }
97
ac6c3ea2 98 /**
ac6c3ea2
JB
99 * @return
100 */
6977e614
JB
101 public WeightView getWeightView() {
102 return weightView;
103 }
104
ac6c3ea2 105 /**
ac6c3ea2
JB
106 * @param weightView
107 */
108 public void setWeightView(WeightView weightView) {
109 this.weightView = weightView;
6977e614
JB
110 }
111
5687dc10
JB
112 /**
113 * @return
114 */
115 public EyeView getEyeView() {
116 return eyeView;
117 }
118
ac6c3ea2 119 /**
ac6c3ea2
JB
120 * @param eyeView
121 */
6977e614
JB
122 public void setEyeView(EyeView eyeView) {
123 this.eyeView = eyeView;
124 }
125
5687dc10
JB
126 public ArrayList<Person> getPersonArrayList() {
127 return personArrayList;
128 }
129
130 public void setPersonArrayList(ArrayList<Person> personArrayList) {
131 this.personArrayList = personArrayList;
6977e614 132 }
c8da8eb7 133
d252f672
JB
134 public Dimension getDimension() {
135 return new Dimension(this.width, this.height);
136 }
137
138 public JPanel getNorthPanel() {
139 return northPanel;
140 }
141
142 public JPanel getSouthPanel() {
143 return southPanel;
144 }
145
146 public JPanel getEastPanel() {
147 return eastPanel;
148 }
149
150 public JPanel getWestPanel() {
151 return westPanel;
152 }
153
154 public PersonPanel getPersonPanel() {
155 return personPanel;
156 }
157
c8da8eb7
JB
158 @Override
159 public void actionPerformed(ActionEvent actionEvent) {
9b3bfcdd
JB
160 //TODO: one can implement a smarter way of getting all inputs values inside the main panel.
161 String firstname = this.getFirstnameView().getTextField().getText();
162 String country = null;
163 if (this.getOriginView().getComboBox().getSelectedIndex() != -1)
164 country = (String) this.getOriginView().getComboBox().getSelectedObjects()[0];
165 Integer size = (Integer) this.getSizeView().getSpinner().getValue();
166 Integer weight = (Integer) this.getWeightView().getComboBox().getSelectedItem();
167 String eye_color = (String) this.getEyeView().getColorsList().getSelectedValue();
168 if (firstname.equals("") || country == null || eye_color == null) {
169 JOptionPane.showMessageDialog(this,
170 "Some required fields are missing.",
171 "Error",
172 JOptionPane.ERROR_MESSAGE);
173 } else {
174 this.getPersonObj().setFirstname(new Firstname(firstname));
175 this.getPersonObj().setOrigin(new Origin(country));
176 this.getPersonObj().setPersonSize(new Size(size));
177 this.getPersonObj().setWeight(new Weight(weight));
178 this.getPersonObj().setEye(new Eye(eye_color));
5687dc10
JB
179 ArrayList<Person> personArrayList = this.getPersonArrayList();
180 //Utils.displayArrayList(personArrayList);
181 Utils.computeDistanceFrom(getPersonObj(), personArrayList);
9b3bfcdd 182 personArrayList.sort(getPersonObj());
5687dc10
JB
183 //Utils.displayArrayList(personArrayList);
184 Person closestPerson = personArrayList.get(personArrayList.indexOf(this.getPersonObj()) + 1);
255d3354 185 personPanel.setRightContentText(closestPerson.getFirstname().getFirstname() + " at distance " + closestPerson.getDistanceFromReference());
d418dae1 186 personPanel.getRightPanel().drawPerson(closestPerson);
9b3bfcdd 187 }
c8da8eb7 188 }
6977e614 189}