diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-04-29 16:25:30 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-04-29 16:25:30 +0200 |
commit | 839e020f4bbd85a76a970cf7c56538a82e1075b6 (patch) | |
tree | 4240f28ea3467539f2f2ddbe83c1c0fb34f135b2 | |
parent | 5a0be3261a52f6a0849941283487d08212a35983 (diff) | |
download | JRummikub-839e020f4bbd85a76a970cf7c56538a82e1075b6.tar JRummikub-839e020f4bbd85a76a970cf7c56538a82e1075b6.zip |
Added Event handling to buttons
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@16 72836036-5685-4462-b002-a69064685172
-rw-r--r-- | src/jrummikub/util/Event.java | 23 | ||||
-rw-r--r-- | src/jrummikub/util/Event1.java | 23 | ||||
-rw-r--r-- | src/jrummikub/util/IEvent.java | 6 | ||||
-rw-r--r-- | src/jrummikub/util/IEvent1.java | 6 | ||||
-rw-r--r-- | src/jrummikub/util/IListener.java | 5 | ||||
-rw-r--r-- | src/jrummikub/util/IListener1.java | 5 | ||||
-rw-r--r-- | src/jrummikub/view/IPlayerPanel.java | 6 | ||||
-rw-r--r-- | src/jrummikub/view/PlayerPanel.java | 42 | ||||
-rw-r--r-- | src/jrummikub/view/View.java | 19 |
9 files changed, 135 insertions, 0 deletions
diff --git a/src/jrummikub/util/Event.java b/src/jrummikub/util/Event.java new file mode 100644 index 0000000..0b2317f --- /dev/null +++ b/src/jrummikub/util/Event.java @@ -0,0 +1,23 @@ +package jrummikub.util; + +import java.util.HashSet; + +public class Event implements IEvent { + private HashSet<IListener> listeners = new HashSet<IListener>(); + + @Override + public void add(IListener listener) { + listeners.add(listener); + } + + @Override + public void remove(IListener listener) { + listeners.remove(listener); + } + + public void fire() { + for (IListener listener : listeners) { + listener.fire(); + } + } +} diff --git a/src/jrummikub/util/Event1.java b/src/jrummikub/util/Event1.java new file mode 100644 index 0000000..e8ccb43 --- /dev/null +++ b/src/jrummikub/util/Event1.java @@ -0,0 +1,23 @@ +package jrummikub.util; + +import java.util.HashSet; + +public class Event1<T> implements IEvent1<T> { + private HashSet<IListener1<T>> listeners = new HashSet<IListener1<T>>(); + + @Override + public void add(IListener1<T> listener) { + listeners.add(listener); + } + + @Override + public void remove(IListener1<T> listener) { + listeners.remove(listener); + } + + public void fire(T value) { + for (IListener1<T> listener : listeners) { + listener.fire(value); + } + } +} diff --git a/src/jrummikub/util/IEvent.java b/src/jrummikub/util/IEvent.java new file mode 100644 index 0000000..ce0e13d --- /dev/null +++ b/src/jrummikub/util/IEvent.java @@ -0,0 +1,6 @@ +package jrummikub.util; + +public interface IEvent { + public void add(IListener listener); + public void remove(IListener listener); +} diff --git a/src/jrummikub/util/IEvent1.java b/src/jrummikub/util/IEvent1.java new file mode 100644 index 0000000..47075db --- /dev/null +++ b/src/jrummikub/util/IEvent1.java @@ -0,0 +1,6 @@ +package jrummikub.util; + +public interface IEvent1<T> { + public void add(IListener1<T> listener); + public void remove(IListener1<T> listener); +} diff --git a/src/jrummikub/util/IListener.java b/src/jrummikub/util/IListener.java new file mode 100644 index 0000000..37d79c0 --- /dev/null +++ b/src/jrummikub/util/IListener.java @@ -0,0 +1,5 @@ +package jrummikub.util; + +public interface IListener { + public void fire(); +} diff --git a/src/jrummikub/util/IListener1.java b/src/jrummikub/util/IListener1.java new file mode 100644 index 0000000..8dfeaf3 --- /dev/null +++ b/src/jrummikub/util/IListener1.java @@ -0,0 +1,5 @@ +package jrummikub.util; + +public interface IListener1<T> { + public void fire(T value); +} 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("<html><center>Sort by<br>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("<html><center>Sort by<br>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); } } |