Results 1 to 7 of 7

Thread: ShowViewMenu and toolbar buttons

  1. #1
    Join Date
    Aug 2005
    Location
    Austin, TX
    Posts
    425

    Default ShowViewMenu and toolbar buttons

    From the petclinic app I can see how the ShowViewMenu is added to the window menu in commands-context.xml.

    What I'm wondering is if there is any way (within the commands-context.xml) to configure toolbar buttons to show a given view (by Id).

    I know there are a lot of "magic" tricks for making spring instantiate things for you and I just haven't learned them all yet :wink:

    Any advice would be greatly appreciated.

    Thanks,
    Larry.

    P.S. In the meantime, I tried to figure out where I would do this in code, and the best I can determine is using a LifecycleAdvisor in the onCommandsCreated() method. Like this:

    Code:
        public void onCommandsCreated(ApplicationWindow window) {
            addViewToolbarButtons(window);
        }
    
        /**
         * Add buttons for each of our Views to the toolbar.
         */
        private void addViewToolbarButtons(ApplicationWindow window) {
            ViewDescriptor[] views = Application.services().getViewDescriptorRegistry().getViewDescriptors();
            CommandGroup toolbar = getToolBarCommandGroup();
    
            for &#40;int i = 0; i < views.length; i++&#41; &#123;
                ViewDescriptor view = views&#91;i&#93;;
                ActionCommand command = view.createShowViewCommand&#40;window&#41;;
                toolbar.add&#40;command&#41;;
            &#125;
        &#125;

    Would that be the right place?

  2. #2
    Join Date
    Aug 2004
    Posts
    109

    Default

    I just cooked up a little action of my own that looks like this:
    Code:
    public class SwitchToViewCommand extends ApplicationWindowAwareCommand &#123;
    
        private ViewDescriptor viewDescriptor;
        public ViewDescriptor getViewDescriptor &#40;&#41; &#123; return viewDescriptor; &#125;
        public void setViewDescriptor &#40;final ViewDescriptor viewDescriptor&#41; &#123; 
            this.viewDescriptor = viewDescriptor;
            setDefaultButton&#40;&#41;;
        &#125;
        
        protected void doExecuteCommand &#40;&#41; &#123;
            getApplicationWindow&#40;&#41;.getPage&#40;&#41;.showView&#40;getViewDescriptor&#40;&#41;&#41;;
        &#125;
    &#125;
    then in your commands-ctx.xml you can specify those right into the toolbar passing the view descriptors that you declare in your app context like this:

    Code:
    <bean id="toolBar"
    class="org.springframework.richclient.command.CommandGroupFactoryBean">
       <property name="members">
          <list>
             <bean class="SwitchToViewCommand">
                <property name="viewDescriptor">
                   <ref bean="view1" />
                </property>
             </bean>
             <bean class="SwitchToViewCommand">
                <property name="viewDescriptor">
                   <ref bean="view2" />
                </property>
             </bean>
          </list>
       </property>
    </bean>

    I also played around with a toggle group, which makes sense in this situation, but I was not able to configure it to look the way I think it should (it should look exactly like Eclipse's perpective switch bar). I just could not figure out where I would tweak the platform to do what I want. Use of toggle group would be really cool here because then all the other buttons can be changed visually by the group controller.
    Thanks,
    Alex.

  3. #3
    Join Date
    Aug 2005
    Location
    Austin, TX
    Posts
    425

    Default

    Cool - thanks for the code. I gave it a try and found that it needed one simple change to fit in nicely. It needs to ensure that the command has an Id (so the various messages and icons get located), so I made this change:

    Code:
        public void setViewDescriptor&#40;final ViewDescriptor viewDescriptor&#41; &#123;
            this.viewDescriptor = viewDescriptor;
            setId&#40;viewDescriptor.getId&#40;&#41;&#41;;
            setDefaultButton&#40;&#41;;
        &#125;
    Note the call to setId().

    Otherwise, it works great and I have the toolbar created from the xml file!

    I'll dig into the issue of creating the buttons as an exclusive group and post what I find.

    Thanks.
    Larry.

  4. #4
    Join Date
    Sep 2004
    Location
    Ghent, Belgium
    Posts
    224

    Default

    I've been working on something similar, take a look at http://opensource2.atlassian.com/pro...browse/RCP-161

    Hope this helps,

    Peter

  5. #5
    Join Date
    Aug 2005
    Location
    Austin, TX
    Posts
    425

    Default

    Thanks for the pointer Peter, but that's not quite what I'm looking for. I have poked some more at this problem and I'm closer to a real solution. I've got one problem remaining, which I'll raise after the code sample.

    Here's the XML config:

    Code:
    	<bean id="switchToPhysicianViewCommand"
    	      class="com.fhm.pdbm.ui.command.SwitchToViewCommand">
            <property name="viewDescriptor">
                <ref bean="PhysicianTableView" />
            </property>
        </bean>
        <bean id="switchToOfficeLocationViewCommand"
              class="com.fhm.pdbm.ui.command.SwitchToViewCommand">
            <property name="viewDescriptor">
                <ref bean="OfficeLocationView" />
            </property>
        </bean> 
    
    	<bean id="toolBar"
    		class="org.springframework.richclient.command.CommandGroupFactoryBean">
    		<property name="members">
    			<list>
    				<bean class="org.springframework.richclient.command.CommandGroup"
    					factory-method="createExclusiveCommandGroup">
    					<constructor-arg>
    						<list>
    							<ref bean="switchToPhysicianViewCommand"/>
    							<ref bean="switchToOfficeLocationViewCommand"/>
    						</list>
    					</constructor-arg>
    				</bean>
    			</list>
    		</property>
        </bean>
    and here's the supporting SwitchToViewCommand class (which is now an extension of ToggleCommand):

    Code:
    public class SwitchToViewCommand extends ToggleCommand implements ApplicationWindowAware &#123;
    
        private ApplicationWindow window;
        private ViewDescriptor viewDescriptor;
    
        /**
         * @return the view descriptor
         */
        public ViewDescriptor getViewDescriptor&#40;&#41; &#123;
            return viewDescriptor;
        &#125;
    
        /**
         * Set the view descriptor
         * 
         * @param viewDescriptor
         */
        public void setViewDescriptor&#40;final ViewDescriptor viewDescriptor&#41; &#123;
            this.viewDescriptor = viewDescriptor;
            setId&#40; viewDescriptor.getId&#40;&#41; &#41;;
            setDefaultButton&#40;&#41;;
        &#125;
    
        /**
         * Execute the command - show the view.
         */
        public void onSelection&#40;&#41; &#123;
            window.getPage&#40;&#41;.showView&#40; getViewDescriptor&#40;&#41; &#41;;
        &#125;
    
        public void setApplicationWindow&#40;ApplicationWindow window&#41; &#123;
            this.window = window;
        &#125;
    &#125;

    However, that ends up putting a single button on the toolbar that, when clicked, drops down a menu of the action buttons. What I wanted to accomplish was to have all of the buttons in the exclusive group show up directly on the toolbar.

    Any ideas on how to control this behavior?

    Thanks,
    Larry.

  6. #6
    Join Date
    Oct 2008
    Location
    Budapest
    Posts
    20

    Default

    Hi Larry,

    It is quite a late question to your post, but does your posted sample code (message #1) work?
    I tried the same and it is not working.

    I would like to do exactly what you wanted. Any success so far?

    UPDATE: I managed to make it work, however if i wanted to add ExitCommand similar way, then it was not working.


    /Gergo
    Last edited by Gergo; Oct 5th, 2008 at 01:10 PM. Reason: Update

  7. #7
    Join Date
    Mar 2007
    Location
    Oudenaarde
    Posts
    294

    Default

    Don't take threads out of the graveyard please. Most samples in this code are based on 3 year-old code. Try restating the problem in another thread, it might be that there is a more straightforward solution developed since then.
    MSN: PM me please
    Skype: doclo_lieven

    Spring Rich Client Project Lead

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •