MerAll

MerChatApp:clientside:MerChatWin

Aug 1st, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.23 KB | None | 0 0
  1. package clientside;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7. import java.awt.event.WindowEvent;
  8. import java.util.Observable;
  9. import java.util.Observer;
  10.  
  11. import javax.swing.GroupLayout;
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JTextArea;
  17. import javax.swing.JTextField;
  18. import javax.swing.LayoutStyle;
  19. import javax.swing.WindowConstants;
  20.  
  21. import serverside.MerChatLog;
  22.  
  23. /**Window representing a 2-way chat, as it appears to one of the participants.
  24.  * Each participant in the chat will have such a window open on their machine.
  25.  *
  26.  * @author Meredith Allen
  27.  */
  28. public class MerChatWin extends JFrame implements Observer {
  29.    
  30.     MerChatClient theClient;
  31.     MerChatLog theLog;
  32.    
  33.     private JButton jButton1;
  34.     private JButton jButton2;
  35.     private JLabel jLabel1;
  36.     private JScrollPane jScrollPane1;
  37.     private JTextArea jTextArea1;
  38.     private JTextField jTextField1;
  39.  
  40.     public MerChatWin(MerChatClient currentClient,MerChatLog currentLog) {
  41.         this.theClient=currentClient;
  42.         this.theLog=currentLog;
  43.         this.theLog.addObserver(this);
  44.         jButton1 = new JButton();
  45.         jButton2 = new JButton();
  46.         jButton2.addActionListener(new ActionListener() {
  47.             @Override
  48.         public void actionPerformed(ActionEvent e) {
  49.                 System.out.println("hit view log.");
  50.             }
  51.         });
  52.         jScrollPane1 = new JScrollPane();
  53.         jTextArea1 = new JTextArea();
  54.         jTextArea1.setEditable(false);
  55.         jTextField1 = new JTextField();
  56.         jTextField1.addKeyListener(new KeyListener() {
  57.  
  58.         @Override
  59.         public void keyTyped(KeyEvent e) {
  60.         // TODO Auto-generated method stub
  61.        
  62.         }
  63.  
  64.         @Override
  65.         public void keyPressed(KeyEvent e) {
  66.         if(e.getKeyCode()==KeyEvent.VK_ENTER)
  67.         {
  68.             MerChatWin.this.jButton1ActionPerformed(null);
  69.         }
  70.        
  71.         }
  72.  
  73.         @Override
  74.         public void keyReleased(KeyEvent e) {
  75.         // TODO Auto-generated method stub
  76.        
  77.         }
  78.        
  79.      
  80.         });
  81.         jLabel1 = new JLabel();
  82.      
  83.         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  84.  
  85.         jButton1.setText("Send");
  86.         jButton1.addActionListener(new ActionListener() {
  87.             @Override
  88.         public void actionPerformed(ActionEvent evt) {
  89.                 jButton1ActionPerformed(evt);
  90.             }
  91.         });
  92.  
  93.         jButton2.setText("View Log");
  94.         jButton2.addActionListener(new ActionListener() {
  95.             @Override
  96.         public void actionPerformed(ActionEvent evt) {
  97.                 jButton12ActionPerformed(evt);
  98.             }
  99.  
  100.        
  101.         });
  102.  
  103.         jTextArea1.setColumns(20);
  104.         jTextArea1.setRows(5);
  105.         jScrollPane1.setViewportView(jTextArea1);
  106.  
  107.         jTextField1.setText("jTextField1");
  108.  
  109.         jLabel1.setText("Conversation between $User1 and $User2");
  110.  
  111.         GroupLayout layout = new GroupLayout(getContentPane());
  112.         getContentPane().setLayout(layout);
  113.         layout.setHorizontalGroup(
  114.             layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  115.             .addGroup(layout.createSequentialGroup()
  116.                 .addContainerGap()
  117.                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  118.                     .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  119.                         .addGap(0, 0, Short.MAX_VALUE)
  120.                         .addComponent(jButton1)
  121.                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  122.                         .addComponent(jButton2))
  123.                     .addComponent(jScrollPane1)
  124.                     .addComponent(jTextField1)
  125.                     .addGroup(layout.createSequentialGroup()
  126.                         .addComponent(jLabel1)
  127.                         .addGap(0, 92, Short.MAX_VALUE)))
  128.                 .addContainerGap())
  129.         );
  130.         layout.setVerticalGroup(
  131.             layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  132.             .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  133.                 .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  134.                 .addComponent(jLabel1)
  135.                 .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
  136.                 .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 186, GroupLayout.PREFERRED_SIZE)
  137.                 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  138.                 .addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  139.                 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  140.                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  141.                     .addComponent(jButton1)
  142.                     .addComponent(jButton2))
  143.                 .addContainerGap())
  144.         );
  145.  
  146.         pack();
  147.         this.jLabel1.setText(String.format("Conversation between %s and %s.",this.theLog.getSelf(),this.theLog.getOtherContact()));
  148.         this.jTextArea1.setText(this.theLog.getLogAsString());
  149.         this.setVisible(true);
  150.     }
  151.  
  152.    
  153.     /**Send message buttion action*/
  154.     private void jButton1ActionPerformed(ActionEvent evt) {                                        
  155.        System.out.printf("hit send. Text field text is %s. Text area text is %s.%n",this.jTextField1.getText(),
  156.            this.jTextArea1.getText());
  157.        this.theLog.logMessage(this.theClient, this.jTextField1.getText());
  158.        this.jTextField1.setText("");
  159.        
  160.     }    
  161.     /**View Log button action*/
  162.     private void jButton12ActionPerformed(ActionEvent evt) {
  163.     MerChatLogViewer mclv = new MerChatLogViewer(this.theLog);
  164.     }
  165.    
  166.     /**Method to facilitate closing the window programmatically*/
  167.     public void closeWindow()
  168.     {
  169.     this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
  170.     }
  171.  
  172.  
  173.     /**Observing MerChatLog instance; will update window accordingly*/
  174.     @Override
  175.     public void update(Observable o, Object arg) {
  176.     this.jTextArea1.setText(this.theLog.getLogAsString());
  177.     }
  178.                    
  179. }
Advertisement
Add Comment
Please, Sign In to add comment