diff --git a/FormMapWithSetPlanes.java b/FormMapWithSetPlanes.java index aa37fe1..d9d4a6b 100644 --- a/FormMapWithSetPlanes.java +++ b/FormMapWithSetPlanes.java @@ -1,39 +1,71 @@ import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; - -public class FormMapWithSetPlanes extends JFrame { - - private MapWithSetPlanesGeneric _mapPlanesCollectionGeneric; - +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.image.BufferedImage; +import java.util.HashMap; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +public class FormMapWithSetPlanes extends JFrame{ public JPanel Mainpanel; - private JButton buttonAdd; - private JComboBox comboBoxSelectorMap; - private JTextField textBoxPosition; - private JButton buttonRemove; - private JButton buttonShowStorage; - private JButton buttonShowOnMap; - private JButton buttonUp; - private JButton buttonLeft; - private JButton buttonRight; - private JButton buttonDown; - private JPanel PictureBox; - private JPanel groupBox; - AbstractMap _abstractMap; - - private JFrame getFrame() { - JFrame frame = new JFrame(); - frame.setVisible(false); - frame.setBounds(300, 100, 800, 600); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - return frame; + private JPanel pictureBoxPlane; + private JPanel GroupBoxTools; + private JComboBox ComboBoxSelectorMap; + private JButton ButtonAddPlane; + private JButton ButtonDeletePlane; + private JButton ButtonShowStorage; + private JButton ButtonShowMap; + private JButton ButtonLeft; + private JButton ButtonDown; + private JButton ButtonUp; + private JButton ButtonRight; + private JTextField maskedTextBoxPosition; + private JTextField textBoxNewMapName; + private int picWidth=600; + private int picHeight=500; + private JButton ButtonAddMap; + private JList ListBoxMaps; + private JButton ButtonDeleteMap; + private JPanel GroupBoxMaps; + private JButton ButtonCheckDel; + private MapWithSetPlanesGeneric _mapPlanesCollectionGeneric; + private final HashMap _mapsDict = new HashMap<>(){{ + put("Простая карта",new SimpleMap()); + put("Вторая карта",new MyMap()); + }}; + private final MapsCollection _mapsCollection; + public void UpdateWindow(BufferedImage bmp) + { + pictureBoxPlane.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(pictureBoxPlane.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(bmp)); + pictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); + pictureBoxPlane.revalidate(); + } + private void ReloadMaps(){ + int index = ListBoxMaps.getSelectedIndex(); + DefaultListModel model1 = (DefaultListModel) ListBoxMaps.getModel(); + model1.removeAllElements(); + for(int i=0;i<_mapsCollection.Keys().size();i++) + { + model1.addElement(_mapsCollection.Keys().get(i)); + } + if (ListBoxMaps.getModel().getSize() > 0 && (index == -1 || index >= ListBoxMaps.getModel().getSize())) + { + ListBoxMaps.setSelectedIndex(0); + } + else if (ListBoxMaps.getModel().getSize() > 0 && index > -1 && index < ListBoxMaps.getModel().getSize()) + { + ListBoxMaps.setSelectedIndex(index); + } } - - JFrame jFrame = getFrame(); - private void ButtonMove_Click(String name) { - if (_mapPlanesCollectionGeneric == null) return; Direction direction = Direction.None; switch (name) { @@ -50,54 +82,30 @@ public class FormMapWithSetPlanes extends JFrame { direction = Direction.Down; break; } - PictureBox.removeAll(); - JLabel imageWithMapAndObject = new JLabel(); - imageWithMapAndObject.setPreferredSize(PictureBox.getSize()); - imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject(direction))); - PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER); - PictureBox.revalidate(); - PictureBox.repaint(); + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(direction)); } - - public FormMapWithSetPlanes() { - comboBoxSelectorMap.addItem("Простая карта"); - comboBoxSelectorMap.addItem("Вторая карта"); + public FormMapWithSetPlanes() + { + _mapsCollection = new MapsCollection(picWidth, picHeight); + ComboBoxSelectorMap.removeAllItems(); + for (String elem : _mapsDict.keySet()) { + ComboBoxSelectorMap.addItem(elem); + } try { - Image img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowUp.jpg")); - buttonUp.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowDown.jpg")); - buttonDown.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowLeft.jpg")); - buttonLeft.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowRight.jpg")); - buttonRight.setIcon(new ImageIcon(img)); + Image img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowUp.jpg")); + ButtonUp.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowDown.jpg")); + ButtonDown.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowLeft.jpg")); + ButtonLeft.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowRight.jpg")); + ButtonRight.setIcon(new ImageIcon(img)); } catch (Exception ex) { System.out.println(ex); } - comboBoxSelectorMap.addActionListener(e -> { - AbstractMap map = null; - switch (comboBoxSelectorMap.getSelectedItem().toString()) - { - case "Простая карта": - map = new SimpleMap(); - break; - case "Вторая карта": - map = new MyMap(); - break; - } - if (map != null) - { - _mapPlanesCollectionGeneric = new MapWithSetPlanesGeneric(PictureBox.getWidth(), PictureBox.getHeight(), map); - } - else - { - _mapPlanesCollectionGeneric = null; - } - }); - buttonAdd.addActionListener(e -> { - if (_mapPlanesCollectionGeneric == null) + ButtonAddPlane.addActionListener(e -> { + if (ListBoxMaps.getSelectedIndex() == -1) { return; } @@ -109,84 +117,113 @@ public class FormMapWithSetPlanes extends JFrame { dialog.setVisible(true); if (dialog.GetSelectedPlane() == null) return; - - DrawningObjectPlane plane = new DrawningObjectPlane(dialog.GetSelectedPlane()); - - if (_mapPlanesCollectionGeneric.addPlane(plane) != -1) + DrawningObjectPlane Plane = new DrawningObjectPlane(dialog.GetSelectedPlane()); + if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).addPlane(Plane)!=-1) { - JOptionPane.showMessageDialog(jFrame, "Объект добавлен"); - PictureBox.removeAll(); - JLabel imageOfShip = new JLabel(); - imageOfShip.setPreferredSize(PictureBox.getSize()); - imageOfShip.setMinimumSize(new Dimension(1, 1)); - imageOfShip.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.ShowSet())); - PictureBox.add(imageOfShip,BorderLayout.CENTER); - PictureBox.revalidate(); - PictureBox.repaint(); + JOptionPane.showMessageDialog(null,"Объект добавлен"); + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); } else { - JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект"); + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); } }); - buttonRemove.addActionListener(e -> { - if(_mapPlanesCollectionGeneric == null) return; - - String text = textBoxPosition.getText(); - if(text.isEmpty()) return; - - if(JOptionPane.showConfirmDialog(jFrame, "Вы действительно хотите удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return; - - try { - Integer.parseInt(text); - } - catch (Exception ex){ + ListBoxMaps.addListSelectionListener(e -> { + if (ListBoxMaps.getSelectedIndex() == -1) + return; + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); + }); + ButtonDeletePlane.addActionListener(e -> { + if (maskedTextBoxPosition.getText().isEmpty()) + { return; } - - int pos = Integer.parseInt(text); - if (_mapPlanesCollectionGeneric.removePlane(pos) != null) + int result = JOptionPane.showConfirmDialog(null,"Удалить объект?","Удаление",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE); + if(result==JOptionPane.NO_OPTION) { - JOptionPane.showMessageDialog(jFrame, "Объект удален"); - PictureBox.removeAll(); - JLabel imageOfShip = new JLabel(); - imageOfShip.setPreferredSize(PictureBox.getSize()); - imageOfShip.setMinimumSize(new Dimension(1, 1)); - imageOfShip.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.ShowSet())); - PictureBox.add(imageOfShip,BorderLayout.CENTER); - PictureBox.revalidate(); - PictureBox.repaint(); + return; + } + int pos=Integer.parseInt(maskedTextBoxPosition.getText()); + if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).removePlane(pos)!=null) + { + JOptionPane.showMessageDialog(null, "Объект удален"); + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); } else { - JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект"); + JOptionPane.showMessageDialog(null, "Не удалось удалить объект"); } }); - buttonShowStorage.addActionListener(e -> { - if (_mapPlanesCollectionGeneric == null) return; - PictureBox.removeAll(); - JLabel imageOfShip = new JLabel(); - imageOfShip.setPreferredSize(PictureBox.getSize()); - imageOfShip.setMinimumSize(new Dimension(1, 1)); - imageOfShip.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.ShowSet())); - PictureBox.add(imageOfShip,BorderLayout.CENTER); - PictureBox.revalidate(); - PictureBox.repaint(); + ButtonShowStorage.addActionListener(e -> { + if (ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); }); - buttonShowOnMap.addActionListener(e -> { - if (_mapPlanesCollectionGeneric == null) return; - PictureBox.removeAll(); - JLabel imageOfShip = new JLabel(); - imageOfShip.setPreferredSize(PictureBox.getSize()); - imageOfShip.setMinimumSize(new Dimension(1, 1)); - imageOfShip.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.ShowOnMap())); - PictureBox.add(imageOfShip,BorderLayout.CENTER); - PictureBox.revalidate(); - PictureBox.repaint(); + ButtonShowMap.addActionListener(e -> { + if (ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap()); }); - buttonUp.addActionListener(e -> ButtonMove_Click("buttonUp")); - buttonLeft.addActionListener(e -> ButtonMove_Click("buttonLeft")); - buttonDown.addActionListener(e -> ButtonMove_Click("buttonDown")); - buttonRight.addActionListener(e -> ButtonMove_Click("buttonRight")); + ButtonUp.addActionListener(e -> ButtonMove_Click("buttonUp")); + ButtonLeft.addActionListener(e -> ButtonMove_Click("buttonLeft")); + ButtonDown.addActionListener(e -> ButtonMove_Click("buttonDown")); + ButtonRight.addActionListener(e -> ButtonMove_Click("buttonRight")); + maskedTextBoxPosition.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + char c = e.getKeyChar(); + if ( ((c < '0') || (c > '9')) || maskedTextBoxPosition.getText().length() >= 2) { + e.consume(); + } + } + }); + ButtonAddMap.addActionListener(e -> { + if (ComboBoxSelectorMap.getSelectedIndex() == -1 || textBoxNewMapName.getText().isEmpty()) + { + JOptionPane.showMessageDialog(null,"Не все данные заполнены","Ошибка",JOptionPane.ERROR_MESSAGE); + return; + } + if(!_mapsDict.containsKey(ComboBoxSelectorMap.getSelectedItem())) + { + JOptionPane.showMessageDialog(null,"Нет такой карты","Ошибка",JOptionPane.ERROR_MESSAGE); + } + _mapsCollection.AddMap(textBoxNewMapName.getText(),_mapsDict.get(ComboBoxSelectorMap.getSelectedItem().toString())); + ReloadMaps(); + }); + ButtonDeleteMap.addActionListener(e -> { + if (ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + if(JOptionPane.showConfirmDialog(null,"Удалить карту"+ListBoxMaps.getSelectedValue().toString()+"?","Удаление",JOptionPane.YES_NO_OPTION)==0) + { + _mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString()); + ReloadMaps(); + } + }); + ButtonCheckDel.addActionListener(e -> { + if (ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + DrawningObjectPlane Plane=_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).GetPlanesDeleted(); + if(Plane==null) + { + JOptionPane.showMessageDialog(null,"Коллекция пуста","Ошибка",JOptionPane.ERROR_MESSAGE); + return; + } + FormPlane formPlane=new FormPlane(Plane); + formPlane.setSize(1000,800); + formPlane.setVisible(true); + }); + } + + private void createUIComponents() { + DefaultListModel dlm = new DefaultListModel(); + ListBoxMaps = new JList(dlm); } }