| 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 | } |
| 52 | |
| 53 | private static class EyeCellRenderer extends DefaultListCellRenderer { |
| 54 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
| 55 | Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
| 56 | Eye currentEye = new Eye((String) list.getModel().getElementAt(index)); |
| 57 | c.setBackground(currentEye.getColor()); |
| 58 | c.setForeground(Color.WHITE); |
| 59 | if (isSelected) { |
| 60 | setBackground(getBackground().darker()); |
| 61 | } |
| 62 | return c; |
| 63 | } |
| 64 | } |
| 65 | } |