From dd8a94a492bccd38d78a80d0847b56ee89b0b38b Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 14 Jan 2021 09:25:26 +0100 Subject: [PATCH] Refactor GUI, ProjectionHandler and ProjectionSelecter --- src/main/java/GUI/GUI.java | 48 +++++++++-------------- src/main/java/GUI/ProjectionHandler.java | 6 +-- src/main/java/GUI/ProjectionSelecter.java | 23 +++-------- 3 files changed, 24 insertions(+), 53 deletions(-) diff --git a/src/main/java/GUI/GUI.java b/src/main/java/GUI/GUI.java index 48c94d8..0d4718b 100644 --- a/src/main/java/GUI/GUI.java +++ b/src/main/java/GUI/GUI.java @@ -12,7 +12,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; -public class GUI extends JFrame implements ActionListener { +public class GUI extends JFrame { private JPanel mainPanel; private JPanel agendaPanel; private JPanel arrowPanel; @@ -56,10 +56,10 @@ public class GUI extends JFrame implements ActionListener { this.renderMenu(); agenda = new Agenda(agendaPanel, this.currentPage, this.currentCompetition); - previousButton.addActionListener(this); - nextButton.addActionListener(this); - menuItemAddProj.addActionListener(this); - menuItemRemoveProj.addActionListener(this); + previousButton.addActionListener(e -> nextPreviousPage(false)); + nextButton.addActionListener(e -> nextPreviousPage(true)); + menuItemAddProj.addActionListener(e -> agenda.openDialog(ProjectionType.ADD)); + menuItemRemoveProj.addActionListener(e -> agenda.openDialog(ProjectionType.REMOVE)); setVisible(true); } @@ -73,34 +73,22 @@ public class GUI extends JFrame implements ActionListener { } } - @Override - public void actionPerformed(ActionEvent e) { - Object source = e.getSource(); - if (source == previousButton) { - this.currentPage--; - Agenda agenda = new Agenda(agendaPanel, this.currentPage, this.currentCompetition); - this.currentPage = agenda.getCurrentPage(); - this.repaint(); - this.revalidate(); - } else if (source == nextButton) { - this.currentPage++; - Agenda agenda = new Agenda(agendaPanel, this.currentPage, this.currentCompetition); - this.currentPage = agenda.getCurrentPage(); - - this.repaint(); - this.revalidate(); - } else if (source == this.menuItemAddProj) { - agenda.openDialog(ProjectionType.ADD); - } else if (source == this.menuItemRemoveProj) { - agenda.openDialog(ProjectionType.REMOVE); - } + private void nextPreviousPage(boolean next) { + if (next) + currentPage++; + else + currentPage--; + agenda = new Agenda(agendaPanel, currentPage, currentCompetition); + currentPage = agenda.getCurrentPage(); + repaint(); + revalidate(); } private void switchAgenda(Competition competition) { - this.currentCompetition = competition; - this.agenda = new Agenda(agendaPanel, 0, this.currentCompetition); - this.repaint(); - this.revalidate(); + currentCompetition = competition; + agenda = new Agenda(agendaPanel, 0, currentCompetition); + repaint(); + revalidate(); } private void createUIComponents() { diff --git a/src/main/java/GUI/ProjectionHandler.java b/src/main/java/GUI/ProjectionHandler.java index 00746ee..60313c1 100644 --- a/src/main/java/GUI/ProjectionHandler.java +++ b/src/main/java/GUI/ProjectionHandler.java @@ -96,11 +96,7 @@ public class ProjectionHandler extends JDialog { slotComboBox.addActionListener(actionEvent -> updateRooms()); cancelButton.addActionListener(actionEvent -> dispose()); confirmButton.addActionListener(actionEvent -> confirm()); - contentPane.registerKeyboardAction(new ActionListener() { - public void actionPerformed(ActionEvent e) { - dispose(); - } - }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + contentPane.registerKeyboardAction(e -> dispose(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); } private void onCancel() { diff --git a/src/main/java/GUI/ProjectionSelecter.java b/src/main/java/GUI/ProjectionSelecter.java index 3a96796..53871dc 100644 --- a/src/main/java/GUI/ProjectionSelecter.java +++ b/src/main/java/GUI/ProjectionSelecter.java @@ -29,17 +29,9 @@ public class ProjectionSelecter extends JDialog { getRootPane().setDefaultButton(buttonOK); ProjectionTableModel pTM = new ProjectionTableModel(projections); table1.setModel(pTM); - buttonOK.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - onOK(); - } - }); + buttonOK.addActionListener(e -> onOK()); - buttonCancel.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - onCancel(); - } - }); + buttonCancel.addActionListener(e -> onCancel()); table1.addMouseListener( new MouseAdapter() { @Override @@ -55,7 +47,7 @@ public class ProjectionSelecter extends JDialog { } } ); - // call onCancel() when cross is clicked + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { @@ -63,20 +55,15 @@ public class ProjectionSelecter extends JDialog { } }); - // call onCancel() on ESCAPE - contentPane.registerKeyboardAction(new ActionListener() { - public void actionPerformed(ActionEvent e) { - onCancel(); - } - }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); } + private void onOK() { agenda.openDialog(ProjectionType.EDIT, currentProjection); dispose(); } private void onCancel() { - // add your code here if necessary dispose(); }