Only draw eyes on selection and the full closest person on validation.
[Persons_Comparator.git] / src / EyeView.java
CommitLineData
089fcbfc 1import javax.swing.*;
7add5cb9
JB
2import javax.swing.event.ListSelectionEvent;
3import javax.swing.event.ListSelectionListener;
b05df284 4import java.awt.*;
3d9fdaf4 5import java.util.Arrays;
089fcbfc 6
7add5cb9 7public class EyeView extends JPanel implements ListSelectionListener {
6977e614 8 private Eye eyeObj;
883508ca 9 private JLabel label;
3d9fdaf4 10 private JList colorsList;
6977e614 11
b974e749
JB
12 /**
13 * @param eyeObj
14 */
15 EyeView(Eye eyeObj) {
16 setEyeObj(eyeObj);
883508ca
JB
17 this.label = new JLabel();
18 this.label.setText("Eyes color");
3d9fdaf4
JB
19 this.colorsList = new JList<>(this.getEyeObj().getColorsList());
20 this.colorsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
21 this.colorsList.setLayoutOrientation(JList.VERTICAL);
22 this.colorsList.setSelectedIndex(Arrays.asList(this.getEyeObj().getColorsList()).indexOf(this.getEyeObj().getStrColor()));
b05df284 23 this.colorsList.setCellRenderer(new EyeCellRenderer());
7add5cb9 24 this.colorsList.addListSelectionListener(this);
883508ca
JB
25 add(label);
26 add(colorsList);
089fcbfc
JB
27 }
28
ac6c3ea2 29 /**
ac6c3ea2
JB
30 * @return
31 */
6977e614
JB
32 public Eye getEyeObj() {
33 return eyeObj;
34 }
35
ac6c3ea2 36 /**
ac6c3ea2
JB
37 * @param eyeObj
38 */
6977e614
JB
39 public void setEyeObj(Eye eyeObj) {
40 this.eyeObj = eyeObj;
41 }
9b3bfcdd
JB
42
43 public JList getColorsList() {
44 return colorsList;
45 }
b05df284 46
7add5cb9
JB
47 @Override
48 public void valueChanged(ListSelectionEvent listSelectionEvent) {
49 Eye currentEye = new Eye((String) getColorsList().getSelectedValue());
50 PersonLeftPanel personLeftPanel = MainWindowsView.getInstance().getCurrentPersonView().getPersonPanel().getLeftPanel();
d418dae1 51 personLeftPanel.drawEyes(currentEye.getColor());
7add5cb9
JB
52 }
53
b05df284
JB
54 private static class EyeCellRenderer extends DefaultListCellRenderer {
55 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
56 Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
57 Eye currentEye = new Eye((String) list.getModel().getElementAt(index));
58 c.setBackground(currentEye.getColor());
59 c.setForeground(Color.WHITE);
60 if (isSelected) {
61 setBackground(getBackground().darker());
62 }
63 return c;
64 }
65 }
6977e614 66}