From 839e020f4bbd85a76a970cf7c56538a82e1075b6 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 29 Apr 2011 16:25:30 +0200 Subject: Added Event handling to buttons git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@16 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/view/IPlayerPanel.java | 6 ++++++ src/jrummikub/view/PlayerPanel.java | 42 ++++++++++++++++++++++++++++++++++++ src/jrummikub/view/View.java | 19 ++++++++++++++++ 3 files changed, 67 insertions(+) (limited to 'src/jrummikub/view') diff --git a/src/jrummikub/view/IPlayerPanel.java b/src/jrummikub/view/IPlayerPanel.java index 3330804..79c15a4 100644 --- a/src/jrummikub/view/IPlayerPanel.java +++ b/src/jrummikub/view/IPlayerPanel.java @@ -1,8 +1,14 @@ package jrummikub.view; +import jrummikub.util.IEvent; + public interface IPlayerPanel { public IBoard getBoard(); public void setCurrentPlayerName(String playerName); public void setTimeLeft(int time); + + public IEvent getSortByNumberEvent(); + public IEvent getSortByColorEvent(); + public IEvent getEndTurnEvent(); } \ No newline at end of file diff --git a/src/jrummikub/view/PlayerPanel.java b/src/jrummikub/view/PlayerPanel.java index e9ffff3..385499e 100644 --- a/src/jrummikub/view/PlayerPanel.java +++ b/src/jrummikub/view/PlayerPanel.java @@ -5,6 +5,8 @@ import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JButton; @@ -12,6 +14,9 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JProgressBar; +import jrummikub.util.Event; +import jrummikub.util.IEvent; + @SuppressWarnings("serial") public class PlayerPanel extends JPanel implements IPlayerPanel { private final static DecimalFormat secondFormat = new DecimalFormat("00"); @@ -24,6 +29,10 @@ public class PlayerPanel extends JPanel implements IPlayerPanel { private JProgressBar timeBar; private JButton endTurnButton; + private Event sortByNumberEvent = new Event(); + private Event sortByColorEvent = new Event(); + private Event endTurnEvent = new Event(); + @Override public IBoard getBoard() { @@ -42,6 +51,21 @@ public class PlayerPanel extends JPanel implements IPlayerPanel { timeBar.setString(Integer.toString(time/60) + ":" + secondFormat.format(time%60)); } + @Override + public IEvent getSortByNumberEvent() { + return sortByNumberEvent; + } + + @Override + public IEvent getSortByColorEvent() { + return sortByColorEvent; + } + + @Override + public IEvent getEndTurnEvent() { + return endTurnEvent; + } + JPanel createLeftPanel() { JPanel panel = new JPanel(); @@ -67,6 +91,12 @@ public class PlayerPanel extends JPanel implements IPlayerPanel { sortByNumberButton = new JButton("
Sort by
number"); sortByNumberButton.setPreferredSize(new Dimension(85, 50)); + sortByNumberButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent arg0) { + sortByNumberEvent.fire(); + }}); + c.gridwidth = GridBagConstraints.RELATIVE; c.gridheight = GridBagConstraints.REMAINDER; c.insets = new Insets(15, 0, 20, 5); @@ -75,6 +105,12 @@ public class PlayerPanel extends JPanel implements IPlayerPanel { sortByColorButton = new JButton("
Sort by
color"); sortByColorButton.setPreferredSize(new Dimension(85, 50)); + sortByColorButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent arg0) { + sortByColorEvent.fire(); + }}); + c.gridwidth = GridBagConstraints.REMAINDER; c.insets = new Insets(15, 5, 20, 0); layout.setConstraints(sortByColorButton, c); @@ -103,6 +139,12 @@ public class PlayerPanel extends JPanel implements IPlayerPanel { endTurnButton = new JButton("End turn"); endTurnButton.setPreferredSize(new Dimension(180, 50)); + endTurnButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent arg0) { + endTurnEvent.fire(); + }}); + c.gridheight = GridBagConstraints.REMAINDER; c.insets = new Insets(15, 0, 20, 0); layout.setConstraints(endTurnButton, c); diff --git a/src/jrummikub/view/View.java b/src/jrummikub/view/View.java index 65e26b5..8589e8f 100644 --- a/src/jrummikub/view/View.java +++ b/src/jrummikub/view/View.java @@ -7,6 +7,8 @@ import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.UIManager; +import jrummikub.util.IListener; + @SuppressWarnings("serial") public class View extends JFrame implements IView { private Table table; @@ -45,6 +47,7 @@ public class View extends JFrame implements IView { * @param args */ public static void main(String[] args) { + // FIXME Only test main for GUI preview String nativeLF = UIManager.getSystemLookAndFeelClassName(); try { @@ -60,6 +63,22 @@ public class View extends JFrame implements IView { view.getTable().setTopPlayerName("Player 3"); view.getTable().setRightPlayerName("Player 4"); + view.getPlayerPanel().getSortByNumberEvent().add(new IListener() { + @Override + public void fire() { + System.out.println("'Sort by number' fired"); + }}); + view.getPlayerPanel().getSortByColorEvent().add(new IListener() { + @Override + public void fire() { + System.out.println("'Sort by color' fired"); + }}); + view.getPlayerPanel().getEndTurnEvent().add(new IListener() { + @Override + public void fire() { + System.out.println("'End turn' fired"); + }}); + view.setVisible(true); } } -- cgit v1.2.3