diff --git a/ProjectElectroTrans/src/CollectionGenericObjects/CollectionType.java b/ProjectElectroTrans/src/CollectionGenericObjects/CollectionType.java new file mode 100644 index 0000000..cc1ae3e --- /dev/null +++ b/ProjectElectroTrans/src/CollectionGenericObjects/CollectionType.java @@ -0,0 +1,16 @@ +package CollectionGenericObjects; + +public enum CollectionType { + None(0), + Massive(1), + List(2); + + final private int collectionTypeValue; + + CollectionType(int value){ + collectionTypeValue = value; + } + public int getCollectionTypeValue() { + return collectionTypeValue; + } +} \ No newline at end of file diff --git a/ProjectElectroTrans/src/CollectionGenericObjects/ListGenericCollection.java b/ProjectElectroTrans/src/CollectionGenericObjects/ListGenericCollection.java new file mode 100644 index 0000000..f189676 --- /dev/null +++ b/ProjectElectroTrans/src/CollectionGenericObjects/ListGenericCollection.java @@ -0,0 +1,59 @@ +package CollectionGenericObjects; + +import Drawnings.DrawningTrans; + +import java.util.ArrayList; + +public class ListGenericCollection implements ICollectionGenericObjects { + private final ArrayList _collection; + public ListGenericCollection() { + _collection = new ArrayList<>(); + } + private int maxCount; + public int count; + @Override + public int getCount() { + return count; + } + @Override + public void setMaxCount(int count) { + maxCount = count; + } + @Override + public int insert(T obj) { + return insert(obj, count); + } + + @Override + public int insert(T obj, int index) { + if (index > maxCount || index < 0 || index > count){ + return -1; + } + if(index == count){ + _collection.add(obj); + } + else { + _collection.add(index, obj); + } + count = _collection.size(); + return index; + } + + @Override + public T remove(int index) { + if (index > maxCount || index < 0 || index >= count){ + return null; + } + count = _collection.size() - 1; + return _collection.remove(index); + + } + + @Override + public T get(int index) { + if (index >= count){ + return null; + } + return _collection.get(index); + } +} \ No newline at end of file diff --git a/ProjectElectroTrans/src/CollectionGenericObjects/StorageCollection.java b/ProjectElectroTrans/src/CollectionGenericObjects/StorageCollection.java new file mode 100644 index 0000000..8bcf79d --- /dev/null +++ b/ProjectElectroTrans/src/CollectionGenericObjects/StorageCollection.java @@ -0,0 +1,48 @@ +package CollectionGenericObjects; + +import Drawnings.DrawningTrans; + +import java.util.ArrayList; +import java.util.HashMap; + +public class StorageCollection { + private final HashMap> _storages; + public final ArrayList keys; + + public StorageCollection() { + _storages = new HashMap<>(); + keys = new ArrayList<>(); + } + + public void addCollection(String name, CollectionType collectionType){ + if (_storages.containsKey(name) || name.isEmpty()){ + return; + } + switch (collectionType){ + case None: + return; + case Massive: + _storages.put(name, new MassiveGenericObjects<>()); + keys.add(name); + break; + case List: + _storages.put(name, new ListGenericCollection<>()); + keys.add(name); + break; + } + } + + public void delCollection(String name){ + _storages.remove(name); + keys.remove(name); + } + + public ICollectionGenericObjects getCollection(String name){ + return _storages.get(name); + } + + public T getObjectFromChooseCollection(String name, int ind){ + return this.getCollection(name).get(ind); + } + +} \ No newline at end of file diff --git a/ProjectElectroTrans/src/Drawnings/DrawningAbstractCompany.java b/ProjectElectroTrans/src/Drawnings/DrawningAbstractCompany.java index c0ef58e..8a4b93e 100644 --- a/ProjectElectroTrans/src/Drawnings/DrawningAbstractCompany.java +++ b/ProjectElectroTrans/src/Drawnings/DrawningAbstractCompany.java @@ -1,8 +1,6 @@ package Drawnings; -import CollectionGenericObjects.AbstractCompany; -import CollectionGenericObjects.MassiveGenericObjects; -import CollectionGenericObjects.TransSharingService; +import CollectionGenericObjects.*; import Forms.FormConstructor; import Forms.FormElectroTrans; @@ -10,18 +8,29 @@ import javax.swing.*; import java.awt.*; import java.awt.event.WindowEvent; import java.util.Random; +import java.util.Stack; public class DrawningAbstractCompany extends JComponent { private AbstractCompany _company = null; - public void collectionComboBox_SelectedIndexChanged(JComboBox obj, int width, int height) { + // public void collectionComboBox_SelectedIndexChanged(JComboBox obj, int width, int height) { + private final StorageCollection storageCollection = new StorageCollection<>(); + private Stack rubbishBinStack = new Stack<>(); + + public boolean collectionComboBox_SelectedIndexChanged(JFrame frame, JList collectionList, JComboBox obj, int width, int height) { switch (obj.getSelectedIndex()) { case 1: - _company = new TransSharingService(width, height, new MassiveGenericObjects()); - break; + if (collectionList.getSelectedIndex() == -1) { + JOptionPane.showMessageDialog(frame, + "Коллекция не выбрана"); + return false; + } + _company = new TransSharingService(width, height, storageCollection.getCollection(collectionList.getSelectedValue())); + return true; default: - break; + return false; } } + public void createObject(int type, JFrame obj) { if (_company == null) { return; @@ -44,11 +53,11 @@ public class DrawningAbstractCompany extends JComponent { } if (AbstractCompany.add(_company, _drawningTrans) != -1) { JOptionPane.showMessageDialog(obj, "Объект добавлен"); - } - else { + } else { JOptionPane.showMessageDialog(obj, "Не удалось добавить объект"); } } + Color getColorR(JFrame obj, Random rnd) { Color color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); if (obj == null) { @@ -59,9 +68,9 @@ public class DrawningAbstractCompany extends JComponent { return JColorChooser.showDialog(obj, "Выберите цвет", color); } - public void deleteButtonAction(int val, Frame obj) { + public boolean deleteButtonAction(int val, Frame obj) { if (_company == null) { - return; + return false; } int result = JOptionPane.showConfirmDialog( obj, @@ -69,13 +78,19 @@ public class DrawningAbstractCompany extends JComponent { "Подтвердение", JOptionPane.YES_NO_OPTION); if (result == JOptionPane.YES_OPTION) { - if (AbstractCompany.remove(_company, val) != null) { + DrawningTrans deletableTrans = AbstractCompany.remove(_company, val); + if (deletableTrans != null) { + rubbishBinStack.add(deletableTrans); JOptionPane.showMessageDialog(obj, "Выполнено"); + return true; } else { JOptionPane.showMessageDialog(obj, "Удаление не удалось"); + return false; } } + return false; } + public void goToCheckButtonAction() { if (_company == null) { return; @@ -89,14 +104,26 @@ public class DrawningAbstractCompany extends JComponent { if (trans == null) { return; } + createFormCatamaran(trans); + } + + public boolean goToCheckFromRubbishBinAction() { + if (rubbishBinStack.isEmpty()) { + return false; + } + DrawningTrans trans = rubbishBinStack.pop(); + createFormCatamaran(trans); + return !rubbishBinStack.isEmpty(); + } + + public void createFormCatamaran(DrawningTrans trans) { FormElectroTrans formElectroTrans = new FormElectroTrans(); formElectroTrans.setDrawningTrans(trans); formElectroTrans.OpenFrame(); - - } private DrawningTrans createObject; + public void getObjFromConstructor(JFrame obj) { if (_company == null) { return; @@ -116,6 +143,36 @@ public class DrawningAbstractCompany extends JComponent { formConstructor.getjFrameConstructor().dispatchEvent(new WindowEvent(formConstructor.getjFrameConstructor(), WindowEvent.WINDOW_CLOSING)); }); } + + public StorageCollection addCollectionButtonAction(JFrame jFrameCollectionBoats, JTextField textFieldSetCollectionName, + JRadioButton massiveRadioButton, JRadioButton listRadioButton) { + + if (textFieldSetCollectionName.getText().isEmpty() || (!massiveRadioButton.isSelected() && !listRadioButton.isSelected())) { + JOptionPane.showMessageDialog(jFrameCollectionBoats, "Не все элементы заполнены", "ERROR", JOptionPane.ERROR_MESSAGE); + return null; + } + CollectionType collectionType = CollectionType.None; + if (massiveRadioButton.isSelected()) + collectionType = CollectionType.Massive; + else if (listRadioButton.isSelected()) + collectionType = CollectionType.List; + storageCollection.addCollection(textFieldSetCollectionName.getText(), collectionType); + return storageCollection; + } + + public StorageCollection deleteCollectionButtonAction(JFrame jFrameCollectionLocomotive, JList keysList) { + if (keysList.getSelectedIndex() != -1) { + int result = JOptionPane.showConfirmDialog(jFrameCollectionLocomotive, "Удалить объект?", + "Подтверждение", JOptionPane.YES_NO_OPTION); + if (result == JOptionPane.YES_OPTION) { + storageCollection.delCollection(keysList.getSelectedValue()); + return storageCollection; + } + } + return null; + } + + @Override public void paintComponent(Graphics g) { super.paintComponent(g); diff --git a/ProjectElectroTrans/src/Drawnings/DrawningRectangleWheels.java b/ProjectElectroTrans/src/Drawnings/DrawningRectangleWheels.java index 7010019..9e24b4c 100644 --- a/ProjectElectroTrans/src/Drawnings/DrawningRectangleWheels.java +++ b/ProjectElectroTrans/src/Drawnings/DrawningRectangleWheels.java @@ -13,6 +13,7 @@ public class DrawningRectangleWheels implements IDrawWheels { return; } } + _wheelsCount = WheelsCount.Two; } @Override diff --git a/ProjectElectroTrans/src/Drawnings/DrawningTrans.java b/ProjectElectroTrans/src/Drawnings/DrawningTrans.java index 51892f2..8815040 100644 --- a/ProjectElectroTrans/src/Drawnings/DrawningTrans.java +++ b/ProjectElectroTrans/src/Drawnings/DrawningTrans.java @@ -40,7 +40,7 @@ public class DrawningTrans { break; } Random random = new Random(); - int wheelsCount = random.nextInt(1,4); + int wheelsCount = random.nextInt(2,4); drawWheels.setNumber(wheelsCount); } diff --git a/ProjectElectroTrans/src/Forms/FormTransCollection.java b/ProjectElectroTrans/src/Forms/FormTransCollection.java index 4618e29..8189232 100644 --- a/ProjectElectroTrans/src/Forms/FormTransCollection.java +++ b/ProjectElectroTrans/src/Forms/FormTransCollection.java @@ -1,6 +1,9 @@ package Forms; +import CollectionGenericObjects.CollectionType; +import CollectionGenericObjects.StorageCollection; import Drawnings.DrawningAbstractCompany; +import Drawnings.DrawningTrans; import javax.swing.*; import javax.swing.text.NumberFormatter; @@ -8,6 +11,8 @@ import java.awt.*; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.text.NumberFormat; +import java.util.ArrayList; +import java.util.Collection; public class FormTransCollection extends JFrame { final private JFrame jFrameCollectionTranss = new JFrame(); @@ -35,6 +40,34 @@ public class FormTransCollection extends JFrame { final private JPanel addFromConstructorPanel = new JPanel(); final private JButton addFromConstructorButton = new JButton("Добавить из констркутора"); + final JRadioButton massiveRadioButton = new JRadioButton("Массив"); + final JRadioButton listRadioButton = new JRadioButton("Лист"); + final JPanel radioButtonsPanel = new JPanel(); + + final JButton addCollectionButton = new JButton("Добавить коллекцию"); + final JPanel addCollectionPanel = new JPanel(); + + + final JButton createCompanyButton = new JButton("Создать компанию"); + final JPanel createCompanyPanel = new JPanel(); + + + final JButton deleteCollectionButton = new JButton("Удалить коллекцию"); + final JPanel deleteCollectionPanel = new JPanel(); + + DefaultListModel listModel = new DefaultListModel<>(); + JList collectionsList = new JList<>(listModel); + final JPanel collectionsListPanel = new JPanel(); + + final JTextField setCollectionName = new JTextField(); + final JLabel setCollectionNameLabel = new JLabel("Название коллекции:"); + final JPanel setCollectionNamePanel = new JPanel(); + + final private ArrayList listOfDownPanel = new ArrayList<>(); + + final private JButton goGoToCheckFromRubbishBinButton = new JButton("
Отправить на тест
объект из мусорки"); + final private JPanel goGoToCheckFromRubbishBinPanel = new JPanel(); + public void OpenFrame() { jFrameCollectionTranss.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -49,33 +82,68 @@ public class FormTransCollection extends JFrame { labelPanel.add(toolsNameLabel); comboBoxPanel.setLayout(new BorderLayout()); - comboBoxPanel.setSize(new Dimension(130, 30)); + comboBoxPanel.setSize(new Dimension(170, 25)); comboBoxPanel.add(comboBoxSelectorCompany, BorderLayout.CENTER); addTransPanel.setLayout(new BorderLayout()); - addTransPanel.setSize(170, 40); + addTransPanel.setSize(170, 25); addTransPanel.add(buttonAddTrans, BorderLayout.CENTER); addElectroTransPanel.setLayout(new BorderLayout()); - addElectroTransPanel.setSize(170, 40); + addElectroTransPanel.setSize(170, 25); addElectroTransPanel.add(buttonAddElectroTrans, BorderLayout.CENTER); removePanel.setLayout(new BorderLayout()); - removePanel.setSize(170, 40); + removePanel.setSize(170, 25); removePanel.add(buttonRemove, BorderLayout.CENTER); goToCheckPanel.setLayout(new BorderLayout()); - goToCheckPanel.setSize(170, 40); + goToCheckPanel.setSize(170, 25); goToCheckPanel.add(goToCheckButton, BorderLayout.CENTER); refreshPanel.setLayout(new BorderLayout()); - refreshPanel.setSize(170, 40); + refreshPanel.setSize(170, 25); refreshPanel.add(refreshButton, BorderLayout.CENTER); addFromConstructorPanel.setLayout(new BorderLayout()); - addFromConstructorPanel.setSize(170, 40); + addFromConstructorPanel.setSize(170, 25); addFromConstructorPanel.add(addFromConstructorButton, BorderLayout.CENTER); + setCollectionNamePanel.setLayout(new BorderLayout()); + setCollectionNamePanel.setSize(170, 45); + setCollectionNamePanel.add(setCollectionNameLabel, BorderLayout.NORTH); + setCollectionNamePanel.add(setCollectionName, BorderLayout.SOUTH); + + radioButtonsPanel.setLayout(new BorderLayout()); + radioButtonsPanel.setSize(170, 25); + ButtonGroup group = new ButtonGroup(); + group.add(massiveRadioButton); + group.add(listRadioButton); + radioButtonsPanel.add(massiveRadioButton, BorderLayout.WEST); + radioButtonsPanel.add(listRadioButton, BorderLayout.EAST); + + addCollectionPanel.setLayout(new BorderLayout()); + addCollectionPanel.setSize(170, 30); + addCollectionPanel.add(addCollectionButton, BorderLayout.CENTER); + + collectionsListPanel.setLayout(new BorderLayout()); + collectionsListPanel.setSize(170, 100); + + JScrollPane scrollPane = new JScrollPane(collectionsList); + collectionsListPanel.add(scrollPane, BorderLayout.CENTER); + + deleteCollectionPanel.setLayout(new BorderLayout()); + deleteCollectionPanel.setSize(170, 30); + deleteCollectionPanel.add(deleteCollectionButton, BorderLayout.CENTER); + + createCompanyPanel.setLayout(new BorderLayout()); + createCompanyPanel.setSize(170, 30); + createCompanyPanel.add(createCompanyButton, BorderLayout.CENTER); + + goGoToCheckFromRubbishBinPanel.setLayout(new BorderLayout()); + goGoToCheckFromRubbishBinPanel.setSize(170, 50); + goGoToCheckFromRubbishBinPanel.add(goGoToCheckFromRubbishBinButton, BorderLayout.CENTER); + NumberFormat format = NumberFormat.getInstance(); NumberFormatter formatter = new NumberFormatter(format); formatter.setValueClass(Integer.class); @@ -87,12 +155,18 @@ public class FormTransCollection extends JFrame { JTextField textBoxPosition = new JFormattedTextField(formatter); JPanel textBoxPanel = new JPanel(); textBoxPanel.setLayout(new BorderLayout()); - textBoxPanel.setSize(170, 40); + textBoxPanel.setSize(170, 25); textBoxPanel.add(textBoxPosition, BorderLayout.CENTER); jFrameCollectionTranss.add(toolsPanel); jFrameCollectionTranss.add(labelPanel); + jFrameCollectionTranss.add(setCollectionNamePanel); + jFrameCollectionTranss.add(radioButtonsPanel); + jFrameCollectionTranss.add(addCollectionPanel); + jFrameCollectionTranss.add(collectionsListPanel); + jFrameCollectionTranss.add(deleteCollectionPanel); jFrameCollectionTranss.add(comboBoxPanel); + jFrameCollectionTranss.add(createCompanyPanel); jFrameCollectionTranss.add(addTransPanel); jFrameCollectionTranss.add(addElectroTransPanel); jFrameCollectionTranss.add(textBoxPanel); @@ -100,33 +174,48 @@ public class FormTransCollection extends JFrame { jFrameCollectionTranss.add(goToCheckPanel); jFrameCollectionTranss.add(refreshPanel); jFrameCollectionTranss.add(addFromConstructorPanel); + jFrameCollectionTranss.add(goGoToCheckFromRubbishBinPanel); jFrameCollectionTranss.add(_company); + listOfDownPanel.add(buttonAddTrans); + listOfDownPanel.add(buttonAddElectroTrans); + listOfDownPanel.add(addFromConstructorButton); + listOfDownPanel.add(textBoxPosition); + listOfDownPanel.add(buttonRemove); + listOfDownPanel.add(goToCheckButton); + listOfDownPanel.add(goGoToCheckFromRubbishBinButton); + listOfDownPanel.add(refreshButton); + + setEnableComponentsOfList(listOfDownPanel, false); jFrameCollectionTranss.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent componentEvent) { + labelPanel.setLocation(jFrameCollectionTranss.getWidth() - 210, 0); toolsPanel.setLocation(jFrameCollectionTranss.getWidth() - 233, 0); - comboBoxPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, 30); - addTransPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, 70); - addElectroTransPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, 120); - textBoxPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, 220); - removePanel.setLocation(jFrameCollectionTranss.getWidth() - 200, 260); - goToCheckPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, 300); - refreshPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, 340); - addFromConstructorPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, 170); + setCollectionNamePanel.setLocation(jFrameCollectionTranss.getWidth() - 200, 30); + radioButtonsPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, 78); + addCollectionPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, 103); + collectionsListPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, 137); + deleteCollectionPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, 227); + comboBoxPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, jFrameCollectionTranss.getHeight() - 400); + createCompanyPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, jFrameCollectionTranss.getHeight() - 370); + goToCheckPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, + jFrameCollectionTranss.getHeight() - 320); + goGoToCheckFromRubbishBinPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, + jFrameCollectionTranss.getHeight() - 295); + addTransPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, jFrameCollectionTranss.getHeight() - 241); + addElectroTransPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, jFrameCollectionTranss.getHeight() - 214); + addFromConstructorPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, jFrameCollectionTranss.getHeight() - 187); + textBoxPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, jFrameCollectionTranss.getHeight() - 146); + removePanel.setLocation(jFrameCollectionTranss.getWidth() - 200, jFrameCollectionTranss.getHeight() - 119); + + refreshPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, jFrameCollectionTranss.getHeight() - 67); toolsPanel.setSize(new Dimension(10, jFrameCollectionTranss.getHeight())); jFrameCollectionTranss.repaint(); } }); - comboBoxSelectorCompany.addActionListener(e -> { - - _company.collectionComboBox_SelectedIndexChanged(comboBoxSelectorCompany, - jFrameCollectionTranss.getWidth() - 233, jFrameCollectionTranss.getHeight()); - jFrameCollectionTranss.repaint(); - - }); buttonAddTrans.addActionListener(e -> { _company.createObject(0, jFrameCollectionTranss); jFrameCollectionTranss.repaint(); @@ -137,16 +226,22 @@ public class FormTransCollection extends JFrame { }); buttonRemove.addActionListener(e -> { + int pos = -1; + if (!textBoxPosition.getText().isEmpty()) { - if (Integer.parseInt(textBoxPosition.getText()) <= 99 - && Integer.parseInt(textBoxPosition.getText()) >= 0) { - int pos = Integer.parseInt(textBoxPosition.getText()); - _company.deleteButtonAction(pos, jFrameCollectionTranss); + int inputPos = Integer.parseInt(textBoxPosition.getText()); + if (inputPos <= 99 && inputPos >= 0) { + pos = inputPos; } } - jFrameCollectionTranss.repaint(); - + if (pos != -1) { + if (_company.deleteButtonAction(pos, jFrameCollectionTranss)) { + goGoToCheckFromRubbishBinButton.setEnabled(true); + } + jFrameCollectionTranss.repaint(); + } }); + refreshButton.addActionListener(e -> { jFrameCollectionTranss.repaint(); }); @@ -154,11 +249,47 @@ public class FormTransCollection extends JFrame { _company.goToCheckButtonAction(); jFrameCollectionTranss.repaint(); }); + goGoToCheckFromRubbishBinButton.addActionListener(e -> { + goGoToCheckFromRubbishBinButton.setEnabled(_company.goToCheckFromRubbishBinAction()); + jFrameCollectionTranss.repaint(); + }); + createCompanyButton.addActionListener(e -> { + boolean res = _company.collectionComboBox_SelectedIndexChanged(jFrameCollectionTranss, collectionsList, comboBoxSelectorCompany, + jFrameCollectionTranss.getWidth() - 233, jFrameCollectionTranss.getHeight()); + setEnableComponentsOfList(listOfDownPanel, res); + jFrameCollectionTranss.repaint(); + }); + addFromConstructorButton.addActionListener(e -> { _company.getObjFromConstructor(jFrameCollectionTranss); }); + addCollectionButton.addActionListener(e -> { + StorageCollection result = _company.addCollectionButtonAction(jFrameCollectionTranss, setCollectionName, + massiveRadioButton, listRadioButton); + updateCollectionsList(result); + }); + deleteCollectionButton.addActionListener(e -> { + StorageCollection result = _company.deleteCollectionButtonAction(jFrameCollectionTranss, collectionsList); + updateCollectionsList(result); + }); jFrameCollectionTranss.setVisible(true); } + private void updateCollectionsList(StorageCollection storageCollection) { + if (storageCollection == null) { + return; + } + listModel.clear(); + ArrayList keys = storageCollection.keys; + for (String key : keys) { + listModel.addElement(key); + } + } + + private void setEnableComponentsOfList(ArrayList list, boolean type) { + for (var i : list) { + i.setEnabled(type); + } + } }