src/de/uapcore/sudoku/MainMenu.java

changeset 9
576e7a2861ae
parent 3
ed931970b4ac
equal deleted inserted replaced
8:e70a0e3555fb 9:576e7a2861ae
1 /*
2 * Copyright 2013 Mike Becker. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 package de.uapcore.sudoku;
28
29 import javax.swing.JMenu;
30 import javax.swing.JMenuBar;
31 import javax.swing.JMenuItem;
32 import javax.swing.KeyStroke;
33
34 /**
35 *
36 * @author mike
37 */
38 public class MainMenu {
39
40 private ActionHandler handler;
41 private JMenuBar menuBar;
42
43 public MainMenu(ActionHandler h) {
44 handler = h;
45 menuBar = new JMenuBar();
46 menuBar.add(createMenu("Datei", 'd',
47 createMenuItem("Neu", 'n', "control N", ActionHandler.NEW),
48 createMenuItem("Öffnen", 'f', "control O", ActionHandler.OPEN),
49 createMenuItem("Speichern", 's', "control S", ActionHandler.SAVE),
50 createMenuItem("Speichern als...", 'a', ActionHandler.SAVE_AS),
51 createSeparator(),
52 createMenuItem("Prüfen", 'p', "control P", ActionHandler.CHECK),
53 createMenuItem("Lösen", 'l', "control L", ActionHandler.SOLVE),
54 createSeparator(),
55 createMenuItem("Beenden", 'e', ActionHandler.QUIT)
56 ));
57 menuBar.add(createMenu("Help", 'h',
58 createMenuItem("Über...", 'b', "F1", ActionHandler.ABOUT)
59 ));
60 }
61
62 private JMenuItem createSeparator() {
63 // Return null, the createMenu method knows how to handle it
64 return null;
65 }
66
67 private JMenu createMenu(String caption, char mnemonic, JMenuItem...items) {
68 JMenu menu = new JMenu(caption);
69 menu.setMnemonic(mnemonic);
70 for (JMenuItem item : items) {
71 if (item == null) {
72 menu.addSeparator();
73 } else {
74 menu.add(item);
75 }
76 }
77 return menu;
78 }
79
80 private JMenuItem createMenuItem(String caption, char mnemonic,
81 String command) {
82 return createMenuItem(caption, mnemonic, null, command);
83 }
84
85 private JMenuItem createMenuItem(String caption, char mnemonic,
86 String stroke, String command) {
87 JMenuItem item = new JMenuItem(caption);
88 item.setMnemonic(mnemonic);
89 if (stroke != null) {
90 item.setAccelerator(KeyStroke.getKeyStroke(stroke));
91 }
92 item.setActionCommand(command);
93 item.addActionListener(handler);
94
95 return item;
96 }
97
98 public JMenuBar getMenuBar() {
99 return menuBar;
100 }
101 }

mercurial