Commit 41c1b120 authored by fpeterfalvi's avatar fpeterfalvi
Browse files

Writing observer name and connecting to server in Swing.

parent b5eb0fe4
......@@ -80,8 +80,8 @@ The project was originally started in December 2002 by Arthur van Hoff at Strang
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${jvm.version}</source>
<target>${jvm.version}</target>
<source>1.9</source>
<target>1.9</target>
</configuration>
</plugin>
<plugin>
......
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
......@@ -15,7 +18,8 @@ import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceInfo;
import javax.jmdns.ServiceListener;
import javax.swing.SwingWorker;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import model.ServerModel;
......@@ -42,6 +46,9 @@ public class Main {
private static PrintWriter out = null;
private static JFrame frame;
private static JPanel mainPanel;
private static JPanel serverListPanel;
private static class SampleListener implements ServiceListener {
@Override
......@@ -101,31 +108,58 @@ public class Main {
}
public static void main(String[] args) throws InterruptedException {
try {
servers = new ArrayList<ServerModel>();
threads = new ArrayList<Thread>();
System.out.println("Set The ObserverName:");
Scanner s = new Scanner(System.in);
CLIENT_NAME = s.nextLine();
servers = new ArrayList<ServerModel>();
threads = new ArrayList<Thread>();
frame = new JFrame("KvizObserver");
mainPanel = new JPanel(new CardLayout());
JPanel observerNamePanel = new JPanel();
observerNamePanel.setLayout(new BoxLayout(observerNamePanel, BoxLayout.Y_AXIS));
JLabel observerNameLabel = new JLabel("Observer name:");
observerNamePanel.add(observerNameLabel);
JTextField observerNameTextField = new JTextField();
observerNamePanel.add(observerNameTextField);
JButton startSearchingButton = new JButton("Start searching servers");
startSearchingButton.addActionListener(new StartSearchingButtonListener(observerNameTextField));
observerNamePanel.add(startSearchingButton);
mainPanel.add(observerNamePanel, "OBSERVER_NAME_PANEL");
serverListPanel = new JPanel();
serverListPanel.setLayout(new BoxLayout(serverListPanel, BoxLayout.Y_AXIS));
mainPanel.add(serverListPanel, "SERVER_LIST_PANEL");
frame.add(mainPanel, BorderLayout.NORTH);
frame.setPreferredSize(new Dimension(300, 200));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//try {
//System.out.println("Set The ObserverName:");
//Scanner s = new Scanner(System.in);
//CLIENT_NAME = s.nextLine();
// Create a JmDNS instance
JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());
//JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());
// Add a service listener
jmdns.addServiceListener("_http._tcp.local.", new SampleListener());
//jmdns.addServiceListener("_http._tcp.local.", new SampleListener());
// Wait a bit
Thread.sleep(3000);
//Thread.sleep(3000);
String msg = "";
//String msg = "";
s = new Scanner(System.in);
/*Scanner s = new Scanner(System.in);
System.out.println("Type 'Connect' to Connect:");
msg = s.nextLine();
if(msg.equals("Connect"))
......@@ -137,18 +171,95 @@ public class Main {
if(msg.equals("Disconnect"))
{
onDisconnect();
}
} catch (UnknownHostException e) {
}*/
/*} catch (UnknownHostException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
System.out.println(e.getMessage());*/
//}
}
final static class StartSearchingButtonListener implements ActionListener {
private JTextField tf;
public StartSearchingButtonListener(JTextField tf) {
this.tf = tf;
}
public void actionPerformed(ActionEvent ae) {
CLIENT_NAME = tf.getText();
CardLayout cl = (CardLayout)(mainPanel.getLayout());
cl.show(mainPanel, "SERVER_LIST_PANEL");
try {
// Create a JmDNS instance
JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());
// Add a service listener
jmdns.addServiceListener("_http._tcp.local.", new SampleListener());
} catch (UnknownHostException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
private static void addServerToServerListPanel(ServerModel serverModel) {
JPanel serverPanel = new JPanel();
serverPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel serverName = new JLabel(serverModel.getHostName());
serverPanel.add(serverName);
serverPanel.add(Box.createRigidArea(new Dimension(5,0)));
JButton connectButton = new JButton("Connect");
serverPanel.add(connectButton);
serverPanel.add(Box.createRigidArea(new Dimension(5,0)));
JButton disconnectButton = new JButton("Disconnect");
disconnectButton.setEnabled(false);
serverPanel.add(disconnectButton);
connectButton.addActionListener(new ConnectButtonListener(serverModel, connectButton, disconnectButton));
disconnectButton.addActionListener(new DisconnectButtonListener(serverModel, connectButton, disconnectButton));
serverListPanel.add(serverPanel);
serverListPanel.revalidate();
serverListPanel.repaint();
}
final static class ConnectButtonListener implements ActionListener {
private ServerModel sm;
private JButton connectButton;
private JButton disconnectButton;
public ConnectButtonListener(ServerModel sm, JButton connectButton, JButton disconnectButton) {
this.sm = sm;
this.connectButton = connectButton;
this.disconnectButton = disconnectButton;
}
public void actionPerformed(ActionEvent ae) {
onConnect(servers.indexOf(sm));
connectButton.setEnabled(false);
disconnectButton.setEnabled(true);
}
}
final static class DisconnectButtonListener implements ActionListener {
private ServerModel sm;
private JButton connectButton;
private JButton disconnectButton;
public DisconnectButtonListener(ServerModel sm, JButton connectButton, JButton disconnectButton) {
this.sm = sm;
this.connectButton = connectButton;
this.disconnectButton = disconnectButton;
}
public void actionPerformed(ActionEvent ae) {
onDisconnect();
disconnectButton.setEnabled(false);
connectButton.setEnabled(true);
}
}
class CommunicationThread implements Runnable {
private ServerModel serverModel;
......@@ -191,6 +302,7 @@ public class Main {
serverModel.setHostName(params[1]);
//Kiirni
System.out.println("Server Add: " + serverModel.getHostName());
addServerToServerListPanel(serverModel);
//
break;
case "ConnectionLost":
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment