Доделан логгер, лаб7 хард готова
This commit is contained in:
parent
8dd844aa14
commit
c3fc932a2a
@ -6,7 +6,8 @@ import java.awt.event.ActionListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.text.ParseException;
|
||||
import java.util.HashMap;
|
||||
import java.util.logging.Logger;
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class FormMapWithSetLocomotives extends JComponent {
|
||||
private BufferedImage bufferImg = null;
|
||||
@ -30,7 +31,12 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
JScrollPane listScroller = new JScrollPane();
|
||||
JList listBoxMaps;
|
||||
|
||||
public FormMapWithSetLocomotives() {
|
||||
private final Logger logger;
|
||||
|
||||
public FormMapWithSetLocomotives(Logger logger) {
|
||||
|
||||
this.logger = logger;
|
||||
|
||||
formFrame = new JFrame("Form Map With SetLocomotives");
|
||||
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
formFrame.setSize(750, 500);
|
||||
@ -79,7 +85,7 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
return;
|
||||
}
|
||||
_mapsCollection.AddMap(textFieldNewMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString()));
|
||||
Logger.getGlobal().info("Map " + textFieldNewMapName.getText() + " added");
|
||||
logger.log(Level.INFO, "Map " + textFieldNewMapName.getText() + " added");
|
||||
ReloadMaps();
|
||||
});
|
||||
statusPanel.add(addMapButton);
|
||||
@ -89,7 +95,7 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
listBoxMaps.addListSelectionListener(e -> {
|
||||
if(listBoxMaps.getSelectedValue() == null) return;
|
||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
||||
Logger.getGlobal().info("Map switched to " + listBoxMaps.getSelectedValue().toString());
|
||||
logger.log(Level.INFO,"Map switched to " + listBoxMaps.getSelectedValue().toString());
|
||||
repaint();
|
||||
});
|
||||
statusPanel.add(listBoxMaps);
|
||||
@ -108,7 +114,7 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
}
|
||||
if(listBoxMaps.getSelectedValue().toString() == null) return;
|
||||
_mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString());
|
||||
Logger.getGlobal().info("Map " +listBoxMaps.getSelectedValue().toString() +" deleted");
|
||||
logger.log(Level.INFO,"Map " +listBoxMaps.getSelectedValue().toString() +" deleted");
|
||||
ReloadMaps();
|
||||
repaint();
|
||||
});
|
||||
@ -117,35 +123,35 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
// Кнопка добавления локомотива
|
||||
JButton addLocomotiveButton = new JButton("Add Locomotive");
|
||||
addLocomotiveButton.addActionListener(e -> {
|
||||
FormLocomotiveConfig formLocomotiveConfig = new FormLocomotiveConfig();
|
||||
formLocomotiveConfig.setVisible(true);
|
||||
formLocomotiveConfig.AddListener(locomotive -> {
|
||||
if (listBoxMaps.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
if (locomotive!=null) {
|
||||
try {
|
||||
DrawningObjectLocomotive objectLocomotive = new DrawningObjectLocomotive(locomotive);
|
||||
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(objectLocomotive)!= -1){
|
||||
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
|
||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
||||
Logger.getGlobal().info("Object " + locomotive + " added");
|
||||
repaint();
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION);
|
||||
}
|
||||
FormLocomotiveConfig formLocomotiveConfig = new FormLocomotiveConfig();
|
||||
formLocomotiveConfig.setVisible(true);
|
||||
formLocomotiveConfig.AddListener(locomotive -> {
|
||||
if (listBoxMaps.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
if (locomotive!=null) {
|
||||
try {
|
||||
DrawningObjectLocomotive objectLocomotive = new DrawningObjectLocomotive(locomotive);
|
||||
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(objectLocomotive)!= -1){
|
||||
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
|
||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
||||
logger.log(Level.INFO,"Object " + locomotive + " added");
|
||||
repaint();
|
||||
}
|
||||
catch (StorageOverflowException ex) {
|
||||
JOptionPane.showMessageDialog(formFrame, "Storage overflow error: "+ ex.getMessage());
|
||||
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(formFrame, "Unknown error: "+ ex.getMessage());
|
||||
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||
else {
|
||||
JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION);
|
||||
}
|
||||
}
|
||||
});
|
||||
catch (StorageOverflowException ex) {
|
||||
JOptionPane.showMessageDialog(formFrame, "Storage overflow error: "+ ex.getMessage());
|
||||
logger.log(Level.WARN,"Error " + ex.getMessage());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(formFrame, "Unknown error: "+ ex.getMessage());
|
||||
logger.log(Level.FATAL,"Error " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
statusPanel.add(addLocomotiveButton);
|
||||
|
||||
@ -175,7 +181,7 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Minus(position) != null) {
|
||||
JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION);
|
||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
||||
Logger.getGlobal().info("Locomotive deleted at position " + position);
|
||||
logger.log(Level.INFO,"Locomotive deleted at position " + position);
|
||||
repaint();
|
||||
}
|
||||
else{
|
||||
@ -183,11 +189,11 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
}}
|
||||
catch (LocomotiveNotFoundException ex) {
|
||||
JOptionPane.showMessageDialog(formFrame, "Locomotive not found error: " + ex.getMessage());
|
||||
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||
logger.log(Level.WARN,"Error " + ex.getMessage());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(formFrame, "Unknown error: "+ ex.getMessage());
|
||||
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||
logger.log(Level.FATAL,"Error " + ex.getMessage());
|
||||
}
|
||||
});
|
||||
statusPanel.add(deleteLocomotiveButton);
|
||||
@ -215,9 +221,13 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowOnMap();
|
||||
repaint();
|
||||
}
|
||||
catch (StorageOverflowException ex) {
|
||||
JOptionPane.showMessageDialog(formFrame, "Storage overflow error: "+ ex.getMessage());
|
||||
logger.log(Level.WARN,"Error " + ex.getMessage());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, "Unknown error: "+ ex.getMessage());
|
||||
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||
logger.log(Level.FATAL,"Error " + ex.getMessage());
|
||||
}
|
||||
});
|
||||
statusPanel.add(showOnMapButton);
|
||||
@ -305,11 +315,11 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
try {
|
||||
_mapsCollection.SaveData(fileChooser.getSelectedFile().getAbsolutePath());
|
||||
JOptionPane.showMessageDialog(null, "Save success");
|
||||
Logger.getGlobal().info("Saved all to " + fileChooser.getSelectedFile().getAbsolutePath());
|
||||
logger.log(Level.INFO,"Saved all to " + fileChooser.getSelectedFile().getAbsolutePath());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, "Save error: " + ex.getMessage());
|
||||
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||
logger.log(Level.ERROR,"Error " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@ -324,13 +334,13 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
try {
|
||||
_mapsCollection.LoadData(fileChooser.getSelectedFile().getAbsolutePath());
|
||||
JOptionPane.showMessageDialog(null, "Load success");
|
||||
Logger.getGlobal().info("Loaded all from " + fileChooser.getSelectedFile().getAbsolutePath());
|
||||
logger.log(Level.INFO,"Loaded all from " + fileChooser.getSelectedFile().getAbsolutePath());
|
||||
ReloadMaps();
|
||||
repaint();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, "Load error: " + ex.getMessage());
|
||||
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||
logger.log(Level.ERROR,"Error " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -350,12 +360,12 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
try {
|
||||
_mapsCollection.SaveMap(listBoxMaps.getSelectedValue().toString(), fileChooser.getSelectedFile().getAbsolutePath());
|
||||
JOptionPane.showMessageDialog(null, "Map saving success");
|
||||
Logger.getGlobal().info("Saved map to " + fileChooser.getSelectedFile().getAbsolutePath());
|
||||
logger.log(Level.INFO,"Saved map to " + fileChooser.getSelectedFile().getAbsolutePath());
|
||||
repaint();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, "Map saving error: " + ex.getMessage());
|
||||
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||
logger.log(Level.ERROR,"Error " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -370,13 +380,13 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
try {
|
||||
_mapsCollection.LoadMap(fileChooser.getSelectedFile().getAbsolutePath());
|
||||
JOptionPane.showMessageDialog(null, "Load Map success");
|
||||
Logger.getGlobal().info("Loaded map from " + fileChooser.getSelectedFile().getAbsolutePath());
|
||||
logger.log(Level.INFO,"Loaded map from " + fileChooser.getSelectedFile().getAbsolutePath());
|
||||
ReloadMaps();
|
||||
repaint();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, "Load Map error: " + ex.getMessage());
|
||||
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||
logger.log(Level.ERROR,"Error " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
21
Main.java
21
Main.java
@ -1,24 +1,11 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.LogManager;
|
||||
import java.util.logging.Logger;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class Main {
|
||||
private static Logger logger = null;
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
FileInputStream stream = new FileInputStream(new File("C:/secondCourse/OOP/PIbd-23_Mochalov_D.V._Locomotive_Hard/log.config"));
|
||||
LogManager.getLogManager().readConfiguration(stream);
|
||||
logger = Logger.getGlobal();
|
||||
stream.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
new FormMapWithSetLocomotives();
|
||||
logger = LogManager.getLogger(Main.class);
|
||||
new FormMapWithSetLocomotives(logger);
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.zip.DataFormatException;
|
||||
|
||||
public class MapsCollection {
|
||||
@ -92,13 +91,11 @@ public class MapsCollection {
|
||||
File file = new File(filename);
|
||||
if (!file.exists())
|
||||
{
|
||||
Logger.getGlobal().severe("FileNotFoundException " + filename);
|
||||
throw new FileNotFoundException("Файл не найден");
|
||||
}
|
||||
BufferedReader br = new BufferedReader(new FileReader(filename));
|
||||
String curLine = br.readLine();
|
||||
if (!curLine.contains("SingleMap")) {
|
||||
Logger.getGlobal().severe("FileFormatException " + filename);
|
||||
throw new DataFormatException("Неверный формат данных");
|
||||
}
|
||||
String mapName = br.readLine();
|
||||
@ -136,14 +133,12 @@ public class MapsCollection {
|
||||
File file = new File(filename);
|
||||
if (!file.exists())
|
||||
{
|
||||
Logger.getGlobal().severe("FileNotFoundException " + filename);
|
||||
throw new FileNotFoundException("Файл не найден");
|
||||
}
|
||||
|
||||
BufferedReader br = new BufferedReader(new FileReader(filename));
|
||||
String curLine = br.readLine();
|
||||
if (!curLine.contains("MapsCollection")) {
|
||||
Logger.getGlobal().severe("FileFormatException " + filename);
|
||||
throw new DataFormatException("Неверный формат данных");
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class SetLocomotivesGeneric <T>
|
||||
{
|
||||
@ -26,7 +25,6 @@ public class SetLocomotivesGeneric <T>
|
||||
public int Insert (T locomotive, int position) throws StorageOverflowException{
|
||||
if (position < 0) return -1;
|
||||
if (Count() >= _maxCount) {
|
||||
Logger.getGlobal().warning("StorageOverflowException");
|
||||
throw new StorageOverflowException(_maxCount);
|
||||
}
|
||||
_places.add(position, locomotive);
|
||||
@ -36,7 +34,6 @@ public class SetLocomotivesGeneric <T>
|
||||
public T Remove (int position) throws LocomotiveNotFoundException {
|
||||
if (position >= _maxCount || position < 0) return null;
|
||||
if (_places.get(position) == null) {
|
||||
Logger.getGlobal().warning("LocomotiveNotFoundException at " + position);
|
||||
throw new LocomotiveNotFoundException(position);
|
||||
}
|
||||
T result = _places.get(position);
|
||||
|
25
log4j2.xml
Normal file
25
log4j2.xml
Normal file
@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="info" >
|
||||
<Appenders>
|
||||
<File name="error" fileName="error.log" append="true">
|
||||
<PatternLayout>
|
||||
<Pattern>%-5level %msg (%d{dd.MM.yyyy})%n</Pattern>
|
||||
</PatternLayout>
|
||||
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
</File>
|
||||
<File name="info" fileName="info.log" append="true">
|
||||
<PatternLayout>
|
||||
<Pattern>%-5level %msg (%d{dd.MM.yyyy})%n</Pattern>
|
||||
</PatternLayout>
|
||||
<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="ACCEPT"/>
|
||||
</File>
|
||||
</Appenders>
|
||||
|
||||
<Loggers>
|
||||
<Root level="INFO">
|
||||
<AppenderRef ref="info" />
|
||||
<AppenderRef ref="error" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
|
||||
</Configuration>
|
30
pom.xml
Normal file
30
pom.xml
Normal file
@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>groupId</groupId>
|
||||
<artifactId>PIbd-23_Mochalov_D.V._Locomotive_Hard</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>19</maven.compiler.source>
|
||||
<maven.compiler.target>19</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<version>2.19.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.19.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
Loading…
Reference in New Issue
Block a user