[0b6a4fe] | 1 | import java.awt.*;
|
---|
| 2 | import java.awt.event.*;
|
---|
| 3 | import java.util.*;
|
---|
| 4 | import javax.swing.*;
|
---|
| 5 |
|
---|
[ec3114d] | 6 | /*
|
---|
| 7 | * This is the main class in the portal project and it creates the gui and starts the thread that handles server communication.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
[0b6a4fe] | 10 | public class LostHavenPortal extends JFrame {
|
---|
| 11 | private static final long serialVersionUID = 1L;
|
---|
| 12 |
|
---|
| 13 | public HashMap<String, Player> registered;
|
---|
| 14 | public PriorityQueue<String> orderedReg;
|
---|
| 15 | public PriorityQueue<String> orderedOnline;
|
---|
| 16 |
|
---|
| 17 | public JLabel lblConnectionStatus;
|
---|
| 18 | public JLabel lblServerStatus;
|
---|
| 19 | private JButton btnConnect;
|
---|
| 20 | private JButton btnDisconnect;
|
---|
| 21 | private JButton btnStart;
|
---|
| 22 | private JButton btnStop;
|
---|
| 23 | private JList lstRegistered;
|
---|
| 24 | private JList lstOnline;
|
---|
| 25 | private PortalThread thread;
|
---|
| 26 |
|
---|
| 27 | public LostHavenPortal() {
|
---|
| 28 | registered = new HashMap<String, Player>();
|
---|
| 29 | orderedReg = new PriorityQueue<String>(11, new PlayerComparator());
|
---|
| 30 | orderedOnline = new PriorityQueue<String>(11, new PlayerComparator());
|
---|
| 31 |
|
---|
| 32 | MyWindowListener wndListener = new MyWindowListener();
|
---|
| 33 | MyButtonListener btnListener = new MyButtonListener();
|
---|
| 34 |
|
---|
| 35 | addWindowListener(wndListener);
|
---|
| 36 | setLayout(new FlowLayout());
|
---|
| 37 |
|
---|
| 38 | lblConnectionStatus = new JLabel("disconnected");
|
---|
| 39 | lblServerStatus = new JLabel("not available");
|
---|
| 40 |
|
---|
| 41 | btnConnect = new JButton("connect");
|
---|
| 42 | btnConnect.addActionListener(btnListener);
|
---|
| 43 | btnDisconnect = new JButton("disconnect");
|
---|
| 44 | btnDisconnect.addActionListener(btnListener);
|
---|
| 45 | btnStart = new JButton("start");
|
---|
| 46 | btnStart.addActionListener(btnListener);
|
---|
| 47 | btnStop = new JButton("stop");
|
---|
| 48 | btnStop.addActionListener(btnListener);
|
---|
| 49 |
|
---|
| 50 | lstRegistered = new JList();
|
---|
| 51 | lstOnline = new JList();
|
---|
| 52 |
|
---|
| 53 | add(lblConnectionStatus);
|
---|
| 54 | add(lblServerStatus);
|
---|
| 55 | add(btnConnect);
|
---|
| 56 | add(btnDisconnect);
|
---|
| 57 | add(btnStart);
|
---|
| 58 | add(btnStop);
|
---|
| 59 | add(lstRegistered);
|
---|
| 60 | add(lstOnline);
|
---|
| 61 |
|
---|
| 62 | setSize(600, 400);
|
---|
| 63 | setVisible(true);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public void addRegList(String str, Player player) {
|
---|
| 67 | registered.put(str, player);
|
---|
| 68 | orderedReg.add(str);
|
---|
| 69 | updateGui();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | public void addOnlineList(String str) {
|
---|
| 73 | orderedOnline.add(str);
|
---|
| 74 | updateGui();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public void updateGui() {
|
---|
| 78 | SwingUtilities.invokeLater(new Runnable() {
|
---|
| 79 | public void run() {
|
---|
| 80 | lstRegistered.setListData(orderedReg.toArray());
|
---|
| 81 | lstOnline.setListData(orderedOnline.toArray());
|
---|
| 82 | }
|
---|
| 83 | });
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | private void start() {
|
---|
| 87 | if(!Connection.isConnected(thread)) {
|
---|
| 88 | //thread = new PortalThread("64.9.205.76", 5829, this);
|
---|
| 89 | //thread = new PortalThread("69.138.160.41", 5829, this);
|
---|
| 90 | thread = new PortalThread("127.0.0.1", 5829, this);
|
---|
| 91 | thread.start();
|
---|
| 92 | }else {
|
---|
| 93 | JOptionPane.showMessageDialog(this, "Connection has already been established.");
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | private void stop() {
|
---|
| 98 | if(Connection.isConnected(thread)) {
|
---|
| 99 | thread.closeConnection();
|
---|
| 100 | }else {
|
---|
| 101 | JOptionPane.showMessageDialog(this, "Connection has not yet been established.");
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | private class MyWindowListener extends WindowAdapter {
|
---|
| 106 | public void windowClosing(WindowEvent e) {
|
---|
| 107 | if(thread != null)
|
---|
| 108 | thread.closeConnection();
|
---|
| 109 | System.exit(0);
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | private class MyButtonListener implements ActionListener {
|
---|
| 114 | public void actionPerformed(ActionEvent e) {
|
---|
| 115 | String str = ((JButton)e.getSource()).getText();
|
---|
| 116 |
|
---|
| 117 | if(str.equals("connect")){
|
---|
| 118 | start();
|
---|
| 119 | }else if(str.equals("disconnect")){
|
---|
| 120 | stop();
|
---|
| 121 | }else if(str.equals("start")){
|
---|
| 122 | if(Connection.isConnected(thread))
|
---|
| 123 | thread.sendMessage(MessageType.StartServer, "");
|
---|
| 124 | }else if(str.equals("stop")){
|
---|
| 125 | if(Connection.isConnected(thread))
|
---|
| 126 | thread.sendMessage(MessageType.StopServer, "");
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | public static void main(String[] args) {
|
---|
| 132 | new LostHavenPortal();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | private class PlayerComparator implements Comparator<String> {
|
---|
| 136 | public int compare(String str1, String str2) {
|
---|
| 137 | return registered.get(str1).getId() - registered.get(str2).getId();
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|