001    /*
002     * Copyright 2002-2008 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.selection.dialog;
017    
018    import java.awt.Window;
019    import java.awt.event.MouseAdapter;
020    import java.awt.event.MouseEvent;
021    import java.util.List;
022    
023    import javax.swing.JComponent;
024    import javax.swing.JList;
025    import javax.swing.JScrollPane;
026    import javax.swing.ListCellRenderer;
027    import javax.swing.ListSelectionModel;
028    import javax.swing.event.ListSelectionEvent;
029    import javax.swing.event.ListSelectionListener;
030    
031    import org.springframework.util.Assert;
032    
033    import ca.odell.glazedlists.EventList;
034    import ca.odell.glazedlists.GlazedLists;
035    import ca.odell.glazedlists.swing.EventListModel;
036    
037    /**
038     * A <code>ListSelectionDialog</code> can be used to select an item from a
039     * list.
040     * 
041     * @author Peter De Bruycker
042     */
043    public class ListSelectionDialog extends AbstractSelectionDialog {
044    
045            private ListCellRenderer renderer;
046    
047            private JList list;
048    
049            private EventList items;
050    
051            public ListSelectionDialog(String title, List items) {
052                    this(title, null, GlazedLists.eventList(items));
053            }
054            
055            public ListSelectionDialog(String title, Window parent, List items) {
056                    this(title, parent, GlazedLists.eventList(items));
057            }
058    
059            public ListSelectionDialog(String title, Window parent, EventList items) {
060                    super(title, parent);
061                    this.items = items;
062            }
063            
064            public void setRenderer(ListCellRenderer renderer) {
065                    Assert.notNull(renderer, "Renderer cannot be null.");
066                    Assert.isTrue(!isControlCreated(), "Install the renderer before the control is created.");
067    
068                    this.renderer = renderer;
069            }
070    
071            protected JComponent createSelectionComponent() {
072                    list = getComponentFactory().createList();
073                    list.setModel(new EventListModel(items));
074    
075                    list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
076    
077                    list.addListSelectionListener(new ListSelectionListener() {
078    
079                            private int lastIndex = -1;
080    
081                            public void valueChanged(ListSelectionEvent e) {
082                                    if (e.getValueIsAdjusting()) {
083                                            return;
084                                    }
085    
086                                    if (list.getSelectionModel().isSelectionEmpty() && lastIndex > -1) {
087                                            if (list.getModel().getSize() > 0) {
088                                                    list.setSelectedIndex(lastIndex);
089                                                    return;
090                                            }
091                                    }
092    
093                                    setFinishEnabled(!list.getSelectionModel().isSelectionEmpty());
094                                    lastIndex = list.getSelectedIndex();
095                            }
096                    });
097    
098                    list.addMouseListener(new MouseAdapter() {
099                            public void mouseClicked(MouseEvent e) {
100                                    if (e.getClickCount() == 2) {
101                                            getFinishCommand().execute();
102                                    }
103                            }
104                    });
105    
106                    if (renderer != null) {
107                            list.setCellRenderer(renderer);
108                    }
109    
110                    setFinishEnabled(false);
111    
112                    if (!items.isEmpty()) {
113                            list.setSelectedIndex(0);
114                    }
115    
116                    return new JScrollPane(list);
117            }
118    
119            protected Object getSelectedObject() {
120                    return items.get(list.getSelectedIndex());
121            }
122    
123            protected final JList getList() {
124                    return list;
125            }
126    }