Skip to content

Commit a2f3eee

Browse files
committed
⬆️ Init
1 parent a9a10b4 commit a2f3eee

37 files changed

+880
-0
lines changed

Java/.classpath

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="lib" path="swing2swt.jar"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<classpathentry kind="lib" path="C:/Users/Admin/Desktop/Projekte/AlteDateienWHC/workspace/ChatThreadClient/swt.jar"/>
7+
<classpathentry kind="output" path="bin"/>
8+
</classpath>

Java/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ChatThreadClient</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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

Java/bin/Client/ChatRoomGUI$1.class

794 Bytes
Binary file not shown.

Java/bin/Client/ChatRoomGUI$2.class

1.41 KB
Binary file not shown.

Java/bin/Client/ChatRoomGUI$3.class

2.18 KB
Binary file not shown.

Java/bin/Client/ChatRoomGUI$4.class

2.12 KB
Binary file not shown.

Java/bin/Client/ChatRoomGUI$5.class

860 Bytes
Binary file not shown.

Java/bin/Client/ChatRoomGUI$6$1.class

1.35 KB
Binary file not shown.

Java/bin/Client/ChatRoomGUI$6.class

1.01 KB
Binary file not shown.

Java/bin/Client/ChatRoomGUI$7$1.class

1.52 KB
Binary file not shown.

Java/bin/Client/ChatRoomGUI$7.class

1022 Bytes
Binary file not shown.

Java/bin/Client/ChatRoomGUI.class

5.25 KB
Binary file not shown.

Java/bin/Client/Client.class

6.09 KB
Binary file not shown.
3.74 KB
Binary file not shown.

Java/bin/Client/ConnectGUI$1.class

901 Bytes
Binary file not shown.

Java/bin/Client/ConnectGUI$2.class

1.27 KB
Binary file not shown.

Java/bin/Client/ConnectGUI$3.class

1.27 KB
Binary file not shown.

Java/bin/Client/ConnectGUI$4.class

1.2 KB
Binary file not shown.

Java/bin/Client/ConnectGUI.class

2.84 KB
Binary file not shown.

Java/bin/Client/ErrorBoxGUI$1$1.class

1.17 KB
Binary file not shown.

Java/bin/Client/ErrorBoxGUI$1.class

892 Bytes
Binary file not shown.

Java/bin/Client/ErrorBoxGUI.class

1.77 KB
Binary file not shown.

Java/bin/Client/Main.class

466 Bytes
Binary file not shown.

Java/bin/Client/UsernameGUI$1.class

949 Bytes
Binary file not shown.

Java/bin/Client/UsernameGUI$2.class

1.27 KB
Binary file not shown.

Java/bin/Client/UsernameGUI$3.class

1.2 KB
Binary file not shown.

Java/bin/Client/UsernameGUI.class

2.86 KB
Binary file not shown.

Java/src/Client/ChatRoomGUI.java

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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<String> 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+
}

0 commit comments

Comments
 (0)