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

Sat, 25 Jul 2020 14:01:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Jul 2020 14:01:28 +0200
changeset 9
576e7a2861ae
parent 3
src/de/uapcore/sudoku/MainMenu.java@ed931970b4ac
child 10
369903afbb29
permissions
-rw-r--r--

converts to maven project

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

mercurial