package pdp.scrabble.ihm;
import static javax.swing.JOptionPane.YES_NO_OPTION;
import static javax.swing.JOptionPane.YES_OPTION;
import static javax.swing.JOptionPane.showConfirmDialog;
import static pdp.scrabble.Language.getGameLang;
import static pdp.scrabble.Language.getMessagesLang;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.TreeMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import pdp.scrabble.Background;
import pdp.scrabble.InfiniteProgressPanel;
import pdp.scrabble.Main_old;
import pdp.scrabble.game.GameEngine;
import pdp.scrabble.game.GameEnvironment;
import pdp.scrabble.game.Player;
import pdp.scrabble.game.impl.Human;
public class MainFrame extends JFrame{
private static final long serialVersionUID = 1L;
/** List of all panels. */
private TreeMap<String, JPanel> panels = null;
/** Menu bar reference. */
private MenuBar menuBar = null;
/** Background panel used on loading. */
private JPanel background = null;
/** Frame language. */
@SuppressWarnings("unused")
private String lang = null;
/** Progress loader reference. */
private InfiniteProgressPanel progress = null;
/** Center panel reference. */
private JPanel centerPanel = null;
/** Option panel reference. */
private JPanel options = null;
private GraphGameEnv gameEnv=null;
private GameEngine gameEng=null;
/** Create a new main frame.
* @param language default language, null for input choice.
*/
public MainFrame(String language, GraphGameEnv ge) {
super(Main_old.PROGRAM_NAME + " " + Main_old.PROGRAM_VERSION);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setLayout(new BorderLayout());
this.setResizable(false);
this.background = new Background();
this.add(this.background);
this.pack();
this.progress = new InfiniteProgressPanel();
this.setGlassPane(this.progress);
this.gameEnv=ge;
this.validate();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void initialize(){
// Set default close operation (when clicking on exit)
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exit();
}
});
this.panels = new TreeMap<String, JPanel>();
this.menuBar = new MenuBar(this);
this.menuBar.create();
// Create center panel (containing board and multi)
this.centerPanel = new JPanel();
this.centerPanel.setLayout(new FlowLayout());
this.add(this.centerPanel, BorderLayout.CENTER);
// Board panel (in centerPanel)
JPanel boardpanel = new BoardPanel(this, this.gameEnv);
this.centerPanel.add(boardpanel);
this.panels.put("board", boardpanel);
//Create option panel (containing replay and stats)
this.options = new OptionsPanel(this, this.gameEnv);
this.options.setVisible(false);
this.centerPanel.add(this.options);
this.panels.put("options", this.options);
// Multiplayer panel (in centerPanel)
JPanel multiplayer = new MultiplayerPanel(this, this.gameEnv);
multiplayer.setVisible(false);
this.centerPanel.add(multiplayer);
this.panels.put("multiplayer", multiplayer);
// Create down panel (containing player planel and score)
JPanel downPanel = new JPanel();
downPanel.setLayout(new BorderLayout());
this.add(downPanel, BorderLayout.SOUTH);
// Player panel (in downPanel)
JPanel player = new PlayerPanel(this.gameEnv);
downPanel.add(player, BorderLayout.CENTER);
this.panels.put("player", player);
// Create stats panel (in downPanel)
JPanel stats = new StatsPanel();
downPanel.add(stats, BorderLayout.SOUTH);
this.panels.put("stats", stats);
}
public void createSoloBasedGame(String name, GameEngine gameEng) {
this.gameEng=gameEng;
this.reset();
this.getOptionsPanel().setVisible(true);
//Player player = FACTORY.createPlayer(this.game, name, 0, false, null);
Player player = new Human(name, 0, gameEnv);
player.getRack().fill();
//player.initialize();
this.gameEnv.addPlayer(player);
this.getPlayerPanel().createScores();
PlayerPanel ppanel = this.getPlayerPanel();
ppanel.getButton("Validate").setEnabled(false);
ppanel.getButton("Cancel").setEnabled(false);
ppanel.getButton("Reset").setEnabled(false);
ppanel.action().clearLettersToReset();
this.setEnabled(true);
gameEng.start();
}
public OptionsPanel getOptionsPanel() {
return (OptionsPanel) this.getPanel("options");
}
public JPanel getPanel(String name) {
return this.panels.get(name);
}
public PlayerPanel getPlayerPanel() {
return (PlayerPanel) this.getPanel("player");
}
/** Get menubar.
* @return menubar reference.
*/
public MenuBar getMenubar() {
return this.menuBar;
}
public MultiplayerPanel getMultiplayerPanel() {
return (MultiplayerPanel) this.getPanel("multiplayer");
}
public GameEngine getGameEng(){
return gameEng;
}
public GameEnvironment getGameEnv(){
return gameEnv;
}
public void reset() {
this.centerPanel.remove(this.options);
this.panels.remove("options");
this.options = new OptionsPanel(this, this.gameEnv);
this.options.setVisible(true);
this.centerPanel.add(this.options);
this.panels.put("options", this.options);
this.centerPanel.validate();
}
/** Terminate program. */
public void exit() {
int answer = showConfirmDialog(
null, getMessagesLang("Do you really want to exit ?"),
getGameLang("Exit"), YES_NO_OPTION);
if (answer == YES_OPTION) {
System.exit(0);
}
}
}