/**
* 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.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import client.gui.dialogs.ConnexionDialog;
import client.gui.misc.GU;
/**
*
*/
public class MainConnexionPanel extends JPanel implements ActionListener{
/** type d'action a entreprendre en cas d'appui sur un bouton
* Par defaut 1 = connexion
* -1 = reconnexion.
*/
private int CONNECT_ACTION = 1;
/** liste des jeux ouverts */
// private JList listeSalons;
private ConnexionDialog pere;
private JButton btConnex;
private JButton btAnnuler;
private JTextField nomUser;
private JTextField hostName;
// private JScrollPane port;
private JSpinner port;
// private JTextField nbCasesSpinner;
// private JSpinner nbCasesSpinner;
/**
* Creation du panel de connexion
*/
public MainConnexionPanel(ConnexionDialog owner) {
this.pere = owner;
// this.listeSalons = new JList();
this.setLayout(new BorderLayout());
// this.add(listeSalons, BorderLayout.CENTER);
// GridLayout grid = new GridLayout(0,1);
GridLayout grid = new GridLayout(0,2);
grid.setVgap(2);
grid.setHgap(2);
JPanel contents = new JPanel(grid);
// JPanel userInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel nom = new JLabel("Nom du joueur :");
String sysUserName ;
try{
sysUserName = System.getProperty("user.name");
}catch(Exception e){
sysUserName = "joueur";
}
nomUser = new JTextField(sysUserName,10);
// userInfo.add(nom);
// userInfo.add(nomUser);
// JPanel serverInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel host = new JLabel("Adresse du serveur: ");
hostName = new JTextField("localhost",10);
// serverInfo.add(host);
// serverInfo.add(hostName);
// JPanel portInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
// port = new JTextField(6);
// JLabel portNo = new JLabel("Numero de port");
// port = this.createSpinner(false, 1, 65535, 7050, 1);
// portInfo.add(port);
// portInfo.add(portNo);
// JLabel cases = new JLabel("Nombre de cases");
// nbCasesSpinner = this.createSpinner(false, 3, 36, 3, 3);
// JPanel casesInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
// casesInfo.add(cases);
// casesInfo.add(nbCasesSpinner);
// contents.add(userInfo);
// contents.add(serverInfo);
// contents.add(portInfo);
// contents.add(casesInfo);
// informations de base.
contents.add(nom);
contents.add(nomUser);
contents.add(host);
contents.add(hostName);
// contents.add(portNo);
// contents.add(port);
// contents.add(cases);
// contents.add(nbCasesSpinner);
JPanel boutons = new JPanel(new FlowLayout(FlowLayout.TRAILING));
btConnex = new JButton("Connecter");
btAnnuler = new JButton("Annuler");
boutons.add(btConnex);
boutons.add(btAnnuler);
this.add(boutons, BorderLayout.SOUTH);
this.add(contents, BorderLayout.CENTER);
btConnex.addActionListener(this);
btAnnuler.addActionListener(this);
}
/**
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if(o == btConnex){
// int tailleGrille = ((Integer)nbCasesSpinner.getValue()).intValue();
// this.owner.setGridSize(tailleGrille);
String userN = nomUser.getText();
String hostN = hostName.getText();
// int portN = ((Integer)port.getValue()).intValue();
if(userN.equals("")){
GU.warn("Le champ nom d'utilisateur est vide!");
return;
}
if(hostN.equals("")){
GU.warn("Le champ de nom de serveur");
return;
}
this.pere.getPere().setHostName(hostN);
this.pere.getPere().setUserName(userN);
/*
* L'idee est que si on a evoque ce dialogue a nouveau, on se reconnecte
* sinon on est en train de lancer le programme, alors on laisse la connexion
* au serveur aux soins du JxoClient qui sera cree.
*/
if(this.pere.getPere().getCONNECT_ACTION() == ConnexionDialog.RECONNECT) {
// reconnexion a un (autre) serveur avec un (autre) pseudo.
try{
System.out.println("[DialogCx] reconnect!");
this.pere.getPere().reconnect();
}catch(UnknownHostException uhe){
GU.warn("The host "+hostN+ " is not reachable!");
}catch(IOException ioe){
GU.warn("Une erreur est apparue lors de la creation de la connexion.");
}
}
// fermeture de la fenetre
this.pere.setVisible(false);
}else if(o == btAnnuler){
System.out.println("Pas de poursuite du jeu.");
this.pere.getPere().setCONNECT_ACTION(ConnexionDialog.RECONNECT);
this.pere.setVisible(false);
}else{
System.out.println("nothing will happen");
}
}
/**
* Créer et renvoie un <code>JSpinner</code> pour choisir le nombre de
* paves dans le carre de jeu
*
* @author Brahima@gmail.com
*
* @return Le <code>JSpinner</code> crée
*/
private JSpinner createSpinner(boolean modifiable, int min,
int max, int initValue, int step) {
SpinnerModel model = new SpinnerNumberModel(initValue, min, max, step);
JSpinner spinner = new JSpinner(model);
// Disable keyboard edits in the spinner
JFormattedTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor())
.getTextField();
tf.setEditable(modifiable);
tf.setBackground(Color.white);
return spinner;
}
/**
* @param connect_action the cONNECT_ACTION to set
*/
// public void setCONNECT_ACTION(int connect_action) {
// this.CONNECT_ACTION = connect_action;
// }
}