001    /*
002     * Copyright 2002-2004 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.list;
017    
018    import java.util.List;
019    
020    import javax.swing.ListModel;
021    
022    import org.springframework.binding.value.ValueModel;
023    import org.springframework.richclient.command.ActionCommand;
024    
025    public class ListUtils {
026    
027        private ListUtils() {
028    
029        }
030    
031        public static ActionCommand createRemoveRowCommand(final List list, final ValueModel selectionIndexHolder) {
032            ActionCommand removeCommand = new ActionCommand("removeCommand") {
033                protected void doExecuteCommand() {
034                    int selectedRowIndex = ((Integer) selectionIndexHolder.getValue()).intValue();
035                    list.remove(selectedRowIndex);
036                }
037            };
038            new SingleListSelectionGuard(selectionIndexHolder, removeCommand);
039            return removeCommand;
040        }
041    
042        /**
043         * Returns the list model of a filter chain.
044         * 
045         * @param listModel
046         *            the filtered list model chain
047         * @return the (unfiltered) list model
048         */
049        public static ListModel getFilteredListModel(ListModel listModel) {
050            if (listModel instanceof AbstractFilteredListModel) {
051                return getFilteredListModel(((AbstractFilteredListModel) listModel).getFilteredModel());
052            }
053            return listModel;
054        }
055    
056        /**
057         * Returns the unfiltered element index from a chained filtered list model
058         * 
059         * @param listModel
060         *            the chained filtered list model
061         * @param filteredIndex
062         *            the index of the element to return the unfiltered index for
063         * @return the element index of the unfiltered list model
064         */
065        public static int getElementIndex(ListModel listModel, int filteredIndex) {
066            if (listModel instanceof AbstractFilteredListModel) {
067                AbstractFilteredListModel filteredModel = (AbstractFilteredListModel) listModel;
068                return getElementIndex(filteredModel.getFilteredModel(), filteredModel.getElementIndex(filteredIndex));
069            }
070            return filteredIndex;
071        }
072    }