Package pdp.scrabble.ihm.action.impl

Source Code of pdp.scrabble.ihm.action.impl.MenuBarActionImpl

package pdp.scrabble.ihm.action.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.input.SAXBuilder;
import pdp.scrabble.Main_old;
import pdp.scrabble.utility.DateString;
import pdp.scrabble.utility.Display;
import pdp.scrabble.ihm.MainFrame;
import pdp.scrabble.ihm.Rules;
import pdp.scrabble.ihm.MainFrame_old;
import pdp.scrabble.ihm.NewGameSelection;
import pdp.scrabble.ihm.action.MenuBarAction;
import static pdp.scrabble.Language.getGameLang;
import static pdp.scrabble.Main_old.RESSOURCES_PATH;
import static pdp.scrabble.Main_old.SEPARATOR;

/** Handle menu bar actions, where are defined called functions.
*/
public class MenuBarActionImpl implements MenuBarAction {

  /** Main frame reference. */
  private MainFrame mainFrame = null;

  /** Date for saving file. */
  private String dateString = null;

  /** Rules directory. */
  public static final String LANGUAGES_PATH =
    RESSOURCES_PATH + "rules" + SEPARATOR;

  /** Create a new menu bar action handler.
   * @param mainFrame main frame reference.
   */
  public MenuBarActionImpl(MainFrame mainFrame) {
    this.mainFrame = mainFrame;
  }

  /** Start a new game (call selection). */
  public void newGame() {
    NewGameSelection newGameSelection = new NewGameSelection(this.mainFrame);
    newGameSelection.create();

    this.dateString = DateString.get();
    this.mainFrame.setEnabled(false);
  }

  /** Launch a stats panel */
 
  public void optionsPanel() {
    JPanel panel = this.mainFrame.getOptionsPanel();
    if(panel.isVisible()) {
      this.mainFrame.getOptionsPanel().setVisible(false);
    }
    else {
      panel.setVisible(true);
    }
  }

  /** Save Turn. */
 
  public void saveTurn() {
    String filenameDir = Main_old.RESSOURCES_PATH + "replays" + Main_old.SEPARATOR;
    String filename = filenameDir + Main_old.SEPARATOR + this.dateString +
    "_replay" + ".xml";

    Document game = null;
    Element root = null;
    File gameSave = new File(filename);
    if (gameSave.exists()) {
      SAXBuilder sxb = new SAXBuilder();

      try {
        game = sxb.build(gameSave);
        root = game.getRootElement();
      }
      catch (Exception e) {
        Display.error("Save", "An error occured during saving to XML !");
      }
    } else {
      root = new Element("Scrabble");
      game = new Document(root);
    }

    this.mainFrame.saveTurn(root);
    // Save file
    try {
      XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
      sortie.output(game, new FileOutputStream(filename));
    }
    catch (IOException e) {
      Display.error("Save", "An error occured during saving to XML !");
    }
  }

  /** Save game. */
 
  public void save() {
    String filenameDir = Main_old.RESSOURCES_PATH + "saves" + Main_old.SEPARATOR;
    String filename = filenameDir + Main_old.SEPARATOR + this.dateString + ".xml";

    if (this.mainFrame.game().isLoading()) {
      File saveFile = this.mainFrame.getSaveFile();
      filename = saveFile.getPath();
    }

    Element root = new Element("Scrabble");
    Document game = new Document(root);

    this.mainFrame.save(root);
    // Save file
    try {
      XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
      sortie.output(game, new FileOutputStream(filename));
    }
    catch (IOException e) {
      Display.error("Save", "An error occured during saving to XML !");
    }
  }

  /** Load game. */
 
  public void load() {
    JFileChooser jf = new JFileChooser(Main_old.RESSOURCES_PATH
        + "saves" + Main_old.SEPARATOR);

    jf.setDialogTitle(getGameLang("Load Game"));

    int choice = jf.showOpenDialog(jf);
    if (choice == JFileChooser.APPROVE_OPTION) {
      File file = jf.getSelectedFile();
      this.mainFrame.getOptionsPanel().getReplayPanel().setVisible(false);
      this.mainFrame.setSaveFile(file);
      this.mainFrame.load(file);
    }
  }

  /* Replay game. */
 
  public void replay() {
    JFileChooser jf = new JFileChooser(
        Main_old.RESSOURCES_PATH + "replays" + Main_old.SEPARATOR);

    jf.setDialogTitle(getGameLang("Replay"));
    int choice = jf.showOpenDialog(jf);

    if (choice == JFileChooser.APPROVE_OPTION) {
      File file = jf.getSelectedFile();
      this.mainFrame.getPlayerPanel().lock();
      this.mainFrame.getOptionsPanel().setVisible(true);
      this.mainFrame.getOptionsPanel().getAction().replay(file);
    }
  }

  /** Display about box. */
 
  public void about() {
    JOptionPane.showMessageDialog(
        null, "Version: " + Main_old.PROGRAM_VERSION + "\n"
        + getGameLang("Authors") + ":\n"
        + "Larrieu-Arguillé Pierre-Alexandre\n"
        + "Mesplet Jérémy\n"
        + "Capelle Thomas\n"
        + "Peluard Jérémie\n",
        Main_old.PROGRAM_NAME, JOptionPane.INFORMATION_MESSAGE);
  }

  /** Exit program. */
 
  public void exit() {
    this.mainFrame.exit();
  }

 
  public void rules() { 
    Rules rules = new Rules(this.mainFrame);
    rules.showRules();
  }
}
TOP

Related Classes of pdp.scrabble.ihm.action.impl.MenuBarActionImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.