package com.google.code.timetrail.gui;
import java.io.IOException;
import javax.swing.JPanel;
import com.google.code.timetrail.backend.Control;
import com.google.code.timetrail.backend.GameSave;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class SaveGameFrame extends JPanel{
private static final long serialVersionUID = -3644775856538319019L;
private Control gameControl;
private JPanel myCD;
GameSave saveGame;
JList list;
public SaveGameFrame(Control myControl, JPanel cd) throws ClassNotFoundException, IOException {
saveGame = new GameSave();
this.gameControl = myControl;
this.myCD = cd;
setLayout(null);
list = new JList(saveGame.listOfEntries());
list.setBounds(121, 80, 201, 155);
list.setVisibleRowCount(4);
JScrollPane scrollBar = new JScrollPane(list);
scrollBar.setBounds(77, 80, 279, 155);
add(scrollBar);
JButton btnBack = new JButton("Back");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myCD.add(new InventoryFrame(gameControl, myCD), "TakeMove");
CardLayout cd = (CardLayout) myCD.getLayout();
cd.last(myCD);
}
});
btnBack.setBounds(77, 266, 89, 23);
add(btnBack);
JButton btnLoad = new JButton("New Save");
btnLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
GameSave saveGame = new GameSave();
saveGame.saveGame(gameControl, gameControl.getPlayer().getName());
} catch (ClassNotFoundException s) {
s.printStackTrace();
} catch (IOException s) {
s.printStackTrace();
}
try {
myCD.add(new SaveGameFrame(gameControl, myCD), "TakeMove");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
CardLayout cd = (CardLayout) myCD.getLayout();
cd.last(myCD);
}
});
btnLoad.setBounds(176, 266, 89, 23);
add(btnLoad);
JButton btnNewButton = new JButton("Overwrite");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Control loadedGame = new Control();
int selectedIndex = list.getSelectedIndex();
String[] saveFile = new String[2];
try {
if(selectedIndex >= 0){
saveFile = saveGame.listOfEntries()[selectedIndex].split(" ");
String saveName = "";
String saveDate = "";
int count = 0;
for(String word: saveFile){
if(count < saveFile.length - 2){
saveName += word;
if(count < saveFile.length - 3){
saveName += " ";
}
} else {
saveDate += word;
if(count < saveFile.length - 1){
saveDate += " ";
}
}
count++;
}
saveFile[0] = saveName;
saveFile[1] = saveDate;
}
//This assumes the name is not two words
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if(selectedIndex >= 0){
saveGame.overwriteGame(saveFile[0], gameControl, saveFile[1]);
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
myCD.add(new SaveGameFrame(gameControl, myCD), "TakeMove");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
CardLayout cd = (CardLayout) myCD.getLayout();
cd.last(myCD);
}
});
btnNewButton.setBounds(275, 266, 89, 23);
add(btnNewButton);
}
}