import org.amdesign.beans.systrayicon.SysTrayIcon;
import org.amdesign.beans.systrayicon.SysTrayIconException;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
public class ChatServer extends JFrame implements MouseListener, WindowListener, ActionListener
{
//Instanz von AudioKlasse
Audio au = new Audio();
ChatServerPanel csp;
public static LogoPanel logo;
private static int counter = 0;
private ServerSocket server;
static Vector vecClient = new Vector();
boolean stopchat = true;
//SysTray -------------------------------------------------------------------------------
SysTrayIcon sysTray = new SysTrayIcon(); // Instanziert die Methode für den SystemTray unter Windows
String sysTrayIcon = "org/amdesign/beans/systrayicon/resources/images/default.ico";
String sysTrayIcon2 = "org/amdesign/beans/systrayicon/resources/images/tray.ico";
String sysTrayToolTip = " Chat - Server 1.0 - von Ronny Ewert ";
boolean minArg = false;
//----------------------------------------------------------------------------------------
JButton start,stop;
JTextField jt;
int portnr = 5555;
String portadresse = "5555";
ClientConnect clientc;
public ChatServer() throws Exception
{
fensterkomponenten();
au.clip3.play();
server = new ServerSocket(portnr);
runChat();
}
private void fensterkomponenten()
{
setSize(350,330);
setVisible(true);
setResizable(false);
setLocation(100,160);
setTitle("Chat-Server - Clients: "+counter);
// schließt Fester noch nicht, wartet auf WindowListener
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.getContentPane().setLayout(null);
setzeIcon();//rechts für die Kaffeetasse
logo = new LogoPanel();
//logo.setBounds(0,0,350,320);
logo.setPreferredSize(new Dimension(350,320));
logo.addMouseListener(this);
logo.setToolTipText("Klick - Chat anzeigen");
logo.setSize(350,320);
//Instanz Panel
csp = new ChatServerPanel(portadresse);
//csp.setBounds(0,0,350,320);
csp.setPreferredSize(new Dimension(350,320));
csp.setVisible(false);
csp.addMouseListener(this);
csp.setToolTipText("Doppelklick -Chat verstecken ");
csp.setSize(350,320);
csp.reset();
//----Start Stop Buttons---------------------------------
start = new JButton("Start");
start.setBounds(10,30,70,20);
start.setSize(70,20);
start.addActionListener(this);
start.setEnabled(false);
stop = new JButton("Stop");
stop.setBounds(10,60,70,20);
stop.setSize(70,20);
stop.addActionListener(this);
jt = new JTextField("5555");
jt.setBounds(10,90,70,20);
jt.setSize(70,20);
jt.setEnabled(false);
//-------------------------------------------------
//SystemTrayMethode
mytray();
addWindowListener(this);
this.getContentPane().add(jt);
this.getContentPane().add(start);
this.getContentPane().add(stop);
this.getContentPane().add(logo);
this.getContentPane().add(csp);
//Swing dadurch gleich sichtbar-ähnlich repaint()
validate();
}
//innere Klasse-----------------------------------------------------------------------------
class ClientConnect extends Thread
{
String name;
BufferedReader reader;
BufferedWriter writer;
public ClientConnect(Socket client) throws IOException
{
counter++;
setTitle("Chat-Server - Clients: "+counter);
//Verbundene Clients im Tray aktualisieren-------------------------
try
{
sysTrayToolTip = " Verbunde Clients: "+counter;
sysTray.setIcon(sysTrayIcon2, sysTrayToolTip);
sysTray.replaceIcon(sysTrayIcon2);
}
catch(SysTrayIconException exc) // Fängt Fehler ab
{System.out.println(exc.getMessage());}
//-----------------------------------------------------------------
try
{
reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
name = reader.readLine();
}
catch(IOException erw){}
String zeilelogin = "Nachricht: "+ name +" hat sich eingeloggt!" + '\n';
String zeilelogin2 = "Nachricht: "+ name +" hat sich eingeloggt!";//ohne Zeilenumbruch
login(zeilelogin2);//Funktionsaufruf zum Login
ChatServerPanel.taAusgabe.append(zeilelogin);//Meldung an den Server
au.clip.play();
setzeIcon();//Icon neu setzen
csp.reset();//Balken rückt nach
}
public void run()
{
String zeile;
try
{
while((zeile = reader.readLine()) != null)
{
ChatServerPanel.taAusgabe.append(zeile + '\n');
doSende(zeile);
csp.reset();
}
}
catch(IOException iof)
{
System.out.println(iof);
}
String zeilelogout = "Nachricht: "+ name +" hat sich ausgeloggt!" + '\n';
String zeilelogout2 = "Nachricht: "+ name +" hat sich ausgeloggt!";//ohne Zeilenumbruch
logout(zeilelogout2);//Funktionsaufruf zum Logout
ChatServerPanel.taAusgabe.append(zeilelogout);//Meldung an Server
counter--;
setTitle("Chat-Server - Clients: "+counter);
au.clip2.play();
setzeIcon();//Icon neu setzen
csp.reset();//Balken rückt nach
//Verbundene Clients im Tray aktualisieren-------------------------
try
{
if(counter > 0)
{
sysTrayToolTip = " Verbunde Clients: "+counter;
sysTray.setIcon(sysTrayIcon2, sysTrayToolTip);
sysTray.replaceIcon(sysTrayIcon2);
}
else
{
sysTrayToolTip = " Es sind keine Clients verbunden ";
sysTray.setIcon(sysTrayIcon, sysTrayToolTip);
sysTray.replaceIcon(sysTrayIcon);
}
}
catch(SysTrayIconException exc) // Fängt Fehler ab
{System.out.println(exc.getMessage());}
//-----------------------------------------------------------------
try
{
reader.close();
writer.close();
writer.flush();
}
catch(Exception fe)
{
System.out.println(fe);
}
}
public void doSende(String text)
{
for(int i=0 ; i < vecClient.size() ; i++)
{
ClientConnect cc =((ClientConnect)vecClient.elementAt(i));
try
{
cc.writer.write(text);
cc.writer.newLine();
cc.writer.flush();
}
catch(IOException fehler)
{
System.out.println(fehler);
}
}
}
//---Login + Logout------------------------------------------------------------------
public void logout(String text)
{
for(int i=0 ; i < vecClient.size() ; i++)
{
ClientConnect cc =((ClientConnect)vecClient.elementAt(i));
try
{
cc.writer.write(text);
cc.writer.newLine();
cc.writer.flush();
}
catch(IOException fehler)
{
System.out.println(fehler);
}
}
}
public void login(String text)
{
for(int i=0 ; i < vecClient.size() ; i++)
{
ClientConnect cc =((ClientConnect)vecClient.elementAt(i));
try
{
cc.writer.write(text);
cc.writer.newLine();
cc.writer.flush();
}
catch(IOException fehler)
{
System.out.println(fehler);
}
}
}
public void closeRW()
{
try
{
reader.close();
writer.close();
}
catch(IOException fehler)
{
System.out.println(fehler);
}
}
//----Ende Login + Logout------------------------------------------------------------
}//Ende innere Klasse--------------------------------------------------------------------
public void runChat() throws IOException
{
while(true)
{
if(stopchat == false)
{
System.out.println("Hallo");
break;
}
System.out.println( stopchat );
clientc = new ClientConnect(server.accept());
vecClient.addElement(clientc);
clientc.start();
}
System.out.println("Hallo2");
}
public static void main(String[] args)
{
ChatServer chatS;
try
{
chatS = new ChatServer();
chatS.setVisible(true);
}
catch(Exception e)
{e.printStackTrace();}
}
// ---Mouse Listener----------------------------------------------------------------------------
public void mouseClicked(MouseEvent arg0)
{}
public void mouseEntered(MouseEvent arg0)
{}
public void mouseExited(MouseEvent arg0)
{}
public void mousePressed(MouseEvent arg0)
{
if(arg0.getClickCount()== 1)
{
csp.setVisible(true);
logo.setVisible(false);
start.setVisible(false);
stop.setVisible(false);
jt.setVisible(false);
}
if(arg0.getClickCount()== 2)
{
csp.setVisible(false);
logo.setVisible(true);
start.setVisible(true);
stop.setVisible(true);
jt.setVisible(true);
}
}
public void mouseReleased(MouseEvent arg0)
{}
// ----------------------------------------------------------------------------------------------
//Window Listener---------------------------------------------
public void windowOpened(WindowEvent arg0) {}
public void windowClosing(WindowEvent arg0)
{
if(counter > 0)
{
int status = JOptionPane.showConfirmDialog(this,"Es ist/sind noch "+counter+" Client(s) verbunden! \n\nDienst beenden?","Frage ???",JOptionPane.YES_NO_OPTION);
if(status == 0)
{
try
{
server.close();
dispose();
sysTray.freeMemoryExit();
System.exit(0);
}
catch(IOException fehler)
{
System.out.println(fehler);
}
}
}
else
{
try
{
server.close();
sysTray.freeMemoryExit();
dispose();
System.exit(0);
}
catch(IOException fehler){}
}
}
public void windowClosed(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
//------------------------------------------------------------
//eigenes Icon setzen am linken Fensterrand
private void setzeIcon()
{
MediaTracker mt = new MediaTracker(this);
if(counter == 0)
{
Image icon = getToolkit().getImage(getClass().getResource("Bilder/links.gif"));
mt.addImage(icon,0);
setIconImage(icon);
}
else
{
Image icon2 = getToolkit().getImage(getClass().getResource("Bilder/links2.gif"));
mt.addImage(icon2,0);
setIconImage(icon2);
}
try
{
//wartet bis Bild vollständig geladen
mt.waitForAll();
}
catch(InterruptedException e){}
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equalsIgnoreCase("Start"))
{
//Port ändern---------------------------------------
if(!jt.getText().equalsIgnoreCase("5555"))
{
portnr = Integer.parseInt(jt.getText());
portadresse = jt.getText();
ChatServerPanel.lPortadresse.setText("Portadresse : " +portadresse);
try
{
server = new ServerSocket(portnr);
//stopchat = true;
runChat();
}
catch(IOException fehler)
{
System.out.println(fehler);
}
}
else
{
System.out.println("JA");
System.out.println(portnr);
System.out.println(stopchat);
try
{
server = new ServerSocket(portnr);
stopchat = true;
System.out.println(stopchat);
runChat();
}
catch(IOException fehler)
{
System.out.println(fehler);
}
}
start.setEnabled(false);
stop.setEnabled(true);
jt.setEnabled(false);
}//Ende if Start
if(e.getActionCommand().equalsIgnoreCase("Stop"))
{
try
{
server.close();
stopchat = false;
runChat();
}
catch(IOException fehler){System.out.println(fehler);};
stop.setEnabled(false);
start.setEnabled(true);
jt.setEnabled(true);
}
}//Ende Action Performed
//----Mein SystemTray-------------------------------------------------------------------------------------
public void mytray()
{
try
{
sysTray.setIcon(sysTrayIcon, sysTrayToolTip); // Fügt dem SystemTray mit einem Ikon und Tooltip unter Windows hinzu
}
catch(SysTrayIconException exc) // Fängt Fehler ab
{
System.out.println(exc.getMessage());
}
if (minArg)
{
sysTray.addMenuItem("Zeigen",false); // Fügt ein SystemTray Men¸ Eintrag hinzu
sysTray.addMenuItem("Verstecken",true);
}
else
{
sysTray.addMenuItem("Zeigen",true);
sysTray.addMenuItem("Verstecken",false);
}
//sysTray.addMenuItem("Info",false,true);
sysTray.addMenuItem("Beenden");
sysTray.setPopupMenu(2);
sysTray.addPropertyChangeListener(new java.beans.PropertyChangeListener()
{ // Dieser Bereich definiert was man gemacht werden soll
public final void propertyChange(java.beans.PropertyChangeEvent e)
{
// Left mouse button event listener
if(e.getPropertyName().equals("LM_Button"))
{
if (isVisible())
{
setVisible(false);
sysTray.replaceItem(1,false);
sysTray.replaceItem(2,true);
}
else
{
setVisible(true);
sysTray.replaceItem(1,true);
sysTray.replaceItem(2,false);
}
}
// Right mouse button event listener
/* if(e.getPropertyName().equals("RM_Button")) {
// ...
}
*/ // "Zeigen" menu item event listener
if(e.getPropertyName().equals("Zeigen"))
{
setVisible(true);
sysTray.replaceItem(1,true);
sysTray.replaceItem(2,false);
}
// "Verstecken" menu item event listener
if(e.getPropertyName().equals("Verstecken"))
{
setVisible(false);
sysTray.replaceItem(1,false);
sysTray.replaceItem(2,true);
}
// "Info" menu item event listener
if(e.getPropertyName().equals("Info"))
{
/*if(istInfo == false)
{
InfoFenster info = new InfoFenster();
info.show();
istInfo = true;
}*/
}
// "Exit" menu item event listener
if(e.getPropertyName().equals("Beenden"))
{
if(counter > 0)
{
setVisible(true);
sysTray.replaceItem(1,true);
sysTray.replaceItem(2,false);
//Dialog aus dem Sys Tray heraus
Object[] options = { "JA", "Nein"};
int antwort=-1;
antwort=(JOptionPane.showOptionDialog(null, "Es ist/sind noch "+counter+" Client(s) verbunden! \n\nDienst beenden?", "Warnung", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]));
if(antwort == 0)
{
setVisible(false);
dispose();
System.exit(0);
}
}
else
{
sysTray.freeMemoryExit();
setVisible(false);
dispose();
System.exit(0);
}
}
}
});
}
//Ende Tray---------------------------------------------------------------------------------------------------
}