From 21d0f404f95d4f84a78938272108ac235ce27b84 Mon Sep 17 00:00:00 2001 From: MikhunS <150695058+MikhunS@users.noreply.github.com> Date: Tue, 27 Feb 2024 00:01:39 +0500 Subject: [PATCH 1/4] Update MainApplicationFrame.java --- robots/src/gui/MainApplicationFrame.java | 113 +++++++++++++---------- 1 file changed, 63 insertions(+), 50 deletions(-) diff --git a/robots/src/gui/MainApplicationFrame.java b/robots/src/gui/MainApplicationFrame.java index 62e943ee1..12ed7fd19 100644 --- a/robots/src/gui/MainApplicationFrame.java +++ b/robots/src/gui/MainApplicationFrame.java @@ -2,42 +2,35 @@ import java.awt.Dimension; import java.awt.Toolkit; +import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; -import javax.swing.JDesktopPane; -import javax.swing.JFrame; -import javax.swing.JInternalFrame; -import javax.swing.JMenu; -import javax.swing.JMenuBar; -import javax.swing.JMenuItem; -import javax.swing.SwingUtilities; -import javax.swing.UIManager; -import javax.swing.UnsupportedLookAndFeelException; +import javax.swing.*; import log.Logger; /** * Что требуется сделать: - * 1. Метод создания меню перегружен функционалом и трудно читается. + * 1. Метод создания меню перегружен функционалом и трудно читается. * Следует разделить его на серию более простых методов (или вообще выделить отдельный класс). * */ public class MainApplicationFrame extends JFrame { private final JDesktopPane desktopPane = new JDesktopPane(); - + public MainApplicationFrame() { //Make the big window be indented 50 pixels from each edge //of the screen. - int inset = 50; + int inset = 50; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setBounds(inset, inset, screenSize.width - inset*2, screenSize.height - inset*2); setContentPane(desktopPane); - - + + LogWindow logWindow = createLogWindow(); addWindow(logWindow); @@ -48,7 +41,7 @@ public MainApplicationFrame() { setJMenuBar(generateMenuBar()); setDefaultCloseOperation(EXIT_ON_CLOSE); } - + protected LogWindow createLogWindow() { LogWindow logWindow = new LogWindow(Logger.getDefaultLogSource()); @@ -59,51 +52,57 @@ protected LogWindow createLogWindow() Logger.debug("Протокол работает"); return logWindow; } - + protected void addWindow(JInternalFrame frame) { desktopPane.add(frame); frame.setVisible(true); } - -// protected JMenuBar createMenuBar() { -// JMenuBar menuBar = new JMenuBar(); -// -// //Set up the lone menu. -// JMenu menu = new JMenu("Document"); -// menu.setMnemonic(KeyEvent.VK_D); -// menuBar.add(menu); -// -// //Set up the first menu item. -// JMenuItem menuItem = new JMenuItem("New"); -// menuItem.setMnemonic(KeyEvent.VK_N); -// menuItem.setAccelerator(KeyStroke.getKeyStroke( -// KeyEvent.VK_N, ActionEvent.ALT_MASK)); -// menuItem.setActionCommand("new"); -//// menuItem.addActionListener(this); -// menu.add(menuItem); -// -// //Set up the second menu item. -// menuItem = new JMenuItem("Quit"); -// menuItem.setMnemonic(KeyEvent.VK_Q); -// menuItem.setAccelerator(KeyStroke.getKeyStroke( -// KeyEvent.VK_Q, ActionEvent.ALT_MASK)); -// menuItem.setActionCommand("quit"); -//// menuItem.addActionListener(this); -// menu.add(menuItem); -// -// return menuBar; -// } - + + 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(); - + JMenu lookAndFeelMenu = new JMenu("Режим отображения"); lookAndFeelMenu.setMnemonic(KeyEvent.VK_V); lookAndFeelMenu.getAccessibleContext().setAccessibleDescription( "Управление режимом отображения приложения"); - + { JMenuItem systemLookAndFeel = new JMenuItem("Системная схема", KeyEvent.VK_S); systemLookAndFeel.addActionListener((event) -> { @@ -126,7 +125,7 @@ private JMenuBar generateMenuBar() testMenu.setMnemonic(KeyEvent.VK_T); testMenu.getAccessibleContext().setAccessibleDescription( "Тестовые команды"); - + { JMenuItem addLogMessageItem = new JMenuItem("Сообщение в лог", KeyEvent.VK_S); addLogMessageItem.addActionListener((event) -> { @@ -134,12 +133,26 @@ private JMenuBar generateMenuBar() }); testMenu.add(addLogMessageItem); } + JMenuItem exitItem = new JMenuItem("Выход", KeyEvent.VK_E); + exitItem.addActionListener((event) -> { + UIManager.put("OptionPane.yesButtonText", "Да");; + UIManager.put("OptionPane.noButtonText", "Нет"); + UIManager.put("OptionPane.cancelButtonText", "Отмена"); + int result = JOptionPane.showConfirmDialog(this, "Вы уверены, что хотите выйти из приложения?", "Выход", + JOptionPane.YES_NO_CANCEL_OPTION); + + if (result == JOptionPane.YES_OPTION) { + System.exit(0); + } + }); + menuBar.add(lookAndFeelMenu); menuBar.add(testMenu); + menuBar.add(exitItem); return menuBar; } - + private void setLookAndFeel(String className) { try From 483043ecb5180eb826ec38a03894554b73894dc8 Mon Sep 17 00:00:00 2001 From: MikhunS <150695058+MikhunS@users.noreply.github.com> Date: Mon, 25 Mar 2024 19:14:10 +0500 Subject: [PATCH 2/4] Update MainApplicationFrame.java --- robots/src/gui/MainApplicationFrame.java | 87 ++++++++++++------------ 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/robots/src/gui/MainApplicationFrame.java b/robots/src/gui/MainApplicationFrame.java index 12ed7fd19..cf70a4e8c 100644 --- a/robots/src/gui/MainApplicationFrame.java +++ b/robots/src/gui/MainApplicationFrame.java @@ -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() { + 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")); + 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(); + } + } } + From 9cf042febe76ccdae1d797f78d1d1a0ee697acf4 Mon Sep 17 00:00:00 2001 From: MikhunS <150695058+MikhunS@users.noreply.github.com> Date: Mon, 1 Apr 2024 21:14:40 +0500 Subject: [PATCH 3/4] Update MainApplicationFrame.java --- robots/src/gui/MainApplicationFrame.java | 42 ++---------------------- 1 file changed, 2 insertions(+), 40 deletions(-) diff --git a/robots/src/gui/MainApplicationFrame.java b/robots/src/gui/MainApplicationFrame.java index cf70a4e8c..4bca4e672 100644 --- a/robots/src/gui/MainApplicationFrame.java +++ b/robots/src/gui/MainApplicationFrame.java @@ -38,7 +38,7 @@ public MainApplicationFrame() { setJMenuBar(generateMenuBar()); setDefaultCloseOperation(EXIT_ON_CLOSE); - loadWindowStateFromFile(); + WindowStateManager.loadWindowStateFromFile(desktopPane); } protected LogWindow createLogWindow() @@ -99,7 +99,7 @@ private JMenuBar generateMenuBar() } JMenuItem exitItem = new JMenuItem("Выход", KeyEvent.VK_E); exitItem.addActionListener((event) -> { - saveWindowStateToFile(); + WindowStateManager.saveWindowStateToFile(desktopPane); UIManager.put("OptionPane.yesButtonText", "Да");; UIManager.put("OptionPane.noButtonText", "Нет"); UIManager.put("OptionPane.cancelButtonText", "Извиняюсь!"); @@ -131,42 +131,4 @@ 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() { - 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")); - 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(); - } - } } - From a7f572006dcc7b89467f9f80415adf6399d44d1b Mon Sep 17 00:00:00 2001 From: MikhunS <150695058+MikhunS@users.noreply.github.com> Date: Mon, 1 Apr 2024 21:17:49 +0500 Subject: [PATCH 4/4] Create WindowStateManager.java --- robots/src/gui/WindowStateManager.java | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 robots/src/gui/WindowStateManager.java diff --git a/robots/src/gui/WindowStateManager.java b/robots/src/gui/WindowStateManager.java new file mode 100644 index 000000000..821512db0 --- /dev/null +++ b/robots/src/gui/WindowStateManager.java @@ -0,0 +1,58 @@ +package gui; + +import javax.swing.*; +import java.beans.PropertyVetoException; +import java.io.*; +import java.util.Properties; + +public class WindowStateManager { + private static final String PROPERTIES_FILE = "windowState.properties"; + + static void saveWindowStateToFile(JDesktopPane desktopPane) { + Properties prop = new Properties(); + for (JInternalFrame frame : desktopPane.getAllFrames()) { + saveFrameState(frame, prop); + } + + try (FileOutputStream fos = new FileOutputStream(PROPERTIES_FILE)) { + prop.store(fos, "Window States"); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + + static void loadWindowStateFromFile(JDesktopPane desktopPane) { + Properties prop = new Properties(); + try (FileInputStream fis = new FileInputStream(PROPERTIES_FILE)) { + prop.load(fis); + for (JInternalFrame frame : desktopPane.getAllFrames()) { + restoreFrameState(frame,prop); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + } + private static void restoreFrameState(JInternalFrame frame, Properties prop) { + int x = Integer.parseInt(prop.getProperty(frame.getTitle() + ".x", "0")); + int y = Integer.parseInt(prop.getProperty(frame.getTitle() + ".y", "0")); + int width = Integer.parseInt(prop.getProperty(frame.getTitle() + ".width", "0")); + int height = Integer.parseInt(prop.getProperty(frame.getTitle() + ".height", "0")); + boolean isIcon = Boolean.parseBoolean(prop.getProperty(frame.getTitle() + ".isIcon", "false")); + + frame.setBounds(x, y, width, height); + try { + if (isIcon) { + frame.setIcon(true); + } + } catch (PropertyVetoException e) { + e.printStackTrace(); + } + } + private static void saveFrameState(JInternalFrame frame, Properties prop) { + 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())); + } +}