Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/Robots.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added out/production/Robots/gui/GameVisualizer$1.class
Binary file not shown.
Binary file added out/production/Robots/gui/GameVisualizer$2.class
Binary file not shown.
Binary file added out/production/Robots/gui/GameVisualizer$3.class
Binary file not shown.
Binary file added out/production/Robots/gui/GameVisualizer.class
Binary file not shown.
Binary file added out/production/Robots/gui/GameWindow.class
Binary file not shown.
Binary file added out/production/Robots/gui/Listener.class
Binary file not shown.
Binary file added out/production/Robots/gui/LogWindow.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added out/production/Robots/gui/RobotsProgram.class
Binary file not shown.
Binary file added out/production/Robots/log/LogChangeListener.class
Binary file not shown.
Binary file added out/production/Robots/log/LogEntry.class
Binary file not shown.
Binary file added out/production/Robots/log/LogLevel.class
Binary file not shown.
Binary file added out/production/Robots/log/LogWindowSource.class
Binary file not shown.
Binary file added out/production/Robots/log/Logger.class
Binary file not shown.
8 changes: 8 additions & 0 deletions robots/src/gui/Listener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowListener;

public class Listener extends WindowAdapter {

}
186 changes: 158 additions & 28 deletions robots/src/gui/MainApplicationFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyVetoException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
Expand All @@ -13,6 +20,7 @@
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JOptionPane;

import log.Logger;

Expand All @@ -25,7 +33,23 @@
public class MainApplicationFrame extends JFrame
{
private final JDesktopPane desktopPane = new JDesktopPane();


private final LogWindow logWindowGlobal;
private final GameWindow gameWindowGlobal;
private static final String WINDOW_STATE_FILE = System.getProperty("user.home") + "\\window_state.properties";

private void confirmExit() {
int result = JOptionPane.showConfirmDialog(this,
"Вы действительно хотите выйти?",
"Подтверждение выхода",
JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
saveWindowStates();
dispose();
System.exit(0);
}
}

public MainApplicationFrame() {
//Make the big window be indented 50 pixels from each edge
//of the screen.
Expand All @@ -39,14 +63,24 @@ public MainApplicationFrame() {


LogWindow logWindow = createLogWindow();
logWindowGlobal = logWindow;
addWindow(logWindow);

GameWindow gameWindow = new GameWindow();
gameWindowGlobal = gameWindow;
gameWindow.setSize(400, 400);
addWindow(gameWindow);

loadWindowStates();

setJMenuBar(generateMenuBar());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e){
confirmExit();
}
});
}

protected LogWindow createLogWindow()
Expand All @@ -65,15 +99,85 @@ protected void addWindow(JInternalFrame frame)
desktopPane.add(frame);
frame.setVisible(true);
}


private void saveWindowStates() {
Properties props = new Properties();

props.setProperty("frame.x", String.valueOf(getX()));
props.setProperty("frame.y", String.valueOf(getY()));
props.setProperty("frame.width", String.valueOf(getWidth()));
props.setProperty("frame.height", String.valueOf(getHeight()));
props.setProperty("frame.extendedState", String.valueOf(getExtendedState()));

saveInternalFrameState(props, logWindowGlobal, "LogWindow");
saveInternalFrameState(props, gameWindowGlobal, "GameWindow");

try (FileOutputStream out = new FileOutputStream(WINDOW_STATE_FILE)) {
props.store(out, "Window states");
} catch (IOException e) {
System.err.println("Не удалось сохранить состояние окон: " + e.getMessage());
}
}

private void saveInternalFrameState(Properties props, JInternalFrame frame, String prefix) {
props.setProperty(prefix + ".x", String.valueOf(frame.getX()));
props.setProperty(prefix + ".y", String.valueOf(frame.getY()));
props.setProperty(prefix + ".width", String.valueOf(frame.getWidth()));
props.setProperty(prefix + ".height", String.valueOf(frame.getHeight()));
props.setProperty(prefix + ".isIcon", String.valueOf(frame.isIcon()));
props.setProperty(prefix + ".isMaximum", String.valueOf(frame.isMaximum()));
}

private void loadWindowStates() {
Properties props = new Properties();
try (FileInputStream in = new FileInputStream(WINDOW_STATE_FILE)) {
props.load(in);
} catch (IOException e) {
return;
}

try {
int x = Integer.parseInt(props.getProperty("frame.x"));
int y = Integer.parseInt(props.getProperty("frame.y"));
int width = Integer.parseInt(props.getProperty("frame.width"));
int height = Integer.parseInt(props.getProperty("frame.height"));
setBounds(x, y, width, height);
int extendedState = Integer.parseInt(props.getProperty("frame.extendedState"));
setExtendedState(extendedState);
} catch (NumberFormatException e) {}

loadInternalFrameState(props, logWindowGlobal, "LogWindow");
loadInternalFrameState(props, gameWindowGlobal, "GameWindow");
}


private void loadInternalFrameState(Properties props, JInternalFrame frame, String prefix) {
try {
int x = Integer.parseInt(props.getProperty(prefix + ".x"));
int y = Integer.parseInt(props.getProperty(prefix + ".y"));
int width = Integer.parseInt(props.getProperty(prefix + ".width"));
int height = Integer.parseInt(props.getProperty(prefix + ".height"));
boolean isIcon = Boolean.parseBoolean(props.getProperty(prefix + ".isIcon"));
boolean isMaximum = Boolean.parseBoolean(props.getProperty(prefix + ".isMaximum"));

// Сначала устанавливаем границы, потом развёрнутость/свёрнутость
frame.setBounds(x, y, width, height);
if (isMaximum) {
frame.setMaximum(true);
} else {
frame.setIcon(isIcon);
}
} catch (NumberFormatException | PropertyVetoException e) {}
}

// 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);
Expand All @@ -82,7 +186,7 @@ protected void addWindow(JInternalFrame frame)
// menuItem.setActionCommand("new");
//// menuItem.addActionListener(this);
// menu.add(menuItem);
//
//
// //Set up the second menu item.
// menuItem = new JMenuItem("Quit");
// menuItem.setMnemonic(KeyEvent.VK_Q);
Expand All @@ -91,52 +195,78 @@ protected void addWindow(JInternalFrame frame)
// menuItem.setActionCommand("quit");
//// menuItem.addActionListener(this);
// menu.add(menuItem);
//
//
// return menuBar;
// }
private JMenuBar generateMenuBar()

private void addSystemLookAndFeel(JMenu lookAndFeelMenu)
{
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);
}
}

{
private void addCrossplatformLookAndFeel(JMenu lookAndFeelMenu)
{
JMenuItem crossplatformLookAndFeel = new JMenuItem("Универсальная схема", KeyEvent.VK_S);
crossplatformLookAndFeel.addActionListener((event) -> {
setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
this.invalidate();
});
lookAndFeelMenu.add(crossplatformLookAndFeel);
}
}

private void addLogMessageItem(JMenu testMenu)
{
JMenuItem addLogMessageItem = new JMenuItem("Сообщение в лог", KeyEvent.VK_S);
addLogMessageItem.addActionListener((event) -> {
Logger.debug("Новая строка");
});
testMenu.add(addLogMessageItem);
}

private void addExitLookAndFeel(JMenu exitMenu)
{
JMenuItem addLogMessageItem = new JMenuItem("Завершение работы программы", KeyEvent.VK_S);
addLogMessageItem.addActionListener((event) -> {
confirmExit();
});
exitMenu.add(addLogMessageItem);
}

private JMenuBar generateMenuBar()
{
JMenuBar menuBar = new JMenuBar();

JMenu lookAndFeelMenu = new JMenu("Режим отображения");
lookAndFeelMenu.setMnemonic(KeyEvent.VK_V);
lookAndFeelMenu.getAccessibleContext().setAccessibleDescription(
"Управление режимом отображения приложения");

addSystemLookAndFeel(lookAndFeelMenu);

addCrossplatformLookAndFeel(lookAndFeelMenu);

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);
}

addLogMessageItem(testMenu);

JMenu exitMenu = new JMenu("Выход");
exitMenu.setMnemonic(KeyEvent.VK_T);
exitMenu.getAccessibleContext().setAccessibleDescription(
"Меню выхода");

addExitLookAndFeel(exitMenu);

menuBar.add(lookAndFeelMenu);
menuBar.add(testMenu);
menuBar.add(exitMenu);
return menuBar;
}

Expand Down
6 changes: 6 additions & 0 deletions robots/src/gui/RobotsProgram.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package gui;

import java.awt.Frame;
import java.util.Locale;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class RobotsProgram
{
public static void main(String[] args) {
UIManager.put("OptionPane.yesButtonText", "Да");
UIManager.put("OptionPane.noButtonText", "Нет");
UIManager.put("OptionPane.okButtonText", "OK");
UIManager.put("OptionPane.cancelButtonText", "Отмена");
UIManager.put("OptionPane.titleText", "Подтверждение");
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
// UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
Expand Down
13 changes: 5 additions & 8 deletions robots/src/log/LogWindowSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,16 @@ public void append(LogLevel logLevel, String strMessage)
{
LogEntry entry = new LogEntry(logLevel, strMessage);
m_messages.add(entry);
LogChangeListener [] activeListeners = m_activeListeners;
if (activeListeners == null)
if(m_messages.size() > m_iQueueLength)
m_messages.removeFirst();
if (m_activeListeners == null)
{
synchronized (m_listeners)
{
if (m_activeListeners == null)
{
activeListeners = m_listeners.toArray(new LogChangeListener [0]);
m_activeListeners = activeListeners;
}
m_activeListeners = m_listeners.toArray(new LogChangeListener [0]);
}
}
for (LogChangeListener listener : activeListeners)
for (LogChangeListener listener : m_activeListeners)
{
listener.onLogChanged();
}
Expand Down
2 changes: 1 addition & 1 deletion robots/src/log/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public final class Logger
{
private static final LogWindowSource defaultLogSource;
static {
defaultLogSource = new LogWindowSource(100);
defaultLogSource = new LogWindowSource(5);
}

private Logger()
Expand Down