Mon, 27 Jul 2020 13:12:40 +0200
the MainMenu IS a JMenuBar
3 | 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 | ||
9 | 29 | import javax.swing.*; |
3 | 30 | |
31 | /** | |
10 | 32 | * Main menu bar. |
3 | 33 | */ |
18
530f60fa3dc9
the MainMenu IS a JMenuBar
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
34 | public class MainMenu extends JMenuBar { |
3 | 35 | |
12
1c62c6009161
fixes some code inspection issues
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
36 | private final ActionHandler handler; |
18
530f60fa3dc9
the MainMenu IS a JMenuBar
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
37 | |
3 | 38 | public MainMenu(ActionHandler h) { |
39 | handler = h; | |
18
530f60fa3dc9
the MainMenu IS a JMenuBar
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
40 | add(createMenu("Datei", 'd', |
3 | 41 | createMenuItem("Neu", 'n', "control N", ActionHandler.NEW), |
42 | createMenuItem("Öffnen", 'f', "control O", ActionHandler.OPEN), | |
43 | createMenuItem("Speichern", 's', "control S", ActionHandler.SAVE), | |
44 | createMenuItem("Speichern als...", 'a', ActionHandler.SAVE_AS), | |
45 | createSeparator(), | |
46 | createMenuItem("Prüfen", 'p', "control P", ActionHandler.CHECK), | |
47 | createMenuItem("Lösen", 'l', "control L", ActionHandler.SOLVE), | |
48 | createSeparator(), | |
49 | createMenuItem("Beenden", 'e', ActionHandler.QUIT) | |
50 | )); | |
18
530f60fa3dc9
the MainMenu IS a JMenuBar
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
51 | add(createMenu("Info", 'i', |
3 | 52 | createMenuItem("Über...", 'b', "F1", ActionHandler.ABOUT) |
53 | )); | |
54 | } | |
55 | ||
56 | private JMenuItem createSeparator() { | |
57 | // Return null, the createMenu method knows how to handle it | |
58 | return null; | |
59 | } | |
60 | ||
61 | private JMenu createMenu(String caption, char mnemonic, JMenuItem...items) { | |
62 | JMenu menu = new JMenu(caption); | |
63 | menu.setMnemonic(mnemonic); | |
64 | for (JMenuItem item : items) { | |
65 | if (item == null) { | |
66 | menu.addSeparator(); | |
67 | } else { | |
68 | menu.add(item); | |
69 | } | |
70 | } | |
71 | return menu; | |
72 | } | |
73 | ||
74 | private JMenuItem createMenuItem(String caption, char mnemonic, | |
75 | String command) { | |
76 | return createMenuItem(caption, mnemonic, null, command); | |
77 | } | |
78 | ||
79 | private JMenuItem createMenuItem(String caption, char mnemonic, | |
80 | String stroke, String command) { | |
81 | JMenuItem item = new JMenuItem(caption); | |
82 | item.setMnemonic(mnemonic); | |
83 | if (stroke != null) { | |
84 | item.setAccelerator(KeyStroke.getKeyStroke(stroke)); | |
85 | } | |
86 | item.setActionCommand(command); | |
87 | item.addActionListener(handler); | |
88 | ||
89 | return item; | |
90 | } | |
18
530f60fa3dc9
the MainMenu IS a JMenuBar
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
91 | |
3 | 92 | } |