From b4952d6bc17ccce578ec37ef6fca29f6b69f8efe Mon Sep 17 00:00:00 2001 From: hochyspat Date: Fri, 7 Mar 2025 16:12:03 +0500 Subject: [PATCH 1/6] add close button --- robots/src/gui/RobotsProgram.java | 25 ---- .../{ => main/java}/gui/GameVisualizer.java | 0 .../src/{ => main/java}/gui/GameWindow.java | 0 robots/src/{ => main/java}/gui/LogWindow.java | 0 .../java}/gui/MainApplicationFrame.java | 128 ++++++++++++------ robots/src/main/java/gui/RobotsProgram.java | 24 ++++ .../java}/log/LogChangeListener.java | 0 robots/src/{ => main/java}/log/LogEntry.java | 0 robots/src/{ => main/java}/log/LogLevel.java | 0 .../{ => main/java}/log/LogWindowSource.java | 0 robots/src/{ => main/java}/log/Logger.java | 0 11 files changed, 108 insertions(+), 69 deletions(-) delete mode 100644 robots/src/gui/RobotsProgram.java rename robots/src/{ => main/java}/gui/GameVisualizer.java (100%) rename robots/src/{ => main/java}/gui/GameWindow.java (100%) rename robots/src/{ => main/java}/gui/LogWindow.java (100%) rename robots/src/{ => main/java}/gui/MainApplicationFrame.java (56%) create mode 100644 robots/src/main/java/gui/RobotsProgram.java rename robots/src/{ => main/java}/log/LogChangeListener.java (100%) rename robots/src/{ => main/java}/log/LogEntry.java (100%) rename robots/src/{ => main/java}/log/LogLevel.java (100%) rename robots/src/{ => main/java}/log/LogWindowSource.java (100%) rename robots/src/{ => main/java}/log/Logger.java (100%) diff --git a/robots/src/gui/RobotsProgram.java b/robots/src/gui/RobotsProgram.java deleted file mode 100644 index ae0930a8b..000000000 --- a/robots/src/gui/RobotsProgram.java +++ /dev/null @@ -1,25 +0,0 @@ -package gui; - -import java.awt.Frame; - -import javax.swing.SwingUtilities; -import javax.swing.UIManager; - -public class RobotsProgram -{ - public static void main(String[] args) { - try { - UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); -// UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); -// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); -// UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); - } catch (Exception e) { - e.printStackTrace(); - } - SwingUtilities.invokeLater(() -> { - MainApplicationFrame frame = new MainApplicationFrame(); - frame.pack(); - frame.setVisible(true); - frame.setExtendedState(Frame.MAXIMIZED_BOTH); - }); - }} diff --git a/robots/src/gui/GameVisualizer.java b/robots/src/main/java/gui/GameVisualizer.java similarity index 100% rename from robots/src/gui/GameVisualizer.java rename to robots/src/main/java/gui/GameVisualizer.java diff --git a/robots/src/gui/GameWindow.java b/robots/src/main/java/gui/GameWindow.java similarity index 100% rename from robots/src/gui/GameWindow.java rename to robots/src/main/java/gui/GameWindow.java diff --git a/robots/src/gui/LogWindow.java b/robots/src/main/java/gui/LogWindow.java similarity index 100% rename from robots/src/gui/LogWindow.java rename to robots/src/main/java/gui/LogWindow.java diff --git a/robots/src/gui/MainApplicationFrame.java b/robots/src/main/java/gui/MainApplicationFrame.java similarity index 56% rename from robots/src/gui/MainApplicationFrame.java rename to robots/src/main/java/gui/MainApplicationFrame.java index 62e943ee1..1da75f449 100644 --- a/robots/src/gui/MainApplicationFrame.java +++ b/robots/src/main/java/gui/MainApplicationFrame.java @@ -4,15 +4,7 @@ import java.awt.Toolkit; 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; @@ -98,46 +90,94 @@ protected void addWindow(JInternalFrame frame) 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) -> { - setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - this.invalidate(); - }); - lookAndFeelMenu.add(systemLookAndFeel); - } - { - JMenuItem crossplatformLookAndFeel = new JMenuItem("Универсальная схема", KeyEvent.VK_S); - crossplatformLookAndFeel.addActionListener((event) -> { - setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); - this.invalidate(); - }); - lookAndFeelMenu.add(crossplatformLookAndFeel); - } + makeLookAndFeelMenu(menuBar); + makeTestMenu(menuBar); + makeSystemMenu(menuBar); - JMenu testMenu = new JMenu("Тесты"); - testMenu.setMnemonic(KeyEvent.VK_T); - testMenu.getAccessibleContext().setAccessibleDescription( - "Тестовые команды"); - - { - JMenuItem addLogMessageItem = new JMenuItem("Сообщение в лог", KeyEvent.VK_S); - addLogMessageItem.addActionListener((event) -> { - Logger.debug("Новая строка"); - }); - testMenu.add(addLogMessageItem); - } + return menuBar; + } + + private void makeLookAndFeelMenu(JMenuBar menuBar) { + JMenu lookAndFeelMenu = initializeMenu("Режим отображения", + "Управление режимом отображения приложения"); + + lookAndFeelMenu.add(createSystemLookAndFeelMenuItem("Системная схема")); + lookAndFeelMenu.add(createSystemLookAndFeelMenuItem("Универсальная схема")); menuBar.add(lookAndFeelMenu); + } + + private JMenuItem createSystemLookAndFeelMenuItem(String text) { + JMenuItem systemLookAndFeel = new JMenuItem(text, KeyEvent.VK_S); + systemLookAndFeel.addActionListener((event) -> { + setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + this.invalidate(); + }); + + return systemLookAndFeel; + } + + private void makeTestMenu(JMenuBar menuBar) { + JMenu testMenu = initializeMenu("Тесты", "Тестовые команды"); + + testMenu.add(createTestMenuItem("Сообщение в лог", "Новая строка")); + menuBar.add(testMenu); - return menuBar; + } + + private JMenuItem createTestMenuItem(String text, String logMessage) { + JMenuItem addLogMessageItem = new JMenuItem(text, KeyEvent.VK_S); + addLogMessageItem.addActionListener((event) -> { + Logger.debug(logMessage); + }); + + return addLogMessageItem; + } + + private void makeSystemMenu(JMenuBar menuBar) { + JMenu systemMenu = initializeMenu("Система", "Управление системой"); + + systemMenu.add(createSystemMenuCloseItem("Закрыть")); + + menuBar.add(systemMenu); + } + + private JMenuItem createSystemMenuCloseItem(String text) { + JMenuItem closeItem = new JMenuItem(text, KeyEvent.VK_S); + closeItem.addActionListener((event) -> { + closeWithConfirmation(); + }); + + return closeItem; + } + + private void closeWithConfirmation() { + Object[] options = {"Да", "Нет"}; + int YES = 0; + int NO = 1; + int response = JOptionPane.showOptionDialog(null, + "Закрыть приложение?", + "Подтверждение", + JOptionPane.DEFAULT_OPTION, + JOptionPane.QUESTION_MESSAGE, + null, + options, + options[NO]); + + if (response == YES) { + System.exit(0); + } + } + + + private JMenu initializeMenu(String menuName, String description) { + JMenu someMenu = new JMenu(menuName); + someMenu.setMnemonic(KeyEvent.VK_V); + someMenu.getAccessibleContext().setAccessibleDescription( + description); + + return someMenu; } private void setLookAndFeel(String className) diff --git a/robots/src/main/java/gui/RobotsProgram.java b/robots/src/main/java/gui/RobotsProgram.java new file mode 100644 index 000000000..bb97225db --- /dev/null +++ b/robots/src/main/java/gui/RobotsProgram.java @@ -0,0 +1,24 @@ +package gui; + +import java.awt.Frame; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; + +public class RobotsProgram { + public static void main(String[] args) { + try { + UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); +// UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); +// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); +// UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); + } catch (Exception e) { + e.printStackTrace(); + } + SwingUtilities.invokeLater(() -> { + MainApplicationFrame frame = new MainApplicationFrame(); + frame.pack(); + frame.setVisible(true); + frame.setExtendedState(Frame.MAXIMIZED_BOTH); + }); + } +} diff --git a/robots/src/log/LogChangeListener.java b/robots/src/main/java/log/LogChangeListener.java similarity index 100% rename from robots/src/log/LogChangeListener.java rename to robots/src/main/java/log/LogChangeListener.java diff --git a/robots/src/log/LogEntry.java b/robots/src/main/java/log/LogEntry.java similarity index 100% rename from robots/src/log/LogEntry.java rename to robots/src/main/java/log/LogEntry.java diff --git a/robots/src/log/LogLevel.java b/robots/src/main/java/log/LogLevel.java similarity index 100% rename from robots/src/log/LogLevel.java rename to robots/src/main/java/log/LogLevel.java diff --git a/robots/src/log/LogWindowSource.java b/robots/src/main/java/log/LogWindowSource.java similarity index 100% rename from robots/src/log/LogWindowSource.java rename to robots/src/main/java/log/LogWindowSource.java diff --git a/robots/src/log/Logger.java b/robots/src/main/java/log/Logger.java similarity index 100% rename from robots/src/log/Logger.java rename to robots/src/main/java/log/Logger.java From 243b9b68b7689ac431a74d22d7ddf5bdfe19043f Mon Sep 17 00:00:00 2001 From: hochyspat Date: Wed, 12 Mar 2025 05:28:06 +0500 Subject: [PATCH 2/6] refactored --- .../main/java/gui/MainApplicationFrame.java | 38 ++----------------- robots/src/main/java/gui/RobotsProgram.java | 1 + robots/src/main/java/gui/Select.java | 16 ++++++++ 3 files changed, 20 insertions(+), 35 deletions(-) create mode 100644 robots/src/main/java/gui/Select.java diff --git a/robots/src/main/java/gui/MainApplicationFrame.java b/robots/src/main/java/gui/MainApplicationFrame.java index 1da75f449..2643ab3de 100644 --- a/robots/src/main/java/gui/MainApplicationFrame.java +++ b/robots/src/main/java/gui/MainApplicationFrame.java @@ -58,35 +58,6 @@ protected void addWindow(JInternalFrame 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; -// } - private JMenuBar generateMenuBar() { JMenuBar menuBar = new JMenuBar(); @@ -153,19 +124,16 @@ private JMenuItem createSystemMenuCloseItem(String text) { } private void closeWithConfirmation() { - Object[] options = {"Да", "Нет"}; - int YES = 0; - int NO = 1; int response = JOptionPane.showOptionDialog(null, "Закрыть приложение?", "Подтверждение", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, - options, - options[NO]); + Select.values(), + Select.NO); - if (response == YES) { + if (response == JOptionPane.YES_OPTION) { System.exit(0); } } diff --git a/robots/src/main/java/gui/RobotsProgram.java b/robots/src/main/java/gui/RobotsProgram.java index bb97225db..0b99fe2da 100644 --- a/robots/src/main/java/gui/RobotsProgram.java +++ b/robots/src/main/java/gui/RobotsProgram.java @@ -4,6 +4,7 @@ import javax.swing.SwingUtilities; import javax.swing.UIManager; + public class RobotsProgram { public static void main(String[] args) { try { diff --git a/robots/src/main/java/gui/Select.java b/robots/src/main/java/gui/Select.java new file mode 100644 index 000000000..3cb7fa1d8 --- /dev/null +++ b/robots/src/main/java/gui/Select.java @@ -0,0 +1,16 @@ +package gui; + +public enum Select { + YES("Да"), + NO("Нет"); + + private String res; + Select(String response) { + res = response; + } + + @Override + public String toString() { + return res; + } +} From 140af4848d08b5159de60a2df4f17b335f288b2f Mon Sep 17 00:00:00 2001 From: hochyspat Date: Wed, 19 Mar 2025 04:20:40 +0500 Subject: [PATCH 3/6] refactored --- .gitignore | 4 +- robots/.gitignore | 2 + .../main/java/gui/MainApplicationFrame.java | 51 ++++++++----------- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 2c6eb3893..3f380c3c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ /.metadata /robots/.settings /robots/bin -eclipse.bat \ No newline at end of file +eclipse.bat +/.idea +/out/ \ No newline at end of file diff --git a/robots/.gitignore b/robots/.gitignore index 2757ffa76..f3f64eb0f 100644 --- a/robots/.gitignore +++ b/robots/.gitignore @@ -1,2 +1,4 @@ /bin/ /.settings/ +/.classpath +/.project \ No newline at end of file diff --git a/robots/src/main/java/gui/MainApplicationFrame.java b/robots/src/main/java/gui/MainApplicationFrame.java index 2643ab3de..55cfbc6cb 100644 --- a/robots/src/main/java/gui/MainApplicationFrame.java +++ b/robots/src/main/java/gui/MainApplicationFrame.java @@ -10,56 +10,51 @@ /** * Что требуется сделать: - * 1. Метод создания меню перегружен функционалом и трудно читается. + * 1. Метод создания меню перегружен функционалом и трудно читается. * Следует разделить его на серию более простых методов (или вообще выделить отдельный класс). - * */ -public class MainApplicationFrame extends JFrame -{ +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); + screenSize.width - inset * 2, + screenSize.height - inset * 2); setContentPane(desktopPane); - - + + LogWindow logWindow = createLogWindow(); addWindow(logWindow); GameWindow gameWindow = new GameWindow(); - gameWindow.setSize(400, 400); + gameWindow.setSize(400, 400); addWindow(gameWindow); setJMenuBar(generateMenuBar()); setDefaultCloseOperation(EXIT_ON_CLOSE); } - - protected LogWindow createLogWindow() - { + + protected LogWindow createLogWindow() { LogWindow logWindow = new LogWindow(Logger.getDefaultLogSource()); - logWindow.setLocation(10,10); + logWindow.setLocation(10, 10); logWindow.setSize(300, 800); setMinimumSize(logWindow.getSize()); logWindow.pack(); Logger.debug("Протокол работает"); return logWindow; } - - protected void addWindow(JInternalFrame frame) - { + + protected void addWindow(JInternalFrame frame) { desktopPane.add(frame); frame.setVisible(true); } - - private JMenuBar generateMenuBar() - { + + private JMenuBar generateMenuBar() { JMenuBar menuBar = new JMenuBar(); makeLookAndFeelMenu(menuBar); @@ -147,17 +142,13 @@ private JMenu initializeMenu(String menuName, String description) { return someMenu; } - - private void setLookAndFeel(String className) - { - try - { + + private void setLookAndFeel(String className) { + try { UIManager.setLookAndFeel(className); SwingUtilities.updateComponentTreeUI(this); - } - catch (ClassNotFoundException | InstantiationException - | IllegalAccessException | UnsupportedLookAndFeelException e) - { + } catch (ClassNotFoundException | InstantiationException + | IllegalAccessException | UnsupportedLookAndFeelException e) { // just ignore } } From ccb3cc0a9027407711e0ae55e1469a2579f136f9 Mon Sep 17 00:00:00 2001 From: hochyspat Date: Wed, 19 Mar 2025 04:37:36 +0500 Subject: [PATCH 4/6] refactored --- robots/src/main/java/gui/MainApplicationFrame.java | 9 +-------- robots/src/main/java/gui/RobotsProgram.java | 3 --- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/robots/src/main/java/gui/MainApplicationFrame.java b/robots/src/main/java/gui/MainApplicationFrame.java index 55cfbc6cb..e3a803bbf 100644 --- a/robots/src/main/java/gui/MainApplicationFrame.java +++ b/robots/src/main/java/gui/MainApplicationFrame.java @@ -8,17 +8,11 @@ import log.Logger; -/** - * Что требуется сделать: - * 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; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setBounds(inset, inset, @@ -149,7 +143,6 @@ private void setLookAndFeel(String className) { SwingUtilities.updateComponentTreeUI(this); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { - // just ignore } } } diff --git a/robots/src/main/java/gui/RobotsProgram.java b/robots/src/main/java/gui/RobotsProgram.java index 0b99fe2da..cf75dbc2e 100644 --- a/robots/src/main/java/gui/RobotsProgram.java +++ b/robots/src/main/java/gui/RobotsProgram.java @@ -9,9 +9,6 @@ public class RobotsProgram { public static void main(String[] args) { try { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); -// UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); -// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); -// UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } From 95ef76dc5a7a1e6d87daa90765e7d3af441a3fca Mon Sep 17 00:00:00 2001 From: hochyspat Date: Wed, 19 Mar 2025 04:46:02 +0500 Subject: [PATCH 5/6] refactored --- robots/.classpath | 6 ------ robots/.project | 17 ----------------- .../src/main/java/gui/MainApplicationFrame.java | 3 ++- 3 files changed, 2 insertions(+), 24 deletions(-) delete mode 100644 robots/.classpath delete mode 100644 robots/.project diff --git a/robots/.classpath b/robots/.classpath deleted file mode 100644 index fceb4801b..000000000 --- a/robots/.classpath +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/robots/.project b/robots/.project deleted file mode 100644 index 78e165663..000000000 --- a/robots/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - Robots - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/robots/src/main/java/gui/MainApplicationFrame.java b/robots/src/main/java/gui/MainApplicationFrame.java index e3a803bbf..0e819b99d 100644 --- a/robots/src/main/java/gui/MainApplicationFrame.java +++ b/robots/src/main/java/gui/MainApplicationFrame.java @@ -4,10 +4,11 @@ import java.awt.Toolkit; import java.awt.event.KeyEvent; -import javax.swing.*; import log.Logger; +import javax.swing.*; + public class MainApplicationFrame extends JFrame { private final JDesktopPane desktopPane = new JDesktopPane(); From fab4d3a729e303f077f8a5409d89dbcf58a8d860 Mon Sep 17 00:00:00 2001 From: hochyspat Date: Wed, 19 Mar 2025 04:47:49 +0500 Subject: [PATCH 6/6] delete git ignore --- robots/.gitignore | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 robots/.gitignore diff --git a/robots/.gitignore b/robots/.gitignore deleted file mode 100644 index f3f64eb0f..000000000 --- a/robots/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -/bin/ -/.settings/ -/.classpath -/.project \ No newline at end of file