KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JContactsPanel


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.tree.*;
8 import javax.accessibility.Accessible JavaDoc;
9 import org.jivesoftware.smack.*;
10 import org.jivesoftware.smack.packet.*;
11
12 /** The contacts (aka roster or buddy list) panel for the main window.*/
13 public final class JContactsPanel extends JPanel implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, Serializable JavaDoc{
14     
15     final private JPanel btnPanel=new JPanel(new GridLayout(1,1,0,0));
16     final private JButton add=new JButton("",Icons.ADD_CONTACT);
17     public DefaultMutableTreeNode top=new DefaultMutableTreeNode(new ContactListItem());
18     public DefaultTreeModel treeModel=new DefaultTreeModel(top);
19     final public JTree tree=new JTree(treeModel);
20     final private JScrollPane treeSP=new JScrollPane(tree);
21     
22     final BtnClick click=new BtnClick(); // button action listener
23

24     
25     final JPopupMenu groupPopup=new JPopupMenu();
26         final JMenuItem addContact=new JMenuItem("",Icons.ADD_CONTACT);
27         final JMenuItem renameGroup=new JMenuItem("",Icons.RENAME);
28         final JMenuItem deleteGroup=new JMenuItem("",Icons.DELETE_CONTACT);
29     
30     
31     final JPopupMenu userPopup=new JPopupMenu();
32         final JMenuItem sendMsg=new JMenuItem("",Icons.MESSAGE);
33         final JMenuItem chatWith=new JMenuItem("",Icons.CHAT);
34         final JMenuItem viewProfile=new JMenuItem("",Icons.PROFILE);
35         final JMenuItem renameContact=new JMenuItem("",Icons.RENAME);
36         final JMenuItem deleteContact=new JMenuItem("",Icons.DELETE_CONTACT);
37     
38     final JPopupMenu servicePopup=new JPopupMenu();
39         final JMenuItem exploreService=new JMenuItem("",Icons.DISCO);
40         final JMenuItem renameService=new JMenuItem("",Icons.RENAME);
41         final JMenuItem deleteService=new JMenuItem("",Icons.DELETE_CONTACT);
42     
43     final MenuItemListener menuClick=new MenuItemListener();
44     
45     ContactListItem selectedItem;
46     DefaultMutableTreeNode selectedNode;
47     
48     private boolean enabled=true;
49     final JPanel thisPanel=this;
50     
51     /** creates a new JContactsPanel instance.*/
52     public JContactsPanel(){
53         super(new BorderLayout());
54         add.addActionListener(click);
55         add.setMargin(new Insets(0,0,0,0));
56         //delete.addActionListener(click);
57
btnPanel.add(add);
58         //btnPanel.add(delete);
59
ToolTipManager.sharedInstance().registerComponent(tree);
60         ContactListCellRenderer clcr=new ContactListCellRenderer();
61         tree.setCellRenderer(clcr);
62         tree.setDragEnabled(true);
63         tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
64         tree.setExpandsSelectedPaths(true);
65         tree.setVisibleRowCount(10);
66         tree.setToggleClickCount(1);
67         tree.setScrollsOnExpand(true);
68         tree.setShowsRootHandles(false);
69         treeSP.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
70         if(WhisperIM.isMac){
71             treeSP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
72         }
73         else{
74             treeSP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
75         }
76         add(btnPanel,BorderLayout.NORTH);
77         add(treeSP,BorderLayout.CENTER);
78         
79         // create pop menus
80
groupPopup.add(addContact);
81         groupPopup.addSeparator();
82         groupPopup.add(renameGroup);
83         groupPopup.add(deleteGroup);
84         addContact.addActionListener(menuClick);
85         renameGroup.addActionListener(menuClick);
86         deleteGroup.addActionListener(menuClick);
87         
88         userPopup.add(sendMsg);
89         userPopup.add(chatWith);
90         userPopup.add(viewProfile);
91         userPopup.addSeparator();
92         userPopup.add(renameContact);
93         userPopup.add(deleteContact);
94         sendMsg.addActionListener(menuClick);
95         chatWith.addActionListener(menuClick);
96         viewProfile.addActionListener(menuClick);
97         renameContact.addActionListener(menuClick);
98         deleteContact.addActionListener(menuClick);
99         
100         servicePopup.add(exploreService);
101         exploreService.addActionListener(menuClick);
102         servicePopup.addSeparator();
103         servicePopup.add(renameService);
104         renameService.addActionListener(menuClick);
105         servicePopup.add(deleteService);
106         deleteService.addActionListener(menuClick);
107         
108         tree.addMouseListener(new MouseClickListener());
109         // Key listners
110
tree.getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0),"delete");
111         tree.getActionMap().put("delete", new KeyAction("delete"));
112         
113         tree.getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),"enter");
114         tree.getActionMap().put("enter", new KeyAction("enter"));
115     }
116     
117     public void setLang(){
118         add.setText(Lang.gs("contact_add"));
119         //delete.setText(Lang.gs("contact_delete"));
120
//popups
121
addContact.setText(Lang.gs("pu add contact"));
122         renameGroup.setText(Lang.gs("pu rename group"));
123         deleteGroup.setText(Lang.gs("pu delete group"));
124         
125         sendMsg.setText(Lang.gs("pu send msg"));
126         chatWith.setText(Lang.gs("pu chat with"));
127         viewProfile.setText(Lang.gs("pu profile"));
128         renameContact.setText(Lang.gs("pu rename contact"));
129         deleteContact.setText(Lang.gs("contact_delete"));
130         
131         exploreService.setText(Lang.gs("pu explore service"));
132         renameService.setText(Lang.gs("pu rename service"));
133         deleteService.setText(Lang.gs("pu delete service"));
134     }
135     
136     public void enable(){
137         enabled=true;
138         add.setEnabled(true);
139         //delete.setEnabled(true);
140
}
141     
142     public void disable(){
143         enabled=false;
144         add.setEnabled(false);
145         //delete.setEnabled(false);
146
}
147     
148     public boolean getEnabledState(){
149         return enabled;
150     }
151     
152     // Pop-up Functions
153

154     public void doDelete(){
155         if(selectedNode==null){return;} //shouldnt happen
156
ContactListItem cli=(ContactListItem) selectedNode.getUserObject();
157         if(cli==null){ //shouldnt happen
158
System.err.println("Debug: no contactlistitem at JContactsPanel doDelete");
159             return;
160         }
161         if(cli.getType()==-1){return;} //cant delete entire contacts list
162
if(cli.getType()==ContactListItem.GROUP){
163             if(JOptionPane.showConfirmDialog(this,Lang.gs("delete group")+" "+cli.toString(),Lang.gs("confirm delete"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE)!=JOptionPane.YES_OPTION){
164                 return;
165             }
166             RosterGroup rg=WhisperIM.MainWindow.roster.getGroup(cli.toString());
167             if(rg==null){//shouldnt happen
168
System.err.println("Debug: no rostergroup at JContactsPanel doDelete");
169                 return;
170             }
171             Iterator i=rg.getEntries() ;
172             RosterEntry re;
173             while(i.hasNext()){
174                 re=(RosterEntry)i.next();
175                 rg.removeEntry(re);
176             }
177             selectedNode.removeAllChildren();
178             WhisperIM.MainWindow.Groups.remove(cli.toString());
179         }
180         else{
181             if(JOptionPane.showConfirmDialog(this,Lang.gs("delete contact")+" "+cli.toString(),Lang.gs("confirm delete"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE)!=JOptionPane.YES_OPTION){
182                 return;
183             }
184             
185             WhisperIM.MainWindow.roster.removeEntry(WhisperIM.MainWindow.roster.getEntry(cli.getID()));
186             WhisperIM.MainWindow.Contacts.remove("jabber"+cli.getID());
187             
188         }
189         treeModel.removeNodeFromParent(selectedNode);
190         tree.repaint();
191     }
192     
193     
194     public void doRename(String JavaDoc newname){
195         if(newname==null){
196             return;
197         }
198         if(newname.equals("")){
199             return;
200         }
201         if(selectedItem.getType()==ContactListItem.GROUP){
202             // check new name doesnt already exist
203
Object JavaDoc o=WhisperIM.MainWindow.Groups.get(newname);
204             if(o!=null){
205                 GUI.showInfo(this,"pu rename group","group already exists",newname);
206                 return;
207             }
208             WhisperIM.MainWindow.roster.getGroup(selectedItem.toString()).setName(newname);
209             WhisperIM.MainWindow.Groups.remove(selectedItem.toString());
210             selectedItem.setLabel(newname);
211             WhisperIM.MainWindow.Groups.put(newname,selectedNode);
212             tree.repaint();
213             return;
214         }
215         RosterEntry re=WhisperIM.MainWindow.roster.getEntry(selectedItem.getID());
216         if(re==null){ //shouldnt happen
217
System.err.println("Debug: Missing roster entry:"+selectedItem.getID()+" at JContactsPanel doRename");
218             return;
219         }
220         re.setName(newname);
221         selectedItem.setLabel(newname);
222         tree.repaint();
223     }
224     
225     public void sendMessage(){
226         ComposeMessagePanel cmp=new ComposeMessagePanel();
227         WhisperIM.MainWindow.chatPane.addTab("",Icons.MESSAGE,cmp);
228         cmp.setTo(selectedItem);
229         WhisperIM.MainWindow.chatPane.setSelectedComponent(cmp);
230         cmp.doFocus();
231     }
232
233     // INNER CLASSES
234

235     final class BtnClick implements ActionListener{
236         public void actionPerformed(ActionEvent ae){
237             //get button
238
String JavaDoc b=ae.getActionCommand();
239             // call method depending on button clicked
240
if(b.equals(add.getText())){
241                 (new AddContactDialog(WhisperIM.MainWindow)).show();
242                 return;
243             }
244         }
245     }
246     
247     final class MouseClickListener extends MouseAdapter{
248         
249         public void mousePressed(MouseEvent e){
250             if(!enabled){
251                 return;
252             }
253             if(e.isPopupTrigger()){
254                 showPopUp(e);
255                 return;
256             }
257              if(e.getClickCount()==2){
258                 int selRow = tree.getRowForLocation(e.getX(), e.getY());
259                 if(selRow==-1){return;} // no item
260
selectedNode=(DefaultMutableTreeNode)(tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent() );
261                 if(selectedNode==null){
262                     return;
263                 }
264                 selectedItem=(ContactListItem) selectedNode.getUserObject();
265                 if(selectedItem==null){
266                     return;
267                 }
268                 if(selectedItem.getType()!=ContactListItem.USER){
269                     return;
270                 }
271                 String JavaDoc a=WhisperIM.UserPref.getProperty("double_click");
272                 Presence p=selectedItem.getPresence();
273                 if(a.equals("message") || (a.equals("default") && p==null) ){
274                     //tree.setFocusable(false);
275
sendMessage();
276                     //tree.setFocusable(true);
277
return;
278                 }
279                 if(a.equals("chat")){
280                     //tree.setFocusable(false);
281
WhisperIM.MainWindow.chatWith(selectedItem.getID());
282                     //tree.setFocusable(true);
283
return;
284                 }
285                 if(p.getMode()==Presence.Mode.AVAILABLE || p.getMode()==Presence.Mode.CHAT){
286                     //tree.setFocusable(false);
287
WhisperIM.MainWindow.chatWith(selectedItem.getID());
288                     //tree.setFocusable(true);
289
return;
290                 }
291                 //tree.setFocusable(false);
292
sendMessage();
293                 //tree.setFocusable(true);
294
return;
295             }
296                     
297         }
298         
299         public void mouseReleased(MouseEvent e){
300             if(e.isPopupTrigger()){
301                 showPopUp(e);
302             }
303         }
304         
305         private void showPopUp(MouseEvent e){
306             if(!enabled){return;}
307             int selRow = tree.getRowForLocation(e.getX(), e.getY());
308             if(selRow==-1){return;} // no item
309
selectedNode=(DefaultMutableTreeNode)(tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent() );
310             if(selectedNode==null){//shouldnt happen
311
System.err.println("Debug: no node at JContactsPanel showPopUp");
312                 return;
313             }
314             selectedItem=(ContactListItem) selectedNode.getUserObject();
315             if(selectedItem==null){//shouldnt happen
316
System.err.println("Debug: no contactlistitem at JContactsPanel showPopUp");
317                 return;
318             }
319             if(selectedItem.getType()==-1){return;} // right clicked on the root
320
if(selectedItem.getType()==ContactListItem.GROUP){
321                 groupPopup.show(tree,e.getX(),e.getY());
322                 return;
323             }
324             if(selectedItem.getType()==ContactListItem.USER){
325                 userPopup.show(tree,e.getX(),e.getY());
326                 return;
327             }
328             if(selectedItem.getType()==ContactListItem.SERVICE){
329                 servicePopup.show(tree,e.getX(),e.getY());
330                 return;
331             }
332         }
333         
334     }
335     
336     final class MenuItemListener implements ActionListener{
337         public void actionPerformed(ActionEvent ae){
338             String JavaDoc m=ae.getActionCommand();
339             if(m.equals(addContact.getText())){
340                 AddContactDialog acd=new AddContactDialog(WhisperIM.MainWindow);
341                 acd.setGroup(selectedNode);
342                 acd.show();
343                 return;
344             }
345             if(m.equals(deleteGroup.getText()) || m.equals(deleteContact.getText()) || m.equals(deleteService.getText())){
346                 doDelete();
347                 return;
348             }
349             if(m.equals(renameGroup.getText())){
350                 doRename(JOptionPane.showInputDialog(thisPanel, Lang.gs("new group name"),Lang.gs("pu rename group")+" "+selectedItem.toString(), JOptionPane.PLAIN_MESSAGE));
351                 return;
352             }
353             if(m.equals(renameContact.getText())){
354                 doRename(JOptionPane.showInputDialog(thisPanel, Lang.gs("new contact name"),Lang.gs("pu rename contact")+" "+selectedItem.getID(), JOptionPane.PLAIN_MESSAGE));
355                 return;
356             }
357             if(m.equals(renameService.getText())){
358                 doRename(JOptionPane.showInputDialog(thisPanel, Lang.gs("new contact name"),Lang.gs("pu rename service")+" "+selectedItem.toString(), JOptionPane.PLAIN_MESSAGE));
359                 return;
360             }
361             if(m.equals(chatWith.getText())){
362                 WhisperIM.MainWindow.chatWith(selectedItem.getID());
363                 return;
364             }
365             if(m.equals(sendMsg.getText())){
366                 sendMessage();
367                 return;
368             }
369             if(m.equals(viewProfile.getText())){
370                 new ProfileWindow(selectedItem.getID(),WhisperIM.MainWindow);
371                 return;
372             }
373             if(m.equals(exploreService.getText())){
374                 if(WhisperIM.MainWindow.discoWindow==null){
375                     WhisperIM.MainWindow.setCursor(GUI.WAIT);
376                     WhisperIM.MainWindow.discoWindow=new DISCOWindow();
377                     WhisperIM.MainWindow.setCursor(GUI.NORMAL);
378                 }
379                 WhisperIM.MainWindow.discoWindow.setHost(selectedItem.getID());
380                 WhisperIM.MainWindow.discoWindow.show();
381                 WhisperIM.MainWindow.discoWindow.doBrowse(true);
382                 return;
383             }
384         }
385     }
386     
387     final class KeyAction extends AbstractAction{
388         private String JavaDoc name;
389         
390         public KeyAction(String JavaDoc name){
391             super(name);
392             this.name=name;
393         }
394         
395         public void actionPerformed(ActionEvent e) {
396             if(!enabled){
397                 return;
398             }
399             if(name.equals("delete")){
400                 if(tree.getSelectionCount()!=1){
401                     selectedNode=null;
402                     return;
403                 }
404                 selectedNode=(DefaultMutableTreeNode)(tree.getSelectionPath().getLastPathComponent() );
405                 doDelete();
406                 return;
407             }
408             if(name.equals("enter")){
409                 if((!enabled) || tree.getSelectionCount()!=1){
410                     return;
411                 }
412                 TreePath tp=tree.getSelectionPath();
413                 selectedNode=(DefaultMutableTreeNode)(tp.getLastPathComponent() );
414                 if(selectedNode==null){return;} //shouldnt happen
415
selectedItem=(ContactListItem) selectedNode.getUserObject();
416                 if(selectedItem==null){return;}// shouldnt happen
417

418                 if(selectedItem.getType()==-1){return;} // root is selected
419
Rectangle r=tree.getUI().getPathBounds(tree,tp);
420                 int x=(int)r.getX();
421                 int y=(int)r.getY();
422                 
423                 if(selectedItem.getType()==ContactListItem.GROUP){
424                     groupPopup.show(tree,x,y);
425                     return;
426                 }
427                 if(selectedItem.getType()==ContactListItem.USER){
428                     userPopup.show(tree,x,y);
429                     return;
430                 }
431                 if(selectedItem.getType()==ContactListItem.SERVICE){
432                     servicePopup.show(tree,x,y);
433                     return;
434                 }
435             }
436         }
437     }
438 }
Popular Tags