/*
* 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.client.gui;
import com.client.conn.ConnectionManager;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
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 S A F I
*/
public class ChatWithServerJFrame extends javax.swing.JFrame {
/** Creates new form ChatWithServerJFrame */
private final String DEFAULT_MSG_TXT = " ";
private final Color DEFAULT_MSG_TXT_COLOR = Color.LIGHT_GRAY;
SimpleAttributeSet attrSet = null;
Document doc = null;
ClientMain clientMain = null;
public ChatWithServerJFrame(ClientMain clientMain) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
initComponents();
setIconImage(new ImageIcon(this.getClass().getResource("/com/client/icons/chat-4.png")).getImage());
this.clientMain = clientMain;
attrSet = new SimpleAttributeSet();
doc = receiveTextPane.getStyledDocument();
msgTextField.setText(DEFAULT_MSG_TXT);
msgTextField.setForeground(DEFAULT_MSG_TXT_COLOR);
msgTextField.requestFocus();
} catch (ClassNotFoundException ex) {
Logger.getLogger(ChatWithServerJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(ChatWithServerJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(ChatWithServerJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(ChatWithServerJFrame.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();
talkToggleButton = new javax.swing.JToggleButton();
setTitle("Chat With Administrator");
setResizable(false);
msgTextField.setFont(new java.awt.Font("Vrinda", 1, 14));
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/client/icons/images.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);
talkToggleButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/client/icons/kmixdocked_mute.png"))); // NOI18N
talkToggleButton.setText("Start Talk");
talkToggleButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
talkToggleButtonActionPerformed(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(javax.swing.GroupLayout.Alignment.TRAILING, 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(talkToggleButton))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(7, 7, 7)
.addComponent(talkToggleButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(sendButton, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
.addComponent(msgTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 44, 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 talkToggleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_talkToggleButtonActionPerformed
if (talkToggleButton.isSelected()) {
try {
viewInformation("Please wait.... \n Admin has to approve your request.");
talkToggleButton.setEnabled(false);
clientMain.connectionManager.sendDataToServer("VOICE_CHAT");
} catch (IOException ex) {
talkToggleButton.setEnabled(true);
talkToggleButton.setSelected(true);
viewErreorMsg("Request Sending faild");
}
} else {
stopTalk();
}
}//GEN-LAST:event_talkToggleButtonActionPerformed
// 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 talkToggleButton;
// End of variables declaration//GEN-END:variables
private void sendMsg() {
String msg = msgTextField.getText();
if (!msg.equals("")) {
if (!msg.equals(DEFAULT_MSG_TXT)) {
try {
clientMain.connectionManager.sendDataToServer(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);
msgTextField.setText("");
} catch (IOException ex) {
Logger.getLogger(ChatWithServerJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (BadLocationException ex) {
Logger.getLogger(ChatWithServerJFrame.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(), "Administrator: ", 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(ChatWithServerJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
this.setState(java.awt.Frame.NORMAL);
}
public void stopTalk() {
clientMain.listenForVoiceChat.stopTalk();
talkToggleButton.setSelected(false);
talkToggleButton.setText("Start Talk");
talkToggleButton.setIcon(new ImageIcon(getClass().getResource(
"/com/client/icons/kmixdocked_mute.png")));
this.setVisible(true);
this.setState(Frame.NORMAL);
}
public void startChat() {
if (!talkToggleButton.isEnabled()) {
talkToggleButton.setEnabled(true);
viewInformation("Admin has accepted your request.");
} else {
viewInformation("Administrator comming for voice chat");
}
talkToggleButton.setText("Stop Talk");
talkToggleButton.setSelected(true);
talkToggleButton.setIcon(new ImageIcon(getClass().getResource(
"/com/client/icons/microphone-icon.png")));
this.setVisible(true);
this.setState(Frame.NORMAL);
}
public void viewErreorMsg(String errorMsg) {
try {
if (!talkToggleButton.isEnabled()) {
talkToggleButton.setEnabled(true);
}
StyleConstants.setForeground(attrSet, Color.RED);
StyleConstants.setBold(attrSet, true);
doc.insertString(doc.getLength(), errorMsg + "\n", attrSet);
} catch (BadLocationException ex) {
Logger.getLogger(ChatWithServerJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void viewInformation(String msg) {
try {
StyleConstants.setForeground(attrSet, Color.GREEN);
StyleConstants.setBold(attrSet, true);
doc.insertString(doc.getLength(), msg + "\n", attrSet);
} catch (BadLocationException ex) {
Logger.getLogger(ChatWithServerJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}