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.support;
017    
018    import java.awt.Component;
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import javax.swing.AbstractButton;
023    import javax.swing.JPanel;
024    
025    import org.springframework.richclient.command.CommandGroupFactoryBean;
026    
027    import com.jgoodies.forms.builder.ButtonBarBuilder;
028    import com.jgoodies.forms.layout.ColumnSpec;
029    import com.jgoodies.forms.layout.RowSpec;
030    import com.jgoodies.forms.layout.Size;
031    
032    public class ButtonBarGroupContainerPopulator extends SimpleGroupContainerPopulator {
033        private ColumnSpec columnSpec;
034    
035        private ButtonBarBuilder builder;
036    
037        private List buttons = new ArrayList();
038    
039        public ButtonBarGroupContainerPopulator() {
040            super(new JPanel());
041            builder = new ButtonBarBuilder((JPanel)getContainer());
042        }
043    
044        public void setMinimumButtonSize(Size minimumSize) {
045            this.columnSpec = new ColumnSpec(minimumSize);
046        }
047    
048        public void setColumnSpec(final ColumnSpec columnSpec)
049        {
050            this.columnSpec = columnSpec;
051        }
052        
053        public void setRowSpec(final RowSpec rowSpec)
054        {
055            if (rowSpec != null)
056                builder.getLayout().setRowSpec(1, rowSpec);
057        }
058        
059        public JPanel getButtonBar() {
060            return builder.getPanel();
061        }
062    
063        public void add(Component c) {
064            buttons.add(c);
065        }
066    
067        public void addSeparator() {
068            buttons.add(CommandGroupFactoryBean.SEPARATOR_MEMBER_CODE);
069        }
070    
071        public void onPopulated() {
072            builder.addGlue();
073            int length = buttons.size();
074            for (int i = 0; i < length; i++) {
075                Object o = buttons.get(i);
076                if (o instanceof String && o == CommandGroupFactoryBean.SEPARATOR_MEMBER_CODE) {
077                    builder.addUnrelatedGap();
078                }
079                else if (o instanceof AbstractButton) {
080                    AbstractButton button = (AbstractButton)o;
081                    if (this.columnSpec != null) {
082                        addCustomGridded(button);
083                    }
084                    else {
085                        builder.addGridded(button);
086                    }
087                    if (i < buttons.size() - 1) {
088                        builder.addRelatedGap();
089                    }
090                }
091            }
092        }
093    
094        private void addCustomGridded(AbstractButton button) {
095            builder.getLayout().appendColumn(this.columnSpec);
096            builder.getLayout().addGroupedColumn(builder.getColumn());
097            button.putClientProperty("jgoodies.isNarrow", Boolean.TRUE);
098            builder.add(button);
099            builder.nextColumn();
100        }
101    
102    }