Split the person panel into two sub panels : one for the current person, one for...
[Persons_Comparator.git] / src / PersonView.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.util.ArrayList;
6
7 public class PersonView extends JComponent implements ActionListener {
8 private int width = 600;
9 private int height = 600;
10 private Person personObj;
11 private ArrayList<Person> personArrayList;
12 private FirstnameView firstnameView;
13 private OriginView originView;
14 private SizeView sizeView;
15 private WeightView weightView;
16 private EyeView eyeView;
17 private JPanel northPanel = new JPanel();
18 private JPanel southPanel = new JPanel();
19 private JPanel eastPanel = new JPanel();
20 private JPanel westPanel = new JPanel();
21 private PersonPanel personPanel = new PersonPanel();
22 private JButton compareButton = new JButton("Compare");
23
24 PersonView(Person personObj, ArrayList<Person> personArrayList) {
25 setPersonObj(personObj);
26 setFirstnameView(new FirstnameView(14, this.personObj.getFirstname()));
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()));
31 setPersonArrayList(personArrayList);
32
33 this.compareButton.addActionListener(this);
34 northPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
35 northPanel.add(firstnameView);
36 southPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
37 southPanel.add(compareButton);
38 eastPanel.setLayout(new BoxLayout(this.eastPanel, BoxLayout.PAGE_AXIS));
39 eastPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
40 eastPanel.add(sizeView);
41 eastPanel.add(weightView);
42 eastPanel.add(eyeView);
43 westPanel.setLayout(new BoxLayout(this.westPanel, BoxLayout.PAGE_AXIS));
44 westPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
45 westPanel.add(originView);
46 }
47
48 public Person getPersonObj() {
49 return personObj;
50 }
51
52 public void setPersonObj(Person personObj) {
53 this.personObj = personObj;
54 }
55
56 /**
57 * @return
58 */
59 public FirstnameView getFirstnameView() {
60 return firstnameView;
61 }
62
63 /**
64 * @param firstnameView
65 */
66 public void setFirstnameView(FirstnameView firstnameView) {
67 this.firstnameView = firstnameView;
68 }
69
70 /**
71 * @return
72 */
73 public OriginView getOriginView() {
74 return originView;
75 }
76
77 /**
78 * @param originView
79 */
80 public void setOriginView(OriginView originView) {
81 this.originView = originView;
82 }
83
84 /**
85 * @return
86 */
87 public SizeView getSizeView() {
88 return sizeView;
89 }
90
91 /**
92 * @param sizeView
93 */
94 public void setSizeView(SizeView sizeView) {
95 this.sizeView = sizeView;
96 }
97
98 /**
99 * @return
100 */
101 public WeightView getWeightView() {
102 return weightView;
103 }
104
105 /**
106 * @param weightView
107 */
108 public void setWeightView(WeightView weightView) {
109 this.weightView = weightView;
110 }
111
112 /**
113 * @return
114 */
115 public EyeView getEyeView() {
116 return eyeView;
117 }
118
119 /**
120 * @param eyeView
121 */
122 public void setEyeView(EyeView eyeView) {
123 this.eyeView = eyeView;
124 }
125
126 public ArrayList<Person> getPersonArrayList() {
127 return personArrayList;
128 }
129
130 public void setPersonArrayList(ArrayList<Person> personArrayList) {
131 this.personArrayList = personArrayList;
132 }
133
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
158 @Override
159 public void actionPerformed(ActionEvent actionEvent) {
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));
179 ArrayList<Person> personArrayList = this.getPersonArrayList();
180 //Utils.displayArrayList(personArrayList);
181 Utils.computeDistanceFrom(getPersonObj(), personArrayList);
182 personArrayList.sort(getPersonObj());
183 //Utils.displayArrayList(personArrayList);
184 Person closestPerson = personArrayList.get(personArrayList.indexOf(this.getPersonObj()) + 1);
185 personPanel.setRightContentText(closestPerson.getFirstname().getFirstname() + " at distance " + closestPerson.getDistanceFromReference());
186 }
187 }
188 }