From cd966ac79447621c9a9233613c3713a36e8ee37b Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Thu, 15 Dec 2022 01:25:45 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B5=D0=B4=D1=8C=D0=BC=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AirBomberNotFoundException.java | 15 ++ .../JFrameMapWithSetAirBombers.java | 130 ++++++++++++++---- .../MapWithSetAirBombersGeneric.java | 10 +- .../src/AirBomberPackage/MapsCollection.java | 9 +- .../SetAirBombersGeneric.java | 19 +-- .../StorageOverflowException.java | 15 ++ .../src/AirBomberPackage/loggerSettings.xml | 32 +++++ 7 files changed, 190 insertions(+), 40 deletions(-) create mode 100644 AirBomber/src/AirBomberPackage/AirBomberNotFoundException.java create mode 100644 AirBomber/src/AirBomberPackage/StorageOverflowException.java create mode 100644 AirBomber/src/AirBomberPackage/loggerSettings.xml diff --git a/AirBomber/src/AirBomberPackage/AirBomberNotFoundException.java b/AirBomber/src/AirBomberPackage/AirBomberNotFoundException.java new file mode 100644 index 0000000..c752550 --- /dev/null +++ b/AirBomber/src/AirBomberPackage/AirBomberNotFoundException.java @@ -0,0 +1,15 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package AirBomberPackage; + +/** + * + * @author Андрей + */ +public class AirBomberNotFoundException extends Exception { + public AirBomberNotFoundException(int i){ + super("Не найден объект по позиции " + i); + } +} diff --git a/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java index bcfbd99..4536d41 100644 --- a/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java +++ b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java @@ -8,6 +8,9 @@ import javax.swing.DefaultListModel; import java.util.HashMap; import java.util.LinkedList; import javax.swing.*; +import org.apache.logging.log4j.*; +import java.io.*; +import java.nio.file.FileSystemException; /** * @@ -20,6 +23,8 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { */ public JFrameMapWithSetAirBombers() { initComponents(); + System.setProperty("log4j.configurationFile","C:\\Users\\Андрей\\Documents\\loggerSettings.xml"); + logger = LogManager.getLogger("logger"); _mapsDict.put("Простая карта", new SimpleMap()); _mapsDict.put("Городская карта", new CityMap()); _mapsDict.put("Линейная карта", new LineMap()); @@ -36,6 +41,8 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { private MapsCollection _mapsCollection; private LinkedList deletedAirBombers = new LinkedList<>(); + + private Logger logger; /** * This method is called from within the constructor to initialize the form. @@ -334,8 +341,25 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { return; } JFrameAirBomberConfig airBomberConfig = new JFrameAirBomberConfig(); - airBomberConfig.addEvent(airBomber -> {_mapsCollection.Get(listBoxMaps.getSelectedValue()).add(new DrawingObjectAirBomber(airBomber)); - airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).ShowSet(), 0, 0, null);}); + airBomberConfig.addEvent(airBomber -> { + try{ + _mapsCollection.Get(listBoxMaps.getSelectedValue()).add(new DrawingObjectAirBomber(airBomber)); + airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).ShowSet(), 0, 0, null); + logger.log(Level.INFO, "Добавлен объект " + airBomber); + } + catch (AirBomberNotFoundException ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.WARN, "Ошибка: " + ex.getMessage()); + } + catch (StorageOverflowException ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.WARN, "Ошибка: " + ex.getMessage()); + } + catch (Exception ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.FATAL, "Ошибка: " + ex.getMessage()); + } + }); airBomberConfig.setVisible(true); }//GEN-LAST:event_buttonAddAirBomberActionPerformed @@ -353,16 +377,20 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { return; } int pos = Integer.parseInt(maskedTextBoxPosition.getText()); - DrawingObjectAirBomber deletedAirBomber = (DrawingObjectAirBomber) _mapsCollection.Get(listBoxMaps.getSelectedValue()).remove(pos); - if (deletedAirBomber != null) - { + try{ + DrawingObjectAirBomber deletedAirBomber = (DrawingObjectAirBomber) _mapsCollection.Get(listBoxMaps.getSelectedValue()).remove(pos); deletedAirBombers.add(deletedAirBomber); JOptionPane.showMessageDialog(null,"Объект удален"); airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).ShowSet(), 0, 0, null); + logger.log(Level.INFO, "Удалён объект " + deletedAirBomber); } - else - { - JOptionPane.showMessageDialog(null,"Не удалось удалить объект!"); + catch (AirBomberNotFoundException ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.WARN, "Ошибка: " + ex.getMessage()); + } + catch (Exception ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.FATAL, "Ошибка: " + ex.getMessage()); } }//GEN-LAST:event_buttonRemoveAirBomberActionPerformed @@ -377,7 +405,21 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { if (listBoxMaps.getSelectedIndex() == -1){ return; } - airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).ShowOnMap(), 0, 0, null); + try{ + airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).ShowOnMap(), 0, 0, null); + } + catch (AirBomberNotFoundException ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.WARN, "Ошибка: " + ex.getMessage()); + } + catch (StorageOverflowException ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.WARN, "Ошибка: " + ex.getMessage()); + } + catch (Exception ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.FATAL, "Ошибка: " + ex.getMessage()); + } }//GEN-LAST:event_buttonShowOnMapActionPerformed private void buttonAddMapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddMapActionPerformed @@ -391,15 +433,18 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { } _mapsCollection.AddMap(textBoxNewMapName.getText(), _mapsDict.get((String) comboBoxSelectorMap.getSelectedItem())); ReloadMaps(); + logger.log(Level.INFO, "Добавлна карта " + textBoxNewMapName.getText()); }//GEN-LAST:event_buttonAddMapActionPerformed private void buttonDeleteMapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDeleteMapActionPerformed if (listBoxMaps.getSelectedIndex() == -1){ return; } + String name = listBoxMaps.getSelectedValue(); if (JOptionPane.showConfirmDialog(this, (Object) ("Удалить карту " + listBoxMaps.getSelectedValue() + "?") , "Удаление", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION){ _mapsCollection.DelMap(listBoxMaps.getSelectedValue()); ReloadMaps(); + logger.log(Level.INFO, "Удалена карта " + name); } }//GEN-LAST:event_buttonDeleteMapActionPerformed @@ -416,12 +461,14 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { return; } airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).ShowSet(), 0, 0, null); + logger.log(Level.INFO, "Переход на карту " + listBoxMaps.getSelectedValue()); }//GEN-LAST:event_listBoxMapsValueChanged private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveActionPerformed if (jFileChooser1.showDialog(this, "Сохранить") == JFileChooser.APPROVE_OPTION){ if (_mapsCollection.SaveData(jFileChooser1.getSelectedFile().getPath())){ JOptionPane.showMessageDialog(this, "Cохранено"); + logger.log(Level.INFO, "Сохранение"); } else{ JOptionPane.showMessageDialog(this, "Не сохранилось"); @@ -430,15 +477,38 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { }//GEN-LAST:event_jMenuItemSaveActionPerformed private void jMenuItemLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemLoadActionPerformed - if (jFileChooser1.showDialog(this, "Загрузить") == JFileChooser.APPROVE_OPTION){ - if (_mapsCollection.LoadData(jFileChooser1.getSelectedFile().getPath())){ - JOptionPane.showMessageDialog(this, "Загрузилось"); - ReloadMaps(); - } - else{ - JOptionPane.showMessageDialog(this, "Не загрузилось"); + try{ + if (jFileChooser1.showDialog(this, "Загрузить") == JFileChooser.APPROVE_OPTION){ + if (_mapsCollection.LoadData(jFileChooser1.getSelectedFile().getPath())){ + JOptionPane.showMessageDialog(this, "Загрузилось"); + ReloadMaps(); + logger.log(Level.INFO, "Загрузка"); + } + else{ + JOptionPane.showMessageDialog(this, "Не загрузилось"); + } } } + catch (AirBomberNotFoundException ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.WARN, "Ошибка: " + ex.getMessage()); + } + catch (StorageOverflowException ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.WARN, "Ошибка: " + ex.getMessage()); + } + catch (FileNotFoundException ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.ERROR, "Ошибка: " + ex.getMessage()); + } + catch (FileSystemException ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.ERROR, "Ошибка: " + ex.getMessage()); + } + catch (Exception ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.FATAL, "Ошибка: " + ex.getMessage()); + } }//GEN-LAST:event_jMenuItemLoadActionPerformed private void jMenuItemSaveStorageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveStorageActionPerformed @@ -453,15 +523,29 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { }//GEN-LAST:event_jMenuItemSaveStorageActionPerformed private void jMenuItemLoadStorageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemLoadStorageActionPerformed - if (jFileChooser1.showDialog(this, "Загрузить") == JFileChooser.APPROVE_OPTION){ - if (_mapsCollection.LoadStorage(jFileChooser1.getSelectedFile().getPath())){ - JOptionPane.showMessageDialog(this, "Загрузилось"); - ReloadMaps(); - } - else{ - JOptionPane.showMessageDialog(this, "Не загрузилось"); + try{ + if (jFileChooser1.showDialog(this, "Загрузить") == JFileChooser.APPROVE_OPTION){ + if (_mapsCollection.LoadStorage(jFileChooser1.getSelectedFile().getPath())){ + JOptionPane.showMessageDialog(this, "Загрузилось"); + ReloadMaps(); + } + else{ + JOptionPane.showMessageDialog(this, "Не загрузилось"); + } } } + catch (AirBomberNotFoundException ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.WARN, "Ошибка: " + ex.getMessage()); + } + catch (StorageOverflowException ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.WARN, "Ошибка: " + ex.getMessage()); + } + catch (Exception ex){ + JOptionPane.showMessageDialog(null, ex); + logger.log(Level.FATAL, "Ошибка: " + ex.getMessage()); + } }//GEN-LAST:event_jMenuItemLoadStorageActionPerformed private void ReloadMaps() diff --git a/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java b/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java index 3c08840..6b867f0 100644 --- a/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java +++ b/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java @@ -30,11 +30,11 @@ public class MapWithSetAirBombersGeneric /// /// - public boolean LoadData(String filename) + public boolean LoadData(String filename) throws AirBomberNotFoundException, StorageOverflowException, Exception { File loadFile = new File(filename); if (!loadFile.exists()) @@ -174,11 +175,11 @@ public class MapsCollection { return true; } - public boolean LoadStorage(String filename){ + public boolean LoadStorage(String filename) throws AirBomberNotFoundException, StorageOverflowException, FileNotFoundException, FileSystemException, Exception{ File loadFile = new File(filename); if (!loadFile.exists()) { - return false; + throw new FileNotFoundException(filename); } try{ @@ -186,7 +187,7 @@ public class MapsCollection { String line; line = br.readLine(); line.trim(); - if (!line.equals("Storage")) return false; + if (!line.equals("Storage")) throw new FileSystemException("Неверный формат файла!"); String key = br.readLine(); String mapType = br.readLine(); diff --git a/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java b/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java index f711f60..659237f 100644 --- a/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java +++ b/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java @@ -37,7 +37,7 @@ public class SetAirBombersGeneric { /// /// Добавляемый автомобиль /// - public int Insert(T airBomber) + public int Insert(T airBomber)throws AirBomberNotFoundException, StorageOverflowException, Exception { return Insert(airBomber, 0); } @@ -47,11 +47,14 @@ public class SetAirBombersGeneric { /// Добавляемый автомобиль /// Позиция /// - public int Insert(T airBomber, int position) + public int Insert(T airBomber, int position) throws AirBomberNotFoundException, StorageOverflowException, Exception { - if (position < 0 || position >= _maxCount) + if (position < 0 || position > _maxCount) { - return -1; + throw new AirBomberNotFoundException(position); + } + if (_places.size() + 1 > _maxCount){ + throw new StorageOverflowException(_maxCount); } _places.add(position, airBomber); return position; @@ -61,11 +64,11 @@ public class SetAirBombersGeneric { /// /// /// - public T Remove(int position) + public T Remove(int position) throws AirBomberNotFoundException, Exception { - if (position <0 || position >= _maxCount) + if (position <0 || position > _maxCount) { - return null; + throw new AirBomberNotFoundException(position); } T removedObject = _places.get(position); _places.remove(position); @@ -76,7 +79,7 @@ public class SetAirBombersGeneric { /// /// /// - public T Get(int position) + public T Get(int position) { if (position >= _maxCount || position < 0) { diff --git a/AirBomber/src/AirBomberPackage/StorageOverflowException.java b/AirBomber/src/AirBomberPackage/StorageOverflowException.java new file mode 100644 index 0000000..8ff33bf --- /dev/null +++ b/AirBomber/src/AirBomberPackage/StorageOverflowException.java @@ -0,0 +1,15 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package AirBomberPackage; + +/** + * + * @author Андрей + */ +public class StorageOverflowException extends Exception { + public StorageOverflowException(int count){ + super("В наборе превышено допустимое количество: " + count); + } +} diff --git a/AirBomber/src/AirBomberPackage/loggerSettings.xml b/AirBomber/src/AirBomberPackage/loggerSettings.xml new file mode 100644 index 0000000..7953f97 --- /dev/null +++ b/AirBomber/src/AirBomberPackage/loggerSettings.xml @@ -0,0 +1,32 @@ + + + + + + + + %m(дата-%d{d.M.y}) %ex%n + + + + + + %m(дата-%d{d.M.y}) %ex%n + + + + + + + + + + + + + + + + + + \ No newline at end of file -- 2.25.1