KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SubscriptionRequestDialog


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.accessibility.Accessible JavaDoc;
8
9 import org.jivesoftware.smack.packet.Presence;
10 import org.jivesoftware.smack.util.StringUtils;
11
12 public final class SubscriptionRequestDialog extends JDialog implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, RootPaneContainer, Serializable JavaDoc, SwingConstants,WindowConstants{
13     
14     final private static int GAP=10;
15     final BorderLayout layout=new BorderLayout(GAP,GAP);
16     
17     final JPanel infoPanel=new JPanel(new SpringLayout());
18         final JLabel infoLbl=new JLabel(Lang.gs("subscribe"));
19         final JCheckBox add=new JCheckBox(Lang.gs("add to contacts"),true);
20     
21     final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP));
22         final JButton denyBtn=new JButton(Lang.gs("deny"),Icons.DENY);
23         final JButton acceptBtn=new JButton(Lang.gs("accept"),Icons.ACCEPT);
24     
25     final BtnClick click=new BtnClick();
26     
27     private final String JavaDoc From;
28         
29     public SubscriptionRequestDialog(String JavaDoc from){
30         super(WhisperIM.MainWindow,Lang.gs("subscription request"),false);
31         From=from;
32         Container cp=getContentPane();
33         cp.setLayout(layout);
34         
35         String JavaDoc fromtxt=StringUtils.parseBareAddress(From);
36         // See if already on contacts list
37
boolean oncontactslist=false;
38         ContactListItem cli=(ContactListItem)WhisperIM.MainWindow.Contacts.get("jabber"+StringUtils.parseBareAddress(From));
39         if(cli!=null){
40             oncontactslist=true;
41             fromtxt=cli.toString()+" ("+fromtxt+")";
42         }
43         infoLbl.setText("<html><p>"+infoLbl.getText()+fromtxt+"</p></html>");
44         
45         add.setToolTipText(Lang.gs("add to contacts_tt"));
46         add.setMnemonic(Lang.s2k("add to contacts_mn"));
47         add.setVisible(!oncontactslist);
48         
49         infoPanel.add(infoLbl);
50         infoPanel.add(add);
51         SpringUtilities.makeCompactGrid(infoPanel,2,1,GAP,GAP,GAP,GAP);
52         
53         acceptBtn.addActionListener(click);
54         acceptBtn.setDefaultCapable(true);
55         acceptBtn.setMnemonic(Lang.s2k("accept_mn"));
56         acceptBtn.setToolTipText(Lang.gs("accept_tt"));
57         denyBtn.setToolTipText(Lang.gs("deny_tt"));
58         denyBtn.setMnemonic(Lang.s2k("deny_mn"));
59         denyBtn.addActionListener(click);
60         btnPanel.add(denyBtn);
61         btnPanel.add(acceptBtn);
62         
63         cp.add(infoPanel,BorderLayout.CENTER);
64         cp.add(btnPanel,BorderLayout.SOUTH);
65         getRootPane().setDefaultButton(acceptBtn);
66         pack();
67     }
68     
69     public void show(){
70         setLocationRelativeTo(WhisperIM.MainWindow);
71         super.show();
72     }
73     
74     public void doAccept(){
75         setCursor(GUI.WAIT);
76         Presence p=new Presence(Presence.Type.SUBSCRIBED);
77         try{
78             p.setFrom(WhisperIM.MainWindow.Conn.getUser());
79             p.setTo(From);
80             WhisperIM.MainWindow.Conn.sendPacket(p);
81         }
82         catch(Exception JavaDoc e){
83             setCursor(GUI.NORMAL);
84             GUI.showError(this,"subscription request",null,From+"\r\n"+e.getMessage());
85             return;
86         }
87         setCursor(GUI.NORMAL);
88         WhisperIM.MainWindow.contactListPanel.tree.repaint();
89         dispose();
90         if(add.isSelected() && add.isVisible()){
91             AddContactDialog acd=new AddContactDialog(WhisperIM.MainWindow);
92             acd.setID(From);
93             acd.setName(StringUtils.parseName(From));
94             acd.show();
95         }
96     }
97     
98     
99     final class BtnClick implements ActionListener{
100         public void actionPerformed(ActionEvent ae){
101             //get button
102
String JavaDoc b=ae.getActionCommand();
103             // call method depending on button clicked
104
if(b.equals(denyBtn.getText())){
105                 dispose();
106                 return;
107             }
108             if(b.equals(acceptBtn.getText())){
109                 doAccept();
110                 return;
111             }
112         }
113     }
114     
115 }
Popular Tags