001    package org.springframework.richclient.widget;
002    
003    import com.jgoodies.forms.layout.ColumnSpec;
004    import com.jgoodies.forms.layout.RowSpec;
005    import org.springframework.richclient.application.support.AbstractView;
006    import org.springframework.richclient.command.AbstractCommand;
007    import org.springframework.richclient.command.CommandGroup;
008    
009    import javax.swing.*;
010    import java.awt.*;
011    import java.util.List;
012    
013    /**
014     * View implementation to show a widget
015     */
016    public class WidgetView extends AbstractView
017    {
018        private Widget widget;
019    
020        public WidgetView()
021        {
022        }
023    
024        public WidgetView(Widget widget)
025        {
026            setWidget(widget);
027        }
028    
029        public void setWidget(Widget widget)
030        {
031            this.widget = widget;
032        }
033    
034        public Widget getWidget()
035        {
036            return this.widget;
037        }
038    
039        protected JComponent createControl()
040        {
041            JComponent widgetComponent = getWidget().getComponent();
042            JPanel viewPanel = new JPanel(new BorderLayout());
043            viewPanel.add(widgetComponent, BorderLayout.CENTER);
044            Widget widget = getWidget();
045            List<? extends AbstractCommand> widgetCommands = widget.getCommands();
046            if (widgetCommands != null)
047            {
048                JComponent widgetButtonBar = CommandGroup.createCommandGroup(widgetCommands).createButtonBar(ColumnSpec.decode("fill:pref:nogrow"), RowSpec.decode("fill:default:nogrow"), null);
049                JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
050                buttonPanel.add(widgetButtonBar);
051                buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
052                viewPanel.add(buttonPanel, BorderLayout.SOUTH);
053            }
054            return viewPanel;
055        }
056    
057        public boolean canClose()
058        {
059            return getWidget().canClose();
060        }
061    
062        public void componentFocusGained()
063        {
064            getWidget().onAboutToShow();
065        }
066    
067        public void componentFocusLost()
068        {
069            getWidget().onAboutToHide();
070        }
071    }
072