Make the main window a singleton to permit to query it from other views and update...
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 16 Jan 2019 19:13:36 +0000 (20:13 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 16 Jan 2019 19:13:36 +0000 (20:13 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/CSVUtils.java
src/Eye.java
src/EyeView.java
src/Main.java
src/MainWindowsView.java
src/PersonLeftPanel.java [moved from src/LeftPersonPanel.java with 85% similarity]
src/PersonPanel.java
src/PersonRightPanel.java [moved from src/RightPersonPanel.java with 94% similarity]

index 08a1ef91b18171f58fa1181aad2e12f520fe5fee..d586b005d369824b7e849e79346b80f421b15cca 100644 (file)
@@ -6,20 +6,20 @@ public class CSVUtils {
     private static final char DEFAULT_SEPARATOR = ',';
     private static final char DEFAULT_QUOTE = '"';
 
-    public static List<String> parseLine(String cvsLine) {
-        return parseLine(cvsLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE);
+    public static List<String> parseLine(String csvLine) {
+        return parseLine(csvLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE);
     }
 
-    public static List<String> parseLine(String cvsLine, char separators) {
-        return parseLine(cvsLine, separators, DEFAULT_QUOTE);
+    public static List<String> parseLine(String csvLine, char separators) {
+        return parseLine(csvLine, separators, DEFAULT_QUOTE);
     }
 
-    public static List<String> parseLine(String cvsLine, char separators, char customQuote) {
+    public static List<String> parseLine(String csvLine, char separators, char customQuote) {
 
         List<String> result = new ArrayList<>();
 
         //if empty, return!
-        if (cvsLine == null && cvsLine.isEmpty()) {
+        if (csvLine == null && csvLine.isEmpty()) {
             return result;
         }
 
@@ -36,7 +36,7 @@ public class CSVUtils {
         boolean startCollectChar = false;
         boolean doubleQuotesInColumn = false;
 
-        char[] chars = cvsLine.toCharArray();
+        char[] chars = csvLine.toCharArray();
 
         for (char ch : chars) {
 
index 822e6f83dd3fc7aabc5bcf5d864671c828c6f999..5177333340ccaae5405fce6264d927319ef3aa16 100644 (file)
@@ -7,6 +7,7 @@ public class Eye {
     private String[] colorsList = {"black", "blue", "brown", "green"};
 
     Eye() {
+        //this.color = Color.white;
         sortColorList();
     }
 
index 99b5d0b680847537199c0747262398175aa553c4..bc1b89b85665413e51de38360592ba3ede4637f8 100644 (file)
@@ -1,8 +1,10 @@
 import javax.swing.*;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
 import java.awt.*;
 import java.util.Arrays;
 
-public class EyeView extends JPanel {
+public class EyeView extends JPanel implements ListSelectionListener {
     private Eye eyeObj;
     private JLabel label;
     private JList colorsList;
@@ -19,6 +21,7 @@ public class EyeView extends JPanel {
         this.colorsList.setLayoutOrientation(JList.VERTICAL);
         this.colorsList.setSelectedIndex(Arrays.asList(this.getEyeObj().getColorsList()).indexOf(this.getEyeObj().getStrColor()));
         this.colorsList.setCellRenderer(new EyeCellRenderer());
+        this.colorsList.addListSelectionListener(this);
         add(label);
         add(colorsList);
     }
@@ -41,6 +44,12 @@ public class EyeView extends JPanel {
         return colorsList;
     }
 
+    @Override
+    public void valueChanged(ListSelectionEvent listSelectionEvent) {
+        Eye currentEye = new Eye((String) getColorsList().getSelectedValue());
+        PersonLeftPanel personLeftPanel = MainWindowsView.getInstance().getCurrentPersonView().getPersonPanel().getLeftPanel();
+    }
+
     private static class EyeCellRenderer extends DefaultListCellRenderer {
         public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
             Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
index a3bc099b9f6dac2aaf98b69ee1b4952140f45dc2..17cf896c521dc308a5b1fb595f539beb48bb3963 100644 (file)
@@ -80,7 +80,8 @@ public class Main {
         //creating and showing this application's GUI.
         javax.swing.SwingUtilities.invokeLater(new Runnable() {
             public void run() {
-                MainWindowsView mainWindows = new MainWindowsView(programName, emptyPersonView);
+                MainWindowsView mainWindows = MainWindowsView.getInstance();
+                mainWindows.setMainWindowsView(programName, emptyPersonView);
                 mainWindows.showGUI();
             }
         });
index 0ff9bbbe242bbcaaf3c2027d0204a910f929e94f..703792124cb31db55c285d7cd3ff79d65e9c00ec 100644 (file)
@@ -7,12 +7,18 @@ import java.awt.event.KeyEvent;
 import java.util.ArrayList;
 
 public class MainWindowsView extends JFrame {
+    private static MainWindowsView ourInstance = new MainWindowsView();
+    private PersonView currentPersonView;
 
-    MainWindowsView(String title, PersonView view) {
+    private MainWindowsView() {
+    }
+
+    public void setMainWindowsView(String title, PersonView personView) {
+        this.currentPersonView = personView;
         Container panel = getContentPane();
         //Create and set up the window.
         setTitle(title);
-        setSize(view.getDimension());
+        setSize(getCurrentPersonView().getDimension());
         setLocationRelativeTo(null);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
@@ -29,11 +35,19 @@ public class MainWindowsView extends JFrame {
             panel.add(component);
         }
 
-        panel.add(view.getNorthPanel(), BorderLayout.NORTH);
-        panel.add(view.getSouthPanel(), BorderLayout.SOUTH);
-        panel.add(view.getEastPanel(), BorderLayout.EAST);
-        panel.add(view.getWestPanel(), BorderLayout.WEST);
-        panel.add(view.getPersonPanel(), BorderLayout.CENTER);
+        panel.add(getCurrentPersonView().getNorthPanel(), BorderLayout.NORTH);
+        panel.add(getCurrentPersonView().getSouthPanel(), BorderLayout.SOUTH);
+        panel.add(getCurrentPersonView().getEastPanel(), BorderLayout.EAST);
+        panel.add(getCurrentPersonView().getWestPanel(), BorderLayout.WEST);
+        panel.add(getCurrentPersonView().getPersonPanel(), BorderLayout.CENTER);
+    }
+
+    public static MainWindowsView getInstance() {
+        return ourInstance;
+    }
+
+    public PersonView getCurrentPersonView() {
+        return currentPersonView;
     }
 
     /**
similarity index 85%
rename from src/LeftPersonPanel.java
rename to src/PersonLeftPanel.java
index 95d7d02b9355318acea10847218379e59fa11b5e..a7022fb9460de77364a9016d7797982768ec164d 100644 (file)
@@ -4,11 +4,11 @@ import java.awt.*;
 import java.io.File;
 import java.io.IOException;
 
-public class LeftPersonPanel extends JPanel {
+public class PersonLeftPanel extends JPanel {
     private JLabel leftLabel = new JLabel();
     private Image personImage;
 
-    LeftPersonPanel() {
+    PersonLeftPanel() {
         leftLabel.setText("Current person");
         add(leftLabel);
         try {
@@ -18,10 +18,6 @@ public class LeftPersonPanel extends JPanel {
         }
     }
 
-    public Image getPersonImage() {
-        return personImage;
-    }
-
     public void paintComponent(Graphics g) {
         super.paintComponent(g);
 
index 5b2e190531d076f7176feb09e213f546bc21b3bf..4f17945fed81377abe86f13555e6194dbd441e01 100644 (file)
@@ -1,8 +1,8 @@
 import javax.swing.*;
 
 public class PersonPanel extends JPanel {
-    private LeftPersonPanel leftPanel = new LeftPersonPanel();
-    private RightPersonPanel rightPanel = new RightPersonPanel();
+    private PersonLeftPanel leftPanel = new PersonLeftPanel();
+    private PersonRightPanel rightPanel = new PersonRightPanel();
 
     public PersonPanel() {
         setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
@@ -13,4 +13,12 @@ public class PersonPanel extends JPanel {
     public void setRightContentText(String rightContentText) {
         this.rightPanel.setContentText(rightContentText);
     }
+
+    public PersonLeftPanel getLeftPanel() {
+        return leftPanel;
+    }
+
+    public PersonRightPanel getRightPanel() {
+        return rightPanel;
+    }
 }
similarity index 94%
rename from src/RightPersonPanel.java
rename to src/PersonRightPanel.java
index 2e3460b540d2a237fbbad3f407da416dd60ac298..28099b249fcfb0e1d61f99458a2fb94ed000183e 100644 (file)
@@ -4,12 +4,12 @@ import java.awt.*;
 import java.io.File;
 import java.io.IOException;
 
-public class RightPersonPanel extends JPanel {
+public class PersonRightPanel extends JPanel {
     private JLabel rightLabel = new JLabel();
     private String contentText = new String();
     private Image personImage;
 
-    RightPersonPanel() {
+    PersonRightPanel() {
         rightLabel.setText("Closest person found");
         add(rightLabel);
         try {