KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JoinMUCDialog


1 import java.awt.*;
2 import java.awt.event.*;
3 import java.awt.image.ImageObserver JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.util.*;
6 import javax.swing.*;
7 import javax.swing.border.*;
8 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
9 import javax.accessibility.Accessible JavaDoc;
10 import org.jivesoftware.smack.util.StringUtils;
11 import org.jivesoftware.smackx.muc.*;
12 import org.jivesoftware.smackx.Form;
13 import org.jivesoftware.smack.*;
14
15
16 public final class JoinMUCDialog extends JDialog implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, RootPaneContainer, Serializable JavaDoc, SwingConstants,WindowConstants{
17     
18     private static final int GAP=10;
19     
20     final JPanel fields=new JPanel(new SpringLayout());
21     final JLabel hostLbl=new JLabel(Lang.gs("host"));
22     final JTextField host=new JTextField(20);
23     final JLabel roomLbl=new JLabel(Lang.gs("room"));
24     final Box roomPanel=new Box(BoxLayout.X_AXIS);
25         final JTextField room=new JTextField(10);
26         final JButton searchBtn=new JButton("",Icons.DISCO);
27     final JLabel nickLbl=new JLabel(Lang.gs("form_nick"));
28     final JTextField nick=new JTextField(10);
29     final JLabel passwordLbl=new JLabel(Lang.gs("room password"));
30     final JPasswordField password=new JPasswordField(10);
31     final JCheckBox create=new JCheckBox(Lang.gs("create room"),false);
32     
33     
34     final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP));
35         final JButton joinBtn=new JButton(Lang.gs("join"),Icons.GROUP_CHAT);
36         final JButton cancelBtn=new JButton(Lang.gs("cancel"),Icons.CANCEL);
37     
38     final BtnClick click=new BtnClick();
39     
40     JWaitDialog wait;
41     
42     final JoinMUCDialog thisDialog=this;
43     
44     public JoinMUCDialog(){
45         super(WhisperIM.MainWindow,Lang.gs("menu_chat_groupchat"),false);
46         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
47         Container cp=getContentPane();
48         cp.setLayout(new BorderLayout(GAP,GAP));
49         
50         GUI.connect(hostLbl,host,"host_mn","host_tt");
51         GUI.connect(roomLbl,room,"room_mn","room_tt");
52         GUI.connect(nickLbl,nick,"nick_mn","nick_tt");
53         GUI.connect(passwordLbl,password,"room password_mn","room password_tt");
54         create.setToolTipText(Lang.gs("create room_tt"));
55         create.setMnemonic(Lang.s2k("create room_mn"));
56         
57         fields.add(roomLbl);
58         roomPanel.add(room);
59         roomPanel.add(Box.createHorizontalStrut(GAP));
60         roomPanel.add(Box.createGlue());
61         roomPanel.add(searchBtn);
62         searchBtn.setMargin(new Insets(0,0,0,0));
63         searchBtn.setActionCommand("DISCO browse");
64         searchBtn.setToolTipText(Lang.gs("menu_tools_disco"));
65         searchBtn.addActionListener(click);
66         fields.add(roomPanel);
67         fields.add(hostLbl);
68         fields.add(host);
69         fields.add(nickLbl);
70         fields.add(nick);
71         fields.add(passwordLbl);
72         fields.add(password);
73         fields.add(new JLabel(""));
74         fields.add(create);
75         SpringUtilities.makeCompactGrid(fields,5,2,GAP,GAP,GAP,GAP);
76         cp.add(fields,BorderLayout.CENTER);
77         
78         joinBtn.setToolTipText(Lang.gs("join_tt"));
79         joinBtn.setMnemonic(Lang.s2k("join_mn"));
80         joinBtn.setDefaultCapable(true);
81         joinBtn.addActionListener(click);
82         cancelBtn.setMnemonic(Lang.s2k("cancel_mn"));
83         cancelBtn.addActionListener(click);
84         btnPanel.add(joinBtn);
85         btnPanel.add(cancelBtn);
86         cp.add(btnPanel,BorderLayout.SOUTH);
87         getRootPane().setDefaultButton(joinBtn);
88         
89         host.setText(WhisperIM.Config.getProperty("jabberDefaultChatServer")+"."+WhisperIM.accountSettings.JabberServer);
90         if(WhisperIM.Config.getProperty("jabberLockToDefaultChatServer").equals("true")){
91             host.setEnabled(false);
92         }
93         nick.setText(WhisperIM.MainWindow.MyProfile.NickName);
94         wait=new JWaitDialog(this,"talk with server");
95         //help key
96
getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); //put in help key for action help
97
getRootPane().getActionMap().put("help",new HelpAction(this,"group.html#join"));
98         pack();
99         addKeyListener(new KeyAction());
100         addMouseListener(new MouseAction());
101     }
102     
103     public void show(){
104         setLocationRelativeTo(WhisperIM.MainWindow);
105         super.show();
106     }
107     
108     public void show(Component c){
109         setLocationRelativeTo(c);
110         super.show();
111     }
112     
113     public void setRoom(String JavaDoc value){
114         room.setText(value);
115     }
116     
117     public void setHost(String JavaDoc value){
118         if(host.isEnabled()){
119             host.setText(value);
120         }
121     }
122         
123     
124     public void doJoin(){
125         if(!WhisperIM.MainWindow.Conn.isConnected()){
126             GUI.showWarning(this,"menu_chat_groupchat","need to be connected");
127             return;
128         }
129         // validation
130
String JavaDoc hostname=host.getText();
131         String JavaDoc r=StringUtils.parseName(hostname);
132         if(!r.equals("")){ // see if user has entered room@chat.host.net by mistake
133
hostname=StringUtils.parseServer(hostname);
134             host.setText(hostname);
135             if(room.getText().equals("")){
136                 room.setText(r);
137             }
138         }
139         if(hostname.equals("")){
140             GUI.showInfo(this,"menu_chat_groupchat","need host");
141             host.requestFocus();
142             return;
143         }
144         String JavaDoc roomname=room.getText();
145         if(roomname.equals("")){
146             GUI.showInfo(this,"menu_chat_groupchat","need room name");
147             room.requestFocus();
148             return;
149         }
150         String JavaDoc nickname=nick.getText();
151         if(nickname.equals("")){
152             GUI.showInfo(this,"menu_chat_groupchat","need nickname");
153             nick.requestFocus();
154             return;
155         }
156         if(!nickname.equals(WhisperIM.MainWindow.MyProfile.NickName)){
157             WhisperIM.MainWindow.MyProfile.NickName=nickname;
158         }
159         // check not already in room
160
MUCPanel mpanel=(MUCPanel)WhisperIM.MainWindow.MUCs.get(roomname+"@"+hostname);
161         if(mpanel!=null){
162             dispose();
163             WhisperIM.MainWindow.chatPane.setSelectedComponent(mpanel);
164             return;
165         }
166         wait.show();
167         MultiUserChat muc=new MultiUserChat(WhisperIM.MainWindow.Conn,roomname+"@"+hostname);
168         MUCPanel mp=new MUCPanel(muc);
169         (new JoinMUCThread(nickname,muc, roomname,hostname,mp,create.isSelected())).start();
170     }
171     
172     final class BtnClick implements ActionListener{
173         public void actionPerformed(ActionEvent ae){
174             //get button
175
String JavaDoc b=ae.getActionCommand();
176             // call method depending on button clicked
177
if(b.equals(cancelBtn.getText())){
178                 dispose();
179                 return;
180             }
181             if(b.equals(joinBtn.getText())){
182                 doJoin();
183                 return;
184             }
185             if(b.equals("DISCO browse")){
186                 String JavaDoc h=host.getText();
187                 if(h.equals("")){
188                     h=WhisperIM.accountSettings.JabberServer;
189                 }
190                 if(WhisperIM.MainWindow.discoWindow==null){
191                     WhisperIM.MainWindow.setCursor(GUI.WAIT);
192                     WhisperIM.MainWindow.discoWindow=new DISCOWindow();
193                     WhisperIM.MainWindow.setCursor(GUI.NORMAL);
194                 }
195                 WhisperIM.MainWindow.discoWindow.setHost(h);
196                 WhisperIM.MainWindow.discoWindow.show();
197                 WhisperIM.MainWindow.discoWindow.doBrowse(true);
198                 return;
199             }
200         }
201     }
202     
203     
204     final class JoinMUCThread extends Thread JavaDoc{
205         
206         private String JavaDoc nickname;
207         private MultiUserChat muc;
208         private String JavaDoc roomname;
209         private String JavaDoc hostname;
210         private MUCPanel mp;
211         private boolean createroom=false;
212             
213         public JoinMUCThread(String JavaDoc nickname,MultiUserChat muc,String JavaDoc roomname,String JavaDoc hostname,MUCPanel mp,boolean createroom){
214             super("JoinMUCThread in JoinMUCDialog.class");
215             setPriority(Thread.MIN_PRIORITY);
216             setDaemon(true);
217             this.nickname=nickname;
218             this.muc=muc;
219             this.roomname=roomname;
220             this.hostname=hostname;
221             this.mp=mp;
222             this.createroom=createroom;
223         }
224         
225         public void run(){
226             try{
227                 if(!createroom){
228                     if(password.getPassword().length==0){
229                         muc.join(nickname);
230                     }
231                     else{
232                         muc.join(nickname, new String JavaDoc(password.getPassword()));
233                     }
234                 }
235                 else{
236                     muc.create(nickname);
237                     muc.sendConfigurationForm(new org.jivesoftware.smackx.Form(org.jivesoftware.smackx.Form.TYPE_SUBMIT));
238                 }
239             }
240             catch (final XMPPException xe){
241                 javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
242                     public void run(){
243                         password.setText("");
244                         int e=0;
245                         if(xe.getXMPPError()!=null){
246                             e=xe.getXMPPError().getCode();
247                         }
248                         wait.dispose();
249                         if(e==401){
250                             GUI.showWarning(thisDialog,"menu_chat_groupchat","need password");
251                             password.requestFocus();
252                             return;
253                         }
254                         if(e==403){
255                             GUI.showWarning(thisDialog,"menu_chat_groupchat","you banned");
256                             room.requestFocus();
257                             return;
258                         }
259                         if(e==404){
260                             GUI.showWarning(thisDialog,"menu_chat_groupchat","room does not exist");
261                             room.requestFocus();
262                             return;
263                         }
264                         if(e==409){
265                             GUI.showWarning(thisDialog,"menu_chat_groupchat","nickname in use");
266                             nick.requestFocus();
267                             return;
268                         }
269                     
270                         GUI.showError(thisDialog,"menu_chat_groupchat","could not join",roomname+" : "+hostname,xe);
271                         room.requestFocus();
272                         return;
273                     }
274                 });
275                 return;
276             }
277             javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
278                     public void run(){
279                         password.setText("");
280                         WhisperIM.MainWindow.chatPane.addTab(roomname,Icons.GROUP_CHAT,mp,nickname+" - "+roomname+"@"+hostname);
281                         WhisperIM.MainWindow.MUCs.put(roomname+"@"+hostname,mp);
282                         wait.dispose();
283                         thisDialog.dispose();
284                         WhisperIM.MainWindow.chatPane.setSelectedComponent(mp);
285                         mp.go();
286                     }
287                 });
288         }
289     }
290 }
Popular Tags