/**
* This file is part of jXo.
*
* Foobar 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 2 of the License, or
* (at your option) any later version.
*
* Foobar 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 Foobar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* 2007 Matej Hausenblas matej.hausenblas@gmail.com
*
*/
package client.gui.panels;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.Icon;
import javax.swing.JPanel;
import client.gui.JXoFrame;
import client.gui.misc.ClientJButton;
import client.gui.misc.ImagesMap;
import common.Point;
import common.TypeConteneur;
/**
* @author Matej Hausenblas matej.hausenblas@gmail.com
*
*/
public class GrillePanel extends JPanel implements ActionListener{
/** proprietaire de ce panel */
private JXoFrame pere;
private int dimension;
/** matrice de boutons composant la grille */
private ClientJButton[][] grille;
/**
* Constructeur du panel de grille
* @param dimension
*/
public GrillePanel(JXoFrame pere,int dimension){
this.pere = pere;
this.dimension = dimension;
this.grille = new ClientJButton[this.dimension][this.dimension];
this.setLayout(new GridLayout(this.dimension, this.dimension));
for(int i = 1; i <= this.dimension; i++){
for(int j = 1; j <= this.dimension; j++){
/* creer un bouton
* lui ajouter un listener
* l'ajouter sur la grille
* l'ajouter dans la matrice de boutons, pour avoir les coordonnees.
*/
ClientJButton unBouton = new ClientJButton(ImagesMap.get(ImagesMap.EMPTY));
unBouton.addActionListener(this);
unBouton.setBloque(false);
this.add(unBouton);
this.grille[i-1][j-1] = unBouton;
}
}
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Object e = arg0.getSource();
for(int i = 0; i < this.dimension; i++){
for(int j = 0; j < this.dimension; j++){
// recherche du bouton qui a declenche cette action.
if(e == this.grille[i][j]){
// on dit au pere d'envoyer le point
try{
// recuperation du champ
ClientJButton bt = this.grille[i][j];
// si le bouton est deja joue, on le bloque.
if(bt.isBloque()){
this.pere.warning("Impossible de jouer cette case !");
}else{
// envoi du point joue.
this.pere.envoyerPoint(i,j);
bt.setBloque(true);
this.disableAllButtons();
this.setImage(bt);
}
// blocage du bouton joue.
// bt.bloquer();
// this.setEnabled(false);
}catch(IOException ioe){
this.pere.warning("Le tour n'a pas ete envoye au serveur!");
}
}
}
}
}
public void setImage(ClientJButton bt){
// recuperation de l'image a mettre sur le champ joue
Icon image;
String typeSymbole = this.pere.getTypeSymbole();
String nomImage = "";
// icone en fonction de ce qu'on joue
if(typeSymbole.equals(TypeConteneur.CROIX)){
nomImage = ImagesMap.CROIX;
}else if(typeSymbole.equals(TypeConteneur.ROND)){
nomImage = ImagesMap.ROND;
}
image = ImagesMap.get(nomImage);
bt.setIcon(image);
}
public void setOtherImage(ClientJButton bt){
// recuperation de l'image a mettre sur le champ joue
Icon image;
String typeSymbole = this.pere.getTypeSymbole();
String nomImage = "";
// icone en fonction de ce qu'on joue
if(typeSymbole.equals(TypeConteneur.CROIX)){
nomImage = ImagesMap.ROND;
}else if(typeSymbole.equals(TypeConteneur.ROND)){
nomImage = ImagesMap.CROIX;
}
image = ImagesMap.get(nomImage);
bt.setIcon(image);
}
public void showPointAdversaire(Point p){
ClientJButton bt = this.grille[p.getX()][p.getY()];
bt.setBloque(true);
this.setOtherImage(bt);
this.enableAllButtons();
}
private void disableAllButtons(){
for(int i = 0; i < this.dimension; i ++){
for(int j = 0; j < this.dimension; j++){
this.grille[i][j].setEnabled(false);
}
}
}
public void enableAllButtons(){
for(int i = 0; i < this.dimension; i ++){
for(int j = 0; j < this.dimension; j++){
this.grille[i][j].setEnabled(true);
}
}
}
public void blockAllButtons(){
for(int i = 0; i < this.dimension; i ++){
for(int j = 0; j < this.dimension; j++){
this.grille[i][j].setEnabled(false);
}
}
}
public void unblockAllButtons(){
for(int i = 0; i < this.dimension; i ++){
for(int j = 0; j < this.dimension; j++){
this.grille[i][j].setEnabled(true);
}
}
}
public void resetGrille(){
for(int i = 0; i < this.dimension; i ++){
for(int j = 0; j < this.dimension; j++){
ClientJButton b = this.grille[i][j];
b.setBloque(false);
b.setEnabled(false);
b.setIcon(ImagesMap.get(ImagesMap.EMPTY));
}
}
}
}