/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * ChatWithServerJFrame.java * * Created on Jul 29, 2010, 2:16:41 PM */ package com.server.gui; import com.server.conn.ClientManager; import com.server.conn.ConnectionManager; import com.server.voicechat.VoiceChat; import java.awt.Color; import java.awt.event.KeyEvent; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; /** * * @author TF */ public class ChatWithClientJFrame extends javax.swing.JFrame { /** Creates new form ChatWithServerJFrame */ private String ipAddress = ""; private final String DEFAULT_MSG_TXT = "Type Message here..."; private final Color DEFAULT_MSG_TXT_COLOR = Color.LIGHT_GRAY; SimpleAttributeSet attrSet = null; Document doc = null; ClientManager clientManager = null; public ChatWithClientJFrame(ClientManager clientManager) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); SwingUtilities.updateComponentTreeUI(this); initComponents(); this.setIconImage(new ImageIcon(getClass().getResource( "/com/server/icons/chat.png")).getImage()); this.clientManager = clientManager; attrSet = new SimpleAttributeSet(); doc = receiveTextPane.getStyledDocument(); msgTextField.setText(DEFAULT_MSG_TXT); msgTextField.setForeground(DEFAULT_MSG_TXT_COLOR); msgTextField.requestFocus(); } catch (ClassNotFoundException ex) { Logger.getLogger(ChatWithClientJFrame.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(ChatWithClientJFrame.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(ChatWithClientJFrame.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedLookAndFeelException ex) { Logger.getLogger(ChatWithClientJFrame.class.getName()).log(Level.SEVERE, null, ex); } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { msgTextField = new javax.swing.JTextField(); sendButton = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); receiveTextPane = new javax.swing.JTextPane(); voiceChatToggleButton = new javax.swing.JToggleButton(); setResizable(false); msgTextField.setFont(new java.awt.Font("Tahoma", 1, 12)); msgTextField.setForeground(new java.awt.Color(51, 0, 51)); msgTextField.setText("Type Message"); msgTextField.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { msgTextFieldKeyPressed(evt); } public void keyReleased(java.awt.event.KeyEvent evt) { msgTextFieldKeyReleased(evt); } }); sendButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/server/icons/send-1.PNG"))); // NOI18N sendButton.setText("Send"); sendButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { sendButtonActionPerformed(evt); } }); receiveTextPane.setEditable(false); jScrollPane1.setViewportView(receiveTextPane); voiceChatToggleButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/server/icons/kmixdocked_mute.png"))); // NOI18N voiceChatToggleButton.setText("Start Talk"); voiceChatToggleButton.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { voiceChatToggleButtonItemStateChanged(evt); } }); voiceChatToggleButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { voiceChatToggleButtonActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 542, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(msgTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(sendButton, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)) .addComponent(voiceChatToggleButton)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(voiceChatToggleButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(sendButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(msgTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents private void msgTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_msgTextFieldKeyPressed if (msgTextField.getText().equals(DEFAULT_MSG_TXT)) { msgTextField.setText(""); msgTextField.setForeground(new java.awt.Color(51, 0, 51)); } else if (msgTextField.getText().equals("")) { // msgTextField.setText(DEFAULT_MSG_TXT); msgTextField.setForeground(DEFAULT_MSG_TXT_COLOR); } }//GEN-LAST:event_msgTextFieldKeyPressed private void msgTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_msgTextFieldKeyReleased if (msgTextField.getText().equals(DEFAULT_MSG_TXT)) { msgTextField.setText(""); msgTextField.setForeground(new java.awt.Color(51, 0, 51)); } else if (msgTextField.getText().equals("")) { msgTextField.setText(DEFAULT_MSG_TXT); msgTextField.setForeground(DEFAULT_MSG_TXT_COLOR); } if (evt.getKeyCode() == KeyEvent.VK_ENTER) { sendMsg(); msgTextField.setText(""); } }//GEN-LAST:event_msgTextFieldKeyReleased private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed sendMsg(); msgTextField.setText(""); }//GEN-LAST:event_sendButtonActionPerformed private void voiceChatToggleButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_voiceChatToggleButtonItemStateChanged }//GEN-LAST:event_voiceChatToggleButtonItemStateChanged private void voiceChatToggleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_voiceChatToggleButtonActionPerformed if (voiceChatToggleButton.isSelected()) { startTalk(); } else { stopTalk(); } }//GEN-LAST:event_voiceChatToggleButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextField msgTextField; private javax.swing.JTextPane receiveTextPane; private javax.swing.JButton sendButton; private javax.swing.JToggleButton voiceChatToggleButton; // End of variables declaration//GEN-END:variables private void sendMsg() { String msg = msgTextField.getText(); if (!msg.equals("")) { if (!msg.equals(DEFAULT_MSG_TXT)) { try { if (clientManager != null) { clientManager.sendRequestToClient("MESSAGE:" + msg); StyleConstants.setForeground(attrSet, Color.GRAY); StyleConstants.setBold(attrSet, true); doc.insertString(doc.getLength(), "Me: ", attrSet); StyleConstants.setForeground(attrSet, new Color(2, 24, 106)); StyleConstants.setBold(attrSet, true); doc.insertString(doc.getLength(), msg + "\n", attrSet); } else { StyleConstants.setForeground(attrSet, Color.RED); StyleConstants.setBold(attrSet, true); doc.insertString(doc.getLength(), "Client is offline\n", attrSet); } } catch (BadLocationException ex) { Logger.getLogger(ChatWithClientJFrame.class.getName()).log(Level.SEVERE, null, ex); } } } } public void viewSendReceiveMsg(String msg) { try { StyleConstants.setForeground(attrSet, Color.BLACK); StyleConstants.setBold(attrSet, true); doc.insertString(doc.getLength(), clientManager.getIpAddress() + ": ", attrSet); StyleConstants.setForeground(attrSet, new Color(130, 7, 13)); StyleConstants.setBold(attrSet, true); doc.insertString(doc.getLength(), msg + "\n", attrSet); } catch (BadLocationException ex) { Logger.getLogger(ChatWithClientJFrame.class.getName()).log(Level.SEVERE, null, ex); } } public void close() { this.clientManager = null; this.dispose(); } public void startTalk() { if (ConnectionManager.voiceChat == null) { ConnectionManager.voiceChat = new VoiceChat(clientManager.getIpAddress(), this); } else { if (ConnectionManager.voiceChat.isChating() && JOptionPane.showConfirmDialog(this, "Now you are chatting with " + ConnectionManager.voiceChat.getIpAddress() + "\nIf you proceed it will terminate" + "Do you want to continue?", "Voice Chat", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.NO_OPTION) { return; } ConnectionManager.voiceChat.stopCapture(); ConnectionManager.voiceChat = new VoiceChat(clientManager.getIpAddress(), this); } ConnectionManager.voiceChat.startCapture(); voiceChatToggleButton.setSelected(true); voiceChatToggleButton.setText("Stop Talk"); voiceChatToggleButton.setIcon(new ImageIcon(getClass().getResource( "/com/server/icons/microphone-icon.png"))); } public void stopTalk() { if (ConnectionManager.voiceChat != null) { ConnectionManager.voiceChat.stopCapture(); ConnectionManager.voiceChat = null; } voiceChatToggleButton.setText("Start Talk"); voiceChatToggleButton.setIcon(new ImageIcon(getClass().getResource( "/com/server/icons/kmixdocked_mute.png"))); voiceChatToggleButton.setSelected(false); } }