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