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