aboutsummaryrefslogtreecommitdiff
path: root/src/esieequest/view/app/UserInterface.java
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2014-05-24 21:31:11 +0200
committerPacien TRAN-GIRARD2014-05-24 21:31:11 +0200
commitd06c60a6ccc4d97df62b19e91f18d4aa292d9c57 (patch)
treed4a8407181216c2add2bd52cf3e48f93fd5108f5 /src/esieequest/view/app/UserInterface.java
parentd2f160a26bc9899c2646857ae321453346603391 (diff)
downloadesieequest-d06c60a6ccc4d97df62b19e91f18d4aa292d9c57.tar.gz
Better load/save
Diffstat (limited to 'src/esieequest/view/app/UserInterface.java')
-rw-r--r--src/esieequest/view/app/UserInterface.java63
1 files changed, 62 insertions, 1 deletions
diff --git a/src/esieequest/view/app/UserInterface.java b/src/esieequest/view/app/UserInterface.java
index 0f426bd..b00673c 100644
--- a/src/esieequest/view/app/UserInterface.java
+++ b/src/esieequest/view/app/UserInterface.java
@@ -12,8 +12,12 @@ import java.awt.event.FocusEvent;
12import java.awt.event.FocusListener; 12import java.awt.event.FocusListener;
13import java.awt.event.KeyEvent; 13import java.awt.event.KeyEvent;
14import java.awt.image.BufferedImage; 14import java.awt.image.BufferedImage;
15import java.io.FileWriter;
15import java.io.IOException; 16import java.io.IOException;
16import java.net.URL; 17import java.net.URL;
18import java.nio.file.Files;
19import java.nio.file.Path;
20import java.nio.file.Paths;
17import java.util.HashMap; 21import java.util.HashMap;
18import java.util.Map.Entry; 22import java.util.Map.Entry;
19import java.util.Timer; 23import java.util.Timer;
@@ -22,12 +26,14 @@ import java.util.TimerTask;
22import javax.swing.AbstractAction; 26import javax.swing.AbstractAction;
23import javax.swing.JButton; 27import javax.swing.JButton;
24import javax.swing.JComponent; 28import javax.swing.JComponent;
29import javax.swing.JFileChooser;
25import javax.swing.JLabel; 30import javax.swing.JLabel;
26import javax.swing.JPanel; 31import javax.swing.JPanel;
27import javax.swing.JTextField; 32import javax.swing.JTextField;
28import javax.swing.JTextPane; 33import javax.swing.JTextPane;
29import javax.swing.KeyStroke; 34import javax.swing.KeyStroke;
30import javax.swing.border.EmptyBorder; 35import javax.swing.border.EmptyBorder;
36import javax.swing.filechooser.FileNameExtensionFilter;
31 37
32import lombok.Getter; 38import lombok.Getter;
33 39
@@ -62,6 +68,9 @@ abstract class UserInterface implements Viewable, ActionListener {
62 private static final String SOUND_DIR = "res/snd/"; 68 private static final String SOUND_DIR = "res/snd/";
63 private static final String SOUND_EXT = ".ogg"; 69 private static final String SOUND_EXT = ".ogg";
64 70
71 private static final String SAVE_LABEL = "ESIEEquest Game";
72 private static final String SAVE_EXT = ".eqg";
73
65 private GameEngine gameEngine; 74 private GameEngine gameEngine;
66 75
67 @Getter 76 @Getter
@@ -154,6 +163,7 @@ abstract class UserInterface implements Viewable, ActionListener {
154 163
155 this.soundButton = new JButton(Text.TOGGLE_SOUND_BUTTON.toString()); 164 this.soundButton = new JButton(Text.TOGGLE_SOUND_BUTTON.toString());
156 this.soundButton.setToolTipText(Text.TOGGLE_SOUND_TOOLTIP.toString()); 165 this.soundButton.setToolTipText(Text.TOGGLE_SOUND_TOOLTIP.toString());
166 this.newButton.setActionCommand(Command.SOUND.name());
157 this.gamePanel.add(this.soundButton); 167 this.gamePanel.add(this.soundButton);
158 168
159 this.filePanel = new JPanel(); 169 this.filePanel = new JPanel();
@@ -232,6 +242,7 @@ abstract class UserInterface implements Viewable, ActionListener {
232 242
233 this.inventoryButton = new JButton(Text.INVENTORY_BUTTON.toString()); 243 this.inventoryButton = new JButton(Text.INVENTORY_BUTTON.toString());
234 this.inventoryButton.setToolTipText(Text.INVENTORY_TOOLTIP.toString()); 244 this.inventoryButton.setToolTipText(Text.INVENTORY_TOOLTIP.toString());
245 this.inventoryButton.setActionCommand(Command.INVENTORY.name());
235 this.inventoryButton.setFont(font); 246 this.inventoryButton.setFont(font);
236 this.inventoryButton.setPreferredSize(squareButton); 247 this.inventoryButton.setPreferredSize(squareButton);
237 this.topControlPanel.add(this.inventoryButton, BorderLayout.WEST); 248 this.topControlPanel.add(this.inventoryButton, BorderLayout.WEST);
@@ -283,6 +294,7 @@ abstract class UserInterface implements Viewable, ActionListener {
283 this.newButton.addActionListener(actionListener); 294 this.newButton.addActionListener(actionListener);
284 this.soundButton.addActionListener(actionListener); 295 this.soundButton.addActionListener(actionListener);
285 this.loadButton.addActionListener(actionListener); 296 this.loadButton.addActionListener(actionListener);
297 this.saveButton.addActionListener(actionListener);
286 this.forwardButton.addActionListener(actionListener); 298 this.forwardButton.addActionListener(actionListener);
287 this.inventoryButton.addActionListener(actionListener); 299 this.inventoryButton.addActionListener(actionListener);
288 this.actionButton.addActionListener(actionListener); 300 this.actionButton.addActionListener(actionListener);
@@ -535,10 +547,31 @@ abstract class UserInterface implements Viewable, ActionListener {
535 */ 547 */
536 @Override 548 @Override
537 public void actionPerformed(final ActionEvent actionEvent) { 549 public void actionPerformed(final ActionEvent actionEvent) {
538 if (actionEvent.getActionCommand() == Text.INVENTORY_BUTTON.toString()) { 550 if (actionEvent.getActionCommand() == Command.INVENTORY.name()) {
539 this.toggleInventory(); 551 this.toggleInventory();
540 } else if (actionEvent.getActionCommand() == Text.TOGGLE_SOUND_BUTTON.toString()) { 552 } else if (actionEvent.getActionCommand() == Text.TOGGLE_SOUND_BUTTON.toString()) {
541 this.toggleAudio(); 553 this.toggleAudio();
554 } else if (actionEvent.getActionCommand() == Command.LOAD.name()) {
555 final JFileChooser fileChooser = new JFileChooser();
556 fileChooser.setFileFilter(new FileNameExtensionFilter(UserInterface.SAVE_LABEL,
557 UserInterface.SAVE_EXT));
558 final int option = fileChooser.showOpenDialog(this.layout);
559 if (option == JFileChooser.APPROVE_OPTION) {
560 this.gameEngine.interpret(actionEvent.getActionCommand() + Text.COMMAND_SEPARATOR
561 + fileChooser.getSelectedFile().getPath());
562 }
563 } else if (actionEvent.getActionCommand() == Command.SAVE.name()) {
564 final JFileChooser fileChooser = new JFileChooser();
565 fileChooser.setFileFilter(new FileNameExtensionFilter(UserInterface.SAVE_LABEL,
566 UserInterface.SAVE_EXT));
567 final int option = fileChooser.showSaveDialog(this.layout);
568 if (option == JFileChooser.APPROVE_OPTION) {
569 final String path = fileChooser.getSelectedFile().getPath();
570 final String filePath = path.endsWith(UserInterface.SAVE_EXT) ? path : path
571 + UserInterface.SAVE_EXT;
572 this.gameEngine.interpret(actionEvent.getActionCommand() + Text.COMMAND_SEPARATOR
573 + filePath);
574 }
542 } else { 575 } else {
543 this.gameEngine.interpret(actionEvent.getActionCommand()); 576 this.gameEngine.interpret(actionEvent.getActionCommand());
544 this.clearInputField(); 577 this.clearInputField();
@@ -640,4 +673,32 @@ abstract class UserInterface implements Viewable, ActionListener {
640 } 673 }
641 } 674 }
642 675
676 @Override
677 public void toggleSound() {
678 return;
679 }
680
681 @Override
682 public String load(final String path) {
683 final Path filePath = Paths.get(path);
684 String serialisedGame = null;
685 try {
686 serialisedGame = new String(Files.readAllBytes(filePath));
687 } catch (final IOException e) {
688 e.printStackTrace();
689 }
690 return serialisedGame;
691 }
692
693 @Override
694 public void save(final String path, final String serialisedGame) {
695 try {
696 final FileWriter fileWriter = new FileWriter(path);
697 fileWriter.write(serialisedGame);
698 fileWriter.close();
699 } catch (final IOException e) {
700 e.printStackTrace();
701 }
702 }
703
643} 704}