001    /*
002      Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>, 
003                              Laurent Martelli <laurent@aopsys.com>
004      
005      This program is free software; you can redistribute it and/or modify
006      it under the terms of the GNU Lesser General Public License as
007      published by the Free Software Foundation; either version 2 of the
008      License, or (at your option) any later version.
009    
010      This program is distributed in the hope that it will be useful,
011      but WITHOUT ANY WARRANTY; without even the implied warranty of
012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013      GNU Lesser General Public License for more details.
014    
015      You should have received a copy of the GNU Lesser General Public License
016      along with this program; if not, write to the Free Software
017      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
018    
019    package org.objectweb.jac.aspects.gui.swing;
020    
021    import org.objectweb.jac.aspects.gui.*;
022    import org.objectweb.jac.core.rtti.CollectionItem;
023    import java.awt.Point;
024    import javax.swing.JComponent;
025    import javax.swing.JList;
026    import javax.swing.ListSelectionModel;
027    import javax.swing.event.ListSelectionListener;
028    
029    public class List extends AbstractCollection
030        implements ListSelectionListener
031    {
032        // swing table component
033        JList list;
034    
035        public List(ViewFactory factory, DisplayContext context,
036                    CollectionItem collection, Object substance, CollectionModel model,
037                    org.objectweb.jac.aspects.gui.CollectionItemView itemView) {
038            super(factory,context,collection,substance,model,itemView);
039        }
040    
041        protected JComponent getInnerComponent(Model model) {
042            if (list==null) {
043                list = new JList();
044                list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
045                BetterListSelectionModel selModel = new BetterListSelectionModel(list);
046                list.setSelectionModel(selModel);
047                selModel.addListSelectionListener( this );
048                list.setModel((ListModel)model);
049            }
050            return list;
051        }
052    
053        protected void onRemove() {
054            list.clearSelection();
055        }
056    
057        /**
058         * Returns an array of the selected objects. The array is empty if
059         * no object is selected, but not null.
060         */
061        protected int[] getSelectedIndices() {
062            return list.getSelectedIndices();
063        }
064    
065        protected CollectionUpdate getCollectionUpdate() {
066            return (CollectionUpdate)model;
067        }
068    
069        int locationToIndex(Point location) {
070            return list.locationToIndex(location);
071        }
072    
073        protected ListSelectionModel getSelectionModel() {
074            return list.getSelectionModel();
075        }
076    
077        // CollectionView interface
078    
079        public void setSelected(int index) {
080            list.setSelectedIndex(index);
081        }
082    
083    }