package net.sf.arianne.marboard.client.gui.menu;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import net.sf.arianne.marboard.client.core.CurrentBoard;
import net.sf.arianne.marboard.client.gui.BoardState;
import net.sf.arianne.marboard.client.gui.MainWindow;
import net.sf.arianne.marboard.client.gui.drawingtool.DotDrawingTool;
import net.sf.arianne.marboard.client.gui.drawingtool.RectangleDrawingTool;
import net.sf.arianne.marboard.client.gui.drawingtool.StraightLineDrawingTool;
/**
* creates a menu bar
*
* @author hendrik
*/
public class MenuBuilder {
private static final String COLOR_NAMES[] = new String[]{"Black", "Blue", "Cyan", "Dark Gray", "Gray", "Green", "Light Gray", "Magenta", "Orange", "Pink", "Red", "White", "Yellow"};
private static final Color COLOR_OBJECTS[] = new Color[]{Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW};
private JMenuBar menuBar = new JMenuBar();
private MainWindow mainWindow;
private CurrentBoard board;
private BoardState boardState;
/**
* Creates a new MenuBilder
*
* @param window the window to build the menu for
* @param board CurrentBoard
* @param boardState BoardState
*/
public MenuBuilder(MainWindow window, CurrentBoard board, BoardState boardState) {
this.mainWindow = window;
this.board = board;
this.boardState = boardState;
}
/**
* creates the menu bar with all its children
*
* @return JMenuBar
*/
public JMenuBar create() {
createFileMenu();
createToolsMenu();
createThicknessMenu();
createColorMenu();
createFillColorMenu();
return menuBar;
}
/**
* Creates the File menu.
*/
private void createFileMenu() {
JMenu menu = new JMenu("File");
menu.setMnemonic('f');
menuBar.add(menu);
// clear
JMenuItem item = new JMenuItem("Clear", 'c');
item.addActionListener(new ClearActionListener());
menu.add(item);
// open
item = new JMenuItem("Open...", 'o');
item.addActionListener(new OpenActionListener(mainWindow, boardState));
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
menu.add(item);
// save
item = new JMenuItem("Save", 's');
item.addActionListener(new SaveAsActionListener(false, mainWindow, board, boardState));
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
menu.add(item);
// save as
item = new JMenuItem("Save As...", 'a');
item.addActionListener(new SaveAsActionListener(true, mainWindow, board, boardState));
menu.add(item);
// exit menu to quit Marboard
item = new JMenuItem("Exit", 'x');
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
item.addActionListener(new ExitActionListener(mainWindow));
menu.add(item);
}
/**
* Creates the Thickness menu.
*/
private void createThicknessMenu() {
JMenu menu = new JMenu("Thickness");
menuBar.add(menu);
// add en entry for each thickness from 1 to 20
for (int i = 1; i <= 20; i++) {
JMenuItem item = new JMenuItem(Integer.toString(i));
item.addActionListener(new SelectThicknessActionListener(boardState, i));
menu.add(item);
}
}
/**
* Creates the Color menu.
*/
private void createColorMenu() {
JMenu menu = new JMenu("Color");
menuBar.add(menu);
menu.setMnemonic('c');
// add en entry for each standard color
for (int i = 0; i < COLOR_OBJECTS.length; i++) {
JMenuItem item = new JMenuItem(COLOR_NAMES[i]);
item.addActionListener(new SelectColorActionListener(boardState, COLOR_OBJECTS[i]));
menu.add(item);
}
}
/**
* Creates the Fill Color menu.
*/
private void createFillColorMenu() {
JMenu menu = new JMenu("Fillcolor");
menuBar.add(menu);
menu.setMnemonic('f');
// add en entry for each standard color
for (int i = 0; i < COLOR_OBJECTS.length; i++) {
JMenuItem item = new JMenuItem(COLOR_NAMES[i]);
item.addActionListener(new SelectFillColorActionListener(boardState, COLOR_OBJECTS[i]));
menu.add(item);
}
}
/**
* creates the Tools menu.
*/
private void createToolsMenu() {
JMenu menu = new JMenu("Tools");
menuBar.add(menu);
menu.setMnemonic('t');
// dot
JMenuItem item = new JMenuItem("Dot", 'd');
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK));
item.addActionListener(new SelectToolActionListener(boardState, new DotDrawingTool()));
menu.add(item);
// line
item = new JMenuItem("Line", 'l');
item.addActionListener(new SelectToolActionListener(boardState, new StraightLineDrawingTool(mainWindow.getDrawingArea())));
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
menu.add(item);
// rectangle
item = new JMenuItem("Rectangle", 'r');
item.addActionListener(new SelectToolActionListener(boardState, new RectangleDrawingTool(mainWindow.getDrawingArea())));
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
menu.add(item);
}
}