src/de/uapcore/sudoku/MainMenu.java

Sat, 26 Jan 2013 18:38:12 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Jan 2013 18:38:12 +0100
changeset 3
ed931970b4ac
permissions
-rw-r--r--

added license and main menu

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
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 import javax.swing.JMenu;
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 import javax.swing.JMenuBar;
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 import javax.swing.JMenuItem;
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 import javax.swing.KeyStroke;
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
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 *
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 * @author mike
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 */
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 public class MainMenu {
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 private ActionHandler handler;
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 private JMenuBar menuBar;
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 public MainMenu(ActionHandler h) {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 handler = h;
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 menuBar = new JMenuBar();
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 menuBar.add(createMenu("Datei", 'd',
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 createMenuItem("Neu", 'n', "control N", ActionHandler.NEW),
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 createMenuItem("Öffnen", 'f', "control O", ActionHandler.OPEN),
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 createMenuItem("Speichern", 's', "control S", ActionHandler.SAVE),
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 createMenuItem("Speichern als...", 'a', ActionHandler.SAVE_AS),
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("Prüfen", 'p', "control P", ActionHandler.CHECK),
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 createMenuItem("Lösen", 'l', "control L", ActionHandler.SOLVE),
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 createSeparator(),
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 createMenuItem("Beenden", 'e', ActionHandler.QUIT)
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 menuBar.add(createMenu("Help", 'h',
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 createMenuItem("Über...", 'b', "F1", ActionHandler.ABOUT)
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
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 private JMenuItem createSeparator() {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 // 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
64 return null;
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 }
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 private JMenu createMenu(String caption, char mnemonic, JMenuItem...items) {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 JMenu menu = new JMenu(caption);
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 menu.setMnemonic(mnemonic);
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 for (JMenuItem item : items) {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 if (item == null) {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 menu.addSeparator();
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 } else {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 menu.add(item);
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 return menu;
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
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 private JMenuItem createMenuItem(String caption, char mnemonic,
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 String command) {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 return createMenuItem(caption, mnemonic, null, command);
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 }
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 private JMenuItem createMenuItem(String caption, char mnemonic,
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 String stroke, String command) {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 JMenuItem item = new JMenuItem(caption);
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 item.setMnemonic(mnemonic);
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 if (stroke != null) {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 item.setAccelerator(KeyStroke.getKeyStroke(stroke));
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 item.setActionCommand(command);
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 item.addActionListener(handler);
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 return item;
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 }
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 public JMenuBar getMenuBar() {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 return menuBar;
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 }
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 }

mercurial