src/main/java/de/uapcore/sudoku/MainMenu.java

changeset 9
576e7a2861ae
parent 3
ed931970b4ac
child 10
369903afbb29
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.*;
30
31 /**
32 *
33 * @author mike
34 */
35 public class MainMenu {
36
37 private ActionHandler handler;
38 private JMenuBar menuBar;
39
40 public MainMenu(ActionHandler h) {
41 handler = h;
42 menuBar = new JMenuBar();
43 menuBar.add(createMenu("Datei", 'd',
44 createMenuItem("Neu", 'n', "control N", ActionHandler.NEW),
45 createMenuItem("Öffnen", 'f', "control O", ActionHandler.OPEN),
46 createMenuItem("Speichern", 's', "control S", ActionHandler.SAVE),
47 createMenuItem("Speichern als...", 'a', ActionHandler.SAVE_AS),
48 createSeparator(),
49 createMenuItem("Prüfen", 'p', "control P", ActionHandler.CHECK),
50 createMenuItem("Lösen", 'l', "control L", ActionHandler.SOLVE),
51 createSeparator(),
52 createMenuItem("Beenden", 'e', ActionHandler.QUIT)
53 ));
54 menuBar.add(createMenu("Help", 'h',
55 createMenuItem("Über...", 'b', "F1", ActionHandler.ABOUT)
56 ));
57 }
58
59 private JMenuItem createSeparator() {
60 // Return null, the createMenu method knows how to handle it
61 return null;
62 }
63
64 private JMenu createMenu(String caption, char mnemonic, JMenuItem...items) {
65 JMenu menu = new JMenu(caption);
66 menu.setMnemonic(mnemonic);
67 for (JMenuItem item : items) {
68 if (item == null) {
69 menu.addSeparator();
70 } else {
71 menu.add(item);
72 }
73 }
74 return menu;
75 }
76
77 private JMenuItem createMenuItem(String caption, char mnemonic,
78 String command) {
79 return createMenuItem(caption, mnemonic, null, command);
80 }
81
82 private JMenuItem createMenuItem(String caption, char mnemonic,
83 String stroke, String command) {
84 JMenuItem item = new JMenuItem(caption);
85 item.setMnemonic(mnemonic);
86 if (stroke != null) {
87 item.setAccelerator(KeyStroke.getKeyStroke(stroke));
88 }
89 item.setActionCommand(command);
90 item.addActionListener(handler);
91
92 return item;
93 }
94
95 public JMenuBar getMenuBar() {
96 return menuBar;
97 }
98 }

mercurial