Package pdp.scrabble.ihm

Source Code of pdp.scrabble.ihm.MenuBar

package pdp.scrabble.ihm;

import java.util.TreeMap;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import pdp.scrabble.ihm.action.MenuBarAction;
import pdp.scrabble.ihm.action.impl.MenuBarActionImpl;
import static pdp.scrabble.Language.getGameLang;
import static java.awt.event.KeyEvent.VK_N;
import static java.awt.event.KeyEvent.VK_V;
import static java.awt.event.KeyEvent.VK_D;
import static java.awt.event.KeyEvent.VK_L;
import static java.awt.event.KeyEvent.VK_S;
import static java.awt.event.KeyEvent.VK_R;
import static java.awt.event.KeyEvent.VK_E;
import static java.awt.event.KeyEvent.VK_A;

/**
*/
public class MenuBar {

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

  /** Menu bar. */
  private JMenuBar bar = null;

  /** Menu actions. */
  private MenuBarAction action = null;

  /** List of all menus. */
  private TreeMap<String, JMenu> menus = null;

  /** List of all items. */
  private TreeMap<String, JMenuItem> items = null;

  /** Create a new menu bar.
   * @param mainFrame main frame reference.
   */
  public MenuBar(MainFrame mainFrame) {
    this.mainFrame = mainFrame;
    this.action = new MenuBarActionImpl(mainFrame);
    this.menus = new TreeMap<String, JMenu>();
    this.items = new TreeMap<String, JMenuItem>();
  }

  /** Initialize menu bar. */
  public void create() {
    this.bar = new JMenuBar();
    this.mainFrame.add(this.bar, BorderLayout.NORTH);

    // Menu files
    this.addMenu("Game", null);

    // New map
    this.addItem("Game", "New Game", null, 'N', VK_N, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        action.newGame();
      }
    });

    // Stat map
    this.addItem("Game", "Game Options", null, 'D', VK_D, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (mainFrame.getGameEng().getClass().getName().compareTo("SoloEngine")==0) {
          action.optionsPanel();
        }
      }
    });

    this.addSeparator("Game");

    // Load map
    this.addItem("Game", "Load Game", null, 'L', VK_L, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        action.load();
      }
    });

    // Save map
    this.addItem("Game", "Save Game", null, 'S', VK_S, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        action.save();
      }
    });

    // Replay a game
    this.addItem("Game", "Replay", null, 'V', VK_V, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        action.replay();
      }
    });

    this.addSeparator("Game");

    // Exit editor
    this.addItem("Game", "Exit", null, 'E', VK_E, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        action.exit();
      }
    });

    // Menu help
    this.addMenu("Help", null);

    // Rules of Game
    this.addItem("Help", "Rules", null, 'R', VK_R, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        action.rules();
      }
    });

    // About
    this.addItem("Help", "About", null, 'A', VK_A, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        action.about();
      }
    });

  }

  /** Add a menu.
   * @param name menu name.
   * @param icon menu icon.
   */
  private void addMenu(String name, String icon) {
    JMenu menu = new JMenu(getGameLang(name));
    if (icon != null) {
      menu.setIcon(new ImageIcon(icon));
    }
    this.bar.add(menu);
    this.menus.put(name, menu);
  }

  /** Add an item to a menu.
   * @param menu menu where item will be added.
   * @param name item name.
   * @param icon item icon.
   * @param action item action.
   */
  @SuppressWarnings("unused")
  private void addItem(String menu, String name, String icon, ActionListener a) {
    this.addItem(menu, name, icon, -1, 0, a);
  }

  /** Add an item to a menu with a specified shortcut.
   * @param menu menu where item will be added.
   * @param name item name.
   * @param icon item icon.
   * @param shortCut shortcut name.
   * @param key shortcut key.
   * @param action item action.
   */
  private void addItem(String menu, String name, String icon, int shortCut,
      int key, ActionListener action) {

    JMenuItem item = new JMenuItem(getGameLang(name));
    item.addActionListener(action);

    if (shortCut != -1) {
      item.setMnemonic(shortCut);
      item.setAccelerator(KeyStroke.getKeyStroke(key, InputEvent.CTRL_MASK));
    }

    if (icon != null) {
      item.setIcon(new ImageIcon(icon));
    }

    this.menus.get(menu).add(item);
    this.items.put(name, item);
  }

  /** Add a menu separator.
   * @param menu menu where separator will be added.
   */
  private void addSeparator(String menu) {
    this.menus.get(menu).addSeparator();
  }

  /** Get a menu item from its name.
   * @param name item name
   * @return item found
   */
  public JMenuItem getItem(String name) {
    return this.items.get(name);
  }

  /** Get menu bar actions.
   * @return menu bar actions.
   */
  public MenuBarAction getAction() {
    return this.action;
  }

  /** Set enabled state.
   * @param state enabled state.
   */
  public void setEnabled(boolean state) {
    this.menus.get("Game").setEnabled(state);
  }
}
TOP

Related Classes of pdp.scrabble.ihm.MenuBar

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.