Package pdp.scrabble.ihm

Source Code of pdp.scrabble.ihm.ScorePanel$ScoreTable

package pdp.scrabble.ihm;

import java.awt.Dimension;
import javax.swing.JScrollPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.JTable;
import javax.swing.JPanel;
import pdp.scrabble.game.GameEnvironment;
import pdp.scrabble.game.Player;
import static pdp.scrabble.Language.getGameLang;

/** Handle score panel (included in player panel).
*/
public class ScorePanel extends JPanel {
    private static final long serialVersionUID = 1L;

    /** Game reference. */
    private GameEnvironment game = null;

    /** Table reference. */
    private JTable table = null;

    /** Number of player in table. */
    private int numberOfPlayers = 0;

    /** Create a new score panel.
     * @param game game reference.
     */
    public ScorePanel(GameEnvironment game) {
  super();
  this.game = game;
    }

    /** Create new score table. */
    public void create() {
  this.removeAll();
  this.numberOfPlayers = this.game.getPlayerNumber();
  this.table = new JTable(new ScoreTable());
  this.table.setPreferredScrollableViewportSize(new Dimension(212, 44));
  this.table.setFillsViewportHeight(true);

  JScrollPane scrollPane = new JScrollPane(this.table);
  this.add(scrollPane);
  this.repaint();
    }

    /** Notify score table for changes. */
    public void update() {
  for (int i = 0; i < this.numberOfPlayers; i++) {
      Player player = game.getPlayer(i);
      if (player != null) {
    this.table.setValueAt(player.getName(), i, 0);
    this.table.setValueAt(player.getScore(), i, 1);
      }
  }
  this.paintImmediately(0, 0, this.getWidth(), this.getHeight());
    }

    private class ScoreTable extends AbstractTableModel {
  private static final long serialVersionUID = 1L;

  private String[] columnNames = {
      getGameLang("Player"), getGameLang("Score")
  };

  private Object[][] data = null;

  ScoreTable() {
      Object[][] datas = new Object[numberOfPlayers][2];

      for (int i = 0; i < numberOfPlayers; i++) {
    Player player = game.getPlayer(i);
    datas[i][0] = player.getName();
    datas[i][1] = 0;
      }

      this.data = datas;
  }

  @Override
  public int getColumnCount() {
      return this.columnNames.length;
  }

  @Override
  public int getRowCount() {
      return this.data.length;
  }

  @Override
  public String getColumnName(int col) {
      return this.columnNames[col];
  }

  @Override
  public Object getValueAt(int row, int col) {
      return this.data[row][col];
  }

  @Override
  public Class<?> getColumnClass(int c) {
      return this.getValueAt(0, c).getClass();
  }

  @Override
  public boolean isCellEditable(int row, int col) {
      return false;
  }

  @Override
  public void setValueAt(Object value, int row, int col) {
      this.data[row][col] = value;
      this.fireTableCellUpdated(row, col);
  }
    }
}
TOP

Related Classes of pdp.scrabble.ihm.ScorePanel$ScoreTable

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.