Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.Socket;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.ListSelectionEvent;
- import javax.swing.event.ListSelectionListener;
- import java.text.SimpleDateFormat;
- import java.util.*;
- public class ReworkedChat {
- JFrame mainFrame;
- JFrame loginFrame;
- JPanel loginPanel;
- JPanel mainPanel;
- JLabel nameTag;
- JButton loginButton;
- JButton broadcastButton;
- JTextField loginText;
- JList<String> userList;
- Vector<String> userVector = new Vector<String>();
- ArrayList<DialogWindow> currentDialogs = new ArrayList<DialogWindow>();
- ArrayList<String> currentDialogList = new ArrayList<String>();
- JMenuBar menuBar;
- JMenuItem exitItem;
- JMenu fileMenu;
- User currentUser = new User();
- BufferedReader reader;
- PrintWriter writer;
- Socket sock;
- SimpleDateFormat date = new SimpleDateFormat("'('HH:mm:ss')'");
- public static void main(String[] args) {
- new ReworkedChat().go();
- }
- public void go(){
- mainFrame = new JFrame("Unicorn Messenger");
- loginFrame = new JFrame("Unicorn Messanger - Log In");
- menuBar = new JMenuBar();
- fileMenu = new JMenu("File");
- exitItem = new JMenuItem("Exit");
- exitItem.addActionListener(new ExitListener());
- menuBar.add(fileMenu);
- fileMenu.add(exitItem);
- mainFrame.setJMenuBar(menuBar);
- loginPanel = new JPanel();
- loginPanel.setLayout(new BoxLayout(loginPanel, BoxLayout.Y_AXIS));
- mainPanel = new JPanel();
- mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
- loginText = new JTextField(15);
- loginText.addKeyListener(new KeyListener(){
- public void keyPressed(KeyEvent e) {
- switch(e.getKeyCode()){
- case KeyEvent.VK_ENTER:
- if(!loginText.getText().equals("")){
- logIn();
- break;
- }else{
- break;
- }
- }
- }
- public void keyReleased(KeyEvent e) {
- }
- public void keyTyped(KeyEvent e) {
- }
- });
- loginPanel.add(loginText);
- loginButton = new JButton("Login");
- loginButton.addActionListener(new LoginListener());
- loginPanel.add(loginButton);
- nameTag = new JLabel();
- mainPanel.add(nameTag);
- broadcastButton = new JButton("Broadcast");
- broadcastButton.addActionListener(new BroadcastListener());
- mainPanel.add(broadcastButton);
- userList = new JList<String>();
- userList.addListSelectionListener(new SelectionListener());
- userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- JScrollPane list = new JScrollPane(userList);
- userList.setPrototypeCellValue("Index 1234567");
- userList.setListData(userVector);
- mainPanel.add(list);
- mainFrame.setContentPane(mainPanel);
- mainFrame.setBounds(500,300,500,500);
- mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- mainFrame.pack();
- loginFrame.setContentPane(loginPanel);
- loginFrame.setBounds(500,300,300,100);
- loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- loginFrame.setResizable(false);
- loginFrame.setVisible(true);
- }
- public void logIn(){
- nameTag.setText(loginText.getText());
- currentUser.setName(loginText.getText());
- loginFrame.setVisible(false);
- mainFrame.setVisible(true);
- setUpNetwork();
- }
- public void setUpNetwork(){
- try{
- sock =new Socket("127.0.0.1",5003);
- InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
- reader = new BufferedReader(streamReader);
- writer = new PrintWriter(sock.getOutputStream());
- System.out.println("networking established");
- }catch(Exception e){
- e.printStackTrace();
- }
- new Thread(new StatusUpdater()).start();
- new Thread(new UserListUpdater()).start();
- new Thread(new IncomingReader()).start();
- }
- public void send(String message){
- try{
- writer.println(message);
- writer.flush();
- }catch(Exception e ){
- e.printStackTrace();
- }
- }
- public class LoginListener implements ActionListener{
- public void actionPerformed(ActionEvent event){
- if(!loginText.getText().equals("")){
- logIn();
- }else{
- return;
- }
- }
- }
- public class BroadcastListener implements ActionListener{
- public void actionPerformed(ActionEvent event){
- if(!currentDialogList.contains("broadcast")){
- DialogWindow window = new DialogWindow();
- window.init();
- currentDialogs.add(window);
- currentDialogList.add(window.getRecipient());
- }else{
- currentDialogs.get(currentDialogList.indexOf("broadcast")).setVisible(true);
- }
- }
- }
- public class ExitListener implements ActionListener{
- public void actionPerformed(ActionEvent event){
- System.exit(0);
- }
- }
- public class SelectionListener implements ListSelectionListener{
- public void valueChanged(ListSelectionEvent event){
- if(!event.getValueIsAdjusting()){
- String selection = (String) userList.getSelectedValue();
- if(selection!=null){
- if(!currentDialogList.contains(selection)){
- DialogWindow window = new DialogWindow(selection);
- window.init();
- currentDialogs.add(window);
- currentDialogList.add(window.getRecipient());
- }else{
- currentDialogs.get(currentDialogList.indexOf(selection)).setVisible(true);
- }
- }
- }
- }
- }
- public class UserListUpdater implements Runnable{
- public void run(){
- try{
- while(true){
- Socket userListSock = new Socket("127.0.0.1", 5005);
- ObjectInputStream is = new ObjectInputStream(userListSock.getInputStream());
- userVector = (Vector<String>) is.readObject();
- userVector.remove(currentUser.getName());
- userList.setListData(userVector);
- Thread.sleep(2000);
- }
- }catch(Exception e){e.printStackTrace();}
- }
- }
- public class StatusUpdater implements Runnable{
- public void run(){
- try{
- Socket statusSock = new Socket("127.0.0.1",5004);
- ObjectOutputStream os = new ObjectOutputStream(statusSock.getOutputStream());
- os.writeObject(currentUser);
- Thread.sleep(2000);
- }catch(Exception e){e.printStackTrace();}
- }
- }
- public class IncomingReader implements Runnable{
- public void run(){
- String message;
- try{
- while((message=reader.readLine()) != null){
- String[] splitted = message.split(":");
- if(splitted[1].equals("broadcast")){
- if(!currentDialogList.contains("broadcast")){
- DialogWindow window = new DialogWindow();
- window.init();
- window.incomingMessage(date.format(new Date())+" "+splitted[0]+": "+splitted[2]);
- currentDialogs.add(window);
- currentDialogList.add(window.getRecipient());
- }else{
- currentDialogs.get(currentDialogList.indexOf("broadcast")).incomingMessage(date.format(new Date())+" "+splitted[0]+": "+splitted[2]);
- currentDialogs.get(currentDialogList.indexOf("broadcast")).setVisible(true);
- }
- }else{
- if(currentUser.getName().equals(splitted[0])){
- if(!currentDialogList.contains(splitted[1])){
- DialogWindow window = new DialogWindow(splitted[1]);
- window.init();
- window.incomingMessage(date.format(new Date())+" "+splitted[0]+": "+splitted[2]);
- currentDialogs.add(window);
- currentDialogList.add(window.getRecipient());
- }else{
- currentDialogs.get(currentDialogList.indexOf(splitted[1])).incomingMessage(date.format(new Date())+" "+splitted[0]+": "+splitted[2]);
- currentDialogs.get(currentDialogList.indexOf(splitted[1])).setVisible(true);
- }
- }else{
- if(!currentDialogList.contains(splitted[0])){
- DialogWindow window = new DialogWindow(splitted[0]);
- window.init();
- window.incomingMessage(date.format(new Date())+" "+splitted[0]+": "+splitted[2]);
- currentDialogs.add(window);
- currentDialogList.add(window.getRecipient());
- }else{
- currentDialogs.get(currentDialogList.indexOf(splitted[0])).incomingMessage(date.format(new Date())+" "+splitted[0]+": "+splitted[2]);
- currentDialogs.get(currentDialogList.indexOf(splitted[0])).setVisible(true);
- }
- }
- }
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- public class DialogWindow{
- private String recipient = null;
- private String sender = null;
- private JTextArea incoming;
- private JTextField outgoing;
- private JButton sendButton;
- private JFrame messenger;
- public DialogWindow(String reciever){
- recipient = reciever;
- sender = currentUser.getName();
- }
- public DialogWindow(){
- recipient = "broadcast";
- sender = currentUser.getName();
- }
- public void incomingMessage(String message){
- incoming.append(message+"\n");
- messenger.setVisible(true);
- }
- public void setVisible(boolean state){
- messenger.setVisible(state);
- }
- public String getRecipient(){
- return recipient;
- }
- public void init(){
- messenger = new JFrame("Chat - "+recipient);
- messenger.setBounds(450,300,400,300);
- messenger.setResizable(false);
- messenger.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
- JPanel panel = new JPanel();
- incoming = new JTextArea(15,32);
- incoming.setLineWrap(true);
- incoming.setWrapStyleWord(true);
- incoming.setEditable(false);
- JScrollPane scroller = new JScrollPane(incoming);
- scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
- scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
- outgoing = new JTextField(25);
- outgoing.addKeyListener(new KeyListener(){
- public void keyPressed(KeyEvent e) {
- switch(e.getKeyCode()){
- case KeyEvent.VK_ENTER:
- send(sender+":"+recipient+":"+outgoing.getText());
- outgoing.setText("");
- outgoing.requestFocus();
- break;
- }
- }
- public void keyReleased(KeyEvent e) {
- }
- public void keyTyped(KeyEvent e) {
- }
- });
- sendButton = new JButton("Send");
- sendButton.addActionListener(new SendListener());
- panel.add(outgoing);
- panel.add(sendButton);
- messenger.getContentPane().add(scroller, BorderLayout.CENTER);
- messenger.getContentPane().add(panel, BorderLayout.SOUTH);
- messenger.setVisible(true);
- outgoing.requestFocus();
- }
- public class SendListener implements ActionListener{
- public void actionPerformed(ActionEvent event){
- send(sender+":"+recipient+":"+outgoing.getText());
- outgoing.setText("");
- outgoing.requestFocus();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement