/*
* UFO Saved Game Editor
* Copyright (C) 2010 Christopher Davoren
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.rubikscomplex.ufosge.gui;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import net.rubikscomplex.ufosge.data.SavedGame;
import net.rubikscomplex.ufosge.resources.ResourceManager;
/**
*
* @author Chris Davoren
*/
public class SaveEditFrame extends JFrame implements ActionListener, WindowFocusListener, WindowListener {
SavedGame game;
protected GeneralInfoPanel generalInfoPanel;
protected SoldierListPanel soldierListPanel;
protected BaseListPanel baseListPanel;
protected CraftEditPanel craftEditPanel;
protected long saveLastModified;
protected boolean checkReload = true;
public SaveEditFrame(SavedGame game) {
super("UFOSGE - " + game.info.name + " (Game " + game.slot + ", " + game.sourceDirectory + ")");
setIconImage(ResourceManager.getDefaultFrameIcon().getImage());
addWindowFocusListener(this);
addWindowListener(this);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
game.printToConsole();
this.game = game;
saveLastModified = game.lastModifiedTime;
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
JMenuItem saveMenuItem = new JMenuItem("Save", KeyEvent.VK_S);
saveMenuItem.addActionListener(this);
JMenuItem exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
exitMenuItem.addActionListener(this);
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
JMenu editMenu = new JMenu("Edit");
editMenu.setMnemonic(KeyEvent.VK_E);
JMenuItem researchMenuItem = new JMenuItem("Research...", KeyEvent.VK_R);
researchMenuItem.addActionListener(this);
editMenu.add(researchMenuItem);
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic(KeyEvent.VK_H);
JMenuItem aboutMenuItem = new JMenuItem("About...", KeyEvent.VK_A);
aboutMenuItem.addActionListener(this);
helpMenu.add(aboutMenuItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JScrollPane scroller = new JScrollPane(mainPanel);
generalInfoPanel = new GeneralInfoPanel(game);
generalInfoPanel.setBorder(BorderFactory.createTitledBorder("General Info"));
soldierListPanel = new SoldierListPanel(game);
soldierListPanel.setBorder(BorderFactory.createTitledBorder("Soldiers"));
baseListPanel = new BaseListPanel(game);
baseListPanel.setBorder(BorderFactory.createTitledBorder("Bases"));
craftEditPanel = new CraftEditPanel(game);
craftEditPanel.setBorder(BorderFactory.createTitledBorder("Craft"));
mainPanel.add(generalInfoPanel);
mainPanel.add(soldierListPanel);
mainPanel.add(baseListPanel);
mainPanel.add(craftEditPanel);
getContentPane().add(scroller);
pack();
setMinimumSize(getSize());
int locx = ResourceManager.getUserPreferenceInt(ResourceManager.PREF_SAVEEDITFRAME_POSX);
int locy = ResourceManager.getUserPreferenceInt(ResourceManager.PREF_SAVEEDITFRAME_POSY);
if (locx != -1) {
setLocation(locx, locy);
}
else {
setLocationRelativeTo(null);
}
}
public void update() {
craftEditPanel.update();
}
@Override
public void dispose() {
Point loc = getLocation();
ResourceManager.setUserPreferenceInt(ResourceManager.PREF_SAVEEDITFRAME_POSX, loc.x);
ResourceManager.setUserPreferenceInt(ResourceManager.PREF_SAVEEDITFRAME_POSY, loc.y);
super.dispose();
}
public boolean confirmSaveAndExit() {
Object[] options = { "Save", "Don't Save", "Cancel" };
int response = JOptionPane.showOptionDialog(this, "Do you want to save any changes you've made?", "Exit Confirm", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (response == JOptionPane.YES_OPTION) {
try {
update();
game.saveSavedGame(ResourceManager.getUserPreference(ResourceManager.PREF_UFO_DIR));
return true;
}
catch (IOException ioe) {
// JOptionPane.showMessageDialog(this, "Error: " + ioe.getMessage(), "Save Error", JOptionPane.ERROR_MESSAGE);
String[] quitOptions = { "Quit Anyway", "Don't Quit" };
int result = JOptionPane.showOptionDialog(this, "There was an error saving the file: " + ioe.getMessage() + "\n\nQuit anyway?", "Save Error", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, quitOptions, quitOptions[1]);
return (result == JOptionPane.YES_OPTION);
}
}
else if (response == JOptionPane.NO_OPTION) {
return true;
}
return false;
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Save")) {
update();
try {
game.saveSavedGame(ResourceManager.getUserPreference(ResourceManager.PREF_UFO_DIR));
JOptionPane.showMessageDialog(this, "Game saved.", "Save Success", JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException ioe) {
JOptionPane.showMessageDialog(this, "Error: " + ioe.getMessage(), "Save Error", JOptionPane.ERROR_MESSAGE);
}
}
else if (e.getActionCommand().equals("Exit")) {
if(confirmSaveAndExit()) {
this.dispose();
}
}
else if (e.getActionCommand().equals("Research...")) {
ResearchEditDialog red = new ResearchEditDialog(this, game);
red.setVisible(true);
}
else if (e.getActionCommand().equals("About...")) {
AboutDialog aboutDialog = new AboutDialog(this);
aboutDialog.setVisible(true);
}
}
public void windowLostFocus(WindowEvent e) {
}
public void windowGainedFocus(WindowEvent e) {
/* Only respond to external refocus events */
if(e.getOppositeWindow() != null || !checkReload) {
return;
}
if(game.hasBeenModified()) {
String[] options = { "Reload", "Don't Reload", "Never Reload" };
int result = JOptionPane.showOptionDialog(this, "The saved game has been modified outside the editor. Would you like to reload it?", "Reload Game", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (result == JOptionPane.YES_OPTION) {
SavedGame newGame;
try {
newGame = SavedGame.loadSavedGame(new File(game.sourceDirectory).getParent(), game.slot);
this.setVisible(false);
this.dispose();
SaveEditFrame newEditFrame = new SaveEditFrame(newGame);
newEditFrame.setVisible(true);
}
catch(IOException ioe) {
JOptionPane.showMessageDialog(this, "Unable to reload saved game:\n\n" + ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
else if (result == JOptionPane.CANCEL_OPTION) {
checkReload = false;
}
}
}
public void windowClosing(WindowEvent e) {
if(confirmSaveAndExit()) {
this.dispose();
}
}
public void windowOpened(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
}