Hello,
I have a very time-consuming problem which might be easy to solve by someone
who is better in programming JAVA than me. I have to make a chatbox in Corba/java.
In this chatbox i have to be able to chat private with a particular user
i will choose by entering his name in my GUI. I have a method called sendMessage
and sendPrivateMessage. The sendMessage is correct but i don't know how i
can select a particular user from the Hashtable without sending the message
to everybody trough the callback method. Is there some smart programmer who
can help me, please. This is part of my exames so i'm getting very nervous
about this problem. I have put the code of my Implementation and the ClientCode
below. In the implementation code you can find the methodes for sending the
message. Thanks!
Greetings,
Susan


//
// This file was generated from Chat.idl
//

package ChatApp;
import java.util.*;
import ChatClient;
//
// IDL:ChatApp/Chatten:1.0
//

public class ChattenImpl extends ChatApp._ChattenImplBase {
Hashtable chattable = new Hashtable();

public ChattenImpl() {
this._initialize_inheritance_tree();
}

public void Register(ChatApp.User user) {
chattable.put(user.userId, user);

Message msg = new Message(user, "is toegevoegd!!");
this.sendMessage(msg);

System.out.println("toegevoegd: " + user.userId);
}

public void unRegister(ChatApp.User user) {
chattable.remove(user.userId);

Message msg = new Message(user, "heeft de chatroom verlaten.");
this.sendMessage(msg);

System.out.println("verwijderd: " + user.userId);
}

public void sendMessage(ChatApp.Message msg) {
User user = null;

for (Enumeration e = chattable.elements(); e.hasMoreElements()
{
user = (User) e.nextElement();

try {
user.callObj.callback(msg);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

public void sendPrivateMessage(ChatApp.User user, ChatApp.Message msg)
{
user.userId = ChatClient.Tekstveld2.getText();

for (Enumeration e = chattable.elements(); e.hasMoreElements()
{
user = (User) e.nextElement();


try {
user.callObj.callback(msg);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}


public void _initialize_inheritance_tree() {//GEN-BEGIN:M_void__initialize_inheritance_tree_
// Do not edit! This is a method which is necessary for using delegation.
}//GEN-END:M_void__initialize_inheritance_tree_


}

--------------------------------------------------------------------------
/*
* ChatClient.java
*
* Created on 4 april 2002, 0:03
*/
import ChatApp.*;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
import org.omg.CosNaming.NameComponent;
import java.lang.Thread;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
*
* @author Susan
*/
public class ChatClient extends _CallbackImplBase {

// Dit is ons (server-side) Chat-object; deze is buiten de main() nodig
dus
// wordt deze globaal gedeclareerd. Verder is deze STATIC gedeclareerd,
omdat hij binnen
// de main() geďnitialiseerd wordt, en de main() ook static is.
private Chatten chat = null;

//Aan de Object User wordt een variabele toegekend die hier null wordt
gedeclareerd.
//Dit is om aan te geven dat er nog een waarde moet worden ingevoerd
en dat deze daarom begint
//met een lege waarde
private User user = null;

//Declaratie Componenten voor GUI.
private JTextField Tekstveld1 = new JTextField("");
public static JTextField Tekstveld2 = new JTextField("");
public static JTextField Tekstveld3 = new JTextField("");
private JButton Knop1 = new JButton("Verzenden");
private JButton Knop2 = new JButton("Privé");
private JLabel Label1 = new JLabel("Naam privepersoon");
private JLabel Label2 = new JLabel("Verzenden privebericht");
private JLabel Label3 = new JLabel("Druk op de knop om prive te chatten");
public static JTextArea Tekstgebied = new JTextArea();

//Constructor waarbinnen de componenten geinitialiseerd worden.
public ChatClient() {
JFrame f = new JFrame("CHATROOM");
f.getContentPane().setLayout(null);

JPanel p = new JPanel();
p.setLayout(null);

Knop1.setFont(new java.awt.Font("Dialog", 0, 12));
Knop1.setBackground(java.awt.Color.lightGray);
Knop1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
Message msg = new Message(user, Tekstveld1.getText());
chat.sendMessage(msg);
}
});
f.getContentPane().add(Knop1);
Knop1.setBounds(350, 40, 117, 20);


Knop2.setFont(new java.awt.Font("Dialog", 0, 12));
Knop2.setBackground(java.awt.Color.lightGray);
Knop2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
Message msg = new Message(user, Tekstveld3.getText());
chat.sendPrivateMessage(msg);
}
});
f.getContentPane().add(Knop2);
Knop2.setBounds(350, 330, 117, 20);

f.getContentPane().add(Tekstveld1);
Tekstveld1.setBounds(10, 40, 290, 30);

f.getContentPane().add(Tekstveld2);
Tekstveld2.setBounds(350, 200, 100, 20);

f.getContentPane().add(Tekstveld3);
Tekstveld3.setBounds(350, 260, 290, 30);

f.getContentPane().add(Label1);
Label1.setBounds(350, 180, 140, 17);

f.getContentPane().add(Label2);
Label2.setBounds(350, 240, 211, 17);

f.getContentPane().add(Label3);
Label3.setBounds(350, 310, 211, 17);


f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
chat.unRegister(user);
} catch (Exception ex) {}
System.exit(0);
}
});

JScrollPane sc = new JScrollPane(Tekstgebied);
sc.setBounds(10, 100, 290, 260);
f.getContentPane().add(sc);


f.pack();
f.setVisible(true);
}

public void init(String userId) {

try {

ORB orb = ORB.init((String[])null, null);


org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);


NameComponent nc = new NameComponent("Chatten", "");
NameComponent path[] = {nc};
chat = ChattenHelper.narrow(ncRef.resolve(path));


orb.connect(this);

//Hier wordt er een variabele aan het User object toegekend en daarbij

//wordt deze vera
user = new User(userId, this);
// Hier wordt het User-object geregistreerd bij de server
chat.Register(user);
}
catch (Exception e) {
e.printStackTrace();
}
}

//De functie callback wordt hier geimplementeerd omdat het User-object
hier
//binnen de klasse staat gedeclareerd als private.
public void callback(ChatApp.Message msg) {

//Hier staat: Zorg dat de ingevoerde string userId als een 'bericht'
//verstuurd wordt naar alle clients. En dat daaraan een chatbericht
wordt
//gekoppeld als dit in Tekstveld1 is ingevoerd.
Tekstgebied.append("-->" + msg.user.userId + " - " + msg.msg + "\n");
Tekstveld1.setText("");
}

public static void main(String[] args) {
try{
ChatClient sc = new ChatClient();
sc.init(args[0]);
}

catch (Exception ex) {
ex.printStackTrace();
}
}

}