001 package org.springframework.richclient.command; 002 003 import org.springframework.richclient.command.config.CommandButtonConfigurer; 004 import org.springframework.richclient.util.Assert; 005 006 import javax.swing.AbstractButton; 007 008 import java.util.Iterator; 009 import java.util.List; 010 import java.awt.Component; 011 import java.awt.Container; 012 013 /** 014 * A implementation of the {@link GroupMember} interface that can be associated 015 * with instances of {@link Component}s. 016 * 017 * @author Nils Olsson 018 */ 019 public class ComponentGroupMember extends GroupMember { 020 021 private Component component; 022 023 /** 024 * Creates a new {@code ComponentGroupMember}. 025 * @param component The component that this group member represents. 026 */ 027 public ComponentGroupMember(Component component) { 028 Assert.required(component, "component"); 029 this.component = component; 030 } 031 032 /** 033 * Forwards the enabled flag to the managed component. 034 * @param enabled The enabled flag. 035 */ 036 public void setEnabled(boolean enabled) { 037 component.setEnabled(enabled); 038 } 039 040 /** 041 * Searches through the component and nested components in order to see if 042 * the command with supplied id exists in this component. 043 * 044 * @param commandId The id of the command to be checked for. May be null. 045 * @return true if the component, or any of its nested components, is a 046 * command with the given command id. 047 */ 048 public boolean managesCommand(String commandId) { 049 if (null != commandId) { 050 return managesCommand(component, commandId); 051 } 052 else { 053 return false; 054 } 055 } 056 057 /** 058 * Searches through the component and nested components in order to see if 059 * the command with supplied id exists in this component. 060 * 061 * @param component The component that should be searched. 062 * @param commandId The id of the command to be checked for. 063 * @return true if the component, or any of its nested components, is a 064 * command with the given command id. 065 */ 066 private boolean managesCommand(Component component, String commandId) { 067 if (component instanceof AbstractButton) { 068 AbstractButton button = (AbstractButton) component; 069 if (null != button.getActionCommand()) { 070 if (button.getActionCommand().equals(commandId)) { 071 return true; 072 } 073 } 074 } 075 else if (component instanceof Container) { 076 Component[] subComponents = ((Container) component).getComponents(); 077 for (int i = 0; i < subComponents.length; ++i) { 078 if (managesCommand(subComponents[i], commandId)) { 079 return true; 080 } 081 } 082 } 083 return false; 084 } 085 086 /** 087 * Asks the given container populator to add a component to its underlying 088 * container. 089 */ 090 protected void fill(GroupContainerPopulator containerPopulator, Object controlFactory, 091 CommandButtonConfigurer buttonConfigurer, List previousButtons) { 092 Assert.required(containerPopulator, "containerPopulator"); 093 containerPopulator.add(component); 094 } 095 }