PIBD-13 Pazushkin I.P. LabWork04 Hard #4

Closed
chillya wants to merge 7 commits from LabWork04 into LabWork03
9 changed files with 401 additions and 79 deletions

View File

@ -13,7 +13,7 @@ import java.util.Random;
public class AdditionalCollection <T extends EntityTruck, U extends IDrawningWheels>{
public T[] _collectionEntity;
public U[] _collectionWheels;
public AdditionalCollection(int value, Class<T> type1, Class<T> type2) {
public AdditionalCollection(int value, Class<T> type1, Class<U> type2) {
_collectionEntity = (T[]) Array.newInstance(type1, value);
_collectionWheels = (U[]) Array.newInstance(type2, value);
}

View File

@ -0,0 +1,7 @@
package CollectionGenericObjects;
public enum CollectionType {
None,
Massive,
List;
}

View File

@ -0,0 +1,56 @@
package CollectionGenericObjects;
import java.util.ArrayList;
import java.util.List;
public class ListGenericObjects<T> implements ICollectionGenericObjects<T> {
private List<T> _collection;
private int _maxCount;
@Override
public int Count() {
return _collection.size();
}
@Override
public void SetMaxCount(int value, Class<T> type) {
if (value > 0) {
_maxCount = value;
}
}
public ListGenericObjects() {
_collection = new ArrayList<T>();
}
@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);
}
}

View File

@ -7,7 +7,8 @@ public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{
@Override
public void SetMaxCount(int value, Class<T> type) {
if (value > 0) {
_collection = (T[]) Array.newInstance(type, value);
if (_collection == null)
_collection = (T[]) Array.newInstance(type, value);
}
}
@Override

View File

@ -0,0 +1,45 @@
package CollectionGenericObjects;
import java.util.*;
public class StorageCollection<T> {
private Map<String, ICollectionGenericObjects<T>> _storages;
public Set<String> 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<T>());
return;
case List:
_storages.put(name, new ListGenericObjects<T>());
return;
}
}
public void DelCollection(String name) {
if (_storages.containsKey(name)) {
_storages.remove(name);
}
}
public ICollectionGenericObjects<T> 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;
}
}

View File

@ -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;

View File

@ -3,12 +3,12 @@
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="964" height="661"/>
<xy x="20" y="20" width="981" height="661"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="ed6cf" binding="tools" layout-manager="GridLayoutManager" row-count="8" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="ed6cf" binding="tools" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@ -16,41 +16,9 @@
<properties/>
<border type="none"/>
<children>
<component id="112e" class="javax.swing.JButton" binding="buttonAddCleaningCar">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавление уборочной машины"/>
</properties>
</component>
<component id="8fa47" class="javax.swing.JFormattedTextField" binding="maskedTextBox">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="50" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="af87b" class="javax.swing.JButton" binding="buttonDel">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить грузовик"/>
</properties>
</component>
<component id="5a95" class="javax.swing.JButton" binding="buttonAddTruck">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавление грузовика"/>
</properties>
</component>
<component id="f051c" class="javax.swing.JComboBox" binding="comboBoxSelectorCompany">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false">
<maximum-size width="-1" height="70"/>
</grid>
</constraints>
@ -58,30 +26,164 @@
<toolTipText value="" noi18n="true"/>
</properties>
</component>
<component id="7c328" class="javax.swing.JButton" binding="buttonGoToCheck">
<grid id="b3851" binding="panelStorage" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="14367" class="javax.swing.JLabel" binding="labelCollectionName">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Название коллекции:"/>
</properties>
</component>
<component id="7a65d" class="javax.swing.JTextField" binding="textBoxCollectionName">
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="2293" class="javax.swing.JRadioButton" binding="radioButtonMassive">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Массив"/>
</properties>
</component>
<component id="264bb" class="javax.swing.JRadioButton" binding="radioButtonList">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Список"/>
</properties>
</component>
<component id="65912" class="javax.swing.JButton" binding="buttonCollectionAdd">
<constraints>
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить коллекцию"/>
</properties>
</component>
<component id="df305" class="javax.swing.JButton" binding="buttonCollectionDel">
<constraints>
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить коллекцию"/>
</properties>
</component>
<component id="1ff49" class="javax.swing.JList" binding="listBoxCollection">
<constraints>
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
<component id="2b76c" class="javax.swing.JButton" binding="buttonCreateCompany">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Передать на тесты"/>
<text value="Создать компанию"/>
</properties>
</component>
<component id="44aef" class="javax.swing.JButton" binding="buttonRefresh">
<grid id="ceabb" binding="panelCompanyTools" layout-manager="GridLayoutManager" row-count="9" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Обновить"/>
<enabled value="false"/>
</properties>
</component>
<component id="5f2f8" class="javax.swing.JButton" binding="buttonAdditionalCollection">
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Дополнительная коллекция"/>
</properties>
</component>
<border type="none"/>
<children>
<component id="5a95" class="javax.swing.JButton" binding="buttonAddTruck">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавление грузовика"/>
</properties>
</component>
<component id="112e" class="javax.swing.JButton" binding="buttonAddCleaningCar">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавление уборочной машины"/>
</properties>
</component>
<component id="8fa47" class="javax.swing.JFormattedTextField" binding="maskedTextBox">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="50" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="af87b" class="javax.swing.JButton" binding="buttonDel">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить грузовик"/>
</properties>
</component>
<component id="7c328" class="javax.swing.JButton" binding="buttonGoToCheck">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Передать на тесты"/>
</properties>
</component>
<component id="44aef" class="javax.swing.JButton" binding="buttonRefresh">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Обновить"/>
</properties>
</component>
<component id="5f2f8" class="javax.swing.JButton" binding="buttonAdditionalCollection">
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Дополнительная коллекция"/>
</properties>
</component>
<component id="b5aef" class="javax.swing.JButton" binding="buttonResurrected">
<constraints>
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Из удалённых"/>
</properties>
</component>
<component id="bf6d" class="javax.swing.JButton" binding="buttonIndex">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить через индексатор"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="abc4c" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">

View File

@ -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<DrawningTruck> _storageCollection;
private LinkedList<DrawningTruck> _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<DrawningTruck>());
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<DrawningTruck> 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<String> list = new DefaultListModel<String>();
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);
}

View File

@ -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);
}