Package pdp.scrabble.ihm

Source Code of pdp.scrabble.ihm.OptionsPanel

package pdp.scrabble.ihm;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.table.DefaultTableCellRenderer;
import java.util.TreeMap;
import pdp.scrabble.Main;
import pdp.scrabble.game.GameEnvironment;
import pdp.scrabble.ihm.action.OptionsAction;
import pdp.scrabble.utility.IntegerComparator;
import pdp.scrabble.ihm.action.impl.OptionsActionImpl;
import static pdp.scrabble.Language.getGameLang;

/** Representation of options panel.
*/
public class OptionsPanel extends JPanel {
  private static final long serialVersionUID = 1L;

  private TableRowSorter<TableModel> sorter = null;

  private GameStatisticsModel modele = null;

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

  /** List of all panels. */
  private TreeMap<String, JPanel> panels = null;

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

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

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

  private JTable tablePanel = null;

  /** Create a new Options panel.
   * @param mainFrame mainFrame reference.
   * @param gameEnvironment game reference.
   */
  public OptionsPanel(MainFrame mainFrame, GameEnvironment game) {
    super();
    this.mainFrame = mainFrame;
    this.game = game;
    this.modele = game.engine().getStatsModel();
    this.action = new OptionsActionImpl(mainFrame, game, this, this.modele);
    this.panels = new TreeMap<String, JPanel>();
    this.buttons = new TreeMap<String, JButton>();

    this.setPreferredSize(new Dimension(400, 600));
    this.setLayout(new GridLayout(2, 1));
    this.setVisible(false);
    this.create();
  }

  private void create() {
    JPanel stats = new JPanel(new BorderLayout());

    JPanel table = new JPanel();
    table.setLayout(new BorderLayout());
    tablePanel = new JTable((TableModel) this.modele);
    JPanel header = new JPanel();

    //initialize the sorter
    this.sorter = new TableRowSorter<TableModel>(tablePanel.getModel());

    table.add(header, BorderLayout.SOUTH);
    table.add(new JScrollPane(tablePanel), BorderLayout.CENTER);

    JPanel showRackPanel = new JPanel();
    final JPanel playersRack = new PlayersRack(this.game);
    playersRack.setVisible(false);
    this.addCheckBox(showRackPanel, "Show Racks", false, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        playersRack.setVisible(!playersRack.isVisible());
        mainFrame.repaint();
      }
    });

    final JPanel replayPanel = new JPanel();
    replayPanel.setVisible(false);
    replayPanel.setLayout(new BorderLayout());
    JLabel replayIcon = new JLabel(new ImageIcon(Main.RESSOURCES_PATH + "replay.png"));

    JPanel buttonReplay = new JPanel();
    this.panels.put("buttonReplay", buttonReplay);

    // replay previous button
    this.addButton(buttonReplay, "Previous Turn", new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        action.previous();
      }
    });

    // replay next button
    this.addButton(buttonReplay, "Next Turn", new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        action.next();
      }
    });

    // stop replay button
    this.addButton(buttonReplay, "Stop Replay", new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        mainFrame.getPlayerPanel().unlock();
        replayPanel.setVisible(false);
        mainFrame.getMenubar().getAction().newGame();
        repaint();
      }
    });

    replayPanel.add(replayIcon, BorderLayout.CENTER);
    replayPanel.add(buttonReplay, BorderLayout.SOUTH);

    this.panels.put("replayPanel", replayPanel);
    stats.add(showRackPanel, BorderLayout.NORTH);
    this.panels.put("showRackPanel", showRackPanel);
    stats.add(table, BorderLayout.CENTER);
    this.panels.put("table", table);

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

    //Size of the table
    DefaultTableCellRenderer custom = new DefaultTableCellRenderer();
    custom.setHorizontalAlignment(JLabel.CENTER);

    //tableau.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    tablePanel.getColumnModel().getColumn(0).setMaxWidth(50);
    tablePanel.getColumnModel().getColumn(1).setMaxWidth(90);
    tablePanel.getColumnModel().getColumn(2).setMaxWidth(50);

    //Custom cellule
    tablePanel.getColumnModel().getColumn(0).setCellRenderer(custom);
    tablePanel.getColumnModel().getColumn(1).setCellRenderer(custom);
    tablePanel.getColumnModel().getColumn(2).setCellRenderer(custom);
    tablePanel.getColumnModel().getColumn(3).setCellRenderer(custom);

    // filter button (south)
    this.addButton(header, "Filter", new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        action.filter(sorter);
      }
    });
    this.add(stats);

    //Graph button
    this.addButton(header, "Graph", new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        action.showGraph(mainFrame);
      }
    });

    // Other rack
    JPanel racks = new JPanel(new BorderLayout());
    racks.add(playersRack, BorderLayout.CENTER);
    racks.add(replayPanel, BorderLayout.SOUTH);
    this.add(racks);
  }

  /** 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;
  }

  /** Add a checkBox.
   * @param container checkBox container.
   * @param name checkBox name.
   * @param isChecked
   * @param a checkBox action.
   * @return created checkBox
   */
  public JCheckBox addCheckBox(Container container, String name,
      boolean isChecked, ActionListener a) {

    JCheckBox checkBox = new JCheckBox(getGameLang(name), isChecked);
    checkBox.addActionListener(a);
    container.add(checkBox);
    return checkBox;
  }

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

  /** Get replay panel.
   * @return replay panel.
   */
  public JPanel getReplayPanel() {
    return this.panels.get("replayPanel");
  }

  /** Get table panel.
   * @return table panel.
   */
  public JPanel getTablePanel() {
    return this.panels.get("table");
  }

  /** Clear model. */
  public void clear() {
    this.modele.clear();
    this.repaint();
  }

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

  /** Get stats model.
   * @return stats model.
   */
  public GameStatisticsModel getGameStatisticsModel() {
    return this.modele;
  }

  public void update() {
    this.tablePanel.revalidate();
  }
}
TOP

Related Classes of pdp.scrabble.ihm.OptionsPanel

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.