diff options
Diffstat (limited to 'src/jrummikub/view/impl')
-rw-r--r-- | src/jrummikub/view/impl/View.java | 50 |
1 files changed, 40 insertions, 10 deletions
diff --git a/src/jrummikub/view/impl/View.java b/src/jrummikub/view/impl/View.java index 0bc7120..a3bded6 100644 --- a/src/jrummikub/view/impl/View.java +++ b/src/jrummikub/view/impl/View.java @@ -5,11 +5,13 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; +import java.io.File; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; +import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLayeredPane; import javax.swing.JMenu; @@ -17,13 +19,16 @@ import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.border.MatteBorder; +import javax.swing.filechooser.FileNameExtensionFilter; import jrummikub.model.Position; import jrummikub.model.Stone; import jrummikub.model.StoneColor; import jrummikub.model.StoneSet; import jrummikub.util.Event; +import jrummikub.util.Event1; import jrummikub.util.IEvent; +import jrummikub.util.IEvent1; import jrummikub.util.Pair; import jrummikub.view.IHandPanel; import jrummikub.view.IPlayerPanel; @@ -52,11 +57,14 @@ public class View extends JFrame implements IView { private SettingsPanel settingsPanel; private ScorePanel scorePanel; + private JFileChooser chooser; + private Event menuNewGameEvent = new Event(); - private Event menuLoadEvent = new Event(); - private Event menuSaveEvent = new Event(); private Event menuQuitEvent = new Event(); + private Event1<File> loadEvent = new Event1<File>(); + private Event1<File> saveEvent = new Event1<File>(); + private static int even(double d) { return 2 * (int) (d / 2); } @@ -92,18 +100,25 @@ public class View extends JFrame implements IView { } @Override - public IEvent getMenuLoadEvent() { - return menuLoadEvent; + public IEvent getMenuQuitEvent() { + return menuQuitEvent; } @Override - public IEvent getMenuSaveEvent() { - return menuSaveEvent; + public IEvent1<File> getLoadEvent() { + return loadEvent; } @Override - public IEvent getMenuQuitEvent() { - return menuQuitEvent; + public IEvent1<File> getSaveEvent() { + return saveEvent; + } + + private void createFileChooser() { + chooser = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter( + "JRummikub-Spiele", "rum"); + chooser.setFileFilter(filter); } private void createMenuBar() { @@ -127,7 +142,7 @@ public class View extends JFrame implements IView { loadItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - menuLoadEvent.emit(); + load(); } }); gameMenu.add(loadItem); @@ -135,7 +150,7 @@ public class View extends JFrame implements IView { saveItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - menuSaveEvent.emit(); + save(); } }); gameMenu.add(saveItem); @@ -154,6 +169,20 @@ public class View extends JFrame implements IView { setJMenuBar(menuBar); } + private void load() { + int returnVal = chooser.showOpenDialog(View.this); + if (returnVal == JFileChooser.APPROVE_OPTION) { + loadEvent.emit(chooser.getSelectedFile()); + } + } + + private void save() { + int returnVal = chooser.showSaveDialog(View.this); + if (returnVal == JFileChooser.APPROVE_OPTION) { + saveEvent.emit(chooser.getSelectedFile()); + } + } + /** * Create a new instance of the view */ @@ -164,6 +193,7 @@ public class View extends JFrame implements IView { setSize(1000, 700); setDefaultCloseOperation(EXIT_ON_CLOSE); + createFileChooser(); createMenuBar(); layeredPane = new JLayeredPane(); |