├── Java
├── bin
│ ├── .gitignore
│ └── Client
│ │ ├── Main.class
│ │ ├── Client.class
│ │ ├── ChatRoomGUI.class
│ │ ├── ConnectGUI$1.class
│ │ ├── ConnectGUI$2.class
│ │ ├── ConnectGUI$3.class
│ │ ├── ConnectGUI$4.class
│ │ ├── ConnectGUI.class
│ │ ├── ErrorBoxGUI.class
│ │ ├── UsernameGUI.class
│ │ ├── ChatRoomGUI$1.class
│ │ ├── ChatRoomGUI$2.class
│ │ ├── ChatRoomGUI$3.class
│ │ ├── ChatRoomGUI$4.class
│ │ ├── ChatRoomGUI$5.class
│ │ ├── ChatRoomGUI$6.class
│ │ ├── ChatRoomGUI$7.class
│ │ ├── ErrorBoxGUI$1.class
│ │ ├── UsernameGUI$1.class
│ │ ├── UsernameGUI$2.class
│ │ ├── UsernameGUI$3.class
│ │ ├── ChatRoomGUI$6$1.class
│ │ ├── ChatRoomGUI$7$1.class
│ │ ├── ErrorBoxGUI$1$1.class
│ │ └── CommunicationHandler.class
├── swt.jar
├── swing2swt.jar
├── src
│ └── Client
│ │ ├── Main.java
│ │ ├── ErrorBoxGUI.java
│ │ ├── CommunicationHandler.java
│ │ ├── UsernameGUI.java
│ │ ├── ConnectGUI.java
│ │ ├── Client.java
│ │ └── ChatRoomGUI.java
├── .classpath
├── .project
└── .settings
│ └── org.eclipse.jdt.core.prefs
├── Images
├── binance.jpg
├── Chat_Client_1.png
├── Chat_Client_2.png
├── Chat_Client_3.png
└── Chat_Server_1.png
├── README.md
└── LICENSE
/Java/bin/.gitignore:
--------------------------------------------------------------------------------
1 | /Client/
2 |
--------------------------------------------------------------------------------
/Java/swt.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/swt.jar
--------------------------------------------------------------------------------
/Images/binance.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Images/binance.jpg
--------------------------------------------------------------------------------
/Java/swing2swt.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/swing2swt.jar
--------------------------------------------------------------------------------
/Images/Chat_Client_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Images/Chat_Client_1.png
--------------------------------------------------------------------------------
/Images/Chat_Client_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Images/Chat_Client_2.png
--------------------------------------------------------------------------------
/Images/Chat_Client_3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Images/Chat_Client_3.png
--------------------------------------------------------------------------------
/Images/Chat_Server_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Images/Chat_Server_1.png
--------------------------------------------------------------------------------
/Java/bin/Client/Main.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/Main.class
--------------------------------------------------------------------------------
/Java/bin/Client/Client.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/Client.class
--------------------------------------------------------------------------------
/Java/bin/Client/ChatRoomGUI.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ChatRoomGUI.class
--------------------------------------------------------------------------------
/Java/bin/Client/ConnectGUI$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ConnectGUI$1.class
--------------------------------------------------------------------------------
/Java/bin/Client/ConnectGUI$2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ConnectGUI$2.class
--------------------------------------------------------------------------------
/Java/bin/Client/ConnectGUI$3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ConnectGUI$3.class
--------------------------------------------------------------------------------
/Java/bin/Client/ConnectGUI$4.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ConnectGUI$4.class
--------------------------------------------------------------------------------
/Java/bin/Client/ConnectGUI.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ConnectGUI.class
--------------------------------------------------------------------------------
/Java/bin/Client/ErrorBoxGUI.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ErrorBoxGUI.class
--------------------------------------------------------------------------------
/Java/bin/Client/UsernameGUI.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/UsernameGUI.class
--------------------------------------------------------------------------------
/Java/bin/Client/ChatRoomGUI$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ChatRoomGUI$1.class
--------------------------------------------------------------------------------
/Java/bin/Client/ChatRoomGUI$2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ChatRoomGUI$2.class
--------------------------------------------------------------------------------
/Java/bin/Client/ChatRoomGUI$3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ChatRoomGUI$3.class
--------------------------------------------------------------------------------
/Java/bin/Client/ChatRoomGUI$4.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ChatRoomGUI$4.class
--------------------------------------------------------------------------------
/Java/bin/Client/ChatRoomGUI$5.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ChatRoomGUI$5.class
--------------------------------------------------------------------------------
/Java/bin/Client/ChatRoomGUI$6.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ChatRoomGUI$6.class
--------------------------------------------------------------------------------
/Java/bin/Client/ChatRoomGUI$7.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ChatRoomGUI$7.class
--------------------------------------------------------------------------------
/Java/bin/Client/ErrorBoxGUI$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ErrorBoxGUI$1.class
--------------------------------------------------------------------------------
/Java/bin/Client/UsernameGUI$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/UsernameGUI$1.class
--------------------------------------------------------------------------------
/Java/bin/Client/UsernameGUI$2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/UsernameGUI$2.class
--------------------------------------------------------------------------------
/Java/bin/Client/UsernameGUI$3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/UsernameGUI$3.class
--------------------------------------------------------------------------------
/Java/bin/Client/ChatRoomGUI$6$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ChatRoomGUI$6$1.class
--------------------------------------------------------------------------------
/Java/bin/Client/ChatRoomGUI$7$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ChatRoomGUI$7$1.class
--------------------------------------------------------------------------------
/Java/bin/Client/ErrorBoxGUI$1$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/ErrorBoxGUI$1$1.class
--------------------------------------------------------------------------------
/Java/bin/Client/CommunicationHandler.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IDouble/Multithreading-Chat-Client-Java/HEAD/Java/bin/Client/CommunicationHandler.class
--------------------------------------------------------------------------------
/Java/src/Client/Main.java:
--------------------------------------------------------------------------------
1 | package Client;
2 |
3 | public class Main {
4 |
5 | public static void main(String[] args) {
6 | CommunicationHandler cH = new CommunicationHandler();
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/Java/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Java/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ChatThreadClient
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/Java/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.5
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.5
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 👥 Multithreading Chat Client Java 👥
2 | 👥 A simple Chat Client in Java, implemented with the Observer pattern 👥
3 |
4 | ## 🖼 Images 🖼
5 |
6 | ### 👥 Client 👥
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | ### [🖥 Server 🖥](https://github.com/AYIDouble/Multithreading-Chat-Server-Java)
19 |
20 |
21 |
22 |
23 |
24 | 
25 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Alpay Yildirim
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Java/src/Client/ErrorBoxGUI.java:
--------------------------------------------------------------------------------
1 | package Client;
2 |
3 | import org.eclipse.swt.SWT;
4 | import org.eclipse.swt.widgets.Display;
5 | import org.eclipse.swt.widgets.MessageBox;
6 | import org.eclipse.swt.widgets.Shell;
7 |
8 | public class ErrorBoxGUI {
9 |
10 | String errorMessage;
11 |
12 | Display display;
13 | Shell shell = new Shell(display);
14 | public ErrorBoxGUI(String errorMessage){
15 | this.errorMessage = errorMessage;
16 | }
17 |
18 | public void open() {
19 | new Thread(new Runnable() {
20 | public void run() {
21 | Display.getDefault().asyncExec(new Runnable() {
22 | public void run() {
23 | try{
24 | createContents();
25 | shell.open();
26 | shell.layout();
27 | while (!shell.isDisposed()) {
28 | if (!display.readAndDispatch()) {
29 | display.sleep();
30 | }
31 | }
32 | }catch(Exception e){
33 | e.printStackTrace();
34 | }
35 | }
36 | });
37 |
38 | }
39 | }).start();
40 |
41 | }
42 |
43 | public void close(){
44 | shell.close();
45 | }
46 |
47 | protected void createContents() {
48 | display = Display.getDefault();
49 | int style = SWT.ICON_ERROR;
50 | //SWT.ICON_QUESTION | SWT.YES | SWT.NO
51 | MessageBox messageBox = new MessageBox(shell, style);
52 | messageBox.setMessage(errorMessage);
53 | int rc = messageBox.open();
54 | switch (rc) {
55 | case SWT.OK:
56 | System.out.println("SWT.OK");
57 | break;
58 | case SWT.CANCEL:
59 | System.out.println("SWT.CANCEL");
60 | break;
61 | case SWT.YES:
62 | System.out.println("SWT.YES");
63 | break;
64 | case SWT.NO:
65 | System.out.println("SWT.NO");
66 | break;
67 | case SWT.RETRY:
68 | System.out.println("SWT.RETRY");
69 | break;
70 | case SWT.ABORT:
71 | System.out.println("SWT.ABORT");
72 | break;
73 | case SWT.IGNORE:
74 | System.out.println("SWT.IGNORE");
75 | break;
76 | }
77 | close();
78 | }
79 | }
80 |
81 |
--------------------------------------------------------------------------------
/Java/src/Client/CommunicationHandler.java:
--------------------------------------------------------------------------------
1 | package Client;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStreamReader;
6 | import java.io.PrintWriter;
7 | import java.net.ConnectException;
8 | import java.net.InetSocketAddress;
9 | import java.net.Socket;
10 | import java.net.UnknownHostException;
11 | import java.util.ArrayList;
12 | import java.util.Arrays;
13 |
14 | public class CommunicationHandler extends Thread {
15 |
16 | private Boolean isActive = true;
17 |
18 | private ArrayList userList = new ArrayList();
19 | private ArrayList message = new ArrayList();
20 |
21 | private Socket s = new Socket();
22 | private PrintWriter s_out = null;
23 | private BufferedReader s_in = null;
24 | private String response = "";
25 | private Client client;
26 | private Boolean isConnected = false;
27 |
28 | public CommunicationHandler(){
29 | super.start();
30 | }
31 |
32 | public void run(){
33 | try {
34 | client = new Client(this);
35 | while(isActive){
36 | if(getIsConnected()){
37 | while((response = s_in.readLine()) != null){
38 | if(response.equals("/GAU")){
39 | String uList;
40 | while((uList = s_in.readLine()) != null){
41 | System.out.println("TEST Userlist");
42 | userList = setUserList(uList);
43 | System.out.println("Response:" + userList);
44 | client.updateUserListChatGUI(userList);
45 | if(uList.contains("[END]")){
46 | break;
47 | }
48 | }
49 | }
50 | else if(response.equals("/M")){
51 | String messageResponse;
52 | while((messageResponse = s_in.readLine()) != null){
53 | client.updateMessageChatGUI(messageResponse);
54 | break;
55 | }
56 | System.out.println("Message Test");
57 | }
58 | }
59 | }
60 | }
61 | } catch (Exception e) {
62 | e.printStackTrace();
63 | }
64 | }
65 |
66 | public ArrayList setUserList(String rspUserList){
67 | ArrayList parts = new ArrayList(Arrays.asList(rspUserList.split(":")));
68 | parts.remove("[END]"); // Arraylist
69 | return parts;
70 | }
71 |
72 | public synchronized Boolean getIsConnected() {
73 | return isConnected;
74 | }
75 |
76 | public synchronized void setIsConnected(Boolean isConnected) {
77 | this.isConnected = isConnected;
78 | }
79 |
80 | public synchronized Socket getS() {
81 | return s;
82 | }
83 |
84 | public synchronized void setS(Socket s) {
85 | this.s = s;
86 | }
87 |
88 | public synchronized PrintWriter getS_out() {
89 | return s_out;
90 | }
91 |
92 | public synchronized void setS_out(PrintWriter s_out) {
93 | this.s_out = s_out;
94 | }
95 |
96 | public synchronized BufferedReader getS_in() {
97 | return s_in;
98 | }
99 |
100 | public synchronized void setS_in(BufferedReader s_in) {
101 | this.s_in = s_in;
102 | }
103 |
104 | public synchronized Client getClient() {
105 | return client;
106 | }
107 |
108 | public synchronized void setClient(Client client) {
109 | this.client = client;
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/Java/src/Client/UsernameGUI.java:
--------------------------------------------------------------------------------
1 | package Client;
2 |
3 | import org.eclipse.swt.widgets.Display;
4 | import org.eclipse.swt.widgets.Shell;
5 | import org.eclipse.swt.layout.GridLayout;
6 | import org.eclipse.swt.widgets.Label;
7 | import org.eclipse.swt.SWT;
8 | import org.eclipse.swt.widgets.Text;
9 | import org.eclipse.swt.layout.GridData;
10 | import org.eclipse.swt.widgets.Button;
11 | import org.eclipse.swt.events.KeyAdapter;
12 | import org.eclipse.swt.events.KeyEvent;
13 | import org.eclipse.swt.events.SelectionAdapter;
14 | import org.eclipse.swt.events.SelectionEvent;
15 | import org.eclipse.swt.events.ShellAdapter;
16 | import org.eclipse.swt.events.ShellEvent;
17 |
18 | public class UsernameGUI {
19 |
20 | public ErrorBoxGUI eBX = new ErrorBoxGUI("You need to Type a Username");
21 | protected Shell shell;
22 | private Text username;
23 | private Client client;
24 |
25 | public UsernameGUI(Client c){
26 | client = c;
27 | }
28 | /**
29 | * Open the window.
30 | */
31 | public void close(){
32 | shell.close();
33 | }
34 |
35 | public void open() {
36 | Display display = Display.getDefault();
37 | createContents();
38 | shell.open();
39 | shell.layout();
40 | while (!shell.isDisposed()) {
41 | if (!display.readAndDispatch()) {
42 | display.sleep();
43 | }
44 | }
45 | }
46 |
47 | /**
48 | * Create contents of the window.
49 | * @wbp.parser.entryPoint
50 | */
51 | protected void createContents() {
52 | shell = new Shell(Display.getDefault(), SWT.CLOSE | SWT.TITLE);
53 | shell.addShellListener(new ShellAdapter() {
54 | @Override
55 | public void shellClosed(ShellEvent e) {
56 | if(username.getText().equals("")){ // Only closes the application,when user doesn't typed a username and closed it
57 | System.exit(0);
58 | }
59 | }
60 | });
61 | shell.setSize(450, 100);
62 | shell.setText("Username");
63 | shell.setLayout(new GridLayout(2, false));
64 |
65 | Label lblUsername = new Label(shell, SWT.NONE);
66 | lblUsername.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
67 | lblUsername.setText("Username:");
68 | username = new Text(shell, SWT.BORDER);
69 | username.addKeyListener(new KeyAdapter() {
70 | @Override
71 | public void keyPressed(KeyEvent e) {
72 | if(e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR){
73 | try{
74 | if(!username.getText().equals("")){
75 | client.setUsername(username.getText());
76 | close();
77 | }else{
78 | eBX.open();
79 | }
80 | }catch(Exception ex){
81 |
82 | }
83 | }
84 | }
85 | });
86 | username.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
87 |
88 | Button btnUsernameChose = new Button(shell, SWT.NONE);
89 | btnUsernameChose.addSelectionListener(new SelectionAdapter() {
90 | public void widgetSelected(SelectionEvent e) {
91 | try{
92 | if(!username.getText().equals("")){
93 | client.setUsername(username.getText());
94 | close();
95 | }else{
96 | eBX.open();
97 | }
98 | }catch(Exception ex){
99 |
100 | }
101 | }
102 | });
103 | GridData gd_btnUsernameChose = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
104 | gd_btnUsernameChose.widthHint = 424;
105 | btnUsernameChose.setLayoutData(gd_btnUsernameChose);
106 | btnUsernameChose.setText("Chose");
107 |
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/Java/src/Client/ConnectGUI.java:
--------------------------------------------------------------------------------
1 | package Client;
2 |
3 | import org.eclipse.swt.widgets.Display;
4 | import org.eclipse.swt.widgets.Shell;
5 | import org.eclipse.swt.layout.GridLayout;
6 | import org.eclipse.swt.widgets.Label;
7 | import org.eclipse.swt.SWT;
8 | import org.eclipse.swt.widgets.Text;
9 | import org.eclipse.swt.layout.GridData;
10 | import org.eclipse.swt.widgets.Button;
11 | import org.eclipse.swt.events.KeyAdapter;
12 | import org.eclipse.swt.events.KeyEvent;
13 | import org.eclipse.swt.events.SelectionAdapter;
14 | import org.eclipse.swt.events.SelectionEvent;
15 | import org.eclipse.swt.events.ShellAdapter;
16 | import org.eclipse.swt.events.ShellEvent;
17 |
18 | public class ConnectGUI {
19 |
20 | protected Shell shell;
21 | protected Text server;
22 | protected Text port;
23 | private Client client;
24 |
25 |
26 | public ConnectGUI(Client c){
27 | client = c;
28 | }
29 |
30 | /**
31 | * Open the window.
32 | */
33 | public void open() {
34 | Display display = Display.getDefault();
35 | createContents();
36 | shell.open();
37 | shell.layout();
38 | while (!shell.isDisposed()) {
39 | if (!display.readAndDispatch()) {
40 | display.sleep();
41 | }
42 | }
43 | }
44 |
45 | public void close(){
46 | shell.close();
47 | }
48 |
49 | /**
50 | * Create contents of the window.
51 | * @wbp.parser.entryPoint
52 | */
53 | protected void createContents() {
54 | shell = new Shell(Display.getDefault(), SWT.CLOSE | SWT.TITLE);
55 | shell.addShellListener(new ShellAdapter() {
56 | @Override
57 | public void shellClosed(ShellEvent e) {
58 | if(!client.getIsConnected()){ // Only closes the application,when user doesn't want to connect
59 | System.exit(0);
60 | }
61 | }
62 | });
63 | shell.setSize(450, 125);
64 | shell.setText("Connect");
65 | shell.setLayout(new GridLayout(2, false));
66 |
67 | Label lblServer = new Label(shell, SWT.NONE);
68 | lblServer.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
69 | lblServer.setText("Server:");
70 |
71 | server = new Text(shell, SWT.BORDER);
72 | server.addKeyListener(new KeyAdapter() {
73 | @Override
74 | public void keyPressed(KeyEvent e) {
75 | if(e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR){
76 | try{
77 | client.setHost(server.getText());
78 | client.setPortNumber(Integer.parseInt(port.getText()));
79 | client.tryConnect();
80 | }catch(Exception ex){
81 | ex.printStackTrace();
82 | }
83 | }
84 | }
85 | });
86 | server.setText("Admin-PC");
87 | server.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
88 |
89 | Label lblPort = new Label(shell, SWT.NONE);
90 | lblPort.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
91 | lblPort.setText("Port:");
92 |
93 | port = new Text(shell, SWT.BORDER);
94 | port.addKeyListener(new KeyAdapter() {
95 | @Override
96 | public void keyPressed(KeyEvent e) {
97 | if(e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR){
98 | try{
99 | client.setHost(server.getText());
100 | client.setPortNumber(Integer.parseInt(port.getText()));
101 | client.tryConnect();
102 | }catch(Exception ex){
103 | ex.printStackTrace();
104 | }
105 | }
106 | }
107 | });
108 | port.setText("5000");
109 | port.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
110 |
111 |
112 | Button btnConnect = new Button(shell, SWT.NONE);
113 | btnConnect.addSelectionListener(new SelectionAdapter() {
114 | public void widgetSelected(SelectionEvent e) {
115 | try{
116 | client.setHost(server.getText());
117 | client.setPortNumber(Integer.parseInt(port.getText()));
118 | client.tryConnect();
119 | }catch(Exception ex){
120 | ex.printStackTrace();
121 | }
122 | }
123 | });
124 |
125 | GridData gd_btnConnect = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
126 | gd_btnConnect.widthHint = 424;
127 | btnConnect.setLayoutData(gd_btnConnect);
128 | btnConnect.setText("Connect");
129 |
130 | }
131 |
132 | }
133 |
--------------------------------------------------------------------------------
/Java/src/Client/Client.java:
--------------------------------------------------------------------------------
1 | package Client;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStreamReader;
6 | import java.io.PrintWriter;
7 | import java.net.ConnectException;
8 | import java.net.InetSocketAddress;
9 | import java.net.Socket;
10 | import java.net.SocketException;
11 | import java.net.UnknownHostException;
12 | import java.text.SimpleDateFormat;
13 | import java.util.ArrayList;
14 | import java.util.Calendar;
15 |
16 | public class Client extends Thread{
17 |
18 | ConnectGUI connectWindow;
19 | private String host = "Admin-PC";
20 | private int portNumber = 5000;
21 | private Socket s;
22 | private PrintWriter s_out = null;
23 | private BufferedReader s_in = null;
24 | private String username = "";
25 | private CommunicationHandler communicationHandler;
26 | private Boolean isConnected = false;
27 | public ChatRoomGUI window;
28 |
29 | public ErrorBoxGUI eBCS;
30 | public ErrorBoxGUI eBUH;
31 |
32 | private Calendar cal;
33 | private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
34 |
35 | public Client(CommunicationHandler cH){
36 | super.start();
37 | communicationHandler = cH;
38 | }
39 |
40 | public void run(){
41 | try{
42 | connect();
43 | if(isConnected){
44 | choseUsername();
45 | openChat();
46 | }else{
47 | run();
48 | }
49 | }
50 | catch (Exception e) {
51 | e.printStackTrace();
52 | }
53 | }
54 |
55 | public void openChat(){
56 | try {
57 | window = new ChatRoomGUI(this);
58 | communicationHandler.setIsConnected(true);
59 | setCHConfiguration();
60 | window.open();
61 | } catch (Exception e) {
62 | e.printStackTrace();
63 | }
64 | }
65 |
66 | public void connect() throws IOException{
67 | try {
68 | connectWindow = new ConnectGUI(this);
69 | connectWindow.open();
70 | } catch (Exception e) {
71 | e.printStackTrace();
72 | connect();
73 | }
74 | }
75 |
76 | public void tryConnect() throws IOException{
77 | try{
78 | s = new Socket();
79 | s.connect(new InetSocketAddress(host , portNumber));
80 | System.out.println("Connected");
81 | //writer for socket
82 | s_out = new PrintWriter( s.getOutputStream(), true);
83 | //reader for socket
84 | s_in = new BufferedReader(new InputStreamReader(s.getInputStream()));
85 | isConnected = true;
86 | connectWindow.close();
87 | }catch(ConnectException cE){
88 | System.err.println("Cannot Connect");
89 | eBCS = new ErrorBoxGUI("Cannot Connect");
90 | eBCS.open();
91 | }
92 | catch (UnknownHostException e){//Host not found
93 | System.err.println("Don't know about host : " + host);
94 | eBUH = new ErrorBoxGUI("Don't know about host : " + host);
95 | eBUH.open();
96 | }
97 | }
98 |
99 | public void choseUsername(){
100 | try {
101 | UsernameGUI window = new UsernameGUI(this);
102 | window.open();
103 | if(!username.equals("")){
104 | s_out.println(username);
105 | }else{
106 | choseUsername();
107 | }
108 | } catch (Exception e) {
109 | e.printStackTrace();
110 | choseUsername();
111 | }
112 | }
113 |
114 | private void setCHConfiguration(){
115 | communicationHandler.setS_in(s_in);
116 | communicationHandler.setS_out(s_out);
117 | communicationHandler.setS(s);
118 | }
119 |
120 | public void updateUserListChatGUI(ArrayList users){
121 | window.addUserList(users);
122 | }
123 |
124 | public void updateMessageChatGUI(String msg){
125 | try{
126 | if(msg.substring(0,8).equals("[GLOBAL]")){
127 | System.out.println(msg.substring(9, 9 + username.length()));
128 | if(!msg.substring(9, 9 + username.length()).equals(username)){
129 | Calendar cal = Calendar.getInstance();
130 | msg = "(" + sdf.format(cal.getTime()) + ")" + msg;
131 | window.updateChat(msg);
132 | }
133 | }else{
134 | Calendar cal = Calendar.getInstance();
135 | msg = "(" + sdf.format(cal.getTime()) + ")" + msg;
136 | window.updateChat(msg);
137 | }
138 |
139 | }catch(StringIndexOutOfBoundsException SIOBE){
140 | Calendar cal = Calendar.getInstance();
141 | msg = "(" + sdf.format(cal.getTime()) + ")" + msg;
142 | window.updateChat(msg);
143 | }
144 | }
145 |
146 | public void sendMessage(String receiver,String msg){
147 | s_out.println("/M");
148 | s_out.println((receiver.equals("")) ? "GLOBAL" : receiver);
149 | s_out.println(msg);
150 | }
151 |
152 | public void sendEXIT(){
153 | s_out.println("/EXIT");
154 | }
155 |
156 | public synchronized Boolean getIsConnected() {
157 | return isConnected;
158 | }
159 |
160 | public synchronized void setIsConnected(Boolean isConnected) {
161 | this.isConnected = isConnected;
162 | }
163 |
164 | public synchronized String getHost() {
165 | return host;
166 | }
167 |
168 | public synchronized void setHost(String host) {
169 | this.host = host;
170 | }
171 |
172 | public synchronized int getPortNumber() {
173 | return portNumber;
174 | }
175 |
176 | public synchronized void setPortNumber(int portNumber) {
177 | this.portNumber = portNumber;
178 | }
179 |
180 | public synchronized String getUsername() {
181 | return username;
182 | }
183 |
184 | public synchronized void setUsername(String username) {
185 | this.username = username;
186 | }
187 |
188 | public ChatRoomGUI getWindow() {
189 | return window;
190 | }
191 | }
192 |
--------------------------------------------------------------------------------
/Java/src/Client/ChatRoomGUI.java:
--------------------------------------------------------------------------------
1 | package Client;
2 |
3 | import java.io.IOException;
4 | import java.text.SimpleDateFormat;
5 | import java.util.ArrayList;
6 | import java.util.Calendar;
7 |
8 | import org.eclipse.swt.widgets.Display;
9 | import org.eclipse.swt.widgets.Shell;
10 |
11 | import swing2swt.layout.BorderLayout;
12 |
13 | import org.eclipse.swt.widgets.Text;
14 | import org.eclipse.swt.SWT;
15 | import org.eclipse.swt.widgets.Event;
16 | import org.eclipse.swt.widgets.List;
17 | import org.eclipse.swt.widgets.Group;
18 | import org.eclipse.swt.widgets.Button;
19 | import org.eclipse.swt.widgets.Listener;
20 | import org.eclipse.swt.events.SelectionAdapter;
21 | import org.eclipse.swt.events.SelectionEvent;
22 |
23 | import swing2swt.layout.BorderLayout;
24 |
25 | import org.eclipse.swt.events.KeyAdapter;
26 | import org.eclipse.swt.events.KeyEvent;
27 | import org.eclipse.swt.events.ShellAdapter;
28 | import org.eclipse.swt.events.ShellEvent;
29 | import org.eclipse.swt.graphics.Rectangle;
30 |
31 | public class ChatRoomGUI {
32 |
33 | protected Shell shell;
34 | private Group group;
35 |
36 | private Button btnSend;
37 | private Text message;
38 | private Text txtChat;
39 | private String text = "";
40 | private List userList;
41 | private String selectedRCVRString = "";
42 | private static Client client;
43 |
44 | private Calendar cal;
45 | private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
46 |
47 | public ChatRoomGUI(Client c){
48 | setClient(c);
49 | }
50 | /**
51 | * Launch the application.
52 | * @param args
53 | */
54 |
55 | /**
56 | * Open the window.
57 | * @throws IOException
58 | */
59 | /**
60 | * @wbp.parser.entryPoint
61 | */
62 | public void open() throws IOException {
63 | Display display = Display.getDefault();
64 | createContents();
65 | shell.open();
66 | shell.layout();
67 | while (!shell.isDisposed()) {
68 | if (!display.readAndDispatch()) {
69 | display.sleep();
70 | }
71 | }
72 | }
73 |
74 | /**
75 | * Create contents of the window.
76 | * @throws IOException
77 | */
78 | protected void createContents() throws IOException {
79 | shell = new Shell();
80 | shell.addShellListener(new ShellAdapter() {
81 | @Override
82 | public void shellClosed(ShellEvent e) {
83 | client.sendEXIT();
84 | System.exit(0);
85 | }
86 | });
87 | shell.setSize(450, 300);
88 | shell.setText("Username " + "[" + client.getUsername() + "]" + " | Host " + "[" + client.getHost() + "]" + " | Port " + "[" + client.getPortNumber() + "]");
89 | shell.setLayout(new BorderLayout(0, 0));
90 |
91 | userList = new List(shell, SWT.BORDER);
92 | userList.addSelectionListener(new SelectionAdapter() {
93 | @Override
94 | public void widgetSelected(SelectionEvent e) {
95 | int[] selectedItems = userList.getSelectionIndices();
96 | for (int loopIndex = 0; loopIndex < selectedItems.length; loopIndex++){
97 | selectedRCVRString = userList.getItem(selectedItems[loopIndex]);
98 | }
99 | System.out.println("Selected Items: " + selectedRCVRString);
100 | }
101 | });
102 | userList.setItems(new String[] {});
103 | userList.setLayoutData(BorderLayout.EAST);
104 |
105 | group = new Group(shell, SWT.NONE);
106 | group.setLayoutData(BorderLayout.SOUTH);
107 | group.setLayout(new BorderLayout(0, 0));
108 |
109 | message = new Text(group, SWT.BORDER);
110 | message.addKeyListener(new KeyAdapter() {
111 | @Override
112 | public void keyPressed(KeyEvent e) {
113 | if(e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR){
114 | client.sendMessage(selectedRCVRString,message.getText());
115 | Calendar cal = Calendar.getInstance();
116 | if(selectedRCVRString.equals("") || selectedRCVRString.equals("GLOBAL")){
117 | text += "(" + sdf.format(cal.getTime()) + ")" + "[GLOBAL] " + client.getUsername() + ": " + message.getText() + " \n";
118 | }else{
119 | text += "(" + sdf.format(cal.getTime()) + ")" + "[TO " + "[" + selectedRCVRString + "]" + "] " + client.getUsername() + ": " + message.getText() + " \n";
120 | }
121 | txtChat.setText(text);
122 | message.setText("");
123 | }
124 | }
125 | });
126 | message.setLayoutData(BorderLayout.CENTER);
127 |
128 | btnSend = new Button(group, SWT.NONE);
129 | btnSend.addSelectionListener(new SelectionAdapter() {
130 | @Override
131 | public void widgetSelected(SelectionEvent e) {
132 | client.sendMessage(selectedRCVRString,message.getText());
133 | Calendar cal = Calendar.getInstance();
134 | if(selectedRCVRString.equals("") || selectedRCVRString.equals("GLOBAL")){
135 | text += "(" + sdf.format(cal.getTime()) + ")" + "[GLOBAL] " + client.getUsername() + ": " + message.getText() + " \n";
136 | }else{
137 | text += "(" + sdf.format(cal.getTime()) + ")" + "[TO " + "[" + selectedRCVRString + "]" + "] " + client.getUsername() + ": " + message.getText() + " \n";
138 | }
139 | txtChat.setText(text);
140 | message.setText("");
141 | }
142 | });
143 | btnSend.setLayoutData(BorderLayout.EAST);
144 | btnSend.setText("Send");
145 |
146 | txtChat = new Text(shell, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
147 | txtChat.addListener(SWT.Modify, new Listener(){
148 | public void handleEvent(Event e){
149 | txtChat.setTopIndex(txtChat.getLineCount() - 1);
150 | }
151 | });
152 | txtChat.setText(text);
153 | txtChat.setEditable(false);
154 | txtChat.setLayoutData(BorderLayout.CENTER);
155 |
156 | }
157 |
158 | public void addUserList(final ArrayList users){
159 | new Thread(new Runnable() {
160 | public void run() {
161 | Display.getDefault().asyncExec(new Runnable() {
162 | public void run() {
163 | try{
164 | userList.add("");
165 | removeAllUsernames();
166 | for (int i = 0; i < users.size(); i++) {
167 | userList.add(users.get(i));
168 | }
169 | shell.layout();
170 | }catch(Exception e){
171 | e.printStackTrace();
172 | }
173 | }
174 | });
175 |
176 | }
177 | }).start();
178 | }
179 |
180 | public void updateChat(final String serverMSG){
181 | new Thread(new Runnable() {
182 | public void run() {
183 | Display.getDefault().asyncExec(new Runnable() {
184 | public void run() {
185 | try{
186 | text += serverMSG +" \n";
187 | txtChat.setText(text);
188 | shell.layout();
189 | }catch(Exception e){
190 | e.printStackTrace();
191 | }
192 | }
193 | });
194 | }
195 | }).start();
196 | }
197 |
198 | private void removeAllUsernames(){
199 | userList.removeAll();
200 | userList.add("GLOBAL");
201 | }
202 |
203 | public static Client getClient() {
204 | return client;
205 | }
206 |
207 | public static void setClient(Client client) {
208 | ChatRoomGUI.client = client;
209 | }
210 |
211 |
212 | }
213 |
--------------------------------------------------------------------------------