From c3fc932a2a5d87b1ba2edac423642e4a3d8acab6 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sun, 4 Dec 2022 17:50:10 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B3=D0=B5=D1=80,=20=D0=BB=D0=B0=D0=B17=20?= =?UTF-8?q?=D1=85=D0=B0=D1=80=D0=B4=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormMapWithSetLocomotives.java | 96 +++++++++++++++++++--------------- Main.java | 21 ++------ MapsCollection.java | 5 -- SetLocomotivesGeneric.java | 3 -- log4j2.xml | 25 +++++++++ pom.xml | 30 +++++++++++ 6 files changed, 112 insertions(+), 68 deletions(-) create mode 100644 log4j2.xml create mode 100644 pom.xml diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java index 98c25c1..65b6001 100644 --- a/FormMapWithSetLocomotives.java +++ b/FormMapWithSetLocomotives.java @@ -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()); } } }); diff --git a/Main.java b/Main.java index 58bd745..9e7aef7 100644 --- a/Main.java +++ b/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); } } diff --git a/MapsCollection.java b/MapsCollection.java index 7d96348..6efca26 100644 --- a/MapsCollection.java +++ b/MapsCollection.java @@ -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("Неверный формат данных"); } diff --git a/SetLocomotivesGeneric.java b/SetLocomotivesGeneric.java index df6fc1b..1e96946 100644 --- a/SetLocomotivesGeneric.java +++ b/SetLocomotivesGeneric.java @@ -1,6 +1,5 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.logging.Logger; public class SetLocomotivesGeneric { @@ -26,7 +25,6 @@ public class SetLocomotivesGeneric 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 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); diff --git a/log4j2.xml b/log4j2.xml new file mode 100644 index 0000000..ee4f664 --- /dev/null +++ b/log4j2.xml @@ -0,0 +1,25 @@ + + + + + + %-5level %msg (%d{dd.MM.yyyy})%n + + + + + + %-5level %msg (%d{dd.MM.yyyy})%n + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7203947 --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + groupId + PIbd-23_Mochalov_D.V._Locomotive_Hard + 1.0-SNAPSHOT + + + 19 + 19 + UTF-8 + + + + + org.apache.logging.log4j + log4j-api + 2.19.0 + + + org.apache.logging.log4j + log4j-core + 2.19.0 + + + + \ No newline at end of file