371 lines
14 KiB
Java
371 lines
14 KiB
Java
package MonorailHard;
|
||
|
||
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||
import MonorailHard.DrawningObjects.ExtentionDrawningMonorail;
|
||
import MonorailHard.Generics.MonorailGenericCollection;
|
||
import MonorailHard.Generics.MonorailGenericStorage;
|
||
import MonorailHard.Generics.MonorailTrashCollection;
|
||
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
|
||
|
||
import javax.swing.*;
|
||
import javax.swing.filechooser.FileFilter;
|
||
import java.awt.*;
|
||
import java.awt.event.ActionEvent;
|
||
import java.awt.event.ActionListener;
|
||
import java.io.File;
|
||
import java.io.FileNotFoundException;
|
||
import java.io.FileWriter;
|
||
import java.io.IOException;
|
||
import java.text.Normalizer;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Scanner;
|
||
|
||
class TxtSaveFilter extends FileFilter {
|
||
@Override
|
||
public boolean accept(File f) {
|
||
if (f.isDirectory()) {
|
||
return false;
|
||
}
|
||
|
||
String s = f.getName().toLowerCase();
|
||
|
||
return s.endsWith(".txt");
|
||
}
|
||
|
||
@Override
|
||
public String getDescription() {
|
||
return "*.txt";
|
||
}
|
||
}
|
||
|
||
public class FormMonorailCollection {
|
||
private final MonorailGenericStorage _storage;
|
||
|
||
private JList<String> listBoxStorages;
|
||
private DefaultListModel<String> listBoxModel;
|
||
private int pictureBoxWidth = 605;
|
||
private int pictureBoxHeight = 426;
|
||
|
||
CollectionCanvas canv;
|
||
|
||
void Draw(){
|
||
if(canv == null)
|
||
return;
|
||
canv.repaint();
|
||
}
|
||
|
||
private void ReloadObjects(){
|
||
int index = listBoxStorages.getSelectedIndex();
|
||
listBoxModel.clear();
|
||
List<String> keys = _storage.Keys();
|
||
for(int i = 0; i < keys.size(); i++){
|
||
listBoxModel.addElement(keys.get(i));
|
||
}
|
||
if(listBoxModel.size() > 0 && (index == -1 || index >= listBoxModel.size()))
|
||
listBoxStorages.setSelectedIndex(0);
|
||
else if(listBoxModel.size() > 0)
|
||
listBoxStorages.setSelectedIndex(index);
|
||
}
|
||
|
||
public boolean LoadCollection(File f) throws FileNotFoundException {
|
||
if(!f.exists())
|
||
return false;
|
||
StringBuilder bufferTextFromFile =new StringBuilder();
|
||
Scanner s = new Scanner(f);
|
||
while(s.hasNext())
|
||
bufferTextFromFile.append(s.next() + "\n");
|
||
s.close();
|
||
var strs = bufferTextFromFile.toString().split("\n");
|
||
if(strs == null || strs.length == 0)
|
||
return false;
|
||
if (!strs[0].startsWith("MonorailCollection"))
|
||
return false;
|
||
String collectionName = strs[1];
|
||
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> collection = new MonorailGenericCollection<>(pictureBoxWidth, pictureBoxHeight);
|
||
String[] monorailsInfo = strs[2].split(Character.toString(MonorailGenericCollection._separatorRecords));
|
||
for(int i = monorailsInfo.length-1; i >= 0; i--){
|
||
String data = monorailsInfo[i];
|
||
DrawningMonorail monorail = ExtentionDrawningMonorail.CreateDrawningMonorail(data,
|
||
MonorailGenericCollection._separatorForObject, pictureBoxWidth, pictureBoxHeight);
|
||
if (monorail != null)
|
||
{
|
||
if (!(collection.Insert(monorail)))
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
_storage.AddSetFromFile(collectionName, collection);
|
||
ReloadObjects();
|
||
return true;
|
||
}
|
||
|
||
public FormMonorailCollection(){
|
||
JMenuBar menuFile = new JMenuBar();
|
||
JMenu file = new JMenu("Файл");
|
||
menuFile.add(file);
|
||
JMenuItem saveFile = new JMenuItem("Сохранить");
|
||
JMenuItem loadFile = new JMenuItem("Загрузить");
|
||
JMenuItem saveCollection = new JMenuItem("Сохранить коллекцию");
|
||
JMenuItem loadCollection = new JMenuItem("Загрузить коллекцию");
|
||
file.add(saveCollection);
|
||
file.add(loadCollection);
|
||
file.add(saveFile);
|
||
file.add(loadFile);
|
||
MonorailTrashCollection<DrawningMonorail> _trashCollection = new MonorailTrashCollection<>();
|
||
JButton callTrashButton = new JButton("мусор");
|
||
_storage = new MonorailGenericStorage(pictureBoxWidth, pictureBoxHeight);
|
||
JScrollPane scrollPane = new JScrollPane();
|
||
canv = new CollectionCanvas();
|
||
JPanel toolBox = new JPanel();
|
||
JTextField storageName = new JTextField();
|
||
JButton addStorageButton = new JButton("Добавить набор");
|
||
listBoxModel = new DefaultListModel<>();
|
||
listBoxStorages= new JList<>(listBoxModel);
|
||
scrollPane.setViewportView(listBoxStorages);
|
||
JButton delStorageButton = new JButton("Удалить набор");
|
||
toolBox.setBounds(623,12, 227, 80);
|
||
JFrame collectionFrame = new JFrame();
|
||
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||
collectionFrame.setSize(880,497);
|
||
toolBox.setBounds(623, 12, 227, 426);
|
||
canv.setBounds(12,12,pictureBoxWidth,pictureBoxHeight);
|
||
JButton addButton = new JButton("Добавить");
|
||
JButton removeButton = new JButton("Удалить");
|
||
JButton refreshButton = new JButton("Обновить");
|
||
JTextField monorailNumb = new JTextField();
|
||
GridLayout lay = new GridLayout(9,1);
|
||
toolBox.add(storageName);
|
||
toolBox.add(addStorageButton);
|
||
toolBox.add(scrollPane);
|
||
toolBox.add(delStorageButton);
|
||
toolBox.setLayout(lay);
|
||
toolBox.add(addButton);
|
||
toolBox.add(monorailNumb);
|
||
toolBox.add(removeButton);
|
||
toolBox.add(refreshButton);
|
||
toolBox.add(callTrashButton);
|
||
collectionFrame.add(toolBox);
|
||
collectionFrame.setJMenuBar(menuFile);
|
||
collectionFrame.add(canv);
|
||
collectionFrame.setVisible(true);
|
||
canv._storage = _storage;
|
||
canv.listBoxStorages = listBoxStorages;
|
||
|
||
saveFile.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
JFileChooser fc = new JFileChooser("C:\\Users\\frenk\\OneDrive\\Рабочий стол\\lab6saves");
|
||
fc.addChoosableFileFilter(new TxtSaveFilter());
|
||
int retrieval = fc.showSaveDialog(null);
|
||
|
||
if (retrieval == JFileChooser.APPROVE_OPTION) {
|
||
File file = new File(fc.getSelectedFile() + "." + "txt");
|
||
|
||
try {
|
||
_storage.SaveData(file);
|
||
} catch (IOException ex) {
|
||
throw new RuntimeException(ex);
|
||
}
|
||
}
|
||
}
|
||
});
|
||
|
||
saveCollection.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
JFileChooser fc = new JFileChooser("C:\\Users\\frenk\\OneDrive\\Рабочий стол\\lab6saves");
|
||
fc.addChoosableFileFilter(new TxtSaveFilter());
|
||
int retrieval = fc.showSaveDialog(null);
|
||
|
||
if (retrieval == JFileChooser.APPROVE_OPTION) {
|
||
File file = new File(fc.getSelectedFile() + "." + "txt");
|
||
|
||
try {
|
||
if(listBoxStorages.getSelectedIndex() == -1) {
|
||
return;
|
||
}
|
||
_storage._monorailStorages.get(listBoxStorages.getSelectedValue()).SaveData(file, listBoxStorages.getSelectedValue());
|
||
ReloadObjects();
|
||
} catch (IOException ex) {
|
||
throw new RuntimeException(ex);
|
||
}
|
||
}
|
||
}
|
||
});
|
||
|
||
loadFile.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
JFileChooser fc = new JFileChooser("C:\\Users\\frenk\\OneDrive\\Рабочий стол\\lab6saves");
|
||
int ret = fc.showDialog(null, "Открыть файл");
|
||
if(ret == JFileChooser.APPROVE_OPTION){
|
||
File file = fc.getSelectedFile();
|
||
try {
|
||
_storage.LoadData(file);
|
||
canv._storage =_storage;
|
||
ReloadObjects();
|
||
canv.repaint();
|
||
} catch (IOException ex) {
|
||
throw new RuntimeException(ex);
|
||
}
|
||
}
|
||
|
||
}
|
||
});
|
||
|
||
loadCollection.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
JFileChooser fc = new JFileChooser("C:\\Users\\frenk\\OneDrive\\Рабочий стол\\lab6saves");
|
||
int ret = fc.showDialog(null, "Открыть файл");
|
||
if(ret == JFileChooser.APPROVE_OPTION){
|
||
File file = fc.getSelectedFile();
|
||
try {
|
||
LoadCollection(file);
|
||
canv._storage =_storage;
|
||
ReloadObjects();
|
||
canv.repaint();
|
||
} catch (IOException ex) {
|
||
throw new RuntimeException(ex);
|
||
}
|
||
}
|
||
|
||
}
|
||
});
|
||
|
||
addStorageButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(storageName.getText() == null)
|
||
return;
|
||
_storage.AddSet(storageName.getText());
|
||
ReloadObjects();
|
||
}
|
||
});
|
||
|
||
delStorageButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(listBoxStorages.getSelectedIndex() == -1) {
|
||
return;
|
||
}
|
||
_storage.DelSet(listBoxStorages.getSelectedValue(), _trashCollection);
|
||
ReloadObjects();
|
||
}
|
||
});
|
||
|
||
callTrashButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(_trashCollection.GetSize() == 0)
|
||
return;
|
||
FormMonorail form = new FormMonorail();
|
||
form.ChangeMonorail(_trashCollection.GetTop());
|
||
_trashCollection.Pop();
|
||
}
|
||
});
|
||
|
||
addButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(listBoxStorages.getSelectedIndex() == -1) {
|
||
return;
|
||
}
|
||
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails = _storage.Get(listBoxStorages.getSelectedValue());
|
||
FormMonorailConfig form = new FormMonorailConfig();
|
||
form.addButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if (_monorails.Insert(form._monorail))
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
form._monorail._pictureWidth = pictureBoxWidth;
|
||
form._monorail._pictureHeight = pictureBoxHeight;
|
||
Draw();
|
||
}
|
||
else
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
}
|
||
form.frameConfig.dispose();
|
||
Draw();
|
||
}
|
||
});
|
||
form.cancelButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
form.frameConfig.dispose();
|
||
Draw();
|
||
}
|
||
});
|
||
}
|
||
});
|
||
removeButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(listBoxStorages.getSelectedIndex() == -1) {
|
||
return;
|
||
}
|
||
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails = _storage.Get(listBoxStorages.getSelectedValue());
|
||
if(_monorails == null) {
|
||
return;
|
||
}
|
||
String tmp = monorailNumb.getText();
|
||
int numb;
|
||
|
||
try{
|
||
numb = Integer.parseInt(tmp);
|
||
}
|
||
catch(Exception ex){
|
||
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
return;
|
||
}
|
||
DrawningMonorail curMonorail = _monorails.Get(numb);
|
||
_trashCollection.Push(curMonorail);
|
||
_monorails.Remove(numb);
|
||
_monorails.ShowMonorails();
|
||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
Draw();
|
||
}
|
||
});
|
||
|
||
refreshButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(listBoxStorages.getSelectedIndex() == -1) {
|
||
return;
|
||
}
|
||
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails = _storage.Get(listBoxStorages.getSelectedValue());
|
||
if(_monorails == null) {
|
||
return;
|
||
}
|
||
_monorails.ShowMonorails();
|
||
Draw();
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
class CollectionCanvas extends JComponent {
|
||
public MonorailGenericStorage _storage;
|
||
public JList<String> listBoxStorages;
|
||
|
||
|
||
public CollectionCanvas(){
|
||
}
|
||
@Override
|
||
public void paintComponent (Graphics g){
|
||
if (listBoxStorages == null || listBoxStorages.getSelectedIndex() == -1){
|
||
return;
|
||
}
|
||
super.paintComponents (g) ;
|
||
Graphics2D g2d = (Graphics2D)g;
|
||
g2d.drawImage(_storage.Get(listBoxStorages.getSelectedValue()).ShowMonorails(), 0, 0, this);
|
||
super.repaint();
|
||
}
|
||
|
||
}
|