3 import java
.awt
.event
.ActionEvent
;
4 import java
.awt
.event
.ActionListener
;
5 import java
.util
.ArrayList
;
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");
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
);
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
);
48 public Person
getPersonObj() {
52 public void setPersonObj(Person personObj
) {
53 this.personObj
= personObj
;
59 public FirstnameView
getFirstnameView() {
64 * @param firstnameView
66 public void setFirstnameView(FirstnameView firstnameView
) {
67 this.firstnameView
= firstnameView
;
73 public OriginView
getOriginView() {
80 public void setOriginView(OriginView originView
) {
81 this.originView
= originView
;
87 public SizeView
getSizeView() {
94 public void setSizeView(SizeView sizeView
) {
95 this.sizeView
= sizeView
;
101 public WeightView
getWeightView() {
108 public void setWeightView(WeightView weightView
) {
109 this.weightView
= weightView
;
115 public EyeView
getEyeView() {
122 public void setEyeView(EyeView eyeView
) {
123 this.eyeView
= eyeView
;
126 public ArrayList
<Person
> getPersonArrayList() {
127 return personArrayList
;
130 public void setPersonArrayList(ArrayList
<Person
> personArrayList
) {
131 this.personArrayList
= personArrayList
;
134 public Dimension
getDimension() {
135 return new Dimension(this.width
, this.height
);
138 public JPanel
getNorthPanel() {
142 public JPanel
getSouthPanel() {
146 public JPanel
getEastPanel() {
150 public JPanel
getWestPanel() {
154 public PersonPanel
getPersonPanel() {
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.",
172 JOptionPane
.ERROR_MESSAGE
);
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());