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.command;
017    
018    import java.util.ArrayList;
019    import java.util.Iterator;
020    import java.util.List;
021    
022    public class ExclusiveCommandGroupSelectionController {
023        private List commands = new ArrayList();
024    
025        private boolean allowsEmptySelection;
026    
027        public boolean getAllowsEmptySelection() {
028            return allowsEmptySelection;
029        }
030    
031        public void setAllowsEmptySelection(boolean allowsEmptySelection) {
032            this.allowsEmptySelection = allowsEmptySelection;
033        }
034    
035        public void add(ToggleCommand command) {
036            if (!commands.contains(command)) {
037                commands.add(command);
038                command.setExclusiveController(this);
039            }
040        }
041    
042        public void remove(ToggleCommand command) {
043            if (commands.remove(command)) {
044                command.setExclusiveController(null);
045            }
046        }
047    
048        public void handleSelectionRequest(ToggleCommand delegatingCommand, boolean requestingSelection) {
049            if (requestingSelection) {
050                ToggleCommand currentSelectedCommand = null;
051                for (Iterator iterator = commands.iterator(); iterator.hasNext();) {
052                    ToggleCommand command = (ToggleCommand)iterator.next();
053                    if (command.isSelected()) {
054                        currentSelectedCommand = command;
055                        break;
056                    }
057                }
058                if (currentSelectedCommand == null) {
059                    delegatingCommand.requestSetSelection(true);
060                }
061                else {
062                    currentSelectedCommand.requestSetSelection(false);
063                    delegatingCommand.requestSetSelection(!currentSelectedCommand.isSelected());
064                    if (!delegatingCommand.isSelected() && currentSelectedCommand != null) {
065                        currentSelectedCommand.requestSetSelection(true);
066                    }
067                }
068            }
069            else {
070                // its a deselection
071                if (allowsEmptySelection) {
072                    delegatingCommand.requestSetSelection(false);
073                }
074            }
075        }
076    
077    }