KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > demo > ContactList


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.syncclient.demo;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Button JavaDoc;
23 import java.awt.GridLayout JavaDoc;
24 import java.awt.Label JavaDoc;
25 import java.awt.List JavaDoc;
26 import java.awt.Panel JavaDoc;
27
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
30 import java.awt.event.ItemEvent JavaDoc;
31 import java.awt.event.ItemListener JavaDoc;
32 import java.awt.event.MouseEvent JavaDoc;
33 import java.awt.event.MouseListener JavaDoc;
34 import sync4j.foundation.pdi.common.*;
35
36 /**
37  * The contact list panel.
38  *
39  * @author Fabio Maggi @ Funambol
40  * @author Alessandro Morandi, Giorgio Orsi
41  * @version $Id: ContactList.java,v 1.6 2005/01/19 11:01:11 fabius Exp $
42  */

43 public class ContactList
44 extends Panel JavaDoc
45 implements ActionListener JavaDoc,
46            ItemListener JavaDoc,
47            MouseListener JavaDoc,
48            ConfigurationParameters {
49
50     //---------------------------------------------------------- Constants
51

52     //---------------------------------------------------------- Private data
53

54     //
55
// Approximate width of the vertical
56
// scrollbar for the List component
57
//
58
private static final int LIST_SCROLLBAR_WIDTH = 25;
59
60     //
61
// The window containing this panel
62
//
63
private MainWindow mw = null ;
64     private List JavaDoc list = null ;
65     private Button JavaDoc butModify = null ;
66     private Button JavaDoc butDelete = null ;
67     private Button JavaDoc butNew = null ;
68
69     private Language ln = new Language() ;
70
71     //---------------------------------------------------------- Public methods
72

73     /**
74      * Creates the panel.
75      *
76      * @param mw the window containing this panel
77      */

78     public ContactList(MainWindow mw) {
79
80         Button JavaDoc butSync = null ;
81         Label JavaDoc title = null ;
82
83         Panel JavaDoc editButtonPanel = null ;
84         Panel JavaDoc buttonPanel = null ;
85
86         this.mw = mw;
87
88         setLayout(new BorderLayout JavaDoc());
89         title = new Label JavaDoc(ln.getString ("contact_list"));
90
91         list = new List JavaDoc(10);
92         list.addItemListener (this);
93         list.addMouseListener (this);
94
95         butNew = new Button JavaDoc (ln.getString ("new") ) ;
96         butNew.setActionCommand ("new" ) ;
97         butNew.addActionListener (this ) ;
98
99         butModify = new Button JavaDoc (ln.getString ("modify") ) ;
100         butModify.setActionCommand ("modify" ) ;
101         butModify.addActionListener (this ) ;
102         butModify.setEnabled (false ) ;
103
104         butDelete = new Button JavaDoc (ln.getString ("delete") ) ;
105         butDelete.setActionCommand ("delete" ) ;
106         butDelete.addActionListener (this ) ;
107         butDelete.setEnabled (false ) ;
108
109         butSync = new Button JavaDoc (ln.getString ("synchronize") ) ;
110         butSync.setActionCommand ("sync" ) ;
111         butSync.addActionListener (this ) ;
112
113         editButtonPanel = new Panel JavaDoc();
114         editButtonPanel.setLayout(new GridLayout JavaDoc(1, 3));
115         editButtonPanel.add(butNew);
116         editButtonPanel.add(butModify);
117         editButtonPanel.add(butDelete);
118
119         buttonPanel = new Panel JavaDoc();
120         buttonPanel.setLayout(new GridLayout JavaDoc(2, 1));
121         buttonPanel.add(editButtonPanel);
122         buttonPanel.add(butSync);
123
124         add(title,BorderLayout.NORTH);
125         add(list,BorderLayout.CENTER);
126         add(buttonPanel,BorderLayout.SOUTH);
127     }
128
129     /**
130      * Invoked when an action occurs (i.e. a button is pressed).
131      *
132      * @param evt the occurred action
133      */

134     public void actionPerformed(ActionEvent JavaDoc evt) {
135         if (evt.getActionCommand().equals("sync")) {
136             mw.show(KEY_SYNC);
137         }
138         else if (evt.getActionCommand().equals("modify") ) {
139             mw.show(KEY_CONTACTMODIFY);
140         }
141         else if (evt.getActionCommand().equals("delete") ) {
142             mw.deleteContact();
143         }
144         else if (evt.getActionCommand().equals("new" ) ) {
145             mw.show(KEY_CONTACTNEW);
146         }
147     }
148
149     /**
150      * Invoked when an item has been selected or deselected by the user.
151      * This method sets the current index to the index of the selected idem and
152      * enables the Modify and Delete buttons.
153      *
154      * @param evt the occurred event
155      */

156     public void itemStateChanged(ItemEvent JavaDoc evt) {
157         mw.setCurrentIndex((Integer JavaDoc) evt.getItem());
158         butModify.setEnabled(true);
159         butDelete.setEnabled(true);
160     }
161
162     /**
163      * Invoked when the mouse button has been clicked
164      * (pressed and released) on a component.
165      * This method opens the modification page
166      * for the selected contact if a double
167      * click is detected.
168      *
169      * @param evt the occurred event
170      */

171     public void mouseClicked(MouseEvent JavaDoc evt) {
172         if (evt.getClickCount()>=2) {
173             if (butModify.isEnabled() &&
174                 evt.getX()<=(getSize().width - LIST_SCROLLBAR_WIDTH) &&
175                 evt.getY() <= (mw.contacts.size() * list.getPreferredSize().height / 10)) {
176                 mw.show(KEY_CONTACTMODIFY);
177             }
178         }
179     }
180
181     /**
182      * Invoked when a mouse button has been pressed on a component.
183      * Not used.
184      *
185      * @param evt the occurred event
186      */

187     public void mousePressed(MouseEvent JavaDoc evt) {
188         // do nothing
189
}
190
191     /**
192      * Invoked when a mouse button has been released on a component.
193      * Not used.
194      *
195      * @param evt the occurred event
196      */

197     public void mouseReleased(MouseEvent JavaDoc evt) {
198         // do nothing
199
}
200
201     /**
202      * Invoked when the mouse enters a component.
203      * Not used.
204      *
205      * @param evt the occurred event
206      */

207     public void mouseEntered(MouseEvent JavaDoc evt) {
208         // do nothing
209
}
210
211     /**
212      * Invoked when the mouse exits a component.
213      * Not used.
214      *
215      * @param evt the occurred event
216      */

217     public void mouseExited(MouseEvent JavaDoc evt) {
218         // do nothing
219
}
220
221     //---------------------------------------------------------- Protected methods
222

223     /**
224      * Fills the list with the loaded contacts.
225      * This method also disables the Modify and Delete buttons.
226      */

227     protected void fillList() {
228
229         String JavaDoc tmpDisplayName = null;
230         DemoContact tmpContact = null;
231
232         list.removeAll();
233
234         for (int i=0, l = mw.contacts.size(); i < l; i++) {
235             tmpContact = (DemoContact) mw.contacts.elementAt(i);
236             tmpDisplayName = (String JavaDoc) tmpContact.getContact().getName().
237                                         getDisplayName().getPropertyValue();
238             if (tmpDisplayName!=null && !tmpDisplayName.equals("")) {
239                 list.add(tmpDisplayName);
240             }
241             else {
242
243                 String JavaDoc display_name = "";
244
245                 if (tmpContact.getContact().getName().
246                     getLastName().getPropertyValue() != null
247                     && !((String JavaDoc)tmpContact.getContact().getName().
248                         getLastName().getPropertyValue()).trim().equals("")) {
249
250                     display_name += (String JavaDoc)tmpContact.getContact().
251                                 getName().getLastName().getPropertyValue();
252                 }
253
254                 if (tmpContact.getContact().getName().
255                         getFirstName().getPropertyValue() != null
256                     && !((String JavaDoc)tmpContact.getContact().getName().
257                         getFirstName().getPropertyValue()).trim().equals("")) {
258
259                 if (!display_name.equals("")) {
260                     display_name += ",";
261                 }
262
263                 display_name += (String JavaDoc)tmpContact.getContact().getName().
264                                             getFirstName().getPropertyValue();
265
266                 }
267
268                 if (display_name.equals("")) {
269                     display_name = "Unknow";
270                 }
271
272                 tmpContact.getContact().getName().getDisplayName().
273                                            setPropertyValue(display_name);
274
275
276                 list.add(display_name);
277             }
278         }
279
280         //
281
// Disable buttons: no item can be already selected
282
//
283
butModify.setEnabled (false) ;
284         butDelete.setEnabled (false) ;
285     }
286
287     //---------------------------------------------------------- Private methods
288

289 }
Popular Tags