From 0d56ac1e3c91231419e8f3807960d1f4446926c1 Mon Sep 17 00:00:00 2001 From: COLIN Cyril Date: Mon, 14 Oct 2019 14:02:08 +0200 Subject: [PATCH] implement event callbacks --- src/gui/ElevatorApplication.java | 2 +- src/gui/ElevatorPanel.java | 11 ++++++++++- src/gui/FloorPanels.java | 13 +++++++++++++ src/simulation/Simulation.java | 2 ++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/gui/ElevatorApplication.java b/src/gui/ElevatorApplication.java index 8d5dbed..fbf325a 100644 --- a/src/gui/ElevatorApplication.java +++ b/src/gui/ElevatorApplication.java @@ -48,7 +48,7 @@ public class ElevatorApplication implements ActionListener { pane.add(Box.createRigidArea(new Dimension(20, 0))); - ElevatorPanel elevatorPanel = new ElevatorPanel(5); + ElevatorPanel elevatorPanel = new ElevatorPanel(5, simulation); elevatorPanel.setAlignmentY(Component.TOP_ALIGNMENT); pane.add(elevatorPanel); diff --git a/src/gui/ElevatorPanel.java b/src/gui/ElevatorPanel.java index 2d8e6f7..99b78af 100644 --- a/src/gui/ElevatorPanel.java +++ b/src/gui/ElevatorPanel.java @@ -1,20 +1,29 @@ package gui; + import javax.swing.*; import java.awt.event.*; +import commandSystem.ElevatorListener; + @SuppressWarnings("serial") class ElevatorPanel extends JPanel { private JButton emergencyStop = new JButton("Emergency stop."); private JButton[] buttons; - public ElevatorPanel(int nbFloors) { + public ElevatorPanel(int nbFloors, ElevatorListener l) { this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); this.add(new JLabel("Elevator panel.")); buttons = new JButton[nbFloors]; for (int i = nbFloors-1; i >= 0; i--) { + final int j = i; buttons[i] = new JButton("" + i); + buttons[i].addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + l.elevatorCall(j); + } + }); this.add(buttons[i]); } add(emergencyStop); diff --git a/src/gui/FloorPanels.java b/src/gui/FloorPanels.java index 529a4a0..6f3e983 100644 --- a/src/gui/FloorPanels.java +++ b/src/gui/FloorPanels.java @@ -5,6 +5,7 @@ import java.awt.*; import java.awt.event.*; import commandSystem.ElevatorListener; +import commandSystem.Direction; @SuppressWarnings("serial") @@ -22,11 +23,23 @@ class FloorPanels extends JPanel { floors[i].setLayout(new BoxLayout(floors[i], BoxLayout.LINE_AXIS)); floors[i].add(new JLabel("" + i)); if (i < nbFloors - 1) { + final int j = i; JButton upButton = new JButton("↑"); + upButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + l.floorCall(j, Direction.UP); + } + }); floors[i].add(upButton); } if (i > 0) { + final int j = i; JButton downButton = new JButton("↓"); + downButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + l.floorCall(j, Direction.DOWN); + } + }); floors[i].add(downButton); } this.add(floors[i]); diff --git a/src/simulation/Simulation.java b/src/simulation/Simulation.java index f8884bc..cbbec24 100644 --- a/src/simulation/Simulation.java +++ b/src/simulation/Simulation.java @@ -20,9 +20,11 @@ public class Simulation implements ElevatorListener { } public void elevatorCall(int floor) { + System.out.println("elevator call " + floor); } public void floorCall(int floor, Direction direction) { + System.out.println("floor call " + floor + " " + direction); } public void floorChange(Direction d) {