PIbd-21_MasenkinMS_LabWork06
This commit is contained in:
parent
e76067faa2
commit
a0bdbb026d
@ -3,7 +3,7 @@
|
||||
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="1" 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="500" height="400"/>
|
||||
<xy x="20" y="20" width="500" height="402"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
@ -17,7 +17,7 @@
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<grid id="afc9e" binding="toolsPanel" layout-manager="GridLayoutManager" row-count="11" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<grid id="afc9e" binding="toolsPanel" layout-manager="GridLayoutManager" row-count="11" 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="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="4" fill="2" indent="0" use-parent-layout="false"/>
|
||||
@ -27,7 +27,7 @@
|
||||
<children>
|
||||
<component id="a57ce" class="javax.swing.JTextField" binding="textFieldNumber">
|
||||
<constraints>
|
||||
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<grid row="6" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
@ -35,7 +35,7 @@
|
||||
</component>
|
||||
<component id="d50cb" class="javax.swing.JButton" binding="buttonRemoveBus">
|
||||
<constraints>
|
||||
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
<grid row="7" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Удалить автобус"/>
|
||||
@ -43,7 +43,7 @@
|
||||
</component>
|
||||
<component id="3dc96" class="javax.swing.JButton" binding="buttonAddBus">
|
||||
<constraints>
|
||||
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Добавить автобус"/>
|
||||
@ -51,7 +51,7 @@
|
||||
</component>
|
||||
<component id="9cc50" class="javax.swing.JButton" binding="buttonRefreshCollection">
|
||||
<constraints>
|
||||
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
<grid row="8" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalTextPosition value="0"/>
|
||||
@ -68,7 +68,7 @@
|
||||
</component>
|
||||
<component id="2a7de" class="javax.swing.JButton" binding="buttonGenerateBus">
|
||||
<constraints>
|
||||
<grid row="9" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
<grid row="9" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalTextPosition value="0"/>
|
||||
@ -77,7 +77,7 @@
|
||||
</component>
|
||||
<component id="61fbd" class="javax.swing.JTextField" binding="textFieldStorage">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
@ -85,7 +85,7 @@
|
||||
</component>
|
||||
<component id="b20a4" class="javax.swing.JButton" binding="buttonAddObject">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Добавить набор"/>
|
||||
@ -93,7 +93,7 @@
|
||||
</component>
|
||||
<component id="93129" class="javax.swing.JList" binding="listStorages">
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<grid row="3" 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>
|
||||
@ -101,7 +101,7 @@
|
||||
</component>
|
||||
<component id="98ed2" class="javax.swing.JButton" binding="buttonDeleteObject">
|
||||
<constraints>
|
||||
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Удалить набор"/>
|
||||
@ -109,13 +109,22 @@
|
||||
</component>
|
||||
<component id="e67a1" class="javax.swing.JButton" binding="buttonShowDeletedBus">
|
||||
<constraints>
|
||||
<grid row="10" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
<grid row="10" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalTextPosition value="0"/>
|
||||
<text value="Показать удаленный автобус"/>
|
||||
</properties>
|
||||
</component>
|
||||
<grid id="3108f" binding="toolbarPanel" layout-manager="GridLayoutManager" row-count="1" 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="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="4" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
|
@ -3,7 +3,11 @@ package AccordionBus;
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.File;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Random;
|
||||
|
||||
@ -35,15 +39,33 @@ public class BusCollectionForm extends JFrame {
|
||||
private JList listStorages;
|
||||
private JButton buttonDeleteObject;
|
||||
private JButton buttonShowDeletedBus;
|
||||
private JPanel toolbarPanel;
|
||||
private JMenuBar menuBar = new JMenuBar();
|
||||
private JMenu fileMenu = new JMenu("Файл");
|
||||
private JMenuItem saveItem = new JMenuItem("Сохранение");
|
||||
private JMenuItem openItem = new JMenuItem("Загрузка");
|
||||
private JMenuItem saveCollectionItem = new JMenuItem("Сохранение коллекции");
|
||||
private JMenuItem openCollectionItem = new JMenuItem("Загрузка коллекции");
|
||||
|
||||
// Конструктор
|
||||
public BusCollectionForm() {
|
||||
pictureBoxCollection.setSize(new Dimension(700, 450));
|
||||
_storage = new BusGenericStorage(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight());
|
||||
_deletedBuses = new LinkedList<DrawingBus>();
|
||||
|
||||
// Модель списка
|
||||
listModel = new DefaultListModel<String>();
|
||||
listStorages.setModel(listModel);
|
||||
|
||||
// Выпадающее меню для работы с файлами
|
||||
fileMenu.add(saveItem);
|
||||
fileMenu.add(openItem);
|
||||
fileMenu.add(saveCollectionItem);
|
||||
fileMenu.add(openCollectionItem);
|
||||
menuBar.add(fileMenu);
|
||||
toolbarPanel.setLayout(new BorderLayout());
|
||||
toolbarPanel.add(menuBar);
|
||||
|
||||
// Добавление набора
|
||||
buttonAddObject.addActionListener(e -> {
|
||||
if (textFieldStorage.getText().length() == 0) {
|
||||
@ -97,7 +119,7 @@ public class BusCollectionForm extends JFrame {
|
||||
JOptionPane.showMessageDialog(this.getMainPanel(), "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(this.getMainPanel(), " Объект не добавлен", "Ошибка", JOptionPane.INFORMATION_MESSAGE);
|
||||
JOptionPane.showMessageDialog(this.getMainPanel(), "Объект не добавлен", "Ошибка", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -162,6 +184,93 @@ public class BusCollectionForm extends JFrame {
|
||||
frameAccordionBus.accordionBusForm.Draw();
|
||||
frameAccordionBus.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
});
|
||||
|
||||
// Сохранить данные в файл
|
||||
saveItem.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
fileChooser.setDialogTitle("Сохранить");
|
||||
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
||||
|
||||
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
File file = fileChooser.getSelectedFile();
|
||||
if (_storage.SaveData(file.getAbsolutePath())) {
|
||||
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось сохранить данные", "Результат", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Сохранить коллекцию в файл
|
||||
saveCollectionItem.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (listStorages.getSelectedIndex() == -1) {
|
||||
JOptionPane.showMessageDialog(null, "Не выбрана коллекция", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
fileChooser.setDialogTitle("Сохранить коллекцию");
|
||||
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
||||
|
||||
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
File file = fileChooser.getSelectedFile();
|
||||
if (_storage.SaveCollectionData(file.getAbsolutePath(), listStorages.getSelectedValue().toString())) {
|
||||
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось сохранить данные", "Результат", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Загрузить данные из файла
|
||||
openItem.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
fileChooser.setDialogTitle("Загрузить");
|
||||
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
||||
|
||||
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
File file = fileChooser.getSelectedFile();
|
||||
if (_storage.LoadData(file.getAbsolutePath())) {
|
||||
JOptionPane.showMessageDialog(null, "Загрузка прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось загрузить данные", "Результат", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
ReloadObjects();
|
||||
}
|
||||
});
|
||||
|
||||
// Загрузить коллекцию из файла
|
||||
openCollectionItem.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
fileChooser.setDialogTitle("Загрузить коллекцию");
|
||||
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
||||
|
||||
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
File file = fileChooser.getSelectedFile();
|
||||
if (_storage.LoadCollectionData(file.getAbsolutePath())) {
|
||||
JOptionPane.showMessageDialog(null, "Загрузка прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось загрузить данные", "Результат", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
ReloadObjects();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Заполнение ListBoxObject
|
||||
|
@ -19,6 +19,11 @@ public class BusGenericCollection<T extends DrawingBus, U extends IMoveableObjec
|
||||
// Набор объектов
|
||||
private final SetGeneric<T> _collection;
|
||||
|
||||
// Получение объектов коллекции
|
||||
public Iterable<T> GetBuses(final Integer maxBuses) {
|
||||
return _collection.GetBuses(maxBuses);
|
||||
}
|
||||
|
||||
// Конструктор
|
||||
public BusGenericCollection(int picWidth, int picHeight) {
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
@ -49,6 +54,11 @@ public class BusGenericCollection<T extends DrawingBus, U extends IMoveableObjec
|
||||
return obj;
|
||||
}
|
||||
|
||||
// Очистить содержимое коллекции
|
||||
public void Clear() {
|
||||
_collection.Clear();
|
||||
}
|
||||
|
||||
// Получение объекта IMoveableObject
|
||||
public U GetU(int pos)
|
||||
{
|
||||
|
@ -1,7 +1,10 @@
|
||||
package AccordionBus;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// Класс для хранения коллекции
|
||||
@ -20,6 +23,18 @@ public class BusGenericStorage {
|
||||
// Высота окна отрисовки
|
||||
private final int _pictureHeight;
|
||||
|
||||
// Разделитель для записи ключа и значения элемента словаря
|
||||
private static String _separatorForKeyValueWR = "|";
|
||||
private static String _separatorForKeyValue = "\\|";
|
||||
|
||||
// Разделитель для записей коллекции данных в файл
|
||||
private String _separatorRecordsWR = ";";
|
||||
private String _separatorRecords = "\\;";
|
||||
|
||||
// Разделитель для записи информации по объекту в файл
|
||||
private static String _separatorForObjectWR = ":";
|
||||
private static String _separatorForObject = "\\:";
|
||||
|
||||
// Конструктор
|
||||
public BusGenericStorage(int pictureWidth, int pictureHeight) {
|
||||
_busStorages = new HashMap<String, BusGenericCollection<DrawingBus, DrawingObjectBus>>();
|
||||
@ -51,7 +66,7 @@ public class BusGenericStorage {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Доступ к объекту из набору
|
||||
// Доступ к объекту из набора
|
||||
public DrawingObjectBus get(String ind1, int ind2) {
|
||||
// Проверка наличия ключа
|
||||
if (_busStorages.containsKey(ind1)) {
|
||||
@ -59,4 +74,168 @@ public class BusGenericStorage {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Сохранение информации по автобусам в хранилище в файл
|
||||
public boolean SaveData(String filename) {
|
||||
if (new File(filename).exists()) {
|
||||
new File(filename).delete();
|
||||
}
|
||||
|
||||
StringBuilder data = new StringBuilder();
|
||||
for (Map.Entry<String, BusGenericCollection<DrawingBus, DrawingObjectBus>> record : _busStorages.entrySet()) {
|
||||
StringBuilder records = new StringBuilder();
|
||||
for (DrawingBus elem : record.getValue().GetBuses(100)) {
|
||||
records.append(elem != null ? ExtensionDrawingBus.GetDataForSave(elem, _separatorForObjectWR) + _separatorRecordsWR : "");
|
||||
}
|
||||
data.append(record.getKey() + _separatorForKeyValueWR + records + "\n");
|
||||
}
|
||||
|
||||
if (data.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
|
||||
writer.write("BusStorage" + System.lineSeparator() + data.toString());
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Сохранение информации по автобусам из коллекции хранилища в файл
|
||||
public boolean SaveCollectionData(String filename, String key) {
|
||||
if (new File(filename).exists()) {
|
||||
new File(filename).delete();
|
||||
}
|
||||
|
||||
StringBuilder data = new StringBuilder();
|
||||
data.append(key + "\n");
|
||||
for (DrawingBus elem : _busStorages.get(key).GetBuses(100)) {
|
||||
data.append(elem != null ? ExtensionDrawingBus.GetDataForSave(elem, _separatorForObjectWR) + "\n" : "");
|
||||
}
|
||||
|
||||
if (data.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
|
||||
writer.write("BusCollectionStorage" + System.lineSeparator() + data.toString());
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Загрузка информации по автобусам в хранилище из файла
|
||||
public boolean LoadData(String filename) {
|
||||
if (!new File(filename).exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
||||
String line = reader.readLine();
|
||||
// Если строка пустая
|
||||
if (line == null || line.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
// Если нет записи "BusStorage", то это не те данные
|
||||
if (!line.startsWith("BusStorage")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_busStorages.clear();
|
||||
|
||||
line = reader.readLine();
|
||||
while (line != null && line.length() != 0) {
|
||||
String[] record = line.split(_separatorForKeyValue);
|
||||
if (record.length != 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
BusGenericCollection<DrawingBus, DrawingObjectBus> collection = new BusGenericCollection<>(_pictureWidth, _pictureHeight);
|
||||
String[] set = record[1].split(_separatorRecords);
|
||||
for (String elem : set) {
|
||||
DrawingBus bus = ExtensionDrawingBus.CreateDrawingBus(elem, _separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (bus != null) {
|
||||
if (collection.plus(bus) == -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_busStorages.put(record[0], collection);
|
||||
line = reader.readLine();
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Загрузка информации по автобусам в коллекцию хранилища из файла
|
||||
public boolean LoadCollectionData(String filename) {
|
||||
if (!new File(filename).exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
||||
String line = reader.readLine();
|
||||
// Если строка пустая
|
||||
if (line == null || line.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
// Если нет записи "BusCollectionStorage", то это не те данные
|
||||
if (!line.startsWith("BusCollectionStorage")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String key = reader.readLine();
|
||||
// Если значение ключа пустое
|
||||
if (key == null || key.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
BusGenericCollection<DrawingBus, DrawingObjectBus> collection;
|
||||
if (_busStorages.containsKey(key)) {
|
||||
collection = _busStorages.get(key);
|
||||
collection.Clear();
|
||||
}
|
||||
else {
|
||||
collection = new BusGenericCollection<>(_pictureWidth, _pictureHeight);
|
||||
}
|
||||
|
||||
List<String> buses = new ArrayList<String>();
|
||||
line = reader.readLine();
|
||||
while (line != null && line.length() != 0) {
|
||||
buses.add(line);
|
||||
line = reader.readLine();
|
||||
}
|
||||
|
||||
for (String elem : buses) {
|
||||
DrawingBus bus = ExtensionDrawingBus.CreateDrawingBus(elem, _separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (bus != null) {
|
||||
if (collection.plus(bus) == -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_busStorages.put(key, collection);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,14 @@ public class DrawingDoorsTypeOne implements IDrawingDoors {
|
||||
}
|
||||
}
|
||||
|
||||
public int getNumDoors() {
|
||||
return switch (numDoors) {
|
||||
case Three -> 3;
|
||||
case Four -> 4;
|
||||
case Five -> 5;
|
||||
};
|
||||
}
|
||||
|
||||
public void DrawDoors(Graphics2D g2d, Color color, boolean isAdditionalBody, int _startPosX, int _startPosY) {
|
||||
int value = numDoors.getNumDoors();
|
||||
if (value >= 3) {
|
||||
|
@ -15,6 +15,14 @@ public class DrawingDoorsTypeThree implements IDrawingDoors {
|
||||
}
|
||||
}
|
||||
|
||||
public int getNumDoors() {
|
||||
return switch (numDoors) {
|
||||
case Three -> 3;
|
||||
case Four -> 4;
|
||||
case Five -> 5;
|
||||
};
|
||||
}
|
||||
|
||||
public void DrawDoors(Graphics2D g2d, Color color, boolean isAdditionalBody, int _startPosX, int _startPosY) {
|
||||
int value = numDoors.getNumDoors();
|
||||
if (value >= 3) {
|
||||
|
@ -15,6 +15,14 @@ public class DrawingDoorsTypeTwo implements IDrawingDoors {
|
||||
}
|
||||
}
|
||||
|
||||
public int getNumDoors() {
|
||||
return switch (numDoors) {
|
||||
case Three -> 3;
|
||||
case Four -> 4;
|
||||
case Five -> 5;
|
||||
};
|
||||
}
|
||||
|
||||
public void DrawDoors(Graphics2D g2d, Color color, boolean isAdditionalBody, int _startPosX, int _startPosY) {
|
||||
int value = numDoors.getNumDoors();
|
||||
if (value >= 3) {
|
||||
|
94
AccordionBus/ExtensionDrawingBus.java
Normal file
94
AccordionBus/ExtensionDrawingBus.java
Normal file
@ -0,0 +1,94 @@
|
||||
package AccordionBus;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
// Расширение для класса EntityBus
|
||||
public class ExtensionDrawingBus {
|
||||
// Создание объекта из строки
|
||||
public static DrawingBus CreateDrawingBus(String info, String separatorForObject, int width, int height) {
|
||||
String[] strs = info.split(separatorForObject);
|
||||
if (strs.length == 5)
|
||||
{
|
||||
String[] bodyColorValues = strs[2].split(",");
|
||||
|
||||
DrawingBus drawingBus = new DrawingBus(
|
||||
Integer.parseInt(strs[0]),
|
||||
Integer.parseInt(strs[1]),
|
||||
new Color(
|
||||
Integer.parseInt(bodyColorValues[0].replaceAll("\\D", "")),
|
||||
Integer.parseInt(bodyColorValues[1].replaceAll("\\D", "")),
|
||||
Integer.parseInt(bodyColorValues[2].replaceAll("\\D", ""))
|
||||
),
|
||||
width, height);
|
||||
|
||||
try {
|
||||
drawingBus.drawingDoors = (IDrawingDoors)Class.forName(strs[3]).getDeclaredConstructor().newInstance();
|
||||
drawingBus.drawingDoors.setNumDoors(Integer.parseInt(strs[4]));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return drawingBus;
|
||||
}
|
||||
|
||||
if (strs.length == 8)
|
||||
{
|
||||
String[] bodyColorValues = strs[2].split(",");
|
||||
String[] additionalColorValues = strs[5].split(",");
|
||||
|
||||
DrawingAccordionBus drawingAccordionBus = new DrawingAccordionBus(
|
||||
Integer.parseInt(strs[0]),
|
||||
Integer.parseInt(strs[1]),
|
||||
new Color(
|
||||
Integer.parseInt(bodyColorValues[0].replaceAll("\\D", "")),
|
||||
Integer.parseInt(bodyColorValues[1].replaceAll("\\D", "")),
|
||||
Integer.parseInt(bodyColorValues[2].replaceAll("\\D", ""))
|
||||
),
|
||||
new Color(
|
||||
Integer.parseInt(additionalColorValues[0].replaceAll("\\D", "")),
|
||||
Integer.parseInt(additionalColorValues[1].replaceAll("\\D", "")),
|
||||
Integer.parseInt(additionalColorValues[2].replaceAll("\\D", ""))
|
||||
),
|
||||
Boolean.parseBoolean(strs[6]),
|
||||
Boolean.parseBoolean(strs[7]),
|
||||
width, height);
|
||||
|
||||
try {
|
||||
drawingAccordionBus.drawingDoors = (IDrawingDoors)Class.forName(strs[3]).getDeclaredConstructor().newInstance();
|
||||
drawingAccordionBus.drawingDoors.setNumDoors(Integer.parseInt(strs[4]));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return drawingAccordionBus;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Получение данных для сохранения в файл
|
||||
public static String GetDataForSave(DrawingBus drawingBus, String separatorForObject) {
|
||||
EntityBus bus = drawingBus.EntityBus;
|
||||
if (bus == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
String str = "" + bus.Speed + separatorForObject +
|
||||
(int)bus.Weight + separatorForObject +
|
||||
bus.BodyColor + separatorForObject +
|
||||
drawingBus.drawingDoors.getClass().getName() + separatorForObject +
|
||||
drawingBus.drawingDoors.getNumDoors();
|
||||
|
||||
if (!(bus instanceof EntityAccordionBus)) {
|
||||
return str;
|
||||
}
|
||||
|
||||
return str + separatorForObject +
|
||||
((EntityAccordionBus)bus).getAdditionalColor() + separatorForObject +
|
||||
((EntityAccordionBus)bus).isAdditionalBody() + separatorForObject +
|
||||
((EntityAccordionBus)bus).isAdditionalDoor();
|
||||
}
|
||||
}
|
@ -6,5 +6,7 @@ import java.awt.*;
|
||||
public interface IDrawingDoors {
|
||||
public void setNumDoors(int num);
|
||||
|
||||
public int getNumDoors();
|
||||
|
||||
public void DrawDoors(Graphics2D g2d, Color additionalColor, boolean isAdditionalBody, int _startPosX, int _startPosY);
|
||||
}
|
||||
|
@ -52,6 +52,11 @@ public class SetGeneric<T extends Object> {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Очистить список
|
||||
public void Clear() {
|
||||
_places.clear();
|
||||
}
|
||||
|
||||
// Получение объекта из набора по позиции
|
||||
public T Get(int position) {
|
||||
// Проверка позиции
|
||||
|
Loading…
Reference in New Issue
Block a user