Package pdp.scrabble.ihm

Source Code of pdp.scrabble.ihm.MultiplayerPanel

package pdp.scrabble.ihm;

import java.awt.event.KeyEvent;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableRowSorter;
import javax.swing.table.TableModel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.TreeMap;
import pdp.scrabble.utility.IntegerComparator;
import pdp.scrabble.game.GameEnvironment;
import pdp.scrabble.ihm.action.impl.MultiplayerActionImpl;
import pdp.scrabble.ihm.action.MultiplayerAction;
import pdp.scrabble.utility.Display;
import static pdp.scrabble.Language.getGameLang;

/** Handle multiplayer panel (buttons, connected ...).
*/
public class MultiplayerPanel extends JPanel {
  private static final long serialVersionUID = 1L;

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

  /** Multiplayer action. */
  private MultiplayerAction action = null;

  /** Buttons list. */
  private TreeMap<String, JButton> buttons = null;

  /** Table data */
  private MultiplayerTableModelImpl modele = new MultiplayerTableModelImpl();

  /** Connected table */
  private JTable users = null;

  /** Sorter for table. */
  private TableRowSorter<TableModel> sorter = null;

  /** Message list. */
  final JTextArea msgList = new JTextArea();

  /** Create a new multiplayerPanel.
   * @param mainFrame main frame reference.
   * @param game game reference.
   */ 
  public MultiplayerPanel(MainFrame mainFrame, GameEnvironment game) {
    this.mainFrame = mainFrame;
    this.action = new MultiplayerActionImpl(mainFrame, game, this);
    this.buttons = new TreeMap<String, JButton>();
    this.users = new JTable(this.modele);
    this.sorter = new TableRowSorter<TableModel>(this.users.getModel());

    this.setPreferredSize(new Dimension(380, 600));
    this.setLayout(new BorderLayout());
    this.create();
  }

  /** Create panel. */
  private void create() {

    // North components
    JPanel north = new JPanel();
    north.setLayout(new FlowLayout());
    this.add(north, BorderLayout.NORTH);

    // Create button (north)
    this.addButton(north, "Create", new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        action.create();
      }
    });

    // Connect button (north)
    this.addButton(north, "Connect", new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        action.connect();
      }
    });

    // Disconnect button (north)
    this.addButton(north, "Disconnect", new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          try {
            action.disconnect();
          }
          catch (MalformedURLException ex) {
            Display.error("Multiplayer",
            "Error while disconnecting !");
          }
        }
        catch (RemoteException ex) {
          Display.error("Multiplayer",
          "Error while disconnecting !");
        }
      }
    }).setEnabled(false);

    JPanel center = new JPanel();
    center.setLayout(new GridLayout(2, 0));

    // Players list
    JPanel usersTable = new JPanel();
    usersTable.setLayout(new BorderLayout());
    this.users.setEnabled(false);
    usersTable.add(this.users, BorderLayout.CENTER);
    usersTable.add(this.users.getTableHeader(), BorderLayout.NORTH);

    // function of sort
    this.users.setRowSorter(this.sorter);
    this.sorter.setSortsOnUpdates(true);
    this.sorter.setComparator(2, new IntegerComparator());

    // Size of Table
    this.users.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    this.users.getColumnModel().getColumn(0).setPreferredWidth(50);
    this.users.getColumnModel().getColumn(1).setPreferredWidth(50);
    this.users.getColumnModel().getColumn(2).setPreferredWidth(25);

    //Custom Cellule
    DefaultTableCellRenderer custom = new DefaultTableCellRenderer();
    custom.setHorizontalAlignment(JLabel.CENTER);
    this.users.getColumnModel().getColumn(0).setCellRenderer(custom);
    this.users.getColumnModel().getColumn(1).setCellRenderer(custom);
    this.users.getColumnModel().getColumn(2).setCellRenderer(custom);

    center.add(usersTable);

    JPanel tchatContainer = new JPanel();
    JPanel readyPanel = new JPanel();
    tchatContainer.setLayout(new BorderLayout());

    // Ready button
    this.addButton(readyPanel, "Ready", new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          action.readyGameMulti();
        }
        catch (RemoteException ex) {
          Display.error("Multiplayer", "Error on ready click !");
        }
      }
    }).setEnabled(false);

    // Tchat components
    JPanel tchat = new JPanel();
    tchat.setLayout(new GridLayout(0, 1));
    tchatContainer.add(readyPanel, BorderLayout.NORTH);
    tchatContainer.add(tchat, BorderLayout.CENTER);
    center.add(tchatContainer);
    this.add(center);

    // Tchat content
    JScrollPane cscroll = new JScrollPane(this.msgList);
    this.msgList.setEditable(false);
    tchat.add(cscroll);

    // Message to send
    final JTextField msgToSend = new JTextField(25);
    JPanel toSend = new JPanel();
    toSend.setLayout(new FlowLayout());
    toSend.add(msgToSend);
    this.add(toSend, BorderLayout.SOUTH);
    //press enter to send msg
    msgToSend.addKeyListener(new KeyListener() {
      public void keyTyped(KeyEvent e) {
      }

      public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
          if (!msgToSend.getText().isEmpty()) {
            action.sendMessage(msgToSend.getText());
            msgToSend.setText("");
          }
        }
      }

      public void keyReleased(KeyEvent e) {
      }
    });
  }

  /** Add a new button.
   * @param container button container.
   * @param name button name.
   * @param a button action.
   * @return created button.
   */
  public JButton addButton(Container container, String name, ActionListener a) {
    JButton button = new JButton(getGameLang(name));
    button.addActionListener(a);
    container.add(button);
    this.buttons.put(name, button);
    return button;
  }

  /** Get button reference.
   * @param name button name.
   * @return button found.
   */
  public JButton getButton(String name) {
    return this.buttons.get(name);
  }

  /** Update tchat.
   * @param src source.
   * @param msg message.
   */
  public void updateMessageTchat(String src, String msg) {
    this.msgList.setText(
        this.msgList.getText().concat("\n" + src + " : " + msg));
    this.msgList.setCaretPosition(this.msgList.getText().length());
  }

  /** Remove one lign og table
   * @param id
   */
  public void removeTable(int id) {
    this.modele.removeUser(id);
  }

  /** add lign in table
   * @param name
   * @param status
   * @param score
   */
  public void addTable(String name, String status, int score) {
    MultiplayerTable user = new MultiplayerTable(name, status, score);
    this.modele.addUser(user);
  }

  /** set element of lign id of the table
   * @param id
   * @param name
   * @param status
   * @param score
   */
  public void setTable(int id, String name, String status, int score) {
    if (name != null) {
      this.modele.getUser(id).setName(name);
    }
    if (score != -1) {
      this.modele.getUser(id).setScore(score);
    }
    if (status != null) {
      this.modele.getUser(id).setStatus(status);
    }

    this.repaint();
  }

  /** Terminate player turn, give turn to next player.
   * @throws RemoteException exception.
   */
  public void endTurn() throws RemoteException {
    this.action.changeTurnMulti();
    this.action.server().endTurnMulti();
  }

  /** Get main frame.
   * @return main frame reference.
   */
  public MainFrame getMainFrame() {
    return this.mainFrame;
  }

  /** Clear user list. */
  public void clear() {
    this.modele.clear();
    this.msgList.setText("");
  }

  /** Get multiplayer panel actions.
   * @return multiplayer panel actions.
   */
  public MultiplayerAction getAction() {
    return this.action;
  }

  public void sendStatServer(int turn, String name, int points, String words) {
    this.action.sendStatServer(turn, name, points, words);
  }

  public void clearTable() {
    this.modele.clear();
  }

  public void showDialog(String head, String message) {
    Display.information(head, message);
  }

  public void showDialog(String head, String message,String add) {
    Display.information(head, message, add);
  }
}
TOP

Related Classes of pdp.scrabble.ihm.MultiplayerPanel

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.