diff --git a/src/projectMonorail/FrameMonorail.java b/src/projectMonorail/FrameMonorail.java index b16a957..925b33d 100644 --- a/src/projectMonorail/FrameMonorail.java +++ b/src/projectMonorail/FrameMonorail.java @@ -1,5 +1,7 @@ package projectMonorail; +import projectMonorail.DrawingObjects.DrawingLocomotive; + import javax.swing.*; public class FrameMonorail extends JDialog { @@ -10,11 +12,11 @@ public class FrameMonorail extends JDialog { return pictureBox; } - public FrameMonorail(JFrame parent) { + public FrameMonorail(JFrame parent, DrawingLocomotive locomotive) { super(parent ,"Monorail", true); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - pictureBox = new PictureBox(); + pictureBox = new PictureBox(locomotive); add(pictureBox); pack(); diff --git a/src/projectMonorail/FrameMonorailCollection.java b/src/projectMonorail/FrameMonorailCollection.java index 82ab4d5..6a6fc9c 100644 --- a/src/projectMonorail/FrameMonorailCollection.java +++ b/src/projectMonorail/FrameMonorailCollection.java @@ -8,10 +8,14 @@ import javax.swing.*; import javax.swing.text.NumberFormatter; import java.awt.*; import java.text.NumberFormat; +import java.util.LinkedList; +import java.util.Queue; public class FrameMonorailCollection extends JFrame { - private final LocomotivesGenericCollection locomotives; + private final LocomotivesGenericStorage storage; + + private Queue removedLocomotives; private JPanel paddingTopPanel; @@ -21,11 +25,15 @@ public class FrameMonorailCollection extends JFrame { private int pictureBoxCollectionWidth = 900; - private int pictureBoxCollectionHeight = 430; + private int pictureBoxCollectionHeight = 580; private JPanel toolsPanel; - private JLabel nameLabel; + private JPanel setsPanel; + + private JLabel nameToolsLabel; + + private JLabel nameSetsLabel; private JButton buttonAddMonorail; @@ -35,21 +43,49 @@ public class FrameMonorailCollection extends JFrame { private JButton buttonRandomGeneration; + private JButton buttonAddSet; + + private JButton buttonDelSet; + + private JButton buttonShowRemoved; + + private JTextField textFieldNameSet; + private JFormattedTextField textFieldNumber; + private JList listSets; + + private DefaultListModel listModel; + public FrameMonorailCollection() { super("Monorail collection"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - locomotives = new LocomotivesGenericCollection<>(pictureBoxCollectionWidth, pictureBoxCollectionHeight); - pictureBoxCollection = new PictureBoxCollection(locomotives, pictureBoxCollectionWidth, + listModel = new DefaultListModel<>(); + listSets = new JList<>(listModel); + listSets.setMaximumSize(new Dimension(145, 94)); + listSets.setAlignmentX(Component.CENTER_ALIGNMENT); + listSets.addListSelectionListener(e -> { + repaint(); + }); + + storage = new LocomotivesGenericStorage(pictureBoxCollectionWidth, pictureBoxCollectionHeight); + pictureBoxCollection = new PictureBoxCollection(storage, listSets, pictureBoxCollectionWidth, pictureBoxCollectionHeight); - nameLabel = new JLabel(" Tools"); - nameLabel.setFont(new Font("Segoe UI", Font.BOLD, 16)); - nameLabel.setHorizontalTextPosition(JLabel.LEFT); - nameLabel.setMaximumSize(new Dimension(190, 30)); - nameLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + removedLocomotives = new LinkedList<>(); + + nameToolsLabel = new JLabel(" Tools"); + nameToolsLabel.setFont(new Font("Segoe UI", Font.BOLD, 16)); + nameToolsLabel.setHorizontalTextPosition(JLabel.LEFT); + nameToolsLabel.setMaximumSize(new Dimension(190, 30)); + nameToolsLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + + nameSetsLabel = new JLabel(" Sets"); + nameSetsLabel.setFont(new Font("Segoe UI", Font.BOLD, 16)); + nameSetsLabel.setHorizontalTextPosition(JLabel.LEFT); + nameSetsLabel.setMaximumSize(new Dimension(163, 30)); + nameSetsLabel.setAlignmentX(Component.CENTER_ALIGNMENT); buttonAddMonorail = new JButton("Add monorail"); buttonAddMonorail.setHorizontalTextPosition(JButton.CENTER); @@ -62,9 +98,16 @@ public class FrameMonorailCollection extends JFrame { buttonAddMonorail.setAlignmentX(Component.CENTER_ALIGNMENT); buttonAddMonorail.addActionListener(e -> { - FrameMonorail frame = new FrameMonorail(this); + if (listSets.getSelectedIndex() == -1) + return; + + var obj = storage.get(listSets.getSelectedValue()); + if (obj == null) + return; + + FrameMonorail frame = new FrameMonorail(this, null); if (frame.getPictureBox().getDialogResult()) { - if (locomotives.addition(frame.getPictureBox().getSelectedLocomotive()) != -1) { + if (obj.addition(frame.getPictureBox().getSelectedLocomotive()) != -1) { JOptionPane.showMessageDialog(null, "Object added", "Message", JOptionPane.INFORMATION_MESSAGE); pictureBoxCollection.repaint(); @@ -88,6 +131,11 @@ public class FrameMonorailCollection extends JFrame { textFieldNumber.setMaximumSize(new Dimension(155, 25)); textFieldNumber.setAlignmentX(Component.CENTER_ALIGNMENT); + textFieldNameSet = new JTextField(); + textFieldNameSet.setFont(new Font("Segoe UI", Font.PLAIN, 12)); + textFieldNameSet.setMaximumSize(new Dimension(145, 25)); + textFieldNameSet.setAlignmentX(Component.CENTER_ALIGNMENT); + buttonRemoveMonorail = new JButton("Remove monorail"); buttonRemoveMonorail.setHorizontalTextPosition(JButton.CENTER); buttonRemoveMonorail.setVerticalTextPosition(JButton.CENTER); @@ -99,6 +147,13 @@ public class FrameMonorailCollection extends JFrame { buttonRemoveMonorail.setAlignmentX(Component.CENTER_ALIGNMENT); buttonRemoveMonorail.addActionListener(e -> { + if (listSets.getSelectedIndex() == -1) + return; + + var obj = storage.get(listSets.getSelectedValue()); + if (obj == null) + return; + if (JOptionPane.showConfirmDialog(null, "Delete the object?", "Deletion", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { return; @@ -107,7 +162,9 @@ public class FrameMonorailCollection extends JFrame { return; } int pos = Integer.parseInt(textFieldNumber.getText()); - if (locomotives.subtraction(pos)) { + var removedLocomotive = obj.subtraction(pos); + if (removedLocomotive != null) { + removedLocomotives.add(removedLocomotive); JOptionPane.showMessageDialog(null, "Object deleted", "Message", JOptionPane.INFORMATION_MESSAGE); pictureBoxCollection.repaint(); @@ -127,7 +184,16 @@ public class FrameMonorailCollection extends JFrame { buttonRefreshCollection.setBorder(BorderFactory.createLineBorder(Color.black, 2)); buttonRefreshCollection.setAlignmentX(Component.CENTER_ALIGNMENT); - buttonRefreshCollection.addActionListener(e -> pictureBoxCollection.repaint()); + buttonRefreshCollection.addActionListener(e -> { + if (listSets.getSelectedIndex() == -1) + return; + + var obj = storage.get(listSets.getSelectedValue()); + if (obj == null) + return; + + pictureBoxCollection.repaint(); + }); buttonRandomGeneration = new JButton("Show additional frame"); buttonRandomGeneration.setHorizontalTextPosition(JButton.CENTER); @@ -141,20 +207,97 @@ public class FrameMonorailCollection extends JFrame { buttonRandomGeneration.addActionListener(e -> new FrameRandomGeneration(this)); + buttonShowRemoved = new JButton("Show deleted item"); + buttonShowRemoved.setHorizontalTextPosition(JButton.CENTER); + buttonShowRemoved.setVerticalTextPosition(JButton.CENTER); + buttonShowRemoved.setFocusable(false); + buttonShowRemoved.setFont(new Font("Segoe UI", Font.PLAIN, 12)); + buttonShowRemoved.setBackground(Color.WHITE); + buttonShowRemoved.setMaximumSize(new Dimension(155, 36)); + buttonShowRemoved.setBorder(BorderFactory.createLineBorder(Color.black, 2)); + buttonShowRemoved.setAlignmentX(Component.CENTER_ALIGNMENT); + + buttonShowRemoved.addActionListener(e -> { + if (removedLocomotives.isEmpty()) { + JOptionPane.showMessageDialog(null, "The queue is empty", "Message", JOptionPane.INFORMATION_MESSAGE); + return; + } + new FrameMonorail(this, removedLocomotives.remove()); + }); + + buttonAddSet = new JButton("Add set"); + buttonAddSet.setHorizontalTextPosition(JButton.CENTER); + buttonAddSet.setVerticalTextPosition(JButton.CENTER); + buttonAddSet.setFocusable(false); + buttonAddSet.setFont(new Font("Segoe UI", Font.PLAIN, 12)); + buttonAddSet.setBackground(Color.WHITE); + buttonAddSet.setMaximumSize(new Dimension(145, 36)); + buttonAddSet.setBorder(BorderFactory.createLineBorder(Color.black, 2)); + buttonAddSet.setAlignmentX(Component.CENTER_ALIGNMENT); + + buttonAddSet.addActionListener(e -> { + if (textFieldNameSet.getText() == null || textFieldNameSet.getText().isEmpty()) { + JOptionPane.showMessageDialog(null, "Not all data is filled in", "Error", + JOptionPane.ERROR_MESSAGE); + return; + } + storage.addSet(textFieldNameSet.getText()); + reloadObjects(); + }); + + buttonDelSet = new JButton("Remove set"); + buttonDelSet.setHorizontalTextPosition(JButton.CENTER); + buttonDelSet.setVerticalTextPosition(JButton.CENTER); + buttonDelSet.setFocusable(false); + buttonDelSet.setFont(new Font("Segoe UI", Font.PLAIN, 12)); + buttonDelSet.setBackground(Color.WHITE); + buttonDelSet.setMaximumSize(new Dimension(145, 36)); + buttonDelSet.setBorder(BorderFactory.createLineBorder(Color.black, 2)); + buttonDelSet.setAlignmentX(Component.CENTER_ALIGNMENT); + + buttonDelSet.addActionListener(e -> { + if (listSets.getSelectedIndex() == -1) + return; + if (JOptionPane.showConfirmDialog(null, "Delete the set " + + listSets.getSelectedValue() + "?", "Deletion", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { + storage.delSet(listSets.getSelectedValue()); + reloadObjects(); + } + }); + + setsPanel = new JPanel(); + setsPanel.setMaximumSize(new Dimension(163, 292)); + setsPanel.setLayout(new BoxLayout(setsPanel, BoxLayout.Y_AXIS)); + setsPanel.setBorder(BorderFactory.createLineBorder(Color.black)); + setsPanel.add(nameSetsLabel); + setsPanel.add(Box.createVerticalStrut(13)); + setsPanel.add(textFieldNameSet); + setsPanel.add(Box.createVerticalStrut(13)); + setsPanel.add(buttonAddSet); + setsPanel.add(Box.createVerticalStrut(13)); + setsPanel.add(listSets); + setsPanel.add(Box.createVerticalStrut(13)); + setsPanel.add(buttonDelSet); + setsPanel.add(Box.createVerticalStrut(5)); + toolsPanel = new JPanel(); - toolsPanel.setMaximumSize(new Dimension(190, 430)); + toolsPanel.setMaximumSize(new Dimension(190, 620)); toolsPanel.setLayout(new BoxLayout(toolsPanel, BoxLayout.Y_AXIS)); toolsPanel.setBorder(BorderFactory.createLineBorder(Color.black)); - toolsPanel.add(nameLabel); + toolsPanel.add(nameToolsLabel); + toolsPanel.add(Box.createVerticalStrut(14)); + toolsPanel.add(setsPanel); toolsPanel.add(Box.createVerticalStrut(14)); toolsPanel.add(buttonAddMonorail); - toolsPanel.add(Box.createVerticalStrut(77)); + toolsPanel.add(Box.createVerticalStrut(30)); toolsPanel.add(textFieldNumber); toolsPanel.add(Box.createVerticalStrut(6)); toolsPanel.add(buttonRemoveMonorail); - toolsPanel.add(Box.createVerticalStrut(74)); + toolsPanel.add(Box.createVerticalStrut(30)); toolsPanel.add(buttonRefreshCollection); toolsPanel.add(Box.createVerticalStrut(40)); + toolsPanel.add(buttonShowRemoved); + toolsPanel.add(Box.createVerticalStrut(14)); toolsPanel.add(buttonRandomGeneration); mainPanel = new JPanel(); @@ -171,9 +314,23 @@ public class FrameMonorailCollection extends JFrame { add(paddingTopPanel); - setPreferredSize(new Dimension(990, 478)); + setPreferredSize(new Dimension(990, 664)); pack(); setLocationRelativeTo(null); setVisible(true); } + + private void reloadObjects() { + int index = listSets.getSelectedIndex(); + listModel.clear(); + for (int i = 0; i < storage.keys().size(); i++) { + listModel.addElement(storage.keys().get(i)); + } + if (!listModel.isEmpty() && (index == -1 || index >= listModel.size())) { + listSets.setSelectedIndex(0); + } + else if (!listModel.isEmpty() && index > -1 && index < listModel.size()) { + listSets.setSelectedIndex(index); + } + } } diff --git a/src/projectMonorail/Generics/LocomotivesGenericCollection.java b/src/projectMonorail/Generics/LocomotivesGenericCollection.java index 4bbca05..6bda125 100644 --- a/src/projectMonorail/Generics/LocomotivesGenericCollection.java +++ b/src/projectMonorail/Generics/LocomotivesGenericCollection.java @@ -31,13 +31,13 @@ public class LocomotivesGenericCollection width) { diff++; currWidth = 1; } - obj = collection.get(i); - if (obj != null) { - obj.setPosition(i % width * placeSizeWidth + placeSizeWidth / 40, + if (locomotive != null) { + locomotive.setPosition(i % width * placeSizeWidth + placeSizeWidth / 40, (height - diff) * placeSizeHeight + placeSizeHeight / 15); - obj.drawTransport(g2d); + locomotive.drawTransport(g2d); } + i++; } } diff --git a/src/projectMonorail/Generics/LocomotivesGenericStorage.java b/src/projectMonorail/Generics/LocomotivesGenericStorage.java new file mode 100644 index 0000000..4dbf880 --- /dev/null +++ b/src/projectMonorail/Generics/LocomotivesGenericStorage.java @@ -0,0 +1,46 @@ +package projectMonorail.Generics; + +import projectMonorail.DrawingObjects.DrawingLocomotive; +import projectMonorail.MovementStrategy.DrawingObjectLocomotive; + +import java.util.ArrayList; +import java.util.HashMap; + +public class LocomotivesGenericStorage { + final HashMap> locomotivesStorages; + + public ArrayList keys() {return new ArrayList<>(locomotivesStorages.keySet());} + + private final int pictureWidth; + + private final int pictureHeight; + + public LocomotivesGenericStorage(int pictureWidth, int pictureHeight) { + locomotivesStorages = new HashMap<>(); + this.pictureWidth = pictureWidth; + this.pictureHeight = pictureHeight; + } + + public void addSet(String name) { + if (locomotivesStorages.containsKey(name)) + return; + locomotivesStorages.put(name, new LocomotivesGenericCollection<>(pictureWidth, pictureHeight)); + } + + public void delSet(String name) { + if (locomotivesStorages.containsKey(name)) + locomotivesStorages.remove(name); + } + + public LocomotivesGenericCollection get(String ind) { + if (locomotivesStorages.containsKey(ind)) + return locomotivesStorages.get(ind); + return null; + } + + public DrawingObjectLocomotive get(String name, int ind) { + if (locomotivesStorages.containsKey(name)) + return locomotivesStorages.get(name).getU(ind); + return null; + } +} diff --git a/src/projectMonorail/Generics/SetGeneric.java b/src/projectMonorail/Generics/SetGeneric.java index 022997e..142dd36 100644 --- a/src/projectMonorail/Generics/SetGeneric.java +++ b/src/projectMonorail/Generics/SetGeneric.java @@ -1,14 +1,23 @@ package projectMonorail.Generics; +import projectMonorail.DrawingObjects.DrawingLocomotive; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.NoSuchElementException; + public class SetGeneric { - private final T[] places; + private final ArrayList places; public int count() { - return places.length; + return places.size(); } + private final int maxCount; + public SetGeneric(int count) { - places = (T[])new Object[count]; + maxCount = count; + places = new ArrayList<>(count); } public int insert(T locomotive) { @@ -16,38 +25,48 @@ public class SetGeneric { } public int insert(T locomotive, int position) { - int nullIndex = -1, i; - - if (position < 0 || position >= count()) + if (position < 0 || position > count() || count() >= maxCount) return -1; - for (i = position; i < count(); i++) { - if (places[i] == null) { - nullIndex = i; - break; - } - } - if (nullIndex < 0) - return -1; - - for (i = nullIndex; i > position; i--) { - places[i] = places[i - 1]; - } - - places[position] = locomotive; + places.add(position, locomotive); return position; } - public boolean remove(int position) { + public T remove(int position) { if (position < 0 || position >= count()) - return false; - places[position] = null; - return true; + return null; + return places.remove(position); } public T get(int position) { if (position < 0 || position >= count()) return null; - return places[position]; + return places.get(position); + } + + public Iterable GetLocomotives(final Integer maxLocomotives) { + return () -> new Iterator<>() { + private int currentIndex = 0; + private int count = 0; + + @Override + public boolean hasNext() { + return currentIndex < count() && (maxLocomotives == null || count < maxLocomotives); + } + + @Override + public T next() { + if (hasNext()) { + count++; + return places.get(currentIndex++); + } + throw new NoSuchElementException(); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + }; } } diff --git a/src/projectMonorail/PictureBox.java b/src/projectMonorail/PictureBox.java index fcbf352..7126228 100644 --- a/src/projectMonorail/PictureBox.java +++ b/src/projectMonorail/PictureBox.java @@ -53,7 +53,14 @@ public class PictureBox extends JPanel { private JPanel strategyPanel; - public PictureBox() { + public PictureBox(DrawingLocomotive locomotive) { + if (locomotive != null) { + Random random = new Random(); + drawingLocomotive = locomotive; + drawingLocomotive.setPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + repaint(); + } + abstractStrategy = null; selectedLocomotive = null; @@ -290,7 +297,7 @@ public class PictureBox extends JPanel { add(strategyPaddingPanel, BorderLayout.NORTH); - setPreferredSize(new Dimension(900, 500)); + setPreferredSize(new Dimension(900, 660)); } @Override diff --git a/src/projectMonorail/PictureBoxCollection.java b/src/projectMonorail/PictureBoxCollection.java index 9731107..8082e30 100644 --- a/src/projectMonorail/PictureBoxCollection.java +++ b/src/projectMonorail/PictureBoxCollection.java @@ -8,11 +8,13 @@ import javax.swing.*; import java.awt.*; public class PictureBoxCollection extends JPanel { - private final LocomotivesGenericCollection locomotives; + private final LocomotivesGenericStorage storage; - public PictureBoxCollection(LocomotivesGenericCollection locomotives, - int width, int height) { - this.locomotives = locomotives; + private JList listSets; + + public PictureBoxCollection(LocomotivesGenericStorage storage, JList listSets, int width, int height) { + this.storage = storage; + this.listSets = listSets; setMaximumSize(new Dimension(width, height)); } @@ -20,6 +22,11 @@ public class PictureBoxCollection extends JPanel { protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; - locomotives.showLocomotives(g2d); + if (listSets.getSelectedIndex() == -1) + return; + var obj = storage.get(listSets.getSelectedValue()); + if (obj == null) + return; + obj.showLocomotives(g2d); } }