universe@3: /* universe@3: * Copyright 2013 Mike Becker. All rights reserved. universe@3: * universe@3: * Redistribution and use in source and binary forms, with or without universe@3: * modification, are permitted provided that the following conditions are met: universe@3: * universe@3: * 1. Redistributions of source code must retain the above copyright universe@3: * notice, this list of conditions and the following disclaimer. universe@3: * universe@3: * 2. Redistributions in binary form must reproduce the above copyright universe@3: * notice, this list of conditions and the following disclaimer in the universe@3: * documentation and/or other materials provided with the distribution. universe@3: * universe@3: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@3: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@3: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@3: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@3: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@3: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@3: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@3: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@3: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@3: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@3: * POSSIBILITY OF SUCH DAMAGE. universe@3: */ universe@3: universe@3: package de.uapcore.sudoku; universe@3: universe@9: import javax.swing.*; universe@3: universe@3: /** universe@3: * universe@3: * @author mike universe@3: */ universe@3: public class MainMenu { universe@3: universe@3: private ActionHandler handler; universe@3: private JMenuBar menuBar; universe@3: universe@3: public MainMenu(ActionHandler h) { universe@3: handler = h; universe@3: menuBar = new JMenuBar(); universe@3: menuBar.add(createMenu("Datei", 'd', universe@3: createMenuItem("Neu", 'n', "control N", ActionHandler.NEW), universe@3: createMenuItem("Öffnen", 'f', "control O", ActionHandler.OPEN), universe@3: createMenuItem("Speichern", 's', "control S", ActionHandler.SAVE), universe@3: createMenuItem("Speichern als...", 'a', ActionHandler.SAVE_AS), universe@3: createSeparator(), universe@3: createMenuItem("Prüfen", 'p', "control P", ActionHandler.CHECK), universe@3: createMenuItem("Lösen", 'l', "control L", ActionHandler.SOLVE), universe@3: createSeparator(), universe@3: createMenuItem("Beenden", 'e', ActionHandler.QUIT) universe@3: )); universe@3: menuBar.add(createMenu("Help", 'h', universe@3: createMenuItem("Über...", 'b', "F1", ActionHandler.ABOUT) universe@3: )); universe@3: } universe@3: universe@3: private JMenuItem createSeparator() { universe@3: // Return null, the createMenu method knows how to handle it universe@3: return null; universe@3: } universe@3: universe@3: private JMenu createMenu(String caption, char mnemonic, JMenuItem...items) { universe@3: JMenu menu = new JMenu(caption); universe@3: menu.setMnemonic(mnemonic); universe@3: for (JMenuItem item : items) { universe@3: if (item == null) { universe@3: menu.addSeparator(); universe@3: } else { universe@3: menu.add(item); universe@3: } universe@3: } universe@3: return menu; universe@3: } universe@3: universe@3: private JMenuItem createMenuItem(String caption, char mnemonic, universe@3: String command) { universe@3: return createMenuItem(caption, mnemonic, null, command); universe@3: } universe@3: universe@3: private JMenuItem createMenuItem(String caption, char mnemonic, universe@3: String stroke, String command) { universe@3: JMenuItem item = new JMenuItem(caption); universe@3: item.setMnemonic(mnemonic); universe@3: if (stroke != null) { universe@3: item.setAccelerator(KeyStroke.getKeyStroke(stroke)); universe@3: } universe@3: item.setActionCommand(command); universe@3: item.addActionListener(handler); universe@3: universe@3: return item; universe@3: } universe@3: universe@3: public JMenuBar getMenuBar() { universe@3: return menuBar; universe@3: } universe@3: }