Sie sind auf Seite 1von 13

LogInWindow: import java.awt.*; import java.awt.event.*; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.util.ArrayList; import java.util.HashMap; import javax.swing.

*; public class LogInWindow implements ActionListener{ private HashMap<JButton, ChatWindow> mapOfChatWindowAccordingToButton = new HashMap<JButton, ChatWindow>(); private HashMap<SocketAddress,String> mapOfUsers=new HashMap<SocketAddress,String>(); public ArrayList<JButton> buttonList=new ArrayList<JButton>(); //************* //variable declaration public JPanel pane; public JFrame fr = null; public boolean [] buttonClicked=new boolean[10]; public ConnectWindow c; public int flag=1; //Menu Bar for status public JMenuBar menuBar; public JMenu menu;//Status Menu public ButtonGroup bg = new ButtonGroup();//all the radio buttonList belong to this group public JRadioButtonMenuItem available; public JRadioButtonMenuItem dnd; public JRadioButtonMenuItem away; public JRadioButtonMenuItem invisible; //Menu for Sign In and Sign out public JMenu connectMenu; public ButtonGroup bgConnect = new ButtonGroup(); public JRadioButtonMenuItem connect; public JRadioButtonMenuItem terminate; public LogInWindow(){ SocketAddress SA1 = new InetSocketAddress("localhost",12731); this.mapOfUsers.put(SA1,"User1"); SocketAddress SA2 = new InetSocketAddress("localhost",12732); this.mapOfUsers.put(SA2,"User2"); SocketAddress SA3 = new InetSocketAddress("localhost",12733); this.mapOfUsers.put(SA3,"User3"); SocketAddress SA4 = new InetSocketAddress("localhost",12734); this.mapOfUsers.put(SA4,"User4");

this.Menu();//important for displaying menubar this.Frame(); c=new ConnectWindow(flag, this); c.openConnection(); } //defining the menu bar with different menus public void Menu (){ menuBar= new JMenuBar(); //Status menu is defined with 3 radio buttonList menu = new JMenu ("Status"); available = new JRadioButtonMenuItem("Available"); dnd = new JRadioButtonMenuItem("Do Not Disturb"); away = new JRadioButtonMenuItem("Away"); invisible = new JRadioButtonMenuItem("Invisible"); bg.add(available); bg.add(dnd); bg.add(away); bg.add(invisible); menu.add(available); menu.add(dnd); menu.add(away); menu.add(invisible); menuBar.add(menu); //adding action listener to the radio buttonList available.addActionListener(this); dnd.addActionListener(this); away.addActionListener(this); invisible.addActionListener(this); //the effect of the action on these button should be defined in action performed class //Connexion menu is defined with 2 options, connect and terminate connectMenu = new JMenu ("Connexion"); menuBar.add(connectMenu); connect = new JRadioButtonMenuItem("connect"); terminate = new JRadioButtonMenuItem("terminate"); bgConnect.add(connect); bgConnect.add(terminate); connectMenu.add(connect); connectMenu.add(terminate); //adding action listener to the radio buttonList connect.addActionListener(this); terminate.addActionListener(this); } //defining the panel to display the buttons

public JPanel Panel(){ JPanel pane = new JPanel(); pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); int i=0; for(String s: this.mapOfUsers.values()){ buttonList.add(i,new JButton(s)); pane.add(buttonList.get(i)); buttonList.get(i).addActionListener(this); i++; } return pane; } // the panel is added to a frame for display public JFrame Frame(){ fr=new JFrame("WeChat"); fr.setContentPane(this.Panel()); // Use our pane as the default pane fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exit program when frame is closed fr.setJMenuBar(menuBar); //fr.setContentPane(menuBar); this.fr.setBounds (0,0,250,400); fr.setVisible(true); // Make it visible return fr; } public static void main (String args[]){ LogInWindow tm = new LogInWindow(); }

public int getFlag() { return flag; } public void setFlag(int flag) { this.flag = flag; } public void actionPerformed(ActionEvent e) { //define the outcome of clicking the radio button of Connexion if(e.getSource() == connect) { this.c.openConnection(); System.out.println("Connect"); // WP.display() }

if(e.getSource() == terminate) { this.c.closeConnection(); System.out.println("Terminate"); } //define the outcome of clicking the radio button of STATUS if(e.getSource() == dnd) { System.out.println("Do Not Disturb"); } if(e.getSource() == available) { System.out.println("Available"); } if(e.getSource() == away) { System.out.println("Away"); } if(e.getSource() == invisible) { System.out.println("Invisible"); } for (JButton j : this.buttonList) { if (e.getSource() == j) { ChatWindow c = this.mapOfChatWindowAccordingToButton.get(j); if (c == null) { c = new ChatWindow(); c.setName(j.getText()); c.setClick(1, this); c.display(); this.mapOfChatWindowAccordingToButton.put(j, c); break; } else if (!c.chatFrame.isShowing()) { c.setVisible(true); c.display(); } } } } public void getClick(int clickedButton) { buttonClicked[clickedButton]=false; } }

ConnectWindow: import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class ConnectWindow implements ActionListener { public JTextField firstName, lastName, nickName, port, displayName, messageStatus; public JLabel firstNameLabel, lastNameLabel, nickNameLabel, portLabel, displayNameLabel,statusMessageLabel; public JButton okButton, cancelButton; public JPanel paneField, paneButton, paneFrame; public JFrame frame; public String nameFirst, nameLast, nameNick, numberPort, nameDisplay, statusMessage; public int flagConnect; public LogInWindow liw; public ConnectWindow(int flag, LogInWindow logInWindow){ this.liw=logInWindow; this.flagConnect=flag; } public void logInInfo(){ firstName = new JTextField(20); firstNameLabel = new JLabel("First Name"); //firstName.setName("First Name"); firstName.addActionListener(this); lastName = new JTextField(20); lastNameLabel = new JLabel("Last Name"); lastName.addActionListener(this); nickName = new JTextField(20); nickNameLabel = new JLabel("Nick Name"); nickName.addActionListener(this); displayName = new JTextField(20); displayNameLabel = new JLabel("Display Name"); displayName.addActionListener(this); messageStatus = new JTextField(20);

statusMessageLabel = new JLabel("Set Status"); //nickName.setName("First Name"); messageStatus.addActionListener(this); port = new JTextField(5); portLabel = new JLabel("Port"); //port.setName("First Name"); port.addActionListener(this); okButton = new JButton("OK"); cancelButton = new JButton("Cancel"); okButton.addActionListener(this); cancelButton.addActionListener(this); } public void display(){ paneField = new JPanel(); //paneField = new JPanel(new GridLayout(4,1)); paneField.setLayout(new BoxLayout(paneField, BoxLayout.Y_AXIS)); paneField.add(firstNameLabel); paneField.add(firstName); paneField.add(lastNameLabel); paneField.add(lastName); paneField.add(nickNameLabel); paneField.add(nickName); paneField.add(displayNameLabel); paneField.add(displayName); paneField.add(statusMessageLabel); paneField.add(messageStatus); paneField.add(portLabel); paneField.add(port); //pane for Buttons paneButton = new JPanel(); paneButton.add(okButton); paneButton.add(cancelButton); //main pane to contain both paneFrame = new JPanel(); //paneFrame.setLayout(new BoxLayout(paneField, BoxLayout.Y_AXIS)); paneFrame.setPreferredSize(new Dimension(250,280)); paneFrame.add(paneField, BorderLayout.NORTH); paneFrame.add(paneButton, BorderLayout.CENTER); frame = new JFrame("Connection Info"); frame.setContentPane(paneFrame); frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); frame.setLocation(400, 200); // located at (600, 200) frame.pack(); // Frame is ready. Pack it up for display.

frame.setVisible(true); } /* public int getFlagConnect() { return flagConnect; } public void openConnection(){ if (this.flagConnect == 1){ this.logInInfo(); this.display(); } if (this.flagConnect == 0){ frame.setVisible(true); } } public void closeConnection(){ frame.dispose(); } public static void main (String args[]){ //ConnectWindow cw = new ConnectWindow(1); } @Override public void actionPerformed(ActionEvent evt) { if (evt.getSource() == okButton){ this.flagConnect=0; this.liw.setFlag(flagConnect); System.out.println("hey"); nameFirst=firstName.getText(); nameLast=lastName.getText(); nameNick=nickName.getText(); nameDisplay=displayName.getText(); statusMessage=messageStatus.getText(); numberPort=port.getText(); frame.setVisible(false); } if (evt.getSource() == cancelButton){ this.flagConnect=1; this.liw.setFlag(flagConnect); this.closeConnection(); //frame.dispose(); } } }

*/

ChatWindow: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Frame; import java.awt.event.KeyEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import java.awt.event.*; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class ChatWindow extends JPanel implements ActionListener, WindowListener { public JTextArea chatText = new JTextArea();//Area where all the chats are displayed public JTextField chatLine;//to enter the message public JFrame chatFrame; public JButton phoneButton; public JButton videoButton; public JButton hangButton; public JButton saveChat; private final static String newline = "\n"; int clickedButton=0; LogInWindow liw=null; String otherUserName; String whoAmI; public ChatWindow(){ chatFrame = new JFrame(this.otherUserName); chatFrame.addWindowListener(this); } public void display (){ chatLine = new JTextField(); //chatLine.setName("chat"); chatLine.addActionListener(this);

JPanel chatPane = new JPanel(new BorderLayout()); chatText.setLineWrap(true);//so that long line can be shown in more than one line chatText.setEditable(false);//entered chat cannot be edited back chatText.setForeground(Color.gray); //scroll pane JScrollPane chatTextPane = new JScrollPane(chatText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); JPanel dialPane= new JPanel(); phoneButton = new JButton("Call"); phoneButton.setBackground(Color.green); videoButton = new JButton("Video"); videoButton.setBackground(Color.cyan); hangButton = new JButton("End"); hangButton.setEnabled(false); hangButton.setBackground(Color.red); saveChat= new JButton("save"); saveChat.addActionListener(this); dialPane.add(phoneButton); phoneButton.addActionListener(this); dialPane.add(videoButton); videoButton.addActionListener(this); dialPane.add(hangButton); hangButton.addActionListener(this); dialPane.add(saveChat); chatPane.add(dialPane, BorderLayout.NORTH); //chatLine.setEnabled(true); chatPane.add(chatLine, BorderLayout.SOUTH); chatPane.add(chatTextPane, BorderLayout.CENTER); chatPane.setPreferredSize(new Dimension(300,300)); chatFrame.setContentPane(chatPane); // chat pane for user chatFrame.setLocation(600, 200); // located at (600, 200) chatFrame.pack(); // Frame is ready. Pack it up for display. chatFrame.setVisible(true); // Make it visible } // for saving the chat transcript private void doSave() { JFileChooser fileDialog = new JFileChooser(); File selectedFile; //Initially selected file name in the dialog. selectedFile = new File("transcript.txt"); fileDialog.setSelectedFile(selectedFile); fileDialog.setDialogTitle("Select File to be Saved");

int option = fileDialog.showSaveDialog(this); if (option != JFileChooser.APPROVE_OPTION) return; // User canceled or clicked the dialog's close box. selectedFile = fileDialog.getSelectedFile(); if (selectedFile.exists()) { // Ask the user whether to replace the file. int response = JOptionPane.showConfirmDialog( this, "The file \"" + selectedFile.getName() + "\" already exists.\nDo you want to replace it?", "Confirm Save", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE ); if (response != JOptionPane.YES_OPTION) return; // User does not want to replace the file. } PrintWriter out; try { FileWriter stream = new FileWriter(selectedFile); out = new PrintWriter( stream ); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Sorry, but an error occurred while trying to open the file:\n" + e); return; } try { out.print(chatText.getText()); // Write text from the TextArea to the file. out.close(); if (out.checkError()) // (need to check for errors in PrintWriter) throw new IOException("Error check failed."); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Sorry, but an error occurred while trying to write the text:\n" + e); } } //this function takes the text entered into text field //CAN BE USED FOR SENDING THE ENETERD CHAT TO THE CONTROLLER private void sendMessage(String message){ this.controller.sendMessage(message); //System.out.println("hurray"); } //This set the info about itself public void setWhoAmI(String whoAmI){ this.whoAmI = whoAmI; } //Message recieved from the otherend public void newReceivedMessage(String message, String from){ // do stuff for displayin the

//

chatText.append(from+": "+message + newline); chatLine.selectAll(); } public void setClick(int i, LogInWindow tm) { clickedButton=i; this.liw=tm; } //the name of the other end user its chatting with for displaying it on frame header public void setName(String name){ this.otherUserName=name; } // this function is called when the chat window is closed public void closingChatWindow(){ System.out.println("Window closed"); } /*public void restore(){ chatFrame.setState(Frame.NORMAL); } */ public void actionPerformed(ActionEvent evt) { if(evt.getSource()== phoneButton){ phoneButton.setEnabled(false); videoButton.setEnabled(false); hangButton.setEnabled(true); //write the code for voice chat } if(evt.getSource()== videoButton){ phoneButton.setEnabled(false); videoButton.setEnabled(false); hangButton.setEnabled(true); //write the code for video chat } if(evt.getSource()== hangButton){ phoneButton.setEnabled(true); videoButton.setEnabled(true); hangButton.setEnabled(false); //write the code for ending chat } // for saving the transcript if(evt.getSource()== saveChat){ doSave();

} if(evt.getSource()== chatLine){ //entering the text String text = chatLine.getText(); this.sendMessage(text); chatText.append("me"+": "+text + newline); //it also enters newline i.e. just pressing enter chatLine.selectAll(); } } //window closing event public void windowClosing(WindowEvent e) { System.out.println("WindowListener method called: windowClosing."); this.closingChatWindow(); //A pause so user can see the message before //the window actually closes. ActionListener task = new ActionListener() { boolean alreadyDisposed = false; public void actionPerformed(ActionEvent e) { if (!alreadyDisposed) { alreadyDisposed = true; chatFrame.dispose(); } else { //make sure the program exits System.exit(0); } } }; } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub //System.out.println("I'm closing"); //this.closingChatWindow(); } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub

} @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub }

Das könnte Ihnen auch gefallen