aboutsummaryrefslogtreecommitdiff
path: root/src/UI/MainWindow.java
blob: 6c1ef83013342294e5fcb1dfb6473ecec7f99b7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package UI;

import Server.SocketServer;
import Store.Book;
import UI.Components.BooksList;
import UI.Components.MenuBar;
import com.corundumstudio.socketio.Configuration;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class MainWindow extends JFrame {
    private static String hostname = "localhost";
    private static int port = 8080;
    private static SocketServer server;
    public static boolean serverStarted = false;
    private MenuBar menuBar;

    private static NewBookPopupWindow newBookPopup;
    private static SettingsPopupWindow settingsPopup;

    public MainWindow() {
        this.setTitle("Admin Panel - Library Management System");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(650, 500);
        this.setResizable(false);
        this.setLocationRelativeTo(null);

        menuBar = new MenuBar();
        menuBar.settingsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (settingsPopup == null) {
                    settingsPopup = new SettingsPopupWindow();
                    settingsPopup.saveButton.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            hostname = settingsPopup.hostnameInput.getValue();
                            port = Integer.parseInt(settingsPopup.portInput.getValue());

                            settingsPopup.setVisible(false);
                        }
                    });
                }

                settingsPopup.hostnameInput.setValue(hostname);
                settingsPopup.portInput.setValue(String.valueOf(port));

                settingsPopup.setVisible(true);
            }
        });
        menuBar.startStopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (serverStarted) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            stopServer();
                        }
                    }).start();
                } else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            startServer();
                        }
                    }).start();
                }
            }
        });
        menuBar.addBookButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (newBookPopup == null) {
                    newBookPopup = new NewBookPopupWindow();
                }

                newBookPopup.showWindow();
            }
        });
        this.add(menuBar);

        this.add(Box.createRigidArea(new Dimension(0, 5)));

        BooksList booksList = null;
        try {
            booksList = new BooksList();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        this.add(booksList);

        this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
    }

    private void setupServer() {
        Configuration config = new Configuration();
        config.setHostname(hostname);
        config.setPort(port);
        server = new SocketServer(config);
    }

    private void startServer() {
        setupServer();
        server.start();
        serverStarted = true;
        menuBar.settingsButton.setEnabled(false);
        menuBar.startStopButton.setText("Stop Server");
    }

    private void stopServer() {
        server.stop();
        serverStarted = false;
        menuBar.settingsButton.setEnabled(true);
        menuBar.startStopButton.setText("Start Server");
    }
}