summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java
blob: e988d74c7c11f7d11414b0265e521e75079fabdb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package cuchaz.enigma.gui.elements;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.jar.JarFile;

import javax.swing.*;

import cuchaz.enigma.gui.Gui;
import cuchaz.enigma.gui.dialog.AboutDialog;
import cuchaz.enigma.throwables.MappingParseException;

public class MenuBar extends JMenuBar {

    private final Gui gui;

    public final JMenuItem closeJarMenu;

    public final JMenuItem openMappingsJsonMenu;
    public final JMenuItem openEnigmaMappingsMenu;

    public final JMenuItem saveMappingsMenu;
    public final JMenuItem saveMappingsJsonMenu;
    public final JMenuItem saveMappingEnigmaFileMenu;
    public final JMenuItem saveMappingEnigmaDirectoryMenu;
    public final JMenuItem saveMappingsSrgMenu;
    public final JMenuItem closeMappingsMenu;


    public final JMenuItem exportSourceMenu;
    public final JMenuItem exportJarMenu;

    public MenuBar(Gui gui) {
        this.gui = gui;

        {
            JMenu menu = new JMenu("File");
            this.add(menu);
            {
                JMenuItem item = new JMenuItem("Open Jar...");
                menu.add(item);
                item.addActionListener(event -> {
                    if (this.gui.jarFileChooser.showOpenDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
                        // load the jar in a separate thread
                        new Thread() {
                            @Override
                            public void run() {
                                try {
                                    gui.getController().openJar(new JarFile(gui.jarFileChooser.getSelectedFile()));
                                } catch (IOException ex) {
                                    throw new Error(ex);
                                }
                            }
                        }.start();
                    }
                });
            }
            {
                JMenuItem item = new JMenuItem("Close Jar");
                menu.add(item);
                item.addActionListener(event -> this.gui.getController().closeJar());
                this.closeJarMenu = item;
            }
            menu.addSeparator();
            JMenu openMenu = new JMenu("Open Mappings...");
            {
                JMenuItem item = new JMenuItem("Enigma");
                openMenu.add(item);
                item.addActionListener(event -> {
                    if (this.gui.enigmaMappingsFileChooser.showOpenDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
                        try {
                            this.gui.getController().openEnigmaMappings(this.gui.enigmaMappingsFileChooser.getSelectedFile());
                        } catch (IOException ex) {
                            throw new Error(ex);
                        } catch (MappingParseException ex) {
                            JOptionPane.showMessageDialog(this.gui.getFrame(), ex.getMessage());
                        }
                    }
                });
                this.openEnigmaMappingsMenu = item;
            }
            menu.add(openMenu);
            {
                JMenuItem item = new JMenuItem("JSON (directory)");
                openMenu.add(item);
                item.addActionListener(event -> {
                    if (this.gui.jsonMappingsFileChooser.showOpenDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
                        try {
                            this.gui.getController().openJsonMappings(this.gui.jsonMappingsFileChooser.getSelectedFile());
                        } catch (IOException ex) {
                            throw new Error(ex);
                        }
                    }
                });
                this.openMappingsJsonMenu = item;
            }
            {
                JMenuItem item = new JMenuItem("Save Mappings");
                menu.add(item);
                item.addActionListener(event -> {
                    try {
                        this.gui.getController().saveMappings(this.gui.jsonMappingsFileChooser.getSelectedFile());
                    } catch (IOException ex) {
                        throw new Error(ex);
                    }
                });
                item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
                this.saveMappingsMenu = item;
            }
            JMenu saveMenu = new JMenu("Save Mappings As...");
            {
                JMenuItem item = new JMenuItem("Enigma (single file)");
                saveMenu.add(item);
                item.addActionListener(event -> {
                    if (this.gui.jsonMappingsFileChooser.showSaveDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
                        try {
                            this.gui.getController().saveEnigmaMappings(this.gui.jsonMappingsFileChooser.getSelectedFile(), false);
                            this.saveMappingsMenu.setEnabled(true);
                        } catch (IOException ex) {
                            throw new Error(ex);
                        }
                    }
                });
                this.saveMappingEnigmaFileMenu = item;
            }
            {
                JMenuItem item = new JMenuItem("Enigma (directory)");
                saveMenu.add(item);
                item.addActionListener(event -> {
                    if (this.gui.jsonMappingsFileChooser.showSaveDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
                        try {
                            this.gui.getController().saveEnigmaMappings(this.gui.jsonMappingsFileChooser.getSelectedFile(), true);
                            this.saveMappingsMenu.setEnabled(true);
                        } catch (IOException ex) {
                            throw new Error(ex);
                        }
                    }
                });
                item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
                this.saveMappingEnigmaDirectoryMenu = item;
            }
            menu.add(saveMenu);
            {
                JMenuItem item = new JMenuItem("JSON (directory)");
                saveMenu.add(item);
                item.addActionListener(event -> {
                    if (this.gui.jsonMappingsFileChooser.showSaveDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
                        try {
                            this.gui.getController().saveJsonMappings(this.gui.jsonMappingsFileChooser.getSelectedFile());
                            this.saveMappingsMenu.setEnabled(true);
                        } catch (IOException ex) {
                            throw new Error(ex);
                        }
                    }
                });
                this.saveMappingsJsonMenu = item;
            }
            {
                JMenuItem item = new JMenuItem("SRG");
                saveMenu.add(item);
                item.addActionListener(event -> {
                    if (this.gui.jsonMappingsFileChooser.showSaveDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
                        try {
                            this.gui.getController().saveSRGMappings(this.gui.jsonMappingsFileChooser.getSelectedFile());
                            this.saveMappingsMenu.setEnabled(true);
                        } catch (IOException ex) {
                            throw new Error(ex);
                        }
                    }
                });
                this.saveMappingsSrgMenu = item;
            }
            {
                JMenuItem item = new JMenuItem("Close Mappings");
                menu.add(item);
                item.addActionListener(event -> this.gui.getController().closeMappings());
                this.closeMappingsMenu = item;
            }
            menu.addSeparator();
            {
                JMenuItem item = new JMenuItem("Export Source...");
                menu.add(item);
                item.addActionListener(event -> {
                    if (this.gui.exportSourceFileChooser.showSaveDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
                        this.gui.getController().exportSource(this.gui.exportSourceFileChooser.getSelectedFile());
                    }
                });
                this.exportSourceMenu = item;
            }
            {
                JMenuItem item = new JMenuItem("Export Jar...");
                menu.add(item);
                item.addActionListener(event -> {
                    if (this.gui.exportJarFileChooser.showSaveDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
                        this.gui.getController().exportJar(this.gui.exportJarFileChooser.getSelectedFile());
                    }
                });
                this.exportJarMenu = item;
            }
            menu.addSeparator();
            {
                JMenuItem item = new JMenuItem("Exit");
                menu.add(item);
                item.addActionListener(event -> this.gui.close());
            }
        }
        {
            JMenu menu = new JMenu("Help");
            this.add(menu);
            {
                JMenuItem item = new JMenuItem("About");
                menu.add(item);
                item.addActionListener(event -> AboutDialog.show(this.gui.getFrame()));
            }
        }
    }
}