completed

This commit is contained in:
rozenkranzz 2025-02-14 01:01:17 +04:00
parent b48714c9cb
commit 1ce9de4971
5 changed files with 450 additions and 21 deletions

View File

@ -5,11 +5,13 @@ import projectliner.Drawings.*;
import projectliner.Entities.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.LinkedList;
import java.util.Random;
public class FormShipCollection extends JFrame {
@ -23,32 +25,40 @@ public class FormShipCollection extends JFrame {
private JButton buttonCreateRandomShip;
private JTextField textFieldPosition;
private JLabel pictureBox;
private final CollectionStorage<DrawingBaseLiner> collectionStorage;
private JPanel collectionStorages;
private JPanel controlPanel;
private JButton buttonAddCollection;
private JButton buttonRemoveCollection;
private JButton buttonCreateCompany;
private JTextField textBoxCollectionName;
private JRadioButton radioButtonList;
private JRadioButton radioButtonArray;
private JList<String> listBoxCollections;
private JButton buttonShowRemovedObjects;
public FormShipCollection() {
setTitle("FormShipCollection");
setSize(1100, 700);
setSize(1300, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
collectionStorage = new CollectionStorage<>();
comboBoxCompanySelector = new JComboBox<>(new String[]{"", "Landing Stage"});
comboBoxCompanySelector.setMaximumSize(new Dimension(100,
comboBoxCompanySelector.getPreferredSize().height));
comboBoxCompanySelector.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
comboBoxCompanySelector.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (comboBoxCompanySelector.getSelectedItem().toString()) {
case "Landing Stage":
company = new LandingStage(pictureBox.getWidth(), pictureBox.getHeight(),
new GenericObjectsCollection<DrawingBaseLiner>());
break;
default:
JOptionPane.showMessageDialog(null, "No matching case found");
break;
controlPanel.setEnabled(false);
for (Component component : controlPanel.getComponents()) {
component.setEnabled(false);
}
}
});
buttonAddLiner = new JButton("Add Liner");
buttonAddLiner.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
buttonAddLiner.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@ -57,6 +67,7 @@ public class FormShipCollection extends JFrame {
});
buttonAddShip = new JButton("Add Ship");
buttonAddShip.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
buttonAddShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@ -65,6 +76,7 @@ public class FormShipCollection extends JFrame {
});
buttonCreateRandomShip = new JButton("Create Random Ship");
buttonCreateRandomShip.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
buttonCreateRandomShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@ -73,6 +85,7 @@ public class FormShipCollection extends JFrame {
});
buttonRemoveShip = new JButton("Remove Ship");
buttonRemoveShip.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
buttonRemoveShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@ -81,6 +94,7 @@ public class FormShipCollection extends JFrame {
});
buttonSubmitForTesting = new JButton("Submit for Testing");
buttonSubmitForTesting.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
buttonSubmitForTesting.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@ -89,6 +103,7 @@ public class FormShipCollection extends JFrame {
});
buttonRefresh = new JButton("Refresh");
buttonRefresh.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
buttonRefresh.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@ -96,6 +111,88 @@ public class FormShipCollection extends JFrame {
}
});
buttonAddCollection = new JButton("Add Collection");
buttonAddCollection.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
buttonAddCollection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addCollection();
}
});
buttonRemoveCollection = new JButton("Remove Collection");
buttonRemoveCollection.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
buttonRemoveCollection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
removeCollection();
}
});
buttonCreateCompany = new JButton("Create Company");
buttonCreateCompany.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
buttonCreateCompany.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createCompany();
}
});
buttonShowRemovedObjects = new JButton("Show Removed Objects");
buttonShowRemovedObjects.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
buttonShowRemovedObjects.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendToFormFromRemovedCollection();
}
});
JLabel labelCollectionName = new JLabel("Collection Name:");
JLabel labelPosition = new JLabel("Position:");
textBoxCollectionName = new JTextField(10);
textBoxCollectionName.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
radioButtonList = new JRadioButton("List");
radioButtonArray = new JRadioButton("Array");
ButtonGroup radioGroup = new ButtonGroup();
radioGroup.add(radioButtonList);
radioGroup.add(radioButtonArray);
listBoxCollections = new JList<>();
listBoxCollections.setMaximumSize(new Dimension(Integer.MAX_VALUE, 150));
listBoxCollections.setListData(new String[]{});
collectionStorages = new JPanel();
collectionStorages.setLayout(new BoxLayout(collectionStorages, BoxLayout.Y_AXIS));
collectionStorages.setMaximumSize(new Dimension(200, 500));
collectionStorages.setBorder(new EmptyBorder(10, 10, 10, 10));
labelCollectionName.setAlignmentX(Component.CENTER_ALIGNMENT);
textBoxCollectionName.setAlignmentX(Component.CENTER_ALIGNMENT);
radioButtonList.setAlignmentX(Component.CENTER_ALIGNMENT);
radioButtonArray.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonAddCollection.setAlignmentX(Component.CENTER_ALIGNMENT);
listBoxCollections.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonRemoveCollection.setAlignmentX(Component.CENTER_ALIGNMENT);
comboBoxCompanySelector.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonCreateCompany.setAlignmentX(Component.CENTER_ALIGNMENT);
collectionStorages.add(labelCollectionName);
collectionStorages.add(Box.createVerticalStrut(10));
collectionStorages.add(textBoxCollectionName);
collectionStorages.add(radioButtonList);
collectionStorages.add(radioButtonArray);
collectionStorages.add(Box.createVerticalStrut(10));
collectionStorages.add(buttonAddCollection);
collectionStorages.add(Box.createVerticalStrut(10));
collectionStorages.add(listBoxCollections);
collectionStorages.add(Box.createVerticalStrut(10));
collectionStorages.add(buttonRemoveCollection);
collectionStorages.add(Box.createVerticalStrut(30));
collectionStorages.add(comboBoxCompanySelector);
collectionStorages.add(Box.createVerticalStrut(10));
collectionStorages.add(buttonCreateCompany);
collectionStorages.setEnabled(false);
try {
MaskFormatter maskFormatter = new MaskFormatter("##");
maskFormatter.setPlaceholderCharacter('_');
@ -110,39 +207,167 @@ public class FormShipCollection extends JFrame {
pictureBox.setPreferredSize(new Dimension(600, 400));
pictureBox.setBorder(BorderFactory.createLineBorder(Color.BLACK));
comboBoxCompanySelector.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonAddLiner.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonAddShip.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonCreateRandomShip.setAlignmentX(Component.CENTER_ALIGNMENT);
labelPosition.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonRemoveShip.setAlignmentX(Component.CENTER_ALIGNMENT);
textFieldPosition.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonSubmitForTesting.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonShowRemovedObjects.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonRefresh.setAlignmentX(Component.CENTER_ALIGNMENT);
JPanel controlPanel = new JPanel();
controlPanel = new JPanel();
controlPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
controlPanel.setMaximumSize(new Dimension(200, 500));
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
controlPanel.add(comboBoxCompanySelector);
controlPanel.add(Box.createVerticalStrut(100));
controlPanel.add(buttonAddLiner);
controlPanel.add(Box.createVerticalStrut(10));
controlPanel.add(buttonAddShip);
controlPanel.add(Box.createVerticalStrut(10));
controlPanel.add(buttonCreateRandomShip);
controlPanel.add(Box.createVerticalStrut(100));
controlPanel.add(Box.createVerticalStrut(10));
controlPanel.add(buttonRemoveShip);
controlPanel.add(Box.createVerticalStrut(10));
controlPanel.add(new JLabel("Position:"));
controlPanel.add(labelPosition);
controlPanel.add(Box.createVerticalStrut(5));
controlPanel.add(textFieldPosition);
controlPanel.add(Box.createVerticalStrut(100));
controlPanel.add(Box.createVerticalStrut(10));
controlPanel.add(buttonSubmitForTesting);
controlPanel.add(Box.createVerticalStrut(100));
controlPanel.add(Box.createVerticalStrut(10));
controlPanel.add(buttonShowRemovedObjects);
controlPanel.add(Box.createVerticalStrut(10));
controlPanel.add(buttonRefresh);
add(controlPanel, BorderLayout.EAST);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.add(collectionStorages);
rightPanel.add(controlPanel);
add(rightPanel, BorderLayout.EAST);
add(pictureBox, BorderLayout.CENTER);
}
private void createCompany() {
if (listBoxCollections.getSelectedIndex() < 0 || listBoxCollections.getSelectedValue() == null) {
JOptionPane.showMessageDialog(this, "Collection is not selected");
return;
}
IGenericObjectsCollection<DrawingBaseLiner> collection =
collectionStorage.getCollection(listBoxCollections.getSelectedValue());
if (collection == null) {
JOptionPane.showMessageDialog(this, "Collection is not found");
return;
}
switch (comboBoxCompanySelector.getSelectedItem().toString()) {
case "Landing Stage":
company = new LandingStage(pictureBox.getWidth(), pictureBox.getHeight(), collection);
break;
}
controlPanel.setEnabled(true);
for (Component component : controlPanel.getComponents()) {
component.setEnabled(true);
}
refreshListBoxItems();
}
private void sendToFormFromRemovedCollection() {
IGenericObjectsCollection<DrawingBaseLiner> removedCollection
= collectionStorage.getCollection("Removed");
if (removedCollection == null || removedCollection.getCount() == 0) {
JOptionPane.showMessageDialog(this,
"No objects in the Removed collection");
return;
}
String input = JOptionPane.showInputDialog(this,
"There are " + removedCollection.getCount() +
" objects in the Removed collection.\n" +
"Enter the number of the object to send to the form:",
"Select Object",
JOptionPane.QUESTION_MESSAGE);
if (input == null || input.isEmpty()) {
return;
}
int index;
try {
index = Integer.parseInt(input);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this,
"Invalid number entered", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (index < 0 || index >= removedCollection.getCount()) {
JOptionPane.showMessageDialog(this,
"Invalid index", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
DrawingBaseLiner liner = removedCollection.get(index);
if (liner != null) {
FormLiner form = new FormLiner();
form.setLiner(liner);
form.setVisible(true);
} else {
JOptionPane.showMessageDialog(this,
"No object found at the specified index", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void removeCollection() {
if (listBoxCollections.getSelectedIndex() < 0 ||
listBoxCollections.getSelectedValue() == null) {
JOptionPane.showMessageDialog(this,
"Collection is not selected", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
String collectionName = listBoxCollections.getSelectedValue();
if (JOptionPane.showConfirmDialog(this,
"Are you sure you want to remove the collection '" + collectionName + "'?",
"Confirm Removal", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
collectionStorage.delCollection(collectionName);
refreshListBoxItems();
JOptionPane.showMessageDialog(this,
"Collection removed successfully", "Success",
JOptionPane.OK_OPTION);
}
}
private void addCollection() {
if (textBoxCollectionName.getText().isEmpty() ||
(!radioButtonList.isSelected() && !radioButtonArray.isSelected())) {
JOptionPane.showMessageDialog(this,
"Please fill all fields", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
CollectionType collectionType = CollectionType.NONE;
if (radioButtonArray.isSelected()) {
collectionType = CollectionType.ARRAY;
} else if (radioButtonList.isSelected()) {
collectionType = CollectionType.LIST;
}
collectionStorage.addCollection(textBoxCollectionName.getText(), collectionType);
refreshListBoxItems();
}
private void refreshListBoxItems() {
DefaultListModel<String> listModel = new DefaultListModel<>();
for (String colName : collectionStorage.getKeys()) {
if (colName != null && !colName.isEmpty()) {
listModel.addElement(colName);
}
}
listBoxCollections.setModel(listModel);
}
private void createObject(String type) {
if (company == null) {
return;
@ -183,9 +408,19 @@ public class FormShipCollection extends JFrame {
if (textFieldPosition.getText().isEmpty() || company == null) {
return;
}
if (collectionStorage.getCollection("Removed") == null) {
collectionStorage.addCollection("Removed", CollectionType.LINKED_LIST);
}
int pos = Integer.parseInt(textFieldPosition.getText());
if (JOptionPane.showConfirmDialog(this, "Remove object?", "Removing...",
if (JOptionPane.showConfirmDialog(this,
"Remove object?", "Removing...",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
IGenericObjectsCollection<DrawingBaseLiner> removedCollection
= collectionStorage.getCollection("Removed");
DrawingBaseLiner removedObject = collectionStorage.getObject(listBoxCollections.getSelectedValue(), pos);
if (removedObject != null) {
removedCollection.insert(removedObject);
}
if (AbstractCompany.remove(company, pos)) {
JOptionPane.showMessageDialog(this, "Object was removed");
pictureBox.setIcon(new ImageIcon(company.show()));

View File

@ -0,0 +1,53 @@
package projectliner.GenericObjectsCollection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CollectionStorage<T> {
private final Map<String, IGenericObjectsCollection<T>> storages;
public CollectionStorage() {
this.storages = new HashMap<>();
}
public List<String> getKeys() {
return new ArrayList<>(storages.keySet());
}
public void addCollection(String name, CollectionType collectionType) {
if (name == null || name.isEmpty() || storages.containsKey(name)) {
return;
}
switch (collectionType) {
case ARRAY:
storages.put(name, new GenericObjectsCollection<>());
break;
case LIST:
storages.put(name, new ListGenericObjects<>());
break;
case LINKED_LIST:
storages.put(name, new LinkedListGenericObjects<>());
}
}
public void delCollection(String name) {
if (name == null || name.isEmpty() || !storages.containsKey(name)) {
return;
}
storages.remove(name);
}
public IGenericObjectsCollection<T> getCollection(String name) {
return storages.get(name);
}
public T getObject(String name, int position) {
IGenericObjectsCollection<T> collection = storages.get(name);
if (collection == null) {
return null;
}
return collection.get(position);
}
}

View File

@ -0,0 +1,8 @@
package projectliner.GenericObjectsCollection;
public enum CollectionType {
NONE,
ARRAY,
LIST,
LINKED_LIST
}

View File

@ -0,0 +1,64 @@
package projectliner.GenericObjectsCollection;
import java.util.LinkedList;
public class LinkedListGenericObjects<T> implements IGenericObjectsCollection<T> {
private final LinkedList<T> collection;
private int maxCount;
public LinkedListGenericObjects() {
this.collection = new LinkedList<>();
}
@Override
public int getCount() {
return collection.size();
}
@Override
public void setMaxCount(int maxCount) {
if (maxCount > 0) {
this.maxCount = maxCount;
}
}
@Override
public boolean insert(T obj) {
collection.add(obj);
return false;
}
@Override
public boolean insert(T obj, int position) {
if (position < 0 || position >= maxCount) {
return false;
}
if (position >= collection.size()) {
collection.addAll(new LinkedList<>(collection.subList(collection.size(), position)));
}
if (collection.get(position) == null) {
collection.set(position, obj);
return true;
}
return false;
}
@Override
public boolean remove(int position) {
if (position < 0 || position >= collection.size()) {
return false;
}
collection.remove(position);
return true;
}
@Override
public T get(int position) {
if (position < 0 || position >= collection.size()) {
return null;
}
return collection.get(position);
}
}

View File

@ -0,0 +1,69 @@
package projectliner.GenericObjectsCollection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ListGenericObjects<T> implements IGenericObjectsCollection<T> {
private final List<T> collection;
private int maxCount;
public ListGenericObjects() {
this.collection = new ArrayList<>();
}
@Override
public int getCount() {
return collection.size();
}
@Override
public void setMaxCount(int maxCount) {
if (maxCount > 0) {
this.maxCount = maxCount;
}
}
@Override
public T get(int position) {
if (position < 0 || position >= collection.size()) {
return null;
}
return collection.get(position);
}
@Override
public boolean insert(T obj) {
if (collection.size() < maxCount) {
collection.add(obj);
return true;
}
return false;
}
@Override
public boolean insert(T obj, int position) {
if (position < 0 || position >= maxCount) {
return false;
}
if (position >= collection.size()) {
collection.addAll(Collections.nCopies(position - collection.size() + 1, null));
}
if (collection.get(position) == null) {
collection.set(position, obj);
return true;
}
return false;
}
@Override
public boolean remove(int position) {
if (position < 0 || position >= collection.size() || collection.get(position) == null) {
return false;
}
collection.remove(position);
return true;
}
}