1 /*** 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions are 4 * met : 5 * 6 * . Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * 9 * . Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * . The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $Id: AbstractUserListView.java,v 1.5 2005/09/10 14:06:22 mat007 Exp $ 29 */ 30 31 package jtge.sample.chat.common.ui; 32 33 import java.awt.Dimension; 34 import javax.swing.DefaultListModel; 35 import javax.swing.JComponent; 36 import javax.swing.JList; 37 import javax.swing.JPanel; 38 import javax.swing.JScrollPane; 39 import jtge.engine.user.IUser; 40 41 /*** 42 * Provides a base class for user list views. 43 * 44 * @author Mathieu Champlon 45 * @version $Revision: 1.5 $ $Date: 2005/09/10 14:06:22 $ 46 */ 47 public class AbstractUserListView extends JPanel 48 { 49 private static final long serialVersionUID = -7734102879437612043L; 50 private static final int WIDTH = 150; 51 private static final int HEIGHT = 250; 52 private final JList list; 53 private final DefaultListModel model; 54 55 /*** 56 * Create an abstract user list view. 57 */ 58 public AbstractUserListView() 59 { 60 model = new DefaultListModel(); 61 list = new JList( model ); 62 add( createScrollPane( list ) ); 63 } 64 65 private JComponent createScrollPane( final JComponent component ) 66 { 67 final JScrollPane scrollPane = new JScrollPane( component ); 68 scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED ); 69 scrollPane.setPreferredSize( new Dimension( WIDTH, HEIGHT ) ); 70 scrollPane.setAutoscrolls( true ); 71 return scrollPane; 72 } 73 74 /*** 75 * Add a user to the list. 76 * 77 * @param user the user 78 */ 79 protected final void add( final IUser user ) 80 { 81 model.addElement( user.getIdentifier() ); 82 } 83 84 /*** 85 * Remove a user from the list. 86 * 87 * @param user the user 88 */ 89 protected final void remove( final IUser user ) 90 { 91 model.removeElement( user.getIdentifier() ); 92 } 93 94 /*** 95 * Remove all users from the list. 96 */ 97 protected final void clear() 98 { 99 model.removeAllElements(); 100 } 101 }