diff --git a/ProjectStormtrooper/FormPlaneCollection.java b/ProjectStormtrooper/FormPlaneCollection.java index 8adba40..1e76426 100644 --- a/ProjectStormtrooper/FormPlaneCollection.java +++ b/ProjectStormtrooper/FormPlaneCollection.java @@ -2,8 +2,11 @@ package ProjectStormtrooper; import javax.swing.*; import javax.swing.event.ListSelectionEvent; +import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.*; import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; import java.util.Objects; import java.util.Stack; @@ -47,6 +50,47 @@ public class FormPlaneCollection { buttonShowRemovedPlanes.addActionListener(this::buttonShowRemovedPlanesClicked); } + public JMenuBar getMenuBar() { + JMenuBar menuBar = new JMenuBar(); + JMenu fileMenu = new JMenu("Файл"); + JMenuItem openItem = new JMenuItem("Загрузить"); + openItem.addActionListener( + e -> { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt")); + fileChooser.setDialogTitle("Выберите файл для загрузки данных"); + if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { + File selectedFile = fileChooser.getSelectedFile(); + if (_storage.LoadData(selectedFile.getAbsolutePath())) { + JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат", JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(null, "Не загрузилось", "Результат", JOptionPane.ERROR_MESSAGE); + } + } + ReloadObjects(); + } + ); + JMenuItem saveItem = new JMenuItem("Сохранить"); + saveItem.addActionListener( + e -> { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setDialogTitle("Выберите файл для сохранения данных"); + fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt")); + if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { + File selectedFile = fileChooser.getSelectedFile(); + if (_storage.SaveData(selectedFile.getAbsolutePath())) + JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE); + else + JOptionPane.showMessageDialog(null, "Не сохранилось", "Результат", JOptionPane.ERROR_MESSAGE); + } + } + ); + fileMenu.add(openItem); + fileMenu.add(saveItem); + menuBar.add(fileMenu); + return menuBar; + } + private void ReloadObjects() { int index = listBoxStorages.getSelectedIndex(); listBoxStorages.setListData(_storage.Keys().toArray()); diff --git a/ProjectStormtrooper/FramePlaneCollection.java b/ProjectStormtrooper/FramePlaneCollection.java index 3428147..1dbbcf5 100644 --- a/ProjectStormtrooper/FramePlaneCollection.java +++ b/ProjectStormtrooper/FramePlaneCollection.java @@ -10,6 +10,7 @@ public class FramePlaneCollection extends JFrame { setDefaultCloseOperation(EXIT_ON_CLOSE); _formPlaneCollection = new FormPlaneCollection(); setContentPane(_formPlaneCollection.getPanelWrapper()); + this.setJMenuBar(_formPlaneCollection.getMenuBar()); setDefaultLookAndFeelDecorated(false); setLocation(300, 100); pack();