Skip to content
Snippets Groups Projects
SuperVisorGuiControlImpl.java 2.13 KiB
Newer Older
package fucoin.gui;

import fucoin.supervisor.SuperVisorImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class SuperVisorGuiControlImpl implements SuperVisorGuiControl {
    private SuperVisorImpl superVisor;
    private JFrame frame;

David Bohn's avatar
David Bohn committed
    private DefaultListModel<String> log = new DefaultListModel<>();
    private JList<String> txtLog = new JList<>(log);
    private JScrollPane logPane = new JScrollPane(txtLog);

    public SuperVisorGuiControlImpl(SuperVisorImpl sv) {
        superVisor = sv;
        init();
    }

    private void init() {
        //Show AWT window for runtime information
        frame = new JFrame("Server");
David Bohn's avatar
David Bohn committed
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new GridLayout(2, 1));

        //Init Amount Table and SuperVisorImpl

        JTable amountListView = new JTable(superVisor.getAmountTableModel());
David Bohn's avatar
David Bohn committed
        contentPanel.add(new JScrollPane(amountListView));

        contentPanel.add(logPane);
David Bohn's avatar
David Bohn committed
        frame.add(contentPanel, BorderLayout.CENTER);

        //Exit Button and shutdown supervisor
David Bohn's avatar
David Bohn committed
        JButton exitBtn = new JButton("Stop Supervisor");
        exitBtn.addActionListener(e -> {
            superVisor.exit();
            frame.setVisible(false);
            frame.dispose();
        });
David Bohn's avatar
David Bohn committed
        frame.add(exitBtn, BorderLayout.PAGE_END);
        frame.setSize(800, 600);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                superVisor.exit();
            }
        });
    }

    @Override
    public void onLeave() {
        frame.dispose();
    }

    @Override
    public void log(String message) {
        // One day, we may have a server log GUI as well..
        // Until then, we just print it to the console
David Bohn's avatar
David Bohn committed
        //System.out.println(message);
        SwingUtilities.invokeLater(() -> {
            log.addElement(message);

            // auto scroll to the bottom
            txtLog.ensureIndexIsVisible(log.size() - 1);
        });