KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JoinSecureChannelDialog


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
12
13 public final class JoinSecureChannelDialog extends JDialog implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, RootPaneContainer, Serializable JavaDoc, SwingConstants,WindowConstants{
14     
15     private static final int GAP=7;
16     
17     final Box panel=new Box(BoxLayout.Y_AXIS);
18     final JLabel nicknameLbl=new JLabel(Lang.gs("form_nick"));
19     final JTextField nickname=new JTextField(15);
20     final ButtonGroup actionGroup=new ButtonGroup();
21         final Box joinBox=new Box(BoxLayout.Y_AXIS);
22             final JRadioButton joinRB=new JRadioButton(Lang.gs("join channel"),true);
23             final JPanel joinPanel=new JPanel(new FlowLayout(FlowLayout.LEFT,GAP,GAP));
24                 final JLabel whereLbl=new JLabel(Lang.gs("provider id"));
25                 final java.util.Vector JavaDoc data=new java.util.Vector JavaDoc(WhisperIM.MainWindow.Contacts.values());
26                 final JComboBox where=new JComboBox(data);
27         final Box provideBox=new Box(BoxLayout.Y_AXIS);
28             final JRadioButton provideRB=new JRadioButton(Lang.gs("provide channel"),false);
29             final JPanel providePanel=new JPanel(new GridLayout(3,1,GAP,GAP));
30                 final ButtonGroup allowGroup=new ButtonGroup();
31                 final JRadioButton askMeRB=new JRadioButton(Lang.gs("ask me"),true);
32                 final JRadioButton allowAllRB=new JRadioButton(Lang.gs("allow anyone"),false);
33                 final JCheckBox allowContacts=new JCheckBox(Lang.gs("allow contacts"),true);
34     
35     final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP));
36         final JButton goBtn=new JButton(Lang.gs("secure channel"),Icons.SECURE_CHANNEL);
37         final JButton cancelBtn=new JButton(Lang.gs("cancel"),Icons.CANCEL);
38     
39     final BtnClick click=new BtnClick();
40     final RadioBtnClick rbClick=new RadioBtnClick();
41     
42     public JoinSecureChannelDialog(){
43         super(WhisperIM.MainWindow,Lang.gs("secure channel"),false);
44         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
45         Container cp=getContentPane();
46         cp.setLayout(new BorderLayout(GAP,GAP));
47         
48         Insets i=new Insets(GAP,GAP,GAP,GAP);
49         joinPanel.setBorder(new EmptyBorder(i));
50         providePanel.setBorder(new EmptyBorder(i));
51         
52         panel.add(GUI.panel(nicknameLbl,nickname,GAP));
53         nickname.setText(WhisperIM.MainWindow.MyProfile.NickName);
54         actionGroup.add(joinRB);
55         actionGroup.add(provideRB);
56         joinRB.addActionListener(rbClick);
57         provideRB.addActionListener(rbClick);
58         askMeRB.addActionListener(rbClick);
59         allowAllRB.addActionListener(rbClick);
60         
61         joinPanel.add(whereLbl);
62         where.setToolTipText(Lang.gs("provider id_tt"));
63         where.setEditable(true);
64         java.util.Collections.sort(data,new ContactComparator());
65         if(where.getMaximumRowCount()>where.getItemCount() ){
66             where.setMaximumRowCount(where.getItemCount()); // make sure we dont show empty space
67
}
68         where.setSelectedItem("");
69         joinPanel.add(where);
70         joinPanel.add(Box.createGlue());
71         JPanel p1=new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
72         p1.add(joinRB);
73         joinBox.add(p1);
74         joinBox.add(joinPanel);
75         
76         allowGroup.add(askMeRB);
77         allowGroup.add(allowAllRB);
78         providePanel.add(askMeRB);
79         providePanel.add(allowAllRB);
80         providePanel.add(allowContacts);
81         JPanel p2=new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
82         p2.add(provideRB);
83         provideBox.add(p2);
84         provideBox.add(providePanel);
85         
86         panel.add(joinBox);
87         panel.add(provideBox);
88         cp.add(panel,BorderLayout.CENTER);
89         
90         goBtn.setDefaultCapable(true);
91         goBtn.addActionListener(click);
92         cancelBtn.setMnemonic(Lang.s2k("cancel_mn"));
93         cancelBtn.addActionListener(click);
94         btnPanel.add(goBtn);
95         btnPanel.add(cancelBtn);
96         cp.add(btnPanel,BorderLayout.SOUTH);
97         providePanel.setEnabled(false);
98         askMeRB.setEnabled(false);
99         allowAllRB.setEnabled(false);
100         allowContacts.setEnabled(false);
101         //help key
102
getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); //put in help key for action help
103
getRootPane().getActionMap().put("help",new HelpAction(this,"schannel.html#join"));
104         pack();
105         getRootPane().setDefaultButton(goBtn);
106         where.requestFocusInWindow();
107         addKeyListener(new KeyAction());
108         addMouseListener(new MouseAction());
109     }
110     
111     public void show(){
112         setLocationRelativeTo(WhisperIM.MainWindow);
113         super.show();
114     }
115     
116     public void doGo(){
117         if(WhisperIM.MainWindow.secureChannelPanel!=null){
118             dispose();
119             WhisperIM.MainWindow.chatPane.setSelectedComponent(WhisperIM.MainWindow.secureChannelPanel);
120             return;
121         }
122         if(nickname.getText().trim().equals("")){
123             GUI.showInfo(this,"secure channel","need nickname");
124             nickname.requestFocusInWindow();
125             return;
126         }
127         SecureChannelPanel scp;
128         if(joinRB.isSelected()){
129             String JavaDoc address;
130             String JavaDoc name="";
131             if(where.getSelectedIndex()==-1){
132                 address=where.getSelectedItem().toString();
133             }
134             else{
135                 ContactListItem cli=(ContactListItem) where.getSelectedItem();
136                 address=cli.getID();
137                 name="("+cli.toString()+")";
138             }
139             if(address==null || address.equals("")){
140                 GUI.showInfo(this,"secure channel","no address");
141                 where.requestFocus();
142                 return;
143             }
144             address=StringUtils.parseBareAddress(address);
145             scp=new SecureChannelPanel(address,name,nickname.getText().trim().toLowerCase());
146         }
147         else{
148             scp=new SecureChannelPanel(nickname.getText().trim().toLowerCase(),allowContacts.isSelected(),allowAllRB.isSelected());
149         }
150         WhisperIM.MainWindow.chatPane.addTab(Lang.gs("channel"),Icons.SECURE_CHANNEL,scp,Lang.gs("secure channel"));
151         dispose();
152         scp.go();
153         WhisperIM.MainWindow.secureChannelPanel=scp;
154     }
155     
156     final class BtnClick implements ActionListener{
157         public void actionPerformed(ActionEvent ae){
158             //get button
159
String JavaDoc b=ae.getActionCommand();
160             // call method depending on button clicked
161
if(b.equals(cancelBtn.getText())){
162                 dispose();
163                 return;
164             }
165             if(b.equals(goBtn.getText())){
166                 doGo();
167                 return;
168             }
169         }
170     }
171     
172     // en/disables the panel depending on option chosen
173
final class RadioBtnClick implements ActionListener{
174         public void actionPerformed(ActionEvent ae){
175             if(joinRB.isSelected()){
176                 joinPanel.setEnabled(true);
177                 where.setEnabled(true);
178                 providePanel.setEnabled(false);
179                 askMeRB.setEnabled(false);
180                 allowAllRB.setEnabled(false);
181                 allowContacts.setEnabled(false);
182                 where.requestFocusInWindow();
183                 return;
184             }
185             else{
186                 joinPanel.setEnabled(false);
187                 where.setEnabled(false);
188                 providePanel.setEnabled(true);
189                 askMeRB.setEnabled(true);
190                 allowAllRB.setEnabled(true);
191                 allowContacts.setEnabled(askMeRB.isSelected());
192                 askMeRB.requestFocusInWindow();
193             }
194         }
195     }
196 }
Popular Tags