-
Notifications
You must be signed in to change notification settings - Fork 0
Update MainApplicationFrame.java #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(); | ||
|
|
@@ -40,6 +38,7 @@ public MainApplicationFrame() { | |
|
|
||
| setJMenuBar(generateMenuBar()); | ||
| setDefaultCloseOperation(EXIT_ON_CLOSE); | ||
| loadWindowStateFromFile(); | ||
| } | ||
|
|
||
| protected LogWindow createLogWindow() | ||
|
|
@@ -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(); | ||
|
|
@@ -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); | ||
|
|
||
|
|
@@ -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())); | ||
| 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() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
аналогично выносится. +вопрос: а если захочу сохранить как-то по особенному?