001    /*
002     * Copyright 2002-2007 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.progress;
017    
018    import javax.swing.event.ListSelectionListener;
019    
020    import org.springframework.richclient.application.statusbar.StatusBar;
021    import org.springframework.richclient.table.ListSelectionListenerSupport;
022    import org.springframework.util.Assert;
023    
024    /**
025     * <code>ListSelectionListenerSupport</code> implementation that updates the statusbar
026     * of the application. The <code>getSelectedObjectName</code> must return the string
027     * representation of the selected object.
028     * <br>
029     * Usage:
030     * <pre>
031     * JTable table = ...
032     * 
033     * ListStatusBarUpdater updater = new ListStatusBarUpdater(getStatusBar()) {
034     *     protected String getSelectedObjectName() {
035     *         // return the selected Object's name
036     *     }
037     * };
038     * 
039     * table.getSelectionModel().addListSelectionListener(updater);
040     * </pre>
041     * @author peter.de.bruycker
042     */
043    public abstract class ListStatusBarUpdater extends ListSelectionListenerSupport implements ListSelectionListener {
044    
045        private StatusBar statusBar;
046    
047        /**
048         * Constructs a new <code>TableStatusBarUpdater</code> instance.
049         * @param table the table
050         * @param statusBar the status bar
051         */
052        public ListStatusBarUpdater(StatusBar statusBar) {
053            Assert.notNull(statusBar);
054            this.statusBar = statusBar;
055        }
056    
057        /**
058         * Returns the string representation of the selected object.
059         * @return the string representation
060         */
061        protected abstract String getSelectedObjectName();
062    
063        /**
064         * Method getStatusBar.
065         * @return the status bar
066         */
067        public StatusBar getStatusBar() {
068            return statusBar;
069        }
070    
071        /**
072         * @see org.springframework.richclient.table.TableSelectionListenerSupport#onSingleSelection(int)
073         */
074        protected void onSingleSelection(int index) {
075            updateStatusBar(getSelectedObjectName());
076        }
077    
078        /**
079         * @see org.springframework.richclient.table.TableSelectionListenerSupport#onMultiSelection(int[])
080         */
081        protected void onMultiSelection(int[] indexes) {
082            updateStatusBar(getItemsSelected());
083        }
084    
085        /**
086         * @see org.springframework.richclient.table.TableSelectionListenerSupport#onNoSelection()
087         */
088        protected void onNoSelection() {
089            updateStatusBar(null);
090        }
091    
092        private void updateStatusBar(int itemsSelected) {
093            // TODO i18n this message
094            getStatusBar().setMessage(itemsSelected + " items selected");
095        }
096    
097        private void updateStatusBar(String selectedObjectName) {
098            getStatusBar().setMessage(selectedObjectName);
099        }
100    
101    }