это гипо

This commit is contained in:
antoc0der 2023-12-21 19:15:28 +03:00
parent b3c2b175c7
commit 7dd4ec395c
3 changed files with 97 additions and 49 deletions

View File

@ -5,7 +5,8 @@ import src.Generics.AirplaneGenericCollection;
import src.MovementStrategy.DrawningObjectAirplane; import src.MovementStrategy.DrawningObjectAirplane;
import src.Generics.AirplaneGenericStorage; import src.Generics.AirplaneGenericStorage;
import src.Generics.AirplaneTrashCollection; import src.Generics.AirplaneTrashCollection;
import src.DrawningObjects.ExtentionDrawningAirplane; import src.Exceptions.AirplaneNotFoundException;
import src.Exceptions.AirplaneStorageOverflowException;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -14,9 +15,8 @@ import java.awt.event.ActionListener;
import java.util.List; import java.util.List;
import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileFilter;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.FileNotFoundException;
import org.apache.logging.log4j.*; import org.apache.logging.log4j.*;
@ -37,6 +37,7 @@ class TxtSaveFilter extends FileFilter {
} }
public class FormAirplaneCollection { public class FormAirplaneCollection {
private final Logger _logger;
private final AirplaneGenericStorage _storage; private final AirplaneGenericStorage _storage;
private JList<String> listBoxStorages; private JList<String> listBoxStorages;
private DefaultListModel<String> listBoxModel; private DefaultListModel<String> listBoxModel;
@ -64,6 +65,7 @@ public class FormAirplaneCollection {
} }
public FormAirplaneCollection() { public FormAirplaneCollection() {
_logger = LogManager.getLogger("logger");
JMenuBar menuFile = new JMenuBar(); JMenuBar menuFile = new JMenuBar();
JMenu file = new JMenu("Файл"); JMenu file = new JMenu("Файл");
menuFile.add(file); menuFile.add(file);
@ -123,8 +125,10 @@ public class FormAirplaneCollection {
File file = new File(fc.getSelectedFile() + "." + "txt"); File file = new File(fc.getSelectedFile() + "." + "txt");
try { try {
_storage.SaveData(file); _storage.SaveData(file);
} catch (IOException ex) { _logger.info("Сохранено в файл: " + file.getAbsolutePath());
throw new RuntimeException(ex); } catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Ошибка при сохранении всех объектов " + ex.getMessage(), "Результат", JOptionPane.ERROR_MESSAGE);
_logger.error("Не удалось сохранить объекты в файл: " + file.getAbsolutePath());
} }
} }
} }
@ -143,8 +147,10 @@ public class FormAirplaneCollection {
} }
_storage._airplaneStorages.get(listBoxStorages.getSelectedValue()).SaveData(file, listBoxStorages.getSelectedValue()); _storage._airplaneStorages.get(listBoxStorages.getSelectedValue()).SaveData(file, listBoxStorages.getSelectedValue());
ReloadObjects(); ReloadObjects();
} catch (IOException ex) { _logger.info("Сохранена коллекция в файл: " + file.getAbsolutePath());
throw new RuntimeException(ex); } catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Ошибка при сохранении коллекции " + ex.getMessage(), "Результат", JOptionPane.ERROR_MESSAGE);
_logger.error("Не удалось сохранить коллекцию в файл: " + file.getAbsolutePath());
} }
} }
} }
@ -159,13 +165,17 @@ public class FormAirplaneCollection {
try { try {
_storage.LoadData(file); _storage.LoadData(file);
canv._storage =_storage; canv._storage =_storage;
ReloadObjects(); _logger.info("Загружено из файла: " + file.getAbsolutePath());
canv.repaint(); } catch (FileNotFoundException ex) {
} catch (IOException ex) { JOptionPane.showMessageDialog(null, "Ошибка при загрузке всех объектов " + ex.getMessage(), "Результат", JOptionPane.ERROR_MESSAGE);
throw new RuntimeException(ex); _logger.error("Не удалось загрузить объекты из файла: " + file.getAbsolutePath());
}
catch (Exception ex){
_logger.fatal("фатальная ошибка");
} }
} }
ReloadObjects();
canv.repaint();
} }
}); });
loadCollection.addActionListener(new ActionListener() { loadCollection.addActionListener(new ActionListener() {
@ -179,8 +189,10 @@ public class FormAirplaneCollection {
_storage.LoadCollection(file); _storage.LoadCollection(file);
ReloadObjects(); ReloadObjects();
canv.repaint(); canv.repaint();
_logger.info("Загружен файл с коллекцией: " + file.getAbsolutePath());
} catch (IOException ex) { } catch (IOException ex) {
throw new RuntimeException(ex); JOptionPane.showMessageDialog(null, "Ошибка при загрузке коллекции " + ex.getMessage(), "Результат", JOptionPane.ERROR_MESSAGE);
_logger.error("Не удалось загрузить коллекцию из файла: " + file.getAbsolutePath());
} }
} }
} }
@ -189,8 +201,9 @@ public class FormAirplaneCollection {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (storageName.getText() == null) if (storageName.getText() == null)
return; _logger.error("Не удалось добавить набор: " + storageName);
_storage.AddSet(storageName.getText()); _storage.AddSet(storageName.getText());
_logger.info("Добавлен набор: " + storageName.getText());
ReloadObjects(); ReloadObjects();
} }
}); });
@ -198,10 +211,11 @@ public class FormAirplaneCollection {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (listBoxStorages.getSelectedIndex() == -1) { if (listBoxStorages.getSelectedIndex() == -1) {
return; _logger.error("Не выбран набор для удаления");
} }
_storage.DelSet(listBoxStorages.getSelectedValue(), _trashCollection); _storage.DelSet(listBoxStorages.getSelectedValue(), _trashCollection);
ReloadObjects(); ReloadObjects();
_logger.info("Удален набор: " + listBoxStorages.getSelectedValue());
} }
}); });
callTrashButton.addActionListener(new ActionListener() { callTrashButton.addActionListener(new ActionListener() {
@ -218,24 +232,33 @@ public class FormAirplaneCollection {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (listBoxStorages.getSelectedIndex() == -1) { if (listBoxStorages.getSelectedIndex() == -1) {
return; _logger.warn("Не выбрана коллекция для добавления объекта");
} }
AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes = _storage.Get(listBoxStorages.getSelectedValue()); AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes = _storage.Get(listBoxStorages.getSelectedValue());
FormAirplaneConfig form = new FormAirplaneConfig(); FormAirplaneConfig form = new FormAirplaneConfig();
form.addButton.addActionListener(new ActionListener() { form.addButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (_airplanes.Insert(form._airplane)) { try {
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); if (_airplanes.Insert(form._airplane)) {
form._airplane._pictureWidth = pictureBoxWidth; JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
form._airplane._pictureHeight = pictureBoxHeight; form._airplane._pictureWidth = pictureBoxWidth;
Draw(); form._airplane._pictureHeight = pictureBoxHeight;
} else { Draw();
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); } else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
form.frameConfig.dispose();
Draw();
_logger.info("Добавлен объект");
} catch (AirplaneStorageOverflowException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
_logger.warn("Не удалось добавить объект " + ex.getMessage());
}
catch (Exception ex){
_logger.fatal("фатальная ошибка");
} }
form.frameConfig.dispose();
Draw();
} }
}); });
form.cancelButton.addActionListener(new ActionListener() { form.cancelButton.addActionListener(new ActionListener() {
@ -251,26 +274,29 @@ public class FormAirplaneCollection {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (listBoxStorages.getSelectedIndex() == -1) { if (listBoxStorages.getSelectedIndex() == -1) {
return; _logger.warn("Не выбрана коллекция для удаления объекта");
} }
AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes = _storage.Get(listBoxStorages.getSelectedValue()); AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes = _storage.Get(listBoxStorages.getSelectedValue());
if (_airplanes == null) { if (_airplanes == null) {
return; _logger.warn("Набор равен null");
} }
String tmp = airplaneNumb.getText(); String tmp = airplaneNumb.getText();
int numb; int numb=Integer.parseInt(tmp);
try { try {
numb = Integer.parseInt(tmp); DrawningAirplane curAirplane = _airplanes.Get(numb);
} catch (Exception ex) { _trashCollection.Push(curAirplane);
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE); _airplanes.Remove(numb);
return; _airplanes.ShowAirplanes();
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
_logger.info("Удален объект по позиции " + numb);
} catch (AirplaneNotFoundException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
_logger.warn("Не удалось удалить объект" + ex.getMessage());
}
catch (Exception ex){
_logger.fatal("фатальная ошибка");
} }
DrawningAirplane curAirplane = _airplanes.Get(numb);
_trashCollection.Push(curAirplane);
_airplanes.Remove(numb);
_airplanes.ShowAirplanes();
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
} }
}); });

View File

@ -1,19 +1,9 @@
package src; package src;
import src.DrawningObjects.DrawningAirplaneWithRadar;
import src.DrawningObjects.DrawningAirplane;
import src.MovementStrategy.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main { public class Main {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
System.setProperty("log4j.configurationFile", "C:\\Users\\1\\Desktop\\улгту\\2 курс\\РПП\\PIbd-23_Nasyrov_A_G_AirplaneWithRadar_hard\\src\\loggerFile.xml");
FormAirplaneCollection form = new FormAirplaneCollection(); FormAirplaneCollection form = new FormAirplaneCollection();
} }
} }

32
src/loggerFile.xml Normal file
View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<!-- Секция аппендеров -->
<Appenders>
<!-- Файловый аппендер -->
<File name="fileWarnings" fileName="logWarnings.log">
<PatternLayout>
<Pattern> %m (дата-%d{d.M.y}) %ex%n</Pattern>
</PatternLayout>
</File>
<File name="fileInfo" fileName="logInfo.log">
<PatternLayout>
<Pattern> %m (дата-%d{d.M.y}) %ex%n</Pattern>
</PatternLayout>
<Filters>
<!-- Now deny warn, error and fatal messages -->
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<!-- This filter accepts info, warn, error, fatal and denies debug/trace -->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</File>
</Appenders>
<!-- Секция логгеров -->
<Loggers>
<Logger name="logger" level="info" additivity="false">
<AppenderRef ref="fileInfo" level="INFO"/>
<AppenderRef ref="fileWarnings" level="WARN"/>
</Logger>
</Loggers>
</Configuration>