Only draw eyes on selection and the full closest person on validation.
[Persons_Comparator.git] / src / EyeView.java
1 import javax.swing.*;
2 import javax.swing.event.ListSelectionEvent;
3 import javax.swing.event.ListSelectionListener;
4 import java.awt.*;
5 import java.util.Arrays;
6
7 public class EyeView extends JPanel implements ListSelectionListener {
8 private Eye eyeObj;
9 private JLabel label;
10 private JList colorsList;
11
12 /**
13 * @param eyeObj
14 */
15 EyeView(Eye eyeObj) {
16 setEyeObj(eyeObj);
17 this.label = new JLabel();
18 this.label.setText("Eyes color");
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()));
23 this.colorsList.setCellRenderer(new EyeCellRenderer());
24 this.colorsList.addListSelectionListener(this);
25 add(label);
26 add(colorsList);
27 }
28
29 /**
30 * @return
31 */
32 public Eye getEyeObj() {
33 return eyeObj;
34 }
35
36 /**
37 * @param eyeObj
38 */
39 public void setEyeObj(Eye eyeObj) {
40 this.eyeObj = eyeObj;
41 }
42
43 public JList getColorsList() {
44 return colorsList;
45 }
46
47 @Override
48 public void valueChanged(ListSelectionEvent listSelectionEvent) {
49 Eye currentEye = new Eye((String) getColorsList().getSelectedValue());
50 PersonLeftPanel personLeftPanel = MainWindowsView.getInstance().getCurrentPersonView().getPersonPanel().getLeftPanel();
51 personLeftPanel.drawEyes(currentEye.getColor());
52 }
53
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 }
66 }