Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added untitled9/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions untitled9/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions untitled9/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions untitled9/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions untitled9/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions untitled9/.idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions untitled9/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions untitled9/J2_Stream200303.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
13 changes: 13 additions & 0 deletions untitled9/chat-client/chat-client.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="network" />
<orderEntry type="module" module-name="chat-library" />
</component>
</module>
201 changes: 201 additions & 0 deletions untitled9/chat-client/src/ru/gb/jt/chat/client/ClientGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package ru.gb.jt.chat.client;

import ru.gb.jt.chat.library.Library;
import ru.gb.jt.network.SocketThread;
import ru.gb.jt.network.SocketThreadListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.net.Socket;

public class ClientGUI extends JFrame implements ActionListener, Thread.UncaughtExceptionHandler, SocketThreadListener {

private static final int WIDTH = 400;
private static final int HEIGHT = 300;

private final JTextArea log = new JTextArea();
private final JPanel panelTop = new JPanel(new GridLayout(2, 3));
private final JTextField tfIPAddress = new JTextField("127.0.0.1");
private final JTextField tfPort = new JTextField("8190");
private final JCheckBox cbAlwaysOnTop = new JCheckBox("Always on top");
private final JTextField tfLogin = new JTextField("ivan");
private final JPasswordField tfPassword = new JPasswordField("123");
private final JButton btnLogin = new JButton("Login");

private final JPanel panelBottom = new JPanel(new BorderLayout());
private final JButton btnDisconnect = new JButton("<html><b>Disconnect</b></html>");
private final JTextField tfMessage = new JTextField();
private final JButton btnSend = new JButton("Send");

private final JList<String> userList = new JList<>();
private SocketThread socketThread;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ClientGUI();
}
});
}

private ClientGUI() {
Thread.setDefaultUncaughtExceptionHandler(this);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(WIDTH, HEIGHT);
setTitle("Chat client");
String[] users = {"user1", "user2", "user3", "user4", "user5",
"user_with_an_exceptionally_long_name_in_this_chat"};
userList.setListData(users);
log.setEditable(false);
JScrollPane scrollLog = new JScrollPane(log);
JScrollPane scrollUsers = new JScrollPane(userList);
scrollUsers.setPreferredSize(new Dimension(100, 0));
cbAlwaysOnTop.addActionListener(this);
btnSend.addActionListener(this);
tfMessage.addActionListener(this);
btnLogin.addActionListener(this);

panelTop.add(tfIPAddress);
panelTop.add(tfPort);
panelTop.add(cbAlwaysOnTop);
panelTop.add(tfLogin);
panelTop.add(tfPassword);
panelTop.add(btnLogin);
panelBottom.add(btnDisconnect, BorderLayout.WEST);
panelBottom.add(tfMessage, BorderLayout.CENTER);
panelBottom.add(btnSend, BorderLayout.EAST);
panelBottom.setVisible(false);

add(scrollLog, BorderLayout.CENTER);
add(scrollUsers, BorderLayout.EAST);
add(panelTop, BorderLayout.NORTH);
add(panelBottom, BorderLayout.SOUTH);

setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == cbAlwaysOnTop) {
setAlwaysOnTop(cbAlwaysOnTop.isSelected());
} else if (src == btnSend || src == tfMessage) {
sendMessage();
} else if (src == btnLogin) {
connect();
} else if (src == btnDisconnect) {
socketThread.close();
}
else
throw new RuntimeException("Unknown source: " + src);
}

private void connect() {
try {
Socket socket = new Socket(tfIPAddress.getText(), Integer.parseInt(tfPort.getText()));
socketThread = new SocketThread(this, "Client", socket);
} catch (IOException e) {
showException(Thread.currentThread(), e);
}
}

private void sendMessage() {
String msg = tfMessage.getText();
if ("".equals(msg)) return;
tfMessage.setText(null);
tfMessage.grabFocus();
socketThread.sendMessage(msg);

if (!"".equals(msg)) {
tfMessage.setText(null);
tfMessage.grabFocus();
socketThread.sendMessage(msg);
}

// putLog(String.format("%s: %s", username, msg));
// wrtMsgToLogFile(msg, username);
}

private void wrtMsgToLogFile(String msg, String username) {
try (FileWriter out = new FileWriter("log.txt", true)) {
out.write(username + ": " + msg + "\n");
out.flush();
} catch (IOException e) {
showException(Thread.currentThread(), e);
}
}

private void putLog(String msg) {
if ("".equals(msg)) return;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
log.append(msg + "\n");
log.setCaretPosition(log.getDocument().getLength());
}
});
}

private void showException(Thread t, Throwable e) {
String msg;
StackTraceElement[] ste = e.getStackTrace();
if (ste.length == 0)
msg = "Empty Stacktrace";
else {
msg = "Exception in " + t.getName() + " " +
e.getClass().getCanonicalName() + ": " +
e.getMessage() + "\n\t at " + ste[0];
}
JOptionPane.showMessageDialog(null, msg, "Exception", JOptionPane.ERROR_MESSAGE);
}

@Override
public void uncaughtException(Thread t, Throwable e) {
e.printStackTrace();
showException(t, e);
System.exit(1);
}

/**
* Socket thread methods
* */

@Override
public void onSocketStart(SocketThread thread, Socket socket) {
putLog("Start");
}

@Override
public void onSocketStop(SocketThread thread) {
putLog("Stop");
panelBottom.setVisible(false);
panelTop.setVisible(true);
}

@Override
public void onSocketReady(SocketThread thread, Socket socket) {
putLog("Ready");
panelBottom.setVisible(true);
panelTop.setVisible(false);
String login = tfLogin.getText();
String password = new String(tfPassword.getPassword());
thread.sendMessage(Library.getAuthRequest(login, password));

}

@Override
public void onReceiveString(SocketThread thread, Socket socket, String msg) {
putLog(msg);
}

@Override
public void onSocketException(SocketThread thread, Exception exception) {
showException(thread, exception);
}
}
Loading