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 one or
024     * more item selected. This can also be configured to monitor for an exact
025     * number of selected items.
026     * 
027     * @author Larry Streepy
028     */
029    public class ListMultipleSelectionGuard extends AbstractListSelectionGuard {
030    
031        private int requiredCount = -1;
032    
033        /**
034         * Constructor.
035         * 
036         * @param selectionHolder ValueModel holding the list selection (value must
037         *        be an array of int (<code>int[]</code).
038         * @param guarded Object to guard
039         */
040        public ListMultipleSelectionGuard( ValueModel selectionHolder, Guarded guarded ) {
041            super(selectionHolder, guarded);
042        }
043    
044        /**
045         * Constructor.
046         * 
047         * @param selectionHolder ValueModel holding the list selection (value must
048         *        be an array of int (<code>int[]</code).
049         * @param guarded Object to guard
050         * @param requiredCount Required number of selected items to enable
051         */
052        public ListMultipleSelectionGuard( ValueModel selectionHolder, Guarded guarded, int requiredCount ) {
053            super(selectionHolder, guarded);
054            this.requiredCount = requiredCount;
055        }
056    
057        /**
058         * Determine if the guarded object should be enabled based on the contents
059         * of the current selection model value.
060         * 
061         * @param selected The array of selected rows
062         * @return boolean true if the guarded object should be enabled
063         */
064        protected boolean shouldEnable( int[] selected ) {
065            return selected != null && selected.length >= 1 && (requiredCount == -1 || requiredCount == selected.length);
066        }
067    }