Pibd-12 Pyzhov_E.A. LabWork06 Hard #7

Closed
pyzhov.egor wants to merge 4 commits from LabWork06 into LabWork05
16 changed files with 625 additions and 79 deletions

View File

@ -22,7 +22,7 @@ public class BoatSharingService extends AbstractCompany {
g.fillRect(0, 0, _pictureWidth, _pictureHeight);
g.setColor(new Color(165, 42, 42)); // Brown
int offsetX = 10, offsetY = -5;
int offsetX = 10, offsetY = -25;
int x = 1 + offsetX, y = _pictureHeight - _placeSizeHeight + offsetY;
numRows = 0;
while (y >= 0) {

View File

@ -6,6 +6,7 @@ public interface ICollectionGenericObjects<T extends DrawningBoat> {
int getCount();
void setMaxCount(int count);
int getMaxCount();
int insert(T obj);
@ -14,4 +15,8 @@ public interface ICollectionGenericObjects<T extends DrawningBoat> {
T remove(int index);
T get(int index);
CollectionType GetCollectionType();
Iterable<T> GetItems();
void ClearCollection();
}

View File

@ -1,59 +0,0 @@
package CollectionGenericObjects;
import Drawnings.DrawningBoat;
import java.util.ArrayList;
public class ListGenericCollection<T extends DrawningBoat> implements ICollectionGenericObjects<T> {
private final ArrayList<T> _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);
}
}

View File

@ -0,0 +1,103 @@
package CollectionGenericObjects;
import Drawnings.DrawningBoat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ListGenericObjects<T extends DrawningBoat> implements ICollectionGenericObjects<T> {
private final ArrayList<T> _collection;
private CollectionType collectionType = CollectionType.List;
public ListGenericObjects() {
_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 getMaxCount(){
return maxCount;
}
@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);
}
@Override
public Iterable<T> GetItems() {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int currentIndex = 0;
//нужен ли count
private int count = 0;
@Override
public boolean hasNext() {
return currentIndex < getCount();
}
@Override
public T next() {
if (hasNext()) {
count++;
return _collection.get(currentIndex++);
}
throw new NoSuchElementException();
}
};
}
};
}
@Override
public CollectionType GetCollectionType() {
return collectionType;
}
@Override
public void ClearCollection() {
Review

У коллекций есть метод очистки - Clear

У коллекций есть метод очистки - Clear
for (T boat : _collection) {
boat = null;
}
}
}

View File

@ -2,9 +2,12 @@ package CollectionGenericObjects;
import Drawnings.DrawningBoat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MassiveGenericObjects<T extends DrawningBoat> implements ICollectionGenericObjects<T> {
private ArrayList<T> _collection;
private CollectionType collectionType = CollectionType.Massive;
public MassiveGenericObjects() {
_collection = new ArrayList<>();
@ -16,6 +19,10 @@ public class MassiveGenericObjects<T extends DrawningBoat> implements ICollectio
return _collection.size();
}
@Override
public int getMaxCount(){
return maxCount;
}
@Override
public int insert(T obj) {
return insert(obj, 0);
}
@ -88,4 +95,40 @@ public class MassiveGenericObjects<T extends DrawningBoat> implements ICollectio
}
}
@Override
public CollectionType GetCollectionType() {
return collectionType;
}
@Override
public Iterable<T> GetItems() {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int currentIndex = 0;
//нужен ли count
private int count = 0;
@Override
public boolean hasNext() {
return currentIndex < getCount();
}
@Override
public T next() {
if (hasNext()) {
count++;
return _collection.get(currentIndex++);
}
throw new NoSuchElementException();
}
};
}
};
}
@Override
public void ClearCollection() {
for (var boat: _collection) {
boat = null;
}
}
}

View File

@ -1,20 +1,242 @@
package CollectionGenericObjects;
import Drawnings.DrawningBoat;
import Drawnings.ExtentionDrawningBoat;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class StorageCollection<T extends DrawningBoat> {
private final HashMap<String, ICollectionGenericObjects<T>> _storages;
public final ArrayList<String> keys;
private final String _collectionKey = "CollectionsStorage";
private final String _collectionSingleKey = "CollectionSingle";
private final String _separatorForKeyValue = "/";
private final String _separatorItems = ";";
public StorageCollection() {
_storages = new HashMap<>();
keys = new ArrayList<>();
}
public void addCollection(String name, CollectionType collectionType){
public boolean SaveData(String filename){
if (_storages.isEmpty()) {
return false;
}
File file = new File(filename);
if (file.exists()){
file.delete();
}
try {
file.createNewFile();
}
catch (IOException e){
return false;
}
FileWriter fw;
try {
fw = new FileWriter(filename, StandardCharsets.UTF_8, false);
StringBuilder sb = new StringBuilder();
sb.append(_collectionKey);
fw.write(sb.toString());
for (int i = 0; i < keys.size(); i++) {
sb = new StringBuilder();
sb.append('\n');
if (_storages.get(keys.get(i)) == null) {
continue;
}
sb.append(keys.get(i));
sb.append(_separatorForKeyValue);
sb.append(_storages.get(keys.get(i)).GetCollectionType());
sb.append(_separatorForKeyValue);
sb.append(_storages.get(keys.get(i)).getMaxCount());
sb.append(_separatorForKeyValue);
for (int j = 0; j < _storages.get(keys.get(i)).getCount(); j++) {
String data = ExtentionDrawningBoat.GetDataForSave(this.getObjectFromChooseCollection(keys.get(i), j));
if (data==null) {
continue;
}
sb.append(data);
sb.append(_separatorItems);
}
fw.write(sb.toString());
}
fw.close();
}catch (IOException e){
e.getStackTrace();
return false;
}
return true;
}
public boolean SaveOneCollection(String filename, JList<String> stringJList){
if (_storages.isEmpty() || stringJList.getSelectedIndex() == -1) {
return false;
}
FileWriter fw;
File file = new File(filename);
if (file.exists()){
file.delete();
}
try {
file.createNewFile();
}
catch (IOException e){
return false;
}
try {
fw = new FileWriter(filename, StandardCharsets.UTF_8, false);
StringBuilder sb = new StringBuilder();
sb.append(_collectionSingleKey);
int i = stringJList.getSelectedIndex();
fw.write(sb.toString());
sb = new StringBuilder();
sb.append('\n');
if (_storages.get(keys.get(i)) == null) {
fw.close();
return false;
}
sb.append(keys.get(i));
sb.append(_separatorForKeyValue);
sb.append(_storages.get(keys.get(i)).GetCollectionType());
sb.append(_separatorForKeyValue);
sb.append(_storages.get(keys.get(i)).getMaxCount());
sb.append(_separatorForKeyValue);
for (int j = 0; j < _storages.get(keys.get(i)).getCount(); j++) {
String data = ExtentionDrawningBoat.GetDataForSave(this.getObjectFromChooseCollection(keys.get(i), j));
if (data==null) {
continue;
}
sb.append(data);
sb.append(_separatorItems);
}
fw.write(sb.toString());
fw.close();
}catch (IOException e){
e.getStackTrace();
return false;
}
return true;
}
public boolean LoadData(String filename) {
File file = new File(filename);
if (!file.exists()) {
return false;
}
FileReader fr;
try {
String data = "";
fr = new FileReader(file, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(fr);
String buf = bufferedReader.readLine();
if (buf.equals(_collectionKey)) {
_storages.clear();
data = bufferedReader.readLine();
while (data != null) {
String[] record = data.split(_separatorForKeyValue);
if (record.length != 4) {
continue;
}
CollectionType collectionType = Enum.valueOf(CollectionType.class, record[1]);
ICollectionGenericObjects<DrawningBoat> collection = StorageCollection.CreateCollection(collectionType);
if (collection == null) {
bufferedReader.close();
return false;
}
collection.setMaxCount(Integer.parseInt(record[2]));
String[] set = record[3].split(_separatorItems);
for (int i = 0; i < set.length; i++) {
DrawningBoat boat;
if ((boat = ExtentionDrawningBoat.CreateDrawningBoat(set[i])) != null) {
if (collection.insert(boat) == -1) {
bufferedReader.close();
return false;
}
}
}
_storages.put(record[0], (ICollectionGenericObjects<T>) collection);
keys.add(record[0]);
data = bufferedReader.readLine();
}
}
else {
return false;
}
bufferedReader.close();
}catch (IOException e){
e.getStackTrace();
return false;
}
return true;
}
public boolean LoadOneCollection(String filename) {
File file = new File(filename);
if (!file.exists()) return false;
try (BufferedReader fs = new BufferedReader(new FileReader(filename))) {
String s = fs.readLine();
if (s == null || !s.startsWith(_collectionSingleKey))
return false;
ICollectionGenericObjects<DrawningBoat> collection;
s = fs.readLine();
String[] record = s.split(_separatorForKeyValue);
if (record.length != 4) {
return false;
}
if (keys.contains(s)) {
_storages.get(s).ClearCollection();
Review

Сперва получить коллекцию и потом вызывать метод очистки

Сперва получить коллекцию и потом вызывать метод очистки
collection = (ICollectionGenericObjects<DrawningBoat>) _storages.get(s);
}
else {
CollectionType collectionType = Enum.valueOf(CollectionType.class, record[1]);
collection = CreateCollection(collectionType);
}
if (collection == null)
{
return false;
}
collection.setMaxCount(Integer.parseInt(record[2]));
String[] set = record[3].split(_separatorItems);
for (String elem : set) {
DrawningBoat boat = ExtentionDrawningBoat.CreateDrawningBoat(elem);
if (collection.insert((T) boat) == -1)
{
return false;
}
}
if (keys.contains(record[0])) {
keys.set(keys.indexOf(record[0]), record[0]);
}
else {
keys.add(record[0]);
}
_storages.put(record[0], (ICollectionGenericObjects<T>) collection);
Review

Если коллекция есть в словаре заново ее класть не требуется

Если коллекция есть в словаре заново ее класть не требуется
return true;
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
private static ICollectionGenericObjects<DrawningBoat> CreateCollection(CollectionType collectionType) {
return switch (collectionType) {
case List -> new ListGenericObjects<>();
case Massive -> new MassiveGenericObjects<>();
case None -> null;
};
}
public void addCollection(String name, CollectionType collectionType){
if (_storages.containsKey(name) || name.isEmpty()){
return;
}
@ -26,7 +248,7 @@ public class StorageCollection<T extends DrawningBoat> {
keys.add(name);
break;
case List:
_storages.put(name, new ListGenericCollection<>());
_storages.put(name, new ListGenericObjects<>());
keys.add(name);
break;
}
@ -37,6 +259,7 @@ public class StorageCollection<T extends DrawningBoat> {
keys.remove(name);
}
public ICollectionGenericObjects<T> getCollection(String name){
return _storages.get(name);
}

View File

@ -17,6 +17,9 @@ public class DrawningAbstractCompany extends JComponent {
private AbstractCompany _company = null;
DrawningBoat _drawningBoat;
private final StorageCollection<DrawningBoat> storageCollection = new StorageCollection<>();
public StorageCollection<DrawningBoat> getStorageCollection() {
return storageCollection;
}
private Stack<DrawningBoat> rubbishBinStack = new Stack<>();
public boolean collectionComboBox_SelectedIndexChanged(JFrame frame, JList<String> collectionList, JComboBox<String> obj, int width, int height) {
switch (obj.getSelectedIndex()) {

View File

@ -149,4 +149,17 @@ public class DrawningBoat {
drawPaddles.drawPaddles(g2d, entityBoat.getBodyColor(), _startPosX, _startPosY);
}
public String[] GetStringRepresentationPaddles() {
if (drawPaddles instanceof DrawningPaddles) {
return new String[]{String.valueOf(drawPaddles.getNumbersOfPaddles().getEnumNumber()), "DrawningPaddles"};
}
else if (drawPaddles instanceof DrawningOvalPaddles) {
return new String[]{String.valueOf(drawPaddles.getNumbersOfPaddles().getEnumNumber()), "DrawningOvalPaddles"};
}
else if (drawPaddles instanceof DrawningRectanglePaddles) {
return new String[]{String.valueOf(drawPaddles.getNumbersOfPaddles().getEnumNumber()), "DrawningRectanglePaddles"};
}
return null;
}
}

View File

@ -14,6 +14,10 @@ public class DrawningOvalPaddles implements IDrawPaddles {
}
}
@Override
public PaddlesCount getNumbersOfPaddles() {
return _paddlesCount;
}
@Override
public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) {
g2d.setColor(color);
g2d.setStroke(new BasicStroke(4));

View File

@ -14,6 +14,10 @@ public class DrawningPaddles implements IDrawPaddles {
}
}
@Override
public PaddlesCount getNumbersOfPaddles() {
return _paddlesCount;
}
@Override
public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) {
g2d.setColor(color);
g2d.setStroke(new BasicStroke(4));

View File

@ -14,6 +14,10 @@ public class DrawningRectanglePaddles implements IDrawPaddles {
}
}
}
@Override
public PaddlesCount getNumbersOfPaddles() {
return _paddlesCount;
}
@Override
public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) {

View File

@ -0,0 +1,76 @@
package Drawnings;
import Entities.EntityBoat;
import Entities.EntityCatamaran;
import java.util.ArrayList;
import java.util.Collections;
public class ExtentionDrawningBoat {
private static String _separatorForObjectS = ":";
private static String _separatorForObject = "\\:";
public static DrawningBoat CreateDrawningBoat(String info) {
String[] strs = info.split(_separatorForObject);
EntityBoat boat;
IDrawPaddles paddles = null;
if (strs.length == 9)
{
String s = strs[8];
switch (s) {
case "DrawningPaddles":
paddles = new DrawningPaddles();
break;
case "DrawningOvalPaddles":
paddles = new DrawningOvalPaddles();
break;
case "DrawningRectanglePaddles":
paddles = new DrawningRectanglePaddles();
break;
}
if (paddles != null) paddles.setNumber(Integer.parseInt(strs[7]));
}
else if (strs.length == 6) {
String s = strs[5];
switch (s) {
case "DrawningPaddles":
paddles = new DrawningPaddles();
break;
case "DrawningOvalPaddles":
paddles = new DrawningOvalPaddles();
break;
case "DrawningRectanglePaddles":
paddles = new DrawningRectanglePaddles();
break;
}
if (paddles != null) paddles.setNumber(Integer.parseInt(strs[4]));
}
boat = EntityCatamaran.CreateEntityCatamaran(strs);
if (boat != null)
{
return new DrawningCatamaran((EntityCatamaran) boat, paddles);
}
boat = EntityBoat.CreateEntityBoat(strs);
if (boat != null)
{
return new DrawningBoat(boat, paddles);
}
return null;
}
public static String GetDataForSave(DrawningBoat drawningBoat)
{
if (drawningBoat == null) return "";
String[] array1 = drawningBoat.entityBoat.GetStringRepresentation();
String[] array2 = drawningBoat.GetStringRepresentationPaddles();
if (array1 == null)
{
return "";
}
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, array1);
if (array2 == null) {
Collections.addAll(list, "0", " ");
}
else Collections.addAll(list, array2);
return String.join(_separatorForObjectS, list);
}
}

View File

@ -5,4 +5,5 @@ import java.awt.*;
public interface IDrawPaddles {
void setNumber(int x);
void drawPaddles(Graphics2D graphics2D, Color color, int _startX, int _startY);
PaddlesCount getNumbersOfPaddles();
}

View File

@ -1,20 +1,21 @@
package Entities;
import java.awt.*;
import java.util.Objects;
public class EntityBoat {
private int Speed;
public int getSpeed() {
private Integer Speed;
public Integer getSpeed() {
return Speed;
}
public void setSpeed(int speed) {
Speed = speed;
}
private double Weight;
private Double Weight;
public void setWeight(double weight){
Weight = weight;
}
public double getWeight() {
public Double getWeight() {
return Weight;
}
private Color BodyColor;
@ -44,4 +45,23 @@ public class EntityBoat {
this.BodyColor.getRed(), this.BodyColor.getGreen(), this.BodyColor.getBlue());
return buffer;
}
public String[] GetStringRepresentation()
{
return new String[]{"EntityBoat", Speed.toString(), Weight.toString(), colorToHexString(BodyColor)};
}
public static EntityBoat CreateEntityBoat(String[] strs)
{
if (strs.length != 6 || !Objects.equals(strs[0], "EntityBoat"))
{
return null;
}
return new EntityBoat(Integer.parseInt(strs[1]), Double.parseDouble(strs[2]), hexStringToColor(strs[3]));
}
public static String colorToHexString(Color color) {
return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
}
public static Color hexStringToColor(String hexString) {
return Color.decode(hexString);
}
}

View File

@ -1,6 +1,7 @@
package Entities;
import java.awt.*;
import java.util.Objects;
public class EntityCatamaran extends EntityBoat {
private Color AdditionalColor;
@ -39,4 +40,21 @@ public class EntityCatamaran extends EntityBoat {
buffer += ", Поплавки: " + this.Floaters;
return buffer;
}
@Override
public String[] GetStringRepresentation()
{
return new String[]{"EntityCatamaran", getSpeed().toString(), getWeight().toString(),
colorToHexString(getBodyColor()), colorToHexString(getAdditionalColor()),
String.valueOf(Sail), String.valueOf(Floaters)};
}
public static EntityCatamaran CreateEntityCatamaran(String[] strs)
{
if (strs.length != 9 || !Objects.equals(strs[0], "EntityCatamaran"))
{
return null;
}
return new EntityCatamaran(Integer.parseInt(strs[1]), Double.parseDouble(strs[2]), hexStringToColor(strs[3]),
hexStringToColor(strs[4]), Boolean.parseBoolean(strs[5]), Boolean.parseBoolean(strs[6]));
}
}

View File

@ -6,11 +6,13 @@ import Drawnings.DrawningAbstractCompany;
import Drawnings.DrawningBoat;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import javax.swing.text.NumberFormatter;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.*;
import java.io.FileFilter;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
public class FormBoatCollection extends JFrame {
@ -64,14 +66,43 @@ public class FormBoatCollection extends JFrame {
final private JButton goGoToCheckFromRubbishBinButton = new JButton("<html><center>Отправить на тест<br> объект из мусорки</html>");
final private JPanel goGoToCheckFromRubbishBinPanel = new JPanel();
final private JMenuBar menuBar = new JMenuBar();
final private JMenu fileMenu = new JMenu("Файл");
final private JMenuItem loadItem = new JMenuItem("Загрузить");
final private JMenuItem saveItem = new JMenuItem("Сохранить");
final private JMenuItem loadCollection = new JMenuItem("Загрузить коллекцию");
final private JMenuItem saveCollection = new JMenuItem("Сохранить коллекцию");
public void OpenFrame() {
fileMenu.add(loadItem);
fileMenu.add(saveItem);
fileMenu.add(loadCollection);
fileMenu.add(saveCollection);
fileMenu.addSeparator();
menuBar.add(fileMenu);
saveItem.addActionListener(e -> {
SaveFile();
});
loadItem.addActionListener(e -> {
LoadFile();
});
saveCollection.addActionListener(e -> {
SaveCollection();
});
loadCollection.addActionListener(e -> {
LoadCollection();
});
jFrameCollectionBoats.setJMenuBar(menuBar);
jFrameCollectionBoats.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit tolls = Toolkit.getDefaultToolkit();
Dimension dimension = tolls.getScreenSize();
jFrameCollectionBoats.setBounds(dimension.width / 2 - 250, dimension.height / 2 - 250,
1200, 665);
1200, 675);
jFrameCollectionBoats.setTitle("Коллекция лодок");
toolsPanel.setBackground(Color.BLACK);
@ -178,6 +209,8 @@ public class FormBoatCollection extends JFrame {
listOfDownPanel.add(refreshButton);
setEnableComponentsOfList(listOfDownPanel, false);
jFrameCollectionBoats.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent componentEvent) {
@ -187,11 +220,11 @@ public class FormBoatCollection extends JFrame {
radioButtonsPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, 78);
addCollectionPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, 103);
collectionsListPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, 137);
deleteCollectionPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, 227);
deleteCollectionPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, 241);
comboBoxPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 400);
createCompanyPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 370);
goToCheckPanel.setLocation(jFrameCollectionBoats.getWidth() - 200,
jFrameCollectionBoats.getHeight() - 320);
jFrameCollectionBoats.getHeight() - 325);
goGoToCheckFromRubbishBinPanel.setLocation(jFrameCollectionBoats.getWidth() - 200,
jFrameCollectionBoats.getHeight() - 295);
addBoatPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 241);
@ -199,13 +232,15 @@ public class FormBoatCollection extends JFrame {
textBoxPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 146);
removePanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 119);
refreshPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 67);
refreshPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 67- menuBar.getHeight());
toolsPanel.setSize(new Dimension(10, jFrameCollectionBoats.getHeight()));
jFrameCollectionBoats.repaint();
}
});
buttonAddBoat.addActionListener(e -> {
_company.createObject(jFrameCollectionBoats);
jFrameCollectionBoats.repaint();
@ -240,7 +275,7 @@ public class FormBoatCollection extends JFrame {
});
createCompanyButton.addActionListener(e -> {
boolean res = _company.collectionComboBox_SelectedIndexChanged(jFrameCollectionBoats, collectionsList, comboBoxSelectorCompany,
jFrameCollectionBoats.getWidth() - 233, jFrameCollectionBoats.getHeight());
jFrameCollectionBoats.getWidth() - 233, jFrameCollectionBoats.getHeight() - 15);
setEnableComponentsOfList(listOfDownPanel, res);
jFrameCollectionBoats.repaint();
});
@ -260,15 +295,68 @@ public class FormBoatCollection extends JFrame {
jFrameCollectionBoats.setVisible(true);
}
private String SaveWindow() {
FileDialog fileDialog = new FileDialog(this, "Save File", FileDialog.SAVE);
fileDialog.setVisible(true);
String directory = fileDialog.getDirectory();
String file = fileDialog.getFile();
if (directory == null || file == null) return null;
return directory + file;
}
private void SaveFile() {
String filename = SaveWindow();
if (_company.getStorageCollection().SaveData(filename)) {
JOptionPane.showMessageDialog(jFrameCollectionBoats, "Сохранено");
}
else JOptionPane.showMessageDialog(jFrameCollectionBoats, "Ошибка сохранения");
}
private void SaveCollection() {
String filename = SaveWindow();
if (filename == null) {
JOptionPane.showMessageDialog(jFrameCollectionBoats, "Файл не выбран");
return;
}
if (collectionsList.getSelectedIndex() < 0 || collectionsList.getSelectedValue() == null) {
JOptionPane.showMessageDialog(jFrameCollectionBoats, "Коллекция не выбрана");
}
if (_company.getStorageCollection().SaveOneCollection(filename, collectionsList)) {
JOptionPane.showMessageDialog(jFrameCollectionBoats, "Коллекция сохранена");
}
else JOptionPane.showMessageDialog(jFrameCollectionBoats, "Ошибка сохранения");
}
private String LoadWindow() {
FileDialog fileDialog = new FileDialog(this, "Save File", FileDialog.LOAD);
fileDialog.setVisible(true);
String directory = fileDialog.getDirectory();
String file = fileDialog.getFile();
if (directory == null || file == null) return null;
return directory + file;
}
private void LoadFile() {
String filename = LoadWindow();
if (_company.getStorageCollection().LoadData(filename)) {
JOptionPane.showMessageDialog(jFrameCollectionBoats, "Загрузка прошла успешно");
updateCollectionsList(_company.getStorageCollection());
}
else JOptionPane.showMessageDialog(jFrameCollectionBoats, "Не загрузилось");
}
private void LoadCollection() {
String filename = LoadWindow();
if (_company.getStorageCollection().LoadOneCollection(filename)) {
JOptionPane.showMessageDialog(jFrameCollectionBoats, "Коллекция загружена");
updateCollectionsList(_company.getStorageCollection());
}
else JOptionPane.showMessageDialog(jFrameCollectionBoats, "Не загрузилось");
}
private void updateCollectionsList(StorageCollection<DrawningBoat> storageCollection) {
if (storageCollection == null) {
return;
}
listModel.clear();
ArrayList<String> keys = storageCollection.keys;
for (String key : keys) {
listModel.addElement(key);
listModel.removeAllElements();
for (int i = storageCollection.keys.size() - 1; i >= 0; i--) {
listModel.add(0, storageCollection.keys.get(i));
}
}
private void setEnableComponentsOfList(ArrayList<Component> list, boolean type) {