mgarin / weblaf

WebLaF is a fully open-source Look & Feel and component library written in pure Java for cross-platform desktop Swing applications.
http://weblookandfeel.com
GNU General Public License v3.0
1.13k stars 234 forks source link

how to programmatically trigger the animation in WebSwitch #587

Closed mokun closed 4 years ago

mokun commented 4 years ago

The setSelected() method in WebSwitch doesn't seem to work.

In my app, when I call pause() to fire the pause signal, it's supposed to call the following :

webSwitch.setSelected(!webSwitch.getSelected());

But it doesn't cause the animation to start. I expect the clip to move from left to right or from right to left.

mgarin commented 4 years ago

I am going to update WebSwitch quite soon, so I will look into that as well.

mokun commented 4 years ago

I've also added the 2nd param and tried calling webSwitch.setSelected(true, true) and webSwitch.setSelected(false, true) alternatively but it still doesn't start the animation of switching from one state to another programmatically.

I look into the setSelected() method and wonder why startAnimation() doesn't work as expected. Unfortunately, startAnimation() is protected access and is not public.

   /**
     * Sets whether switch is selected or not and animates the transition if requested.
     *
     * @param selected whether switch is selected or not
     * @param animate  whether switch should animate the transition or not
     */
    public void setSelected ( final boolean selected, final boolean animate )
    {
        this.selected = selected;
        if ( animate )
        {
            startAnimation ();
        }
        else
        {
            gripperLocation = selected ? 1f : 0f;
            revalidate ();
        }
        fireActionPerformed ();
    }

So far it works only when I click on the Webswitch component with my mouse as I did add the listener to Webswitch as follows : webswitch.addActionListener(...)...

mgarin commented 4 years ago

Which WebLaF version are you using? Because I wasn't able to reproduce it on v1.2.10 or v1.2.11 (snapshot) locally. It does animate properly for me, but it might be bugged in older versions of the library.

mokun commented 4 years ago

I use v1.2.10.

This is my initial setup.

        WebSwitch webSwitch = new WebSwitch(true);
        webSwitch.setSwitchComponents(
                ImageLoader.getIcon(Msg.getString("img.speed.play")), 
                ImageLoader.getIcon(Msg.getString("img.speed.pause")));
        TooltipManager.setTooltip(webSwitch, "Pause or Resume the Simulation", TooltipWay.down);
        webSwitch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (webSwitch.isSelected())
                    masterClock.setPaused(false, false);
                else
                    masterClock.setPaused(true, false);
            };
        });

My pauseChange() method below will be called as follows :

    /**
     * Change the pause status. Called by Masterclock's firePauseChange() since
     * TimeWindow is on clocklistener.
     * 
     * @param isPaused true if set to pause
     * @param showPane true if the pane will show up
     */
    @Override
    public void pauseChange(boolean isPaused, boolean showPane) {
        // Update pause/resume webswitch buttons, based on masterclock's pause state.
        if (isPaused) {
            // To pause
            webSwitch.setSelected(false);

        } else {
            // To play or to resume 
            webSwitch.setSelected(false);
        }
    }

It's supposed to call webSwitch.setSelected(false) or webSwitch.setSelected(true) to switch to either the Play icon or the Pause icon but I see no animation.

mgarin commented 4 years ago

Thanks for the examples! I did find the problem you've encountered, but it is basically the same issue as described in #400 - you're probably just not seeing the exception in the log.

I've checked how other Swing components solve this problem and added a similar solution - ActionListener will not fire events anymore when selection is changed from the code, but newly added ItemListener will if you need to.

I've pushed the change into git and it will be available with v1.2.11 release soon. It should also be available in SNAPSHOT repository in a few minutes.

mgarin commented 4 years ago

Also since this is solved - less critical WebSwitch revamp ( #582 ) will be moved to v1.2.12.