Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 45 additions & 42 deletions robots/src/gui/MainApplicationFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@

import log.Logger;

/**
* Что требуется сделать:
* 1. Метод создания меню перегружен функционалом и трудно читается.
* Следует разделить его на серию более простых методов (или вообще выделить отдельный класс).
*
*/
import java.beans.PropertyVetoException;
import java.io.*;
import java.util.Properties;

public class MainApplicationFrame extends JFrame
{
private final JDesktopPane desktopPane = new JDesktopPane();
Expand All @@ -40,6 +38,7 @@ public MainApplicationFrame() {

setJMenuBar(generateMenuBar());
setDefaultCloseOperation(EXIT_ON_CLOSE);
loadWindowStateFromFile();
}

protected LogWindow createLogWindow()
Expand All @@ -59,41 +58,6 @@ protected void addWindow(JInternalFrame frame)
frame.setVisible(true);
}

protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();

JMenu menu = new JMenu("File");
menuBar.add(menu);

JMenuItem menuItem = new JMenuItem("New", KeyEvent.VK_N);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
menu.add(menuItem);

menuItem = new JMenuItem("Open", KeyEvent.VK_O);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
menu.add(menuItem);

menuItem = new JMenuItem("Save", KeyEvent.VK_S);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
menu.add(menuItem);

menuItem = new JMenuItem("Exit", KeyEvent.VK_Q);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
menu.add(menuItem);

JMenuItem exitItem = new JMenuItem("Exit", KeyEvent.VK_E);
exitItem.addActionListener((event) -> {
int result = JOptionPane.showConfirmDialog(this, "Вы уверены, что хотите выйти из приложения?", "Выход",
JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
System.exit(0);
}
});
menu.add(exitItem);

return menuBar;
}

private JMenuBar generateMenuBar()
{
JMenuBar menuBar = new JMenuBar();
Expand Down Expand Up @@ -135,9 +99,10 @@ private JMenuBar generateMenuBar()
}
JMenuItem exitItem = new JMenuItem("Выход", KeyEvent.VK_E);
exitItem.addActionListener((event) -> {
saveWindowStateToFile();
UIManager.put("OptionPane.yesButtonText", "Да");;
UIManager.put("OptionPane.noButtonText", "Нет");
UIManager.put("OptionPane.cancelButtonText", "Отмена");
UIManager.put("OptionPane.cancelButtonText", "Извиняюсь!");
int result = JOptionPane.showConfirmDialog(this, "Вы уверены, что хотите выйти из приложения?", "Выход",
JOptionPane.YES_NO_CANCEL_OPTION);

Expand Down Expand Up @@ -166,4 +131,42 @@ private void setLookAndFeel(String className)
// just ignore
}
}
private void saveWindowStateToFile() {
Properties prop = new Properties();
for (JInternalFrame frame : desktopPane.getAllFrames()) {
prop.setProperty(frame.getTitle() + ".x", String.valueOf(frame.getX()));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

аналогично выносится. +вопрос: а если захочу сохранить как-то по особенному?

prop.setProperty(frame.getTitle() + ".y", String.valueOf(frame.getY()));
prop.setProperty(frame.getTitle() + ".width", String.valueOf(frame.getWidth()));
prop.setProperty(frame.getTitle() + ".height", String.valueOf(frame.getHeight()));
prop.setProperty(frame.getTitle() + ".isIcon", String.valueOf(frame.isIcon()));
}

try {
prop.store(new FileOutputStream("windowState.properties"), null);
} catch (IOException ex) {
ex.printStackTrace();
}
}

private void loadWindowStateFromFile() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

загрузка и сохранение - ответственность отдельного класса

Properties prop = new Properties();
try {
prop.load(new FileInputStream("windowState.properties"));
for (JInternalFrame frame : desktopPane.getAllFrames()) {
int x = Integer.parseInt(prop.getProperty(frame.getTitle() + ".x"));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

все, что в теле for спокойно выносится в отдельный метод. Это повысит читаемость и явно выделит функциональность.

int y = Integer.parseInt(prop.getProperty(frame.getTitle() + ".y"));
int width = Integer.parseInt(prop.getProperty(frame.getTitle() + ".width"));
int height = Integer.parseInt(prop.getProperty(frame.getTitle() + ".height"));
boolean isIcon = Boolean.parseBoolean(prop.getProperty(frame.getTitle() + ".isIcon"));

frame.setBounds(x, y, width, height);
if (isIcon) {
frame.setIcon(true);
}
}
} catch (IOException | PropertyVetoException ex) {
ex.printStackTrace();
}
}
}