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

Mon, 27 Jul 2020 13:12:40 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Jul 2020 13:12:40 +0200
changeset 18
530f60fa3dc9
parent 14
959e13642554
permissions
-rw-r--r--

the MainMenu IS a JMenuBar

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

mercurial