KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > AddContactDialog


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 AddContactDialog extends JDialog implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, RootPaneContainer, Serializable JavaDoc, SwingConstants,WindowConstants{
14     
15     private static final int GAP=10;
16     
17     final JPanel whoPanel=new JPanel(new SpringLayout());
18         final JLabel idLbl=new JLabel(Lang.gs("id"));
19         final JTextField id=new JTextField(20);
20         final JLabel nameLbl=new JLabel(Lang.gs("name"));
21         final JTextField name=new JTextField(20);
22     
23     final JPanel groupPanel=new JPanel(new GridLayout(3,1,GAP,GAP));
24         final ButtonGroup group=new ButtonGroup();
25         final JRadioButton groupsRB=new JRadioButton(Lang.gs("group")+": ");
26         final JComboBox groupsList=new JComboBox(new Vector(WhisperIM.MainWindow.Groups.values()));
27         final JRadioButton newGroupRB=new JRadioButton(Lang.gs("group new"));
28         final JTextField newGroupName=new JTextField(8);
29         final JRadioButton noGroupRB=new JRadioButton(Lang.gs("group none"));
30     
31     final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP));
32         final JButton addBtn=new JButton(Lang.gs("add"),Icons.ADD_CONTACT);
33         final JButton cancelBtn=new JButton(Lang.gs("cancel"),Icons.CANCEL);
34     
35     final BtnClick click=new BtnClick(); // button action listener
36
final RadioBtnClick radioBtnClick=new RadioBtnClick();
37     
38     public AddContactDialog(Component c){
39         super(WhisperIM.MainWindow,Lang.gs("contact_add") ,false);
40         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
41         Container cp=getContentPane();
42         cp.setLayout(new BorderLayout(GAP,GAP));
43         
44         GUI.connect(idLbl,id,"id_mn","id_tt");
45         whoPanel.add(idLbl);
46         whoPanel.add(id);
47         GUI.connect(nameLbl,name,"name_mn","name_tt");
48         whoPanel.add(nameLbl);
49         whoPanel.add(name);
50         SpringUtilities.makeCompactGrid(whoPanel,2,2,GAP,GAP,GAP,GAP);
51         cp.add(whoPanel,BorderLayout.NORTH);
52         
53         groupPanel.setBorder(new TitledBorder(Lang.gs("group")));
54         group.add(groupsRB);
55         group.add(newGroupRB);
56         group.add(noGroupRB);
57         groupsRB.addActionListener(radioBtnClick);
58         groupsRB.setToolTipText(Lang.gs("group_tt"));
59         newGroupRB.setToolTipText(Lang.gs("group new_tt"));
60         newGroupRB.addActionListener(radioBtnClick);
61         noGroupRB.addActionListener(radioBtnClick);
62         groupsList.setToolTipText(Lang.gs("group_tt"));
63         newGroupName.setToolTipText(Lang.gs("group new_tt"));
64         noGroupRB.setToolTipText(Lang.gs("group none_tt"));
65         groupsRB.setSelected(true);
66         groupsList.setEditable(false);
67         if(groupsList.getMaximumRowCount()>groupsList.getItemCount() ){
68             groupsList.setMaximumRowCount(groupsList.getItemCount());
69         }
70         if(groupsList.getItemCount()==0){
71             groupsList.setEnabled(false);
72             newGroupRB.setSelected(true);
73             groupsRB.setEnabled(false);
74         }
75         else{
76             newGroupName.setEnabled(false);
77         }
78         groupPanel.add(GUI.panel(groupsRB,groupsList,0));
79         groupPanel.add(GUI.panel(newGroupRB,newGroupName,0));
80         JPanel noGroupPanel=new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
81         noGroupPanel.add(noGroupRB);
82         groupPanel.add(noGroupPanel);
83         cp.add(groupPanel,BorderLayout.CENTER);
84         
85         addBtn.setToolTipText(Lang.gs("add_tt"));
86         addBtn.setMnemonic(Lang.s2k("add_mn"));
87         addBtn.addActionListener(click);
88         addBtn.setDefaultCapable(true);
89         btnPanel.add(addBtn);
90         cancelBtn.setMnemonic(Lang.s2k("cancel_mn"));
91         cancelBtn.addActionListener(click);
92         btnPanel.add(cancelBtn);
93         cp.add(btnPanel,BorderLayout.SOUTH);
94         //help key
95
getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); //put in help key for action help
96
getRootPane().getActionMap().put("help",new HelpAction(this,"contacts.html#adding"));
97         getRootPane().setDefaultButton(addBtn);
98         pack();
99         setLocationRelativeTo(c);
100         addKeyListener(new KeyAction());
101         addMouseListener(new MouseAction());
102     }
103     
104     public void setID(String JavaDoc value){
105         id.setText(value);
106     }
107     
108     public void setName(String JavaDoc value){
109         name.setText(value);
110     }
111     
112     public void setGroup(DefaultMutableTreeNode JavaDoc g){
113         groupsList.setSelectedItem(g);
114     }
115     
116     public void doAdd(){
117         try{
118         // validate first
119
if(id.getText().equals("")){
120             GUI.showInfo(this,"contact_add","no address");
121             id.requestFocus();
122             return;
123         }
124         String JavaDoc ID=id.getText().trim().toLowerCase();
125         if(ID.indexOf("@")!=-1){
126             ID=StringUtils.parseBareAddress(ID);
127         }
128         //~ if(StringUtils.parseName(ID).equals("") || StringUtils.parseServer(ID).equals("")){
129
//~ GUI.showInfo(this,"contact_add","invalid address");
130
//~ id.requestFocus();
131
//~ return;
132
//~ }
133
String JavaDoc Name=name.getText();
134         if(Name.equals("")){
135             if(ID.indexOf("@")!=-1){
136                 Name=StringUtils.parseName(ID);
137             }
138             else{
139                 Name=ID;
140             }
141         }
142         // check doesn't exist already
143
ContactListItem ecli=(ContactListItem)WhisperIM.MainWindow.Contacts.get("jabber"+StringUtils.parseBareAddress(ID));
144         if(ecli!=null){
145             GUI.showInfo(this,"contact_add","already on list",ecli.toString()+"\r\n"+ID);
146             id.requestFocus();
147             return;
148         }
149         // check for new group
150
if(newGroupRB.isSelected()){
151             String JavaDoc NewGroup=newGroupName.getText();
152             if(NewGroup.equals("")){
153                 GUI.showInfo(this,"contact_add","group missing name");
154                 newGroupName.requestFocus();
155                 return;
156             }
157             // check doesn't already exist
158
Object JavaDoc tmp=WhisperIM.MainWindow.Groups.get(NewGroup);
159             if(tmp!=null){
160                 GUI.showInfo(this,"contact_add","group already exists");
161                 newGroupName.requestFocus();
162                 return;
163             }
164             // create the group
165
WhisperIM.MainWindow.addGroup(NewGroup);
166             // add the contact
167
WhisperIM.MainWindow.addUser("jabber",ID,Name,NewGroup);
168             dispose();
169             return;
170         }
171         if(noGroupRB.isSelected()){
172             WhisperIM.MainWindow.addUser("jabber",ID,Name,null);
173             dispose();
174             return;
175         }
176         // existing group
177
ContactListItem cli=(ContactListItem)(((DefaultMutableTreeNode JavaDoc)groupsList.getSelectedItem()).getUserObject());
178         WhisperIM.MainWindow.addUser("jabber",ID,Name,cli.toString());
179         dispose();
180         return;
181         }//end try
182
catch(Exception JavaDoc e){
183             GUI.showError(this,"contact_add",null,e.getMessage());
184         }
185     }
186     
187     final class BtnClick implements ActionListener{
188         public void actionPerformed(ActionEvent ae){
189             //get button
190
String JavaDoc b=ae.getActionCommand();
191             // call method depending on button clicked
192
if(b.equals(cancelBtn.getText())){
193                 dispose();
194                 return;
195             }
196             if(b.equals(addBtn.getText())){
197                 doAdd();
198                 return;
199             }
200         }
201     }
202     
203     
204     // en/disables the fields depending on option chosen
205
final class RadioBtnClick implements ActionListener{
206         public void actionPerformed(ActionEvent ae){
207             if(groupsRB.isSelected()){
208                 newGroupName.setEnabled(false);
209                 groupsList.setEnabled(true);
210                 groupsList.requestFocus();
211                 return;
212             }
213             if(newGroupRB.isSelected()){
214                 newGroupName.setEnabled(true);
215                 groupsList.setEnabled(false);
216                 newGroupName.requestFocus();
217                 return;
218             }
219             if(noGroupRB.isSelected()){
220                 groupsList.setEnabled(false);
221                 newGroupName.setEnabled(false);
222                 return;
223             }
224         }
225     }
226 }
Popular Tags