From fc6d3d46db69c639557462a49ccb16463df91617 Mon Sep 17 00:00:00 2001 From: Ida Massow Date: Sun, 19 Jun 2011 22:27:21 +0200 Subject: quit warnings funktionieren git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@492 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/control/ApplicationControl.java | 55 ++++++++++++++-- src/jrummikub/control/SaveControl.java | 26 +++++++- src/jrummikub/view/IQuitWarningPanel.java | 25 +++++++ src/jrummikub/view/IView.java | 34 ++++++---- src/jrummikub/view/impl/QuitWarningPanel.java | 95 +++++++++++++++++++++++++++ src/jrummikub/view/impl/View.java | 60 +++++++++++++++-- 6 files changed, 270 insertions(+), 25 deletions(-) create mode 100644 src/jrummikub/view/IQuitWarningPanel.java create mode 100644 src/jrummikub/view/impl/QuitWarningPanel.java (limited to 'src/jrummikub') diff --git a/src/jrummikub/control/ApplicationControl.java b/src/jrummikub/control/ApplicationControl.java index 2271526..87516c0 100644 --- a/src/jrummikub/control/ApplicationControl.java +++ b/src/jrummikub/control/ApplicationControl.java @@ -5,10 +5,12 @@ import jrummikub.control.network.NetworkControl; import jrummikub.model.GameSettings; import jrummikub.model.GameState; import jrummikub.model.IRoundState; +import jrummikub.util.Connection; import jrummikub.util.IListener; import jrummikub.util.IListener1; import jrummikub.util.IListener3; import jrummikub.util.LoginData; +import jrummikub.view.IQuitWarningPanel.QuitMode; import jrummikub.view.IView; import jrummikub.view.IView.BottomPanelType; @@ -22,6 +24,7 @@ public class ApplicationControl { private NetworkControl networkControl; private SaveControl saveControl; private GameControl gameControl; + private Connection tempConnection; private IView view; @@ -38,14 +41,59 @@ public class ApplicationControl { view.getMenuNewGameEvent().add(new IListener() { @Override public void handle() { - abortControls(); - startApplication(); + view.getQuitWarningPanel().setMode(QuitMode.QUIT_GAME); + view.showQuitWarningPanel(true); + tempConnection = view.getQuitWarningPanel().getQuitEvent().add(new IListener() { + @Override + public void handle() { + abortControls(); + startApplication(); + view.showQuitWarningPanel(false); + tempConnection.remove(); + } + }); } }); view.getMenuQuitEvent().add(new IListener() { @Override public void handle() { - System.exit(0); + if (gameControl == null) { + System.exit(0); + } else { + view.getQuitWarningPanel().setMode(QuitMode.QUIT_PROCESS); + view.showQuitWarningPanel(true); + view.getQuitWarningPanel().getQuitEvent().add(new IListener() { + @Override + public void handle() { + System.exit(0); + } + }); + } + } + }); + + view.getQuitEvent().add(new IListener() { + @Override + public void handle() { + if (gameControl == null) { + System.exit(0); + } else { + view.getQuitWarningPanel().setMode(QuitMode.QUIT_PROCESS); + view.showQuitWarningPanel(true); + view.getQuitWarningPanel().getQuitEvent().add(new IListener() { + @Override + public void handle() { + System.exit(0); + } + }); + } + } + }); + + view.getQuitWarningPanel().getCancelEvent().add(new IListener() { + @Override + public void handle() { + view.showQuitWarningPanel(false); } }); @@ -53,7 +101,6 @@ public class ApplicationControl { saveControl.getLoadEvent().add( new IListener3() { - @Override public void handle(GameSettings settings, GameState gameState, IRoundState roundState) { diff --git a/src/jrummikub/control/SaveControl.java b/src/jrummikub/control/SaveControl.java index 86aaac6..31e7a12 100644 --- a/src/jrummikub/control/SaveControl.java +++ b/src/jrummikub/control/SaveControl.java @@ -9,10 +9,13 @@ import java.io.ObjectOutputStream; import jrummikub.model.GameSettings; import jrummikub.model.GameState; import jrummikub.model.IRoundState; +import jrummikub.util.Connection; import jrummikub.util.Event; import jrummikub.util.Event3; import jrummikub.util.IEvent3; +import jrummikub.util.IListener; import jrummikub.util.IListener1; +import jrummikub.view.IQuitWarningPanel.QuitMode; import jrummikub.view.IView; /** @@ -24,6 +27,7 @@ public class SaveControl { private IRoundState roundState; private Event3 loadEvent = new Event3(); private Event loadErrorEvent = new Event(); + private Connection tempConnection; /** * Creates a new SaveControl @@ -31,7 +35,7 @@ public class SaveControl { * @param view * the view to use */ - public SaveControl(IView view) { + public SaveControl(final IView view) { view.getSaveEvent().add(new IListener1() { @Override public void handle(File file) { @@ -39,9 +43,25 @@ public class SaveControl { } }); - view.getLoadEvent().add(new IListener1() { + view.getLoadEvent().add(new IListener() { @Override - public void handle(File file) { + public void handle() { + view.getQuitWarningPanel().setMode(QuitMode.QUIT_GAME); + view.showQuitWarningPanel(true); + + tempConnection=view.getQuitWarningPanel().getQuitEvent().add(new IListener() { + @Override + public void handle() { + view.showQuitWarningPanel(false); + view.load(); + tempConnection.remove(); + } + }); + } + }); + view.getLoadFileEvent().add(new IListener1() { + @Override + public void handle(final File file) { load(file); } }); diff --git a/src/jrummikub/view/IQuitWarningPanel.java b/src/jrummikub/view/IQuitWarningPanel.java new file mode 100644 index 0000000..eddf6b1 --- /dev/null +++ b/src/jrummikub/view/IQuitWarningPanel.java @@ -0,0 +1,25 @@ +package jrummikub.view; + +import jrummikub.util.Event; +import jrummikub.view.IQuitWarningPanel.QuitMode; + +public interface IQuitWarningPanel { + + public enum QuitMode { + /** */ + DEFAULT, + /** */ + QUIT_PROCESS, + /** */ + QUIT_GAME + } + + Event getQuitEvent(); + + Event getCancelEvent(); + + public void setMode(QuitMode mode); + + public QuitMode getQuitMode(); + +} \ No newline at end of file diff --git a/src/jrummikub/view/IView.java b/src/jrummikub/view/IView.java index 03bddb5..09ab088 100644 --- a/src/jrummikub/view/IView.java +++ b/src/jrummikub/view/IView.java @@ -6,8 +6,10 @@ import java.util.Collection; import jrummikub.model.Stone; import jrummikub.model.StoneSet; +import jrummikub.util.Event; import jrummikub.util.IEvent; import jrummikub.util.IEvent1; +import jrummikub.view.IQuitWarningPanel.QuitMode; /** * The top-level view interface @@ -168,7 +170,7 @@ public interface IView { * * @return the event */ - public IEvent1 getLoadEvent(); + public IEvent getLoadEvent(); /** * The save event is emitted when the user wants to save the current game @@ -281,6 +283,22 @@ public interface IView { */ public void setInvalidStoneSets(Collection sets); + /** + * Show when loading is unsuccessful + */ + public void showLoadingError(); + + /** + * Enables/disables saving in menu bar + * + * @param enable + */ + public void enableSave(boolean enable); + + public void showQuitWarningPanel(boolean show); + + public IEvent getQuitEvent(); + /** * Different types of bottom panels */ @@ -299,15 +317,9 @@ public interface IView { WIN_PANEL } - /** - * Show when loading is unsuccessful - */ - void showLoadingError(); + public IQuitWarningPanel getQuitWarningPanel(); - /** - * Enables/disables saving in menu bar - * - * @param enable - */ - void enableSave(boolean enable); + public IEvent1 getLoadFileEvent(); + + public void load(); } diff --git a/src/jrummikub/view/impl/QuitWarningPanel.java b/src/jrummikub/view/impl/QuitWarningPanel.java new file mode 100644 index 0000000..d7a304a --- /dev/null +++ b/src/jrummikub/view/impl/QuitWarningPanel.java @@ -0,0 +1,95 @@ +package jrummikub.view.impl; + +import java.awt.Color; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.SwingConstants; +import javax.swing.border.CompoundBorder; +import javax.swing.border.EmptyBorder; +import javax.swing.border.LineBorder; + +import jrummikub.util.Event; +import jrummikub.view.IQuitWarningPanel; + +public class QuitWarningPanel extends JPanel implements IQuitWarningPanel { + private JPanel buttonPanel; + private JButton cancelButton; + private JButton quitButton; + private QuitMode quitMode; + private Event quitEvent = new Event(); + private Event cancelEvent = new Event(); + private JLabel messageLabel; + + public QuitWarningPanel() { + setLayout(new GridBagLayout()); + + GridBagConstraints c = new GridBagConstraints(); + c.gridx = 0; + c.gridy = 0; + c.weightx = 1; + c.weighty = 1; + c.fill = GridBagConstraints.BOTH; + c.gridwidth = 2; + c.anchor = GridBagConstraints.CENTER; + + messageLabel = new JLabel(); + messageLabel.setHorizontalAlignment(SwingConstants.CENTER); + add(messageLabel, c); + + c.gridy = 1; + c.gridwidth = 1; + quitButton = createButton("Beenden", quitEvent); + cancelButton = createButton("Abbrechen", cancelEvent); + add(cancelButton, c); + c.gridx = 1; + add(quitButton, c); + + setBorder(new CompoundBorder(new LineBorder(Color.BLACK), + new EmptyBorder(10, 10, 10, 10))); + } + + private JButton createButton(String title, final Event event) { + JButton button = new JButton(title); + button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + event.emit(); + } + }); + return button; + } + + @Override + public void setMode(QuitMode mode) { + this.quitMode = mode; + switch (quitMode) { + case QUIT_PROCESS: + messageLabel + .setText("Beim Beenden geht das aktuelle Spiel verloren"); + break; + case QUIT_GAME: + messageLabel.setText("Der aktuelle Spielstand geht verloren"); + } + } + + @Override + public QuitMode getQuitMode(){ + return quitMode; + } + + @Override + public Event getQuitEvent() { + return quitEvent; + } + + @Override + public Event getCancelEvent() { + return cancelEvent; + } +} diff --git a/src/jrummikub/view/impl/View.java b/src/jrummikub/view/impl/View.java index dee5352..042421f 100644 --- a/src/jrummikub/view/impl/View.java +++ b/src/jrummikub/view/impl/View.java @@ -6,6 +6,8 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.io.File; import java.util.Arrays; import java.util.Collection; @@ -37,6 +39,8 @@ import jrummikub.view.IGameListPanel; import jrummikub.view.IHandPanel; import jrummikub.view.ILoginPanel; import jrummikub.view.IPlayerPanel; +import jrummikub.view.IQuitWarningPanel; +import jrummikub.view.IQuitWarningPanel.QuitMode; import jrummikub.view.IScorePanel; import jrummikub.view.ISettingsPanel; import jrummikub.view.ISidePanel; @@ -67,6 +71,7 @@ public class View extends JFrame implements IView { private ScorePanel scorePanel; private GameListPanel gameListPanel; private SidePanel sidePanel; + private QuitWarningPanel quitWarningPanel; private BottomPanelType bottomPanelType; @@ -76,8 +81,10 @@ public class View extends JFrame implements IView { private Event menuQuitEvent = new Event(); private Event networkGameEvent = new Event(); - private Event1 loadEvent = new Event1(); + private Event loadEvent = new Event(); + private Event1 loadFileEvent = new Event1(); private Event1 saveEvent = new Event1(); + private Event quitEvent = new Event(); private static int even(double d) { return 2 * (int) (d / 2); @@ -118,6 +125,11 @@ public class View extends JFrame implements IView { return playerPanel; } + @Override + public IQuitWarningPanel getQuitWarningPanel() { + return quitWarningPanel; + } + @Override public IGameListPanel getGameListPanel() { return gameListPanel; @@ -134,7 +146,12 @@ public class View extends JFrame implements IView { } @Override - public IEvent1 getLoadEvent() { + public IEvent1 getLoadFileEvent() { + return loadFileEvent; + } + + @Override + public IEvent getLoadEvent(){ return loadEvent; } @@ -158,6 +175,11 @@ public class View extends JFrame implements IView { return networkGameEvent; } + @Override + public IEvent getQuitEvent() { + return quitEvent; + } + @Override public void clearView() { showScorePanel(false); @@ -201,7 +223,8 @@ public class View extends JFrame implements IView { loadItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - load(); + File file = new File(""); + loadEvent.emit(); } }); gameMenu.add(loadItem); @@ -238,10 +261,11 @@ public class View extends JFrame implements IView { saveItem.setEnabled(enable); } - private void load() { + @Override + public void load() { int returnVal = chooser.showOpenDialog(View.this); if (returnVal == JFileChooser.APPROVE_OPTION) { - loadEvent.emit(chooser.getSelectedFile()); + loadFileEvent.emit(chooser.getSelectedFile()); } } @@ -261,7 +285,14 @@ public class View extends JFrame implements IView { setSize(1000, 700); setMinimumSize(new Dimension(750, 550)); - setDefaultCloseOperation(EXIT_ON_CLOSE); + + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + quitEvent.emit(); + } + }); createFileChooser(); createMenuBar(); @@ -289,6 +320,12 @@ public class View extends JFrame implements IView { layeredPane.setLayer(gameListPanel, JLayeredPane.POPUP_LAYER); layeredPane.add(gameListPanel); + quitWarningPanel = new QuitWarningPanel(); + quitWarningPanel.setVisible(false); + + layeredPane.setLayer(quitWarningPanel, JLayeredPane.POPUP_LAYER); + layeredPane.add(quitWarningPanel); + scorePanel = new ScorePanel(); scorePanel.setVisible(false); layeredPane.setLayer(scorePanel, JLayeredPane.POPUP_LAYER); @@ -346,6 +383,10 @@ public class View extends JFrame implements IView { winPanel.setVisible(false); mainLayer.add(winPanel); + quitWarningPanel = new QuitWarningPanel(); + quitWarningPanel.setVisible(false); + mainLayer.add(quitWarningPanel); + sidePanel = new SidePanel(); sidePanel.setVisible(false); mainLayer.add(sidePanel); @@ -394,7 +435,8 @@ public class View extends JFrame implements IView { rescaleSubpanel(scorePanel, 3 / 4.0, 1 / 2.0, 450, 300); rescaleSubpanel(loginPanel, 1 / 3.0, 1 / 3.0, 200, 200); rescaleSubpanel(gameListPanel, 1 / 2.0, 1 / 2.0, 475, 300); - } + rescaleSubpanel(quitWarningPanel, 1 / 2.0, 1 / 6.0, 400, 150); +} private void rescaleSubpanel(JPanel sub, double widthFactor, double heightFactor, int minWidth, int minHeight) { @@ -454,6 +496,10 @@ public class View extends JFrame implements IView { rescale(); } + public void showQuitWarningPanel(boolean show) { + quitWarningPanel.setVisible(show); + } + @Override public void setCurrentPlayerName(String playerName) { playerPanel.setCurrentPlayerName(playerName); -- cgit v1.2.3