Package pdp.scrabble.ihm

Source Code of pdp.scrabble.ihm.PlayerPanel

package pdp.scrabble.ihm;

import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.TreeMap;
import javax.swing.JButton;
import javax.swing.JPanel;
import pdp.scrabble.ihm.action.PlayerAction;
import pdp.scrabble.ihm.action.impl.PlayerActionImpl;
import static pdp.scrabble.Language.getGameLang;
/** Handle player panel draws, such as player rack.
*/
public class PlayerPanel extends JPanel {
  private static final long serialVersionUID = 1L;

  /** Player action. */
  private PlayerAction action = null;

  /** List of buttons. */
  private TreeMap<String, JButton> buttons = null;

  /** Locked panel state. */
  private boolean lock = false;

  /** Score panel reference. */
  private ScorePanel scorePanel = null;

  private RackView playerRack;

  /** Create a new player panel.
   * @param game game reference.
   */
  public PlayerPanel(GraphGameEnv game) {
    super();
    this.setLayout(new BorderLayout());
    this.setPreferredSize(new Dimension(((GameGEnvImpl) game).getMainFrame().getWidth(), 82));
    this.initialize(game);
  }

  /** Initialize panel. */
  private void initialize(GraphGameEnv game) {
    // TODO extract Listeners from PlayerAction
    this.action = new PlayerActionImpl(this, game);
    this.addMouseListener((MouseListener) this.action);
    this.addMouseMotionListener((MouseMotionListener) this.action);

    this.buttons = new TreeMap<String, JButton>();

    JPanel components = new JPanel(new GridLayout(0, 2));

    playerRack = new RackView(game.getPlayer(0).getRack());
    playerRack.setPreferredSize(new Dimension(460, this.getHeight()));

    this.scorePanel = new ScorePanel(game);

    JPanel east = new JPanel(new GridLayout(0, 2));
    JPanel buttonsPanel = new JPanel(new GridLayout(0, 3));

    components.add(playerRack);
    components.add(east);

    east.add(this.scorePanel);
    east.add(buttonsPanel);

    this.add(components, BorderLayout.CENTER);

    ActionListener buttonListener = new PlayerPanelListener(this, action);
   
    // Validate modifications on board
    this.addAction(buttonsPanel, "Validate", buttonListener).setEnabled(false);

    // Cancel modifications on board
    this.addAction(buttonsPanel, "Cancel", buttonListener).setEnabled(false);

    // Clear selected rack letters and fill it
    this.addAction(buttonsPanel, "Reset", buttonListener).setEnabled(false);

    // Display help interface
    this.addAction(buttonsPanel, "Help", buttonListener).setEnabled(false);

    // Display help interface
    this.addAction(buttonsPanel, "Rules", buttonListener).setEnabled(true);

    // Terminate current game
    this.addAction(buttonsPanel, "Game Over", buttonListener).setEnabled(false);
   
    // Binds rack listeners to rack panel and reset button
    RackController ctrl = new RackController(playerRack, this.getButton("Reset"));
    playerRack.addMouseListener(ctrl);
    playerRack.addMouseMotionListener(ctrl);
  }

  /** Add an action button.
   * @param panel panel container.
   * @param name button name.
   * @param a action reference.
   */
  private JButton addAction(JPanel panel, String name, ActionListener a) {
    JButton button = new JButton(getGameLang(name));
    button.addActionListener(a);
    panel.add(button);
    this.buttons.put(name, button);
    return button;
  }

  /** Get a button from its name.
   * @param name button name.
   * @return button returned.
   */
  public JButton getButton(String name) {
    return this.buttons.get(name);
  }

  /** Prepare and reset score to 0. */
  public void createScores() {
    this.scorePanel.create();
    this.validate();
  }

  /** Update and repaint score after changes. */
  public void updateScores() {
    this.scorePanel.update();
  }

  /** Get player actions.
   * @return player actions.
   */
  public PlayerAction action() {
    return this.action;
  }

  /** Get locked state.
   * @return true if locked, false else.
   */
  public boolean isLocked() {
    return this.lock;
  }

  /** Lock playerPanel. */
  public void lock() {
    this.lock = true;
  }

  /** Unlock playerPanel. */
  public void unlock() {
    this.lock = false;
  }

  /** Get the score Panel. */
  public ScorePanel getScorePanel() {
    return this.scorePanel;
  }
}
TOP

Related Classes of pdp.scrabble.ihm.PlayerPanel

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.