package org.jusecase.properties.ui;

import javax.swing.*;

public class ApplicationMenuBar extends JMenuBar {
    private final Application application;
    private final JFrame frame;

    public ApplicationMenuBar(Application application) {
        this.application = application;
        this.frame = application.getFrame();
        init();
    }

    private void init() {
        add(createFileMenu());
    }

    private JMenu createFileMenu() {
        JMenu file = new JMenu("File");
        file.add(createFileOpenMenuItem());
        file.add(createFileSaveMenuItem());
        return file;
    }

    private JMenuItem createFileOpenMenuItem() {
        JMenuItem open = new JMenuItem("Open");
        open.addActionListener(event -> {
            JFileChooser fileChooser = new NativeJFileChooser();
            fileChooser.setDialogTitle("Open Resource File");
            if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
                application.loadProperties(fileChooser.getSelectedFile());
            }
        });
        return open;
    }

    private JMenuItem createFileSaveMenuItem() {
        JMenuItem open = new JMenuItem("Save");
        open.addActionListener(event -> {
            application.saveProperties();
        });
        return open;
    }
}

@@ -51,16 +51,67 @@
 swing.*;
+%0Aimport java.awt.*;%0Aimport java.awt.event.KeyEvent;
 %0A%0Apublic
@@ -723,33 +723,30 @@
 Item open = 
-new JMenu
+create
 Item(%22Open%22)
@@ -744,16 +744,31 @@
 m(%22Open%22
+, KeyEvent.VK_O
 );%0A     
@@ -1215,35 +1215,47 @@
 tem 
-open = new JMenuItem(%22Save%22
+save = createItem(%22Save%22, KeyEvent.VK_S
 );%0A 
@@ -1253,36 +1253,36 @@
 .VK_S);%0A        
-open
+save
 .addActionListen
@@ -1356,30 +1356,289 @@
 %0A        return 
-open
+save;%0A    %7D%0A%0A    private JMenuItem createItem(String name, int accelerator) %7B%0A        JMenuItem item = new JMenuItem(name);%0A        item.setAccelerator(KeyStroke.getKeyStroke(accelerator, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));%0A        return item
 ;%0A    %7D%0A%7D%0A

Add shortcuts to menu