diff options
author | Pacien TRAN-GIRARD | 2014-05-24 21:31:11 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2014-05-24 21:31:11 +0200 |
commit | d06c60a6ccc4d97df62b19e91f18d4aa292d9c57 (patch) | |
tree | d4a8407181216c2add2bd52cf3e48f93fd5108f5 /src/esieequest/view/app/UserInterface.java | |
parent | d2f160a26bc9899c2646857ae321453346603391 (diff) | |
download | esieequest-d06c60a6ccc4d97df62b19e91f18d4aa292d9c57.tar.gz |
Better load/save
Diffstat (limited to 'src/esieequest/view/app/UserInterface.java')
-rw-r--r-- | src/esieequest/view/app/UserInterface.java | 63 |
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; | |||
12 | import java.awt.event.FocusListener; | 12 | import java.awt.event.FocusListener; |
13 | import java.awt.event.KeyEvent; | 13 | import java.awt.event.KeyEvent; |
14 | import java.awt.image.BufferedImage; | 14 | import java.awt.image.BufferedImage; |
15 | import java.io.FileWriter; | ||
15 | import java.io.IOException; | 16 | import java.io.IOException; |
16 | import java.net.URL; | 17 | import java.net.URL; |
18 | import java.nio.file.Files; | ||
19 | import java.nio.file.Path; | ||
20 | import java.nio.file.Paths; | ||
17 | import java.util.HashMap; | 21 | import java.util.HashMap; |
18 | import java.util.Map.Entry; | 22 | import java.util.Map.Entry; |
19 | import java.util.Timer; | 23 | import java.util.Timer; |
@@ -22,12 +26,14 @@ import java.util.TimerTask; | |||
22 | import javax.swing.AbstractAction; | 26 | import javax.swing.AbstractAction; |
23 | import javax.swing.JButton; | 27 | import javax.swing.JButton; |
24 | import javax.swing.JComponent; | 28 | import javax.swing.JComponent; |
29 | import javax.swing.JFileChooser; | ||
25 | import javax.swing.JLabel; | 30 | import javax.swing.JLabel; |
26 | import javax.swing.JPanel; | 31 | import javax.swing.JPanel; |
27 | import javax.swing.JTextField; | 32 | import javax.swing.JTextField; |
28 | import javax.swing.JTextPane; | 33 | import javax.swing.JTextPane; |
29 | import javax.swing.KeyStroke; | 34 | import javax.swing.KeyStroke; |
30 | import javax.swing.border.EmptyBorder; | 35 | import javax.swing.border.EmptyBorder; |
36 | import javax.swing.filechooser.FileNameExtensionFilter; | ||
31 | 37 | ||
32 | import lombok.Getter; | 38 | import 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 | } |