Package xunome

Source Code of xunome.MultiPlayerForm

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package xunome;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import xunome.game.multiplayerg.BluetoothConnectionClient;
import xunome.game.multiplayerg.BluetoothConnectionServer;
import xunome.game.multiplayerg.SocketConnectionClient;
import xunome.game.multiplayerg.SocketConnectionServer;
/**
*
* @author Sem iNick
*/
public class MultiPlayerForm extends Form implements CommandListener, ItemStateListener {

    private static final Command COMMAND_OK = new Command("Ok", Command.OK, 0);
    private static final Command COMMAND_BACK = new Command("Back", Command.BACK, 1);
    private xUnoME mid;
    private ChoiceGroup options; //Bluetooth/Internet
    private ChoiceGroup type; //Join/Create
    private TextField sessionName;
    private TextField playerName;
    private ChoiceGroup stopSearch;

    public MultiPlayerForm(xUnoME mid) {

        super("xUno ME - Multiplayer");
        this.mid = mid;
        createHeader();

        addCommand(COMMAND_OK);
        addCommand(COMMAND_BACK);
        setCommandListener(this);
        setItemStateListener(this);       
    }

    public void commandAction(Command c, Displayable d) {

        if (c.equals(COMMAND_OK)) {

            if (playerName.getString().equals("")) {

                showError("Invalid player name.");
                return;
            }

            int connectionType = type.getSelectedIndex();
            if (options.getSelectedIndex() == 0) {

                try {

                    Class.forName("javax.bluetooth.LocalDevice");
                } catch (ClassNotFoundException cnfe) {

                    showError("Your device doesn't support bluetooth.");
                }
                if (connectionType == 1) {

                    removeCommand(COMMAND_OK);
                    new BluetoothConnectionServer(mid, playerName.getString());                   
                } else {

                    new BluetoothConnectionClient(mid, this, playerName.getString(),
                                                  stopSearch.isSelected(0)).startSearching();
                }
            } else {

                String name = sessionName.getString();
                if (name.equals("")) {

                    showError("Invalid session name.");
                    return;
                }

                removeCommand(COMMAND_OK);
                if (connectionType == 1) {

                    new SocketConnectionServer(name, playerName.getString(), mid);
                } else {

                    new SocketConnectionClient(name, playerName.getString(), mid);
                }               
            }
        } else if (c.equals(COMMAND_BACK)) {

            mid.setMain();
        }
    }

    public void itemStateChanged(Item i) {

        if (i != sessionName) {
           
            delete(size() - 1);
            if (options.getSelectedIndex() == 0) {

                append(stopSearch);
            } else {

                append(sessionName);
            }
        }
    }

    private void createHeader() {

        String[] optionsTexts = {"Bluetooth", "Internet"};
        options = new ChoiceGroup("Chose type game.", ChoiceGroup.EXCLUSIVE, optionsTexts, null);
        append(options);

        String[] optionsActions = {"Join", "Create"};
        type = new ChoiceGroup("Choose an action.", ChoiceGroup.EXCLUSIVE, optionsActions, null);
        append(type);

        playerName = new TextField("Player Name:", "", 8, TextField.ANY);
        append(playerName);

        sessionName = new TextField("Session Name:", "", 8, TextField.ANY);

        stopSearch = new ChoiceGroup("Stop search when find first server?",
                                     ChoiceGroup.MULTIPLE);
        stopSearch.append("Yes", null);
        boolean[] a = {true};
        stopSearch.setSelectedFlags(a);
        append(stopSearch);
   }

   public void setMain() {

       mid.getLCD().setCurrent(this);
   }

   private void showError(String msg) {

        Alert a = new Alert("Error", msg, null, AlertType.ERROR);
        a.setTimeout(3000);
        mid.getLCD().setCurrent(a, this);
   }
}
TOP

Related Classes of xunome.MultiPlayerForm

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.