From 7add5cb9b2213df01b559a59f39b033e27f50e72 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 16 Jan 2019 20:13:36 +0100 Subject: [PATCH] Make the main window a singleton to permit to query it from other views and update it if needed on GUI events. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/CSVUtils.java | 14 +++++----- src/Eye.java | 1 + src/EyeView.java | 11 +++++++- src/Main.java | 3 +- src/MainWindowsView.java | 28 ++++++++++++++----- ...tPersonPanel.java => PersonLeftPanel.java} | 8 ++---- src/PersonPanel.java | 12 ++++++-- ...PersonPanel.java => PersonRightPanel.java} | 4 +-- 8 files changed, 55 insertions(+), 26 deletions(-) rename src/{LeftPersonPanel.java => PersonLeftPanel.java} (85%) rename src/{RightPersonPanel.java => PersonRightPanel.java} (94%) diff --git a/src/CSVUtils.java b/src/CSVUtils.java index 08a1ef9..d586b00 100644 --- a/src/CSVUtils.java +++ b/src/CSVUtils.java @@ -6,20 +6,20 @@ public class CSVUtils { private static final char DEFAULT_SEPARATOR = ','; private static final char DEFAULT_QUOTE = '"'; - public static List parseLine(String cvsLine) { - return parseLine(cvsLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE); + public static List parseLine(String csvLine) { + return parseLine(csvLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE); } - public static List parseLine(String cvsLine, char separators) { - return parseLine(cvsLine, separators, DEFAULT_QUOTE); + public static List parseLine(String csvLine, char separators) { + return parseLine(csvLine, separators, DEFAULT_QUOTE); } - public static List parseLine(String cvsLine, char separators, char customQuote) { + public static List parseLine(String csvLine, char separators, char customQuote) { List 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) { diff --git a/src/Eye.java b/src/Eye.java index 822e6f8..5177333 100644 --- a/src/Eye.java +++ b/src/Eye.java @@ -7,6 +7,7 @@ public class Eye { private String[] colorsList = {"black", "blue", "brown", "green"}; Eye() { + //this.color = Color.white; sortColorList(); } diff --git a/src/EyeView.java b/src/EyeView.java index 99b5d0b..bc1b89b 100644 --- a/src/EyeView.java +++ b/src/EyeView.java @@ -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); diff --git a/src/Main.java b/src/Main.java index a3bc099..17cf896 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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(); } }); diff --git a/src/MainWindowsView.java b/src/MainWindowsView.java index 0ff9bbb..7037921 100644 --- a/src/MainWindowsView.java +++ b/src/MainWindowsView.java @@ -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; } /** diff --git a/src/LeftPersonPanel.java b/src/PersonLeftPanel.java similarity index 85% rename from src/LeftPersonPanel.java rename to src/PersonLeftPanel.java index 95d7d02..a7022fb 100644 --- a/src/LeftPersonPanel.java +++ b/src/PersonLeftPanel.java @@ -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); diff --git a/src/PersonPanel.java b/src/PersonPanel.java index 5b2e190..4f17945 100644 --- a/src/PersonPanel.java +++ b/src/PersonPanel.java @@ -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; + } } diff --git a/src/RightPersonPanel.java b/src/PersonRightPanel.java similarity index 94% rename from src/RightPersonPanel.java rename to src/PersonRightPanel.java index 2e3460b..28099b2 100644 --- a/src/RightPersonPanel.java +++ b/src/PersonRightPanel.java @@ -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 { -- 2.34.1