KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MUCPopUp


1 import java.awt.*;
2 import java.awt.event.*;
3 import java.awt.image.ImageObserver JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import javax.swing.*;
6 import javax.accessibility.Accessible JavaDoc;
7
8 import org.jivesoftware.smack.Chat;
9 import org.jivesoftware.smack.XMPPException;
10 import org.jivesoftware.smackx.muc.*;
11 import org.jivesoftware.smack.packet.*;
12 import org.jivesoftware.smack.util.StringUtils;
13
14 /** Pop-up menu shown for participants in multi-user-chat.*/
15 public final class MUCPopUp extends JPopupMenu implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, MenuElement, Serializable JavaDoc{
16     
17     private final MUCPanel mucPanel;
18     
19     private String JavaDoc nick; //Nickname of selected participant
20

21     final JLabel who=new JLabel();
22     
23     final JMenu pmMenu=new JMenu(Lang.gs("private msg"));
24         final JPanel panel=new JPanel(new FlowLayout(FlowLayout.LEFT,3,0));
25             final JTextField msg=new JTextField(15);
26             final JButton sendBtn=new JButton(Lang.gs("send"));
27     
28     final JMenuItem viewProfile=new JMenuItem(Lang.gs("pu profile"),Icons.PROFILE);
29     final JMenu modMenu=new JMenu(Lang.gs("moderation"));
30         final JMenuItem grantVoice=new JMenuItem(Lang.gs("grant voice"));
31         final JMenuItem revokeVoice=new JMenuItem(Lang.gs("revoke voice"));
32         final JMenuItem makeMod=new JMenuItem(Lang.gs("make moderator"));
33         final JMenuItem revokeMod=new JMenuItem(Lang.gs("revoke moderator"));
34         final JMenuItem kick=new JMenuItem(Lang.gs("kick"));
35     final Click click=new Click();
36     
37     /** Creates a new MUCPopUp.
38     * @param mucpanel The MUCPanel that this MUCPopUp is associated with.*/

39     public MUCPopUp(MUCPanel mucpanel){
40         super();
41         this.mucPanel=mucpanel;
42         who.setHorizontalTextPosition(SwingConstants.CENTER);
43         who.setHorizontalAlignment(SwingConstants.CENTER);
44         add(who);
45         addSeparator();
46         
47         
48         msg.setToolTipText(Lang.gs("private msg_tt"));
49         msg.getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),"dosend");
50         msg.getActionMap().put("dosend", new KeyAction());
51         panel.add(msg);
52         sendBtn.setMargin(new Insets(0,0,0,0));
53         sendBtn.addActionListener(click);
54         sendBtn.setToolTipText(Lang.gs("private msg_tt"));
55         panel.add(sendBtn);
56         
57         pmMenu.setIcon(Icons.PRIVATE_MESSAGE);
58         pmMenu.add(panel);
59         add(pmMenu);
60         addSeparator();
61         viewProfile.addActionListener(click);
62         add(viewProfile);
63         addSeparator();
64         add(modMenu);
65         grantVoice.addActionListener(click);
66         modMenu.add(grantVoice);
67         revokeVoice.addActionListener(click);
68         modMenu.add(revokeVoice);
69         makeMod.addActionListener(click);
70         modMenu.add(makeMod);
71         revokeMod.addActionListener(click);
72         modMenu.add(revokeMod);
73         kick.addActionListener(click);
74         modMenu.add(kick);
75         pack();
76     }
77     
78     public void show(MouseEvent e){
79         int index=mucPanel.getList().locationToIndex(new Point(e.getX(),e.getY()));
80         if (index==-1){
81             return;
82         }
83         Presence p=(Presence) (mucPanel.getParticipants().elementAt(index));
84         if(p==null){
85             return;
86         }
87         nick=p.getFrom();
88         who.setText(StringUtils.parseResource(nick));
89         who.setToolTipText(nick);
90         msg.setText("");
91         // DEBUG
92
// we dont seem to getting the currect role.
93
// Fault prob. lies in MUCPanel.java
94
//modMenu.setEnabled(mucPanel.getRole().equals("moderator"));
95
super.show(mucPanel.getList(),e.getX(),e.getY());
96         msg.requestFocus();
97     }
98     
99     public void doSendPM(){
100         setVisible(false);
101         String JavaDoc m=msg.getText();
102         if(m==null || m.trim().equals("")){
103             return;
104         }
105         try{
106             Chat c=mucPanel.getMultiUserChat().createPrivateChat(nick);
107             c.sendMessage(m);
108         }
109         catch(Exception JavaDoc e){
110             mucPanel.announce("* "+Lang.gs("private msg failed")+":"+nick+" "+e.getMessage()+" *");
111             return;
112         }
113         mucPanel.pm(m,null,nick,GUI.getTime());
114     }
115     
116     public void doViewProfile(){
117         setVisible(false);
118         final String JavaDoc jid=mucPanel.getMultiUserChat().getParticipantJID(nick);
119         //constructor of ProfileWindow automatically shows the dialog
120
if(jid==null){
121             ProfileWindow pw=new ProfileWindow(nick,this);
122         }
123         else{
124             ProfileWindow pw=new ProfileWindow(StringUtils.parseBareAddress(jid),this);
125         }
126     }
127     
128     public void doKick(){
129         String JavaDoc reason=JOptionPane.showInputDialog(this, Lang.gs("reason"),Lang.gs("kick")+" - "+StringUtils.parseResource(nick), JOptionPane.QUESTION_MESSAGE);
130         if(reason==null){
131             setVisible(false);
132             return;
133         }
134         try{
135             mucPanel.getMultiUserChat().kickParticipant(StringUtils.parseResource(nick),reason);
136         }
137         catch(XMPPException xe){
138             int e=0;
139             if(xe.getXMPPError()!=null){
140                 e=xe.getXMPPError().getCode();
141             }
142             if(e==403 || e==405){
143                 mucPanel.announce("* "+Lang.gs("forbidden")+" *");
144             }
145             else{
146                 mucPanel.announce("* "+xe.getMessage()+" *");
147             }
148         }
149         catch(Exception JavaDoc e){
150             mucPanel.announce("* "+Lang.gs("error")+":"+e.getMessage()+" *");
151             return;
152         }
153         setVisible(false);
154     }
155     
156     public void doGrantVoice(){
157         try{
158             mucPanel.getMultiUserChat().grantVoice(StringUtils.parseResource(nick));
159         }
160         catch(XMPPException xe){
161             int e=0;
162             if(xe.getXMPPError()!=null){
163                 e=xe.getXMPPError().getCode();
164             }
165             if(e==403 || e==405){
166                 mucPanel.announce("* "+Lang.gs("grant voice")+":"+Lang.gs("forbidden")+" *");
167             }
168             else{
169                 mucPanel.announce("* "+Lang.gs("error")+":"+Lang.gs("grant voice")+" - "+xe.getMessage()+" *");
170             }
171         }
172         catch(Exception JavaDoc e){
173             mucPanel.announce("* "+Lang.gs("error")+":"+Lang.gs("grant voice")+" - "+e.getMessage()+" *");
174         }
175         setVisible(false);
176     }
177     
178     public void doRevokeVoice(){
179         try{
180             mucPanel.getMultiUserChat().revokeVoice(StringUtils.parseResource(nick));
181         }
182         catch(XMPPException xe){
183             int e=0;
184             if(xe.getXMPPError()!=null){
185                 e=xe.getXMPPError().getCode();
186             }
187             if(e==403 || e==405){
188                 mucPanel.announce("* "+Lang.gs("revoke voice")+":"+Lang.gs("forbidden")+" *");
189             }
190             else{
191                 mucPanel.announce("* "+Lang.gs("error")+":"+Lang.gs("revoke voice")+" - "+xe.getMessage()+" *");
192             }
193         }
194         catch(Exception JavaDoc e){
195             mucPanel.announce("* "+Lang.gs("error")+":"+Lang.gs("revoke voice")+" - "+e.getMessage()+" *");
196         }
197         setVisible(false);
198     }
199     
200     public void doMakeMod(){
201         try{
202             mucPanel.getMultiUserChat().grantModerator(StringUtils.parseResource(nick));
203         }
204         catch(XMPPException xe){
205             int e=0;
206             if(xe.getXMPPError()!=null){
207                 e=xe.getXMPPError().getCode();
208             }
209             if(e==403 || e==405){
210                 mucPanel.announce("* "+Lang.gs("make moderator")+":"+Lang.gs("forbidden")+" *");
211             }
212             else{
213                 mucPanel.announce("* "+Lang.gs("error")+":"+Lang.gs("make moderator")+" - "+xe.getMessage()+" *");
214             }
215         }
216         catch(Exception JavaDoc e){
217             mucPanel.announce("* "+Lang.gs("error")+":"+Lang.gs("make moderator")+" - "+e.getMessage()+" *");
218         }
219         setVisible(false);
220     }
221
222     public void doRevokeMod(){
223         try{
224             mucPanel.getMultiUserChat().revokeModerator(StringUtils.parseResource(nick));
225         }
226         catch(XMPPException xe){
227             int e=0;
228             if(xe.getXMPPError()!=null){
229                 e=xe.getXMPPError().getCode();
230             }
231             if(e==403 || e==405){
232                 mucPanel.announce("* "+Lang.gs("revoke moderator")+":"+Lang.gs("forbidden")+" *");
233             }
234             else{
235                 mucPanel.announce("* "+Lang.gs("error")+":"+Lang.gs("revoke moderator")+" - "+xe.getMessage()+" *");
236             }
237         }
238         catch(Exception JavaDoc e){
239             mucPanel.announce("* "+Lang.gs("error")+":"+Lang.gs("revoke moderator")+" - "+e.getMessage()+" *");
240         }
241         setVisible(false);
242     }
243     
244     
245     // INNER CLASSES
246

247     final class Click implements ActionListener{
248         public void actionPerformed(ActionEvent ae){
249             //get action commend
250
String JavaDoc ac=ae.getActionCommand();
251             // call method depending on button clicked
252
if(ac.equals(sendBtn.getText())){
253                 doSendPM();
254                 return;
255             }
256             
257             if(ac.equals(viewProfile.getText())){
258                 doViewProfile();
259                 return;
260             }
261             if(ac.equals(kick.getText())){
262                 doKick();
263                 return;
264             }
265             if(ac.equals(grantVoice.getText())){
266                 doGrantVoice();
267                 return;
268             }
269             if(ac.equals(revokeVoice.getText())){
270                 doRevokeVoice();
271                 return;
272             }
273             if(ac.equals(makeMod.getText())){
274                 doMakeMod();
275                 return;
276             }
277             if(ac.equals(revokeMod.getText())){
278                 doRevokeMod();
279                 return;
280             }
281         }
282     }
283     
284     final class KeyAction extends AbstractAction{
285         
286         public void actionPerformed(ActionEvent e) {
287             doSendPM();
288             return;
289         }
290     }
291     
292 }
Popular Tags