/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* ServerGUI.java
*
* Created on Sep 6, 2008, 6:50:52 PM
*/
package PChatServer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Sunny
*/
public class ServerGUI extends javax.swing.JFrame {
SimpleDateFormat simpleDate = new SimpleDateFormat("HH:mm:ss");
public Calendar cal;
public Server server;
public Channel lastSelected;
public Client lastClient;
/** Creates new form ServerGUI */
public ServerGUI() {
initComponents();
server = new Server();
}
/** 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() {
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
serverMsg = new javax.swing.JTextArea();
Start = new javax.swing.JButton();
Stop = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
Kick = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
portField = new javax.swing.JTextField();
jScrollPane2 = new javax.swing.JScrollPane();
channels = new javax.swing.JList();
jScrollPane3 = new javax.swing.JScrollPane();
Chat = new javax.swing.JTextArea();
jLabel5 = new javax.swing.JLabel();
msgBox = new javax.swing.JTextField();
Send = new javax.swing.JButton();
password = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
jScrollPane4 = new javax.swing.JScrollPane();
users = new javax.swing.JList();
room = new javax.swing.JLabel();
kickClient = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("PrivateChat Server");
jLabel1.setText("Server Status");
jScrollPane1.setAutoscrolls(true);
serverMsg.setColumns(20);
serverMsg.setRows(5);
jScrollPane1.setViewportView(serverMsg);
Start.setText("Start Server");
Start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
StartActionPerformed(evt);
}
});
Stop.setText("Stop Server");
Stop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
StopActionPerformed(evt);
}
});
Kick.setText("Kick All");
Kick.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
KickActionPerformed(evt);
}
});
jLabel3.setText("Port:");
jLabel4.setText("Channels");
portField.setText("13370");
portField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
portFieldActionPerformed(evt);
}
});
jScrollPane2.setAutoscrolls(true);
channels.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "No Channels" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
channels.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
channelsValueChanged(evt);
}
});
channels.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
channelsFocusGained(evt);
}
});
jScrollPane2.setViewportView(channels);
jScrollPane3.setAutoscrolls(true);
Chat.setColumns(20);
Chat.setLineWrap(true);
Chat.setRows(5);
jScrollPane3.setViewportView(Chat);
jLabel5.setText("Chat");
msgBox.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
msgBoxKeyTyped(evt);
}
});
Send.setText("Send");
Send.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SendActionPerformed(evt);
}
});
password.setText("0");
jLabel6.setText("Password:");
jScrollPane4.setAutoscrolls(true);
users.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "No Clients" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane4.setViewportView(users);
room.setText("Clients");
kickClient.setText("Kick");
kickClient.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
kickClientActionPerformed(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()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(kickClient)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(msgBox, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(Send, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(room))
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(237, 237, 237))
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27)
.addComponent(jLabel6)
.addGap(18, 18, 18)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(89, 89, 89))
.addGroup(layout.createSequentialGroup()
.addComponent(Start, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Stop, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Kick, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 273, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(229, 229, 229))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(80, 80, 80)
.addComponent(jLabel2))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Stop)
.addComponent(Kick))
.addComponent(Start)))
.addComponent(jScrollPane2))
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(room))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane4, 0, 0, Short.MAX_VALUE)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(Send)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(msgBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(kickClient)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void KickActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_KickActionPerformed
// TODO add your handling code here:
ProjectHandler.kick = true;
updateUsers();
}//GEN-LAST:event_KickActionPerformed
private void portFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_portFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_portFieldActionPerformed
private void StartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartActionPerformed
// TODO add your handling code here:
if (!server.online) {
ProjectHandler.GUI = this;
server.port = Integer.parseInt(portField.getText());
server.password = password.getText();
server.startServer();
}
}//GEN-LAST:event_StartActionPerformed
private void channelsFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_channelsFocusGained
// TODO add your handling code here:
}//GEN-LAST:event_channelsFocusGained
private void SendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SendActionPerformed
// TODO add your handling code here:
lastSelected.msgToAll(msgBox.getText() + " ", "Server");
updateChat();
msgBox.setText("");
}//GEN-LAST:event_SendActionPerformed
private void StopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StopActionPerformed
// TODO add your handling code here:
ProjectHandler.clients.killAll();
server.stopServer();
users.clearSelection();
channels.clearSelection();
serverMsg("Server has now stopped!");
}//GEN-LAST:event_StopActionPerformed
private void msgBoxKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_msgBoxKeyTyped
// TODO add your handling code here:
if (evt.getKeyChar() == '\n') {
SendActionPerformed(null);
}
}//GEN-LAST:event_msgBoxKeyTyped
private void channelsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_channelsValueChanged
// TODO add your handling code here:
if (server.online) {
updateChat();
updateUsers();
}
}//GEN-LAST:event_channelsValueChanged
private void kickClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kickClientActionPerformed
// TODO add your handling code here:
Client temp = (Client) users.getSelectedValue();
if (temp == null) {
return;
}
temp.disconnect();
}//GEN-LAST:event_kickClientActionPerformed
/**
* @param args the command line arguments
*/
@SuppressWarnings("static-access")
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ServerGUI().setVisible(true);
}
});
}
public void updateChannels() {
channels.clearSelection();
ArrayList temp = new ArrayList();
for (Channel c : ProjectHandler.channels.channels) {
if (c != null) {
temp.add(c);
}
}
temp.trimToSize();
channels.setListData(temp.toArray());
//channels.setListData(ProjectHandler.channels.channels);
}
public void updateChat() {
Channel selected = (Channel) channels.getSelectedValue();
if (selected != null) {
lastSelected = selected;
Chat.setText(selected.chatToClient());
} else {
Chat.setText(lastSelected.chatToClient());
}
Chat.setCaretPosition(Chat.getText().length());
}
public void updateUsers() {
users.clearSelection();
users.setListData(lastSelected.clients.toArray());
room.setText(lastSelected.name);
}
/**
* @param msg Message for server box
*/
public void serverMsg(String msg) {
cal = Calendar.getInstance();
serverMsg.append("[" + simpleDate.format(cal.getTime()) + "]: " + msg + "\n");
serverMsg.setCaretPosition(serverMsg.getText().length());
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextArea Chat;
private javax.swing.JButton Kick;
private javax.swing.JButton Send;
private javax.swing.JButton Start;
private javax.swing.JButton Stop;
private javax.swing.JList channels;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JButton kickClient;
private javax.swing.JTextField msgBox;
public javax.swing.JTextField password;
private javax.swing.JTextField portField;
private javax.swing.JLabel room;
private javax.swing.JTextArea serverMsg;
private javax.swing.JList users;
// End of variables declaration//GEN-END:variables
}