-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ricky Yuanqi Li
committed
May 24, 2017
1 parent
4fa6a42
commit 2322050
Showing
9 changed files
with
838 additions
and
553 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="gui.ChatTest"> | ||
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | ||
<margin top="5" left="5" bottom="5" right="5"/> | ||
<constraints> | ||
<xy x="20" y="20" width="825" height="453"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<grid id="650df" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | ||
<margin top="0" left="0" bottom="0" right="0"/> | ||
<constraints> | ||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<component id="57d7a" class="javax.swing.JTextField" binding="msgInput"> | ||
<constraints> | ||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"> | ||
<preferred-size width="150" height="-1"/> | ||
</grid> | ||
</constraints> | ||
<properties/> | ||
</component> | ||
<component id="74d0c" class="javax.swing.JButton" binding="msgSendButton"> | ||
<constraints> | ||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties> | ||
<text value="Send"/> | ||
</properties> | ||
</component> | ||
<grid id="cb71c" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | ||
<margin top="0" left="0" bottom="0" right="0"/> | ||
<constraints> | ||
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<scrollpane id="d865b" binding="msgHistScroller"> | ||
<constraints> | ||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<component id="77bfa" class="javax.swing.JTextArea" binding="msgHist"> | ||
<constraints/> | ||
<properties> | ||
<lineWrap value="true"/> | ||
</properties> | ||
</component> | ||
</children> | ||
</scrollpane> | ||
</children> | ||
</grid> | ||
</children> | ||
</grid> | ||
<grid id="c2364" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | ||
<margin top="0" left="0" bottom="0" right="0"/> | ||
<constraints> | ||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<component id="4ff44" class="javax.swing.JButton" binding="refreshButton" default-binding="true"> | ||
<constraints> | ||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties> | ||
<text value="Refresh"/> | ||
</properties> | ||
</component> | ||
<component id="f708c" class="javax.swing.JList" binding="userlist"> | ||
<constraints> | ||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false"> | ||
<preferred-size width="150" height="50"/> | ||
</grid> | ||
</constraints> | ||
<properties/> | ||
</component> | ||
</children> | ||
</grid> | ||
</children> | ||
</grid> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package gui; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.io.PrintWriter; | ||
import java.net.Socket; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.Scanner; | ||
|
||
public class ChatTest implements Runnable { | ||
|
||
// GUI | ||
public JFrame frame = new JFrame(); | ||
public JPanel panel; | ||
private JTextField msgInput; | ||
private JButton msgSendButton; | ||
private JButton refreshButton; | ||
private DefaultListModel<String> users = new DefaultListModel<>(); | ||
private JList userlist; | ||
private JTextArea msgHist; | ||
private JScrollPane msgHistScroller; | ||
|
||
// Connection | ||
private Socket socket; | ||
private PrintWriter out; | ||
private Scanner in; | ||
String username = "timcook"; | ||
|
||
// Background thread | ||
private Thread thread; | ||
|
||
// Constructor | ||
public ChatTest(Socket socket, Scanner in, PrintWriter out, String username) { | ||
|
||
this.username = username; | ||
this.socket = socket; | ||
this.in = in; | ||
this.out = out; | ||
|
||
frame.add(panel); | ||
frame.setSize(640, 480); | ||
frame.setResizable(false); | ||
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | ||
userlist.setModel(users); | ||
|
||
msgSendButton.addActionListener(e -> { | ||
if (userlist.getSelectedValue() == null) return; | ||
if (msgInput.getText().equals("")) return; | ||
|
||
String msg = msgInput.getText(); | ||
msgInput.setText(""); | ||
try { | ||
msg = String.format("message %s %s", userlist.getSelectedValue(), msg); | ||
out.println(msg); | ||
display(msg, Direction.SEND); | ||
} catch (Exception e1) { | ||
e1.printStackTrace(); | ||
} | ||
}); | ||
|
||
refreshButton.addActionListener(e -> { | ||
try { | ||
out.println("userlist"); | ||
} catch (Exception e1) { | ||
e1.printStackTrace(); | ||
} | ||
}); | ||
|
||
thread = new Thread(this, "chat"); | ||
} | ||
|
||
public void start() { | ||
frame.setVisible(true); | ||
thread.start(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
while (in.hasNextLine()) | ||
display(in.nextLine(), Direction.RECV); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private void display(String msg, Direction dir) { | ||
EventQueue.invokeLater(() -> { | ||
System.out.println(msg); | ||
|
||
String[] tokens = msg.split(" ", 2); | ||
String type = tokens[0]; | ||
String[] receiverBody = null; | ||
String time = new SimpleDateFormat("HH:mm:ss").format(new Date()); | ||
|
||
switch (type) { | ||
case "message": | ||
if (dir == Direction.RECV) break; | ||
receiverBody = tokens[1].split(" ", 2); | ||
msgHist.append(String.format("(%s) to [%s]: %s%n", time, receiverBody[0], receiverBody[1])); | ||
break; | ||
case "recvmsg": | ||
receiverBody = tokens[1].split(" ", 2); | ||
msgHist.append(String.format("(%s) from [%s]: %s%n", time, receiverBody[0], receiverBody[1])); | ||
break; | ||
case "userlist": | ||
users.removeAllElements(); | ||
Arrays.stream(tokens[1].split(" ")).forEach(users::addElement); | ||
} | ||
}); | ||
} | ||
|
||
public static void main(String[] args) { | ||
// EventQueue.invokeLater(() -> new ChatTest().start()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package gui; | ||
|
||
public enum Direction { | ||
SEND, | ||
RECV | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.