diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/AdditionalCollection.java b/ProjectCleaningCar/src/CollectionGenericObjects/AdditionalCollection.java index 4560618..2875f51 100644 --- a/ProjectCleaningCar/src/CollectionGenericObjects/AdditionalCollection.java +++ b/ProjectCleaningCar/src/CollectionGenericObjects/AdditionalCollection.java @@ -13,7 +13,7 @@ import java.util.Random; public class AdditionalCollection { public T[] _collectionEntity; public U[] _collectionWheels; - public AdditionalCollection(int value, Class type1, Class type2) { + public AdditionalCollection(int value, Class type1, Class type2) { _collectionEntity = (T[]) Array.newInstance(type1, value); _collectionWheels = (U[]) Array.newInstance(type2, value); } diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/CollectionType.java b/ProjectCleaningCar/src/CollectionGenericObjects/CollectionType.java new file mode 100644 index 0000000..edf6a6e --- /dev/null +++ b/ProjectCleaningCar/src/CollectionGenericObjects/CollectionType.java @@ -0,0 +1,7 @@ +package CollectionGenericObjects; + +public enum CollectionType { + None, + Massive, + List; +} diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/ListGenericObjects.java b/ProjectCleaningCar/src/CollectionGenericObjects/ListGenericObjects.java new file mode 100644 index 0000000..afd056a --- /dev/null +++ b/ProjectCleaningCar/src/CollectionGenericObjects/ListGenericObjects.java @@ -0,0 +1,56 @@ +package CollectionGenericObjects; + +import java.util.ArrayList; +import java.util.List; + +public class ListGenericObjects implements ICollectionGenericObjects { + private List _collection; + private int _maxCount; + @Override + public int Count() { + return _collection.size(); + } + + @Override + public void SetMaxCount(int value, Class type) { + if (value > 0) { + _maxCount = value; + } + } + public ListGenericObjects() { + _collection = new ArrayList(); + } + + @Override + public int Insert(T obj) { + if (Count() == _maxCount) return -1; + _collection.add(obj); + return Count(); + } + + @Override + public int Insert(T obj, int position) { + if (Count() == _maxCount) return -1; + if (position > Count() || position < 0) return -1; + _collection.add(position, obj); + return 0; + } + + @Override + public T Remove(int position) { + if (position >= _maxCount || position < 0) { + return null; + } + T myObj = _collection.get(position); + _collection.remove(position); + return myObj; + } + + @Override + public T Get(int position) { + if (position >= Count() || position < 0) { + return null; + } + return _collection.get(position); + } +} diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/MassiveGenericObjects.java b/ProjectCleaningCar/src/CollectionGenericObjects/MassiveGenericObjects.java index 6ef1ba4..546b046 100644 --- a/ProjectCleaningCar/src/CollectionGenericObjects/MassiveGenericObjects.java +++ b/ProjectCleaningCar/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -7,7 +7,8 @@ public class MassiveGenericObjects implements ICollectionGenericObjects{ @Override public void SetMaxCount(int value, Class type) { if (value > 0) { - _collection = (T[]) Array.newInstance(type, value); + if (_collection == null) + _collection = (T[]) Array.newInstance(type, value); } } @Override diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/StorageCollection.java b/ProjectCleaningCar/src/CollectionGenericObjects/StorageCollection.java new file mode 100644 index 0000000..3da3891 --- /dev/null +++ b/ProjectCleaningCar/src/CollectionGenericObjects/StorageCollection.java @@ -0,0 +1,45 @@ +package CollectionGenericObjects; + +import java.util.*; + +public class StorageCollection { + private Map> _storages; + public Set Keys() { + return _storages.keySet(); + } + public StorageCollection() { + _storages = new HashMap<>(); + } + public void AddCollection(String name, CollectionType collectionType) { + if (name == null || _storages.containsKey(name)) { + return; + } + switch (collectionType) { + case None: + return; + case Massive: + _storages.put(name, new MassiveGenericObjects()); + return; + case List: + _storages.put(name, new ListGenericObjects()); + return; + } + } + public void DelCollection(String name) { + if (_storages.containsKey(name)) { + _storages.remove(name); + } + } + public ICollectionGenericObjects GetCollectionObject(String name) { + if (_storages.containsKey(name)) { + return _storages.get(name); + } + return null; + } + + public T Remove(String name, int position){ + if(_storages.containsKey(name)) + return _storages.get(name).Remove(position); + return null; + } +} diff --git a/ProjectCleaningCar/src/FormCleaningCar.java b/ProjectCleaningCar/src/FormCleaningCar.java index 3dea35b..bffaff9 100644 --- a/ProjectCleaningCar/src/FormCleaningCar.java +++ b/ProjectCleaningCar/src/FormCleaningCar.java @@ -12,7 +12,7 @@ import java.util.List; import java.util.LinkedList; public class FormCleaningCar extends JFrame { - protected DrawningTruck _drawningTruck; + private DrawningTruck _drawningTruck; private JPanel pictureBox; private JButton buttonRight; private JButton buttonLeft; diff --git a/ProjectCleaningCar/src/FormCleaningCarCollection.form b/ProjectCleaningCar/src/FormCleaningCarCollection.form index dfd4ce9..1e585e4 100644 --- a/ProjectCleaningCar/src/FormCleaningCarCollection.form +++ b/ProjectCleaningCar/src/FormCleaningCarCollection.form @@ -3,12 +3,12 @@ - + - + @@ -16,41 +16,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -58,30 +26,164 @@ - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + + - + - + - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ProjectCleaningCar/src/FormCleaningCarCollection.java b/ProjectCleaningCar/src/FormCleaningCarCollection.java index 6e6ee2f..4f04f2d 100644 --- a/ProjectCleaningCar/src/FormCleaningCarCollection.java +++ b/ProjectCleaningCar/src/FormCleaningCarCollection.java @@ -1,6 +1,4 @@ -import CollectionGenericObjects.AbstractCompany; -import CollectionGenericObjects.AutoParkService; -import CollectionGenericObjects.MassiveGenericObjects; +import CollectionGenericObjects.*; import Drawnings.DrawningCleaningCar; import Drawnings.DrawningTruck; @@ -11,12 +9,16 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.ParseException; import java.util.Arrays; +import java.util.LinkedList; +import java.util.Objects; import java.util.Random; import static java.lang.Integer.parseInt; public class FormCleaningCarCollection extends JFrame{ private AbstractCompany _company = null; + private StorageCollection _storageCollection; + private LinkedList _resurrected; private JPanel panel; private JPanel tools; private JComboBox comboBoxSelectorCompany; @@ -28,7 +30,18 @@ public class FormCleaningCarCollection extends JFrame{ private JButton buttonGoToCheck; private JButton buttonRefresh; private JButton buttonAdditionalCollection; - + private JPanel panelStorage; + private JLabel labelCollectionName; + private JTextField textBoxCollectionName; + private JRadioButton radioButtonMassive; + private JRadioButton radioButtonList; + private JButton buttonCollectionAdd; + private JList listBoxCollection; + private JButton buttonCollectionDel; + private JButton buttonCreateCompany; + private JPanel panelCompanyTools; + private JButton buttonResurrected; + private JButton buttonIndex; public FormCleaningCarCollection() { setTitle("Коллекция уборочных машин"); setSize(1000, 600); @@ -36,7 +49,8 @@ public class FormCleaningCarCollection extends JFrame{ setDefaultCloseOperation(EXIT_ON_CLOSE); add(panel); - + _storageCollection = new StorageCollection<>(); + _resurrected = new LinkedList<>(); tools.setBackground(new Color(220, 220, 220)); tools.setBorder(BorderFactory.createTitledBorder("Инструменты")); @@ -65,14 +79,10 @@ public class FormCleaningCarCollection extends JFrame{ comboBoxSelectorCompany.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - switch (comboBoxSelectorCompany.getSelectedItem().toString()) { - case "Автопарк": - _company = new AutoParkService(pictureBox.getWidth(), pictureBox.getHeight(), new MassiveGenericObjects()); - System.out.println(pictureBox + "/" + pictureBox.getWidth() + "/" + pictureBox.getHeight()); - - break; + panelCompanyTools.setEnabled(false); + for (Component i : panelCompanyTools.getComponents()) { + i.setBackground(Color.GRAY); } - System.out.println(comboBoxSelectorCompany.getSelectedItem().toString()); } }); @@ -88,7 +98,9 @@ public class FormCleaningCarCollection extends JFrame{ switch (JOptionPane.showConfirmDialog(null, "Удалить?", "Удаление", JOptionPane.YES_NO_OPTION)) { case 0: int pos = parseInt(maskedTextBox.getText()); - if (_company._collection.Remove(pos) != null) { + DrawningTruck truck = _company._collection.Remove(pos); + if (truck != null) { + _resurrected.addFirst(truck); JOptionPane.showMessageDialog(null, "Объект удалён"); repaint(); @@ -130,14 +142,112 @@ public class FormCleaningCarCollection extends JFrame{ buttonAdditionalCollection.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_company == null) - { + if (_company == null) { return; } FormAdditionalCollection formAdditionalCollection = new FormAdditionalCollection(); formAdditionalCollection.SetCompany(_company); } }); + //ButtonCollectionAdd_Click + buttonCollectionAdd.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (textBoxCollectionName.getText().isEmpty() || (!radioButtonMassive.isSelected() && !radioButtonList.isSelected())) { + JOptionPane.showMessageDialog(null, "Не все данные заполнены"); + return; + } + CollectionType collectionType = CollectionType.None; + if (radioButtonMassive.isSelected()) { + collectionType = CollectionType.Massive; + } else if (radioButtonList.isSelected()) { + collectionType = CollectionType.List; + } + _storageCollection.AddCollection(textBoxCollectionName.getText(), collectionType); + RefreshListBoxItems(); + } + }); + //ButtonCollectionDel_Click + buttonCollectionDel.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (listBoxCollection.getSelectedIndex() < 0 || listBoxCollection.getSelectedValue() == null) { + JOptionPane.showMessageDialog(null, "Коллекция не выбрана"); + return; + } + + switch (JOptionPane.showConfirmDialog(null, "Удалить коллекцию?", "Удаление", JOptionPane.YES_NO_OPTION)) { + case 1: + return; + case 0: + _storageCollection.DelCollection(listBoxCollection.getSelectedValue().toString()); + break; + } + _storageCollection.DelCollection(listBoxCollection.getSelectedValue().toString()); + RefreshListBoxItems(); + } + }); + //ButtonCreateCompany_Click + buttonCreateCompany.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (listBoxCollection.getSelectedIndex() < 0 || listBoxCollection.getSelectedValue() == null) { + JOptionPane.showMessageDialog(null, "Коллекция не выбрана"); + return; + } + + ICollectionGenericObjects collection = _storageCollection.GetCollectionObject(listBoxCollection.getSelectedValue().toString()); + if (collection == null) { + JOptionPane.showMessageDialog(null, "Коллекция не проинициализирована"); + return; + } + + switch (comboBoxSelectorCompany.getSelectedItem().toString()) { + case "Автопарк": + _company = new AutoParkService(pictureBox.getWidth(), pictureBox.getHeight(), collection); + break; + } + + panelCompanyTools.setEnabled(true); + for (Component i : panelCompanyTools.getComponents()) { + i.setBackground(buttonCreateCompany.getBackground()); + } + RefreshListBoxItems(); + } + }); + buttonResurrected.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_resurrected.peek() != null) { + FormCleaningCar form = new FormCleaningCar(); + form.SetTruck(_resurrected.pop()); + } + } + }); + buttonIndex.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (listBoxCollection.getSelectedValue() == null || listBoxCollection.getSelectedIndex() < 0 + || _company == null || maskedTextBox.getText() == null) return; + int pos = parseInt(maskedTextBox.getText()); + switch (JOptionPane.showConfirmDialog(null, "Удалить?", "Удаление", JOptionPane.YES_NO_OPTION)) { + case 0: + DrawningTruck truck = _storageCollection.Remove(listBoxCollection.getSelectedValue().toString(), pos); + if (truck != null) { + _resurrected.addFirst(truck); + JOptionPane.showMessageDialog(null, "Объект удалён"); + repaint(); + + } else { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект"); + } + break; + case 1: + default: + return; + } + } + }); } private void CreateObject(String type) { if (_company == null) return; @@ -167,6 +277,18 @@ public class FormCleaningCarCollection extends JFrame{ Color colorChooser = JColorChooser.showDialog(this, "Выберите цвет", color); return colorChooser; } + private void RefreshListBoxItems() + { + listBoxCollection.removeAll(); + DefaultListModel list = new DefaultListModel(); + for (String name : _storageCollection.Keys()) { + if (!Objects.equals(name, "")) + { + list.addElement(name); + } + } + listBoxCollection.setModel(list); + } @Override public void paint(Graphics g) { super.paint(g); @@ -179,5 +301,4 @@ public class FormCleaningCarCollection extends JFrame{ public static void Show() {for (Frame i : Frame.getFrames()) {if (!i.isActive()) {i.repaint();}}} // Method method = Component.class.getSuperclass().getDeclaredMethod("repaint"); // method.setAccessible(true); -// method.invoke(method); } diff --git a/ProjectCleaningCar/src/Main.java b/ProjectCleaningCar/src/Main.java index 66d8817..aff5e87 100644 --- a/ProjectCleaningCar/src/Main.java +++ b/ProjectCleaningCar/src/Main.java @@ -1,16 +1,6 @@ -import javax.swing.*; -import java.awt.*; class Main { public static void main(String[] args) { -// JFrame.setDefaultLookAndFeelDecorated(false); -// JFrame frame = new JFrame("Project Cleaning Car"); -// frame.setContentPane(new FormCleaningCarCollection().pictureBox); -// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); -// frame.setLocation(200, 200); -// frame.pack(); -// frame.setSize(1000, 600); -// frame.setVisible(true); FormCleaningCarCollection form = new FormCleaningCarCollection(); form.setVisible(true); }