Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.3 kB
2
Indexable
Never
import ru.vsu.cs.util.SwingUtils;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.io.File;


public class FrameMain extends JFrame {
    private JPanel panelMain;

    private JButton buttonLoadInputFromFile;
    private JButton buttonSaveInputInfoFile;
    private JButton buttonFromArabicToRoman;
    private JButton buttonSaveOutputIntoFile;

    private JScrollPane scrollPaneTableInput;
    private JScrollPane scrollPaneTableOutput;
    private JButton buttonFromRomanToArabic;
    private JTextArea textArea1;
    private JTextPane textPane1;

    private final JFileChooser fileChooserOpen;
    private final JFileChooser fileChooserSave;


    public FrameMain() {
        this.setTitle("FrameMain");
        this.setContentPane(panelMain);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();

        scrollPaneTableInput.setPreferredSize(new Dimension(-1, 90));
        scrollPaneTableOutput.setPreferredSize(new Dimension(-1, 90));

        fileChooserOpen = new JFileChooser();
        fileChooserSave = new JFileChooser();
        fileChooserOpen.setCurrentDirectory(new File("."));
        fileChooserSave.setCurrentDirectory(new File("."));
        FileFilter filter = new FileNameExtensionFilter("Text files", "txt");
        fileChooserOpen.addChoosableFileFilter(filter);
        fileChooserSave.addChoosableFileFilter(filter);

        fileChooserSave.setAcceptAllFileFilterUsed(false);
        fileChooserSave.setDialogType(JFileChooser.SAVE_DIALOG);
        fileChooserSave.setApproveButtonText("Save");

        JMenuBar menuBarMain = new JMenuBar();
        setJMenuBar(menuBarMain);

        JMenu menuLookAndFeel = new JMenu();
        menuLookAndFeel.setText("Вид");
        menuBarMain.add(menuLookAndFeel);
        SwingUtils.initLookAndFeelMenu(menuLookAndFeel);

        this.pack();


        buttonLoadInputFromFile.addActionListener(actionEvent -> {
            try {
                if (fileChooserOpen.showOpenDialog(panelMain) == JFileChooser.APPROVE_OPTION) {
                    textArea1.setText(Task.readFromFile(fileChooserOpen.getSelectedFile().getPath()));
                }
            } catch (Exception e) {
                SwingUtils.showErrorMessageBox(e);
            }
        });
        buttonSaveInputInfoFile.addActionListener(actionEvent -> {
            try {
                if (fileChooserSave.showSaveDialog(panelMain) == JFileChooser.APPROVE_OPTION) {
                    String file = fileChooserSave.getSelectedFile().getPath();
                    if (!file.toLowerCase().endsWith(".txt")) {
                        file += ".txt";
                    }
                    Task.writeToFile(file, textPane1.getText());
                }
            } catch (Exception e) {
                SwingUtils.showErrorMessageBox(e);
            }
        });
        buttonFromArabicToRoman.addActionListener(actionEvent -> {
            try {
                String str = Task.arabicToRoman(Integer.parseInt(textArea1.getText()));
                textPane1.setText(str);
            } catch (Exception e) {
                SwingUtils.showErrorMessageBox(e);
            }
        });
        buttonFromRomanToArabic.addActionListener(actionEvent -> {
            try {
                String str = String.valueOf(Task.romanToArabic(textArea1.getText()));
                textPane1.setText(str);
            } catch (Exception e) {
                SwingUtils.showErrorMessageBox(e);
            }
        });
        buttonSaveOutputIntoFile.addActionListener(actionEvent -> {
            try {
                if (fileChooserSave.showSaveDialog(panelMain) == JFileChooser.APPROVE_OPTION) {
                    String file = fileChooserSave.getSelectedFile().getPath();
                    if (!file.toLowerCase().endsWith(".txt")) {
                        file += ".txt";
                    }
                    Task.writeToFile(file, textPane1.getText());
                }
            } catch (Exception e) {
                SwingUtils.showErrorMessageBox(e);
            }
        });
    }
}