001    /*
002     * Copyright 2002-2006 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 org.springframework.binding.value.ValueModel;
019    import org.springframework.richclient.core.Guarded;
020    
021    /**
022     * This class applies a guard to a {@link Guarded} object that only enables the
023     * guarded object if the provided list selection model value holder has exactly
024     * one item selected.
025     * 
026     * @author Larry Streepy
027     * 
028     */
029    public class ListSingleSelectionGuard extends AbstractListSelectionGuard {
030    
031        /**
032         * Constructor.
033         * 
034         * @param selectionHolder ValueModel holding the list selection (value must
035         *        be an array of int (<code>int[]</code).
036         * @param guarded Object to guard
037         */
038        public ListSingleSelectionGuard( ValueModel selectionHolder, Guarded guarded ) {
039            super(selectionHolder, guarded);
040        }
041    
042        /**
043         * Determine if the guarded object should be enabled based on the contents
044         * of the current selection model value.
045         * 
046         * @param selected The array of selected rows
047         * @return boolean true if the guarded object should be enabled
048         */
049        protected boolean shouldEnable( int[] selected ) {
050            return selected != null && selected.length == 1;
051        }
052    }