001    package org.springframework.richclient.command.support;
002    
003    import org.springframework.beans.BeansException;
004    import org.springframework.context.ApplicationContext;
005    import org.springframework.context.ApplicationContextAware;
006    import org.springframework.richclient.util.RcpSupport;
007    import org.springframework.richclient.widget.Widget;
008    
009    import javax.swing.*;
010    
011    /**
012     * Base class for commands that use widgets. The widget can be injected or found in the context through its id.
013     */
014    public abstract class AbstractWidgetCommand extends ApplicationWindowAwareCommand
015            implements
016            ApplicationContextAware
017    {
018    
019        private String widgetBeanId = null;
020    
021        private Widget widget;
022    
023        private ApplicationContext applicationContext;
024    
025        public void setWidget(Widget widget)
026        {
027            this.widget = widget;
028        }
029    
030        protected Widget getWidget()
031        {
032            if (this.widget == null && this.widgetBeanId != null)
033                this.widget = RcpSupport.getBean(widgetBeanId);
034            return this.widget;
035        }
036    
037        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
038        {
039            this.applicationContext = applicationContext;
040        }
041    
042        public ApplicationContext getApplicationContext()
043        {
044            return applicationContext;
045        }
046    
047        protected final JComponent getWidgetComponent()
048        {
049            if (getWidget() == null)
050                return RcpSupport.createDummyPanel("No widget set for command:" + getId());
051            return getWidget().getComponent();
052        }
053    
054        public String getWidgetBeanId()
055        {
056            return widgetBeanId;
057        }
058    
059        public void setWidgetBeanId(String widgetBeanId)
060        {
061            this.widgetBeanId = widgetBeanId;
062        }
063    }
064