Example usage for com.google.gwt.dom.client Style clearOpacity

List of usage examples for com.google.gwt.dom.client Style clearOpacity

Introduction

In this page you can find the example usage for com.google.gwt.dom.client Style clearOpacity.

Prototype

public void clearOpacity() 

Source Link

Usage

From source file:com.smartgwt.mobile.client.internal.util.AnimationUtil.java

License:Open Source License

/**
 * Fade-transition between the old and new widget.
 *
 * @param oldPane the old widget/*w ww . j av  a  2s . com*/
 * @param newPane the new widget
 * @param container the container panel
 */
public static void fadeTransition(final Canvas oldPane, final Canvas newPane, final Canvas container,
        final Function<Void> beforeCallback, final Function<Void> callback, final boolean removeOldPane) {
    if (container instanceof Layout) {
        assert ((Layout) container).hasMember(oldPane) : "`container' does not contain `oldPane' as a member.";
    } else {
        assert container.hasChild(oldPane) : "`container' does not contain `oldPane' as a child.";
    }
    oldPane.setVisible(true);

    oldPane.getElement().getStyle().setProperty(DOMConstants.INSTANCE.getTransitionShorthandPropertyName(),
            "none");
    newPane.getElement().getStyle().setProperty(DOMConstants.INSTANCE.getTransitionShorthandPropertyName(),
            "none");
    oldPane.getElement().getStyle().setOpacity(1.0);
    if (container instanceof Layout) {
        if (!((Layout) container).hasMember(newPane))
            ((Layout) container).addMember(newPane);
    } else {
        if (!container.hasChild(newPane))
            container.addChild(newPane);
    }
    newPane.setVisible(true);
    newPane.getElement().getStyle().setOpacity(0.0);

    // To avoid issues with simultaneity, we use Timers.
    // http://www.w3.org/TR/css3-transitions/#starting
    new Timer() {
        @Override
        public void run() {
            if (beforeCallback != null)
                beforeCallback.execute();

            oldPane.getElement().getStyle().setProperty(
                    DOMConstants.INSTANCE.getTransitionShorthandPropertyName(),
                    "opacity " + FADE_TRANSITION_DURATION_MILLIS + "ms linear");
            newPane.getElement().getStyle().setProperty(
                    DOMConstants.INSTANCE.getTransitionShorthandPropertyName(),
                    "opacity " + FADE_TRANSITION_DURATION_MILLIS + "ms linear");

            new Timer() {
                @Override
                public void run() {
                    oldPane.getElement().getStyle().setOpacity(0.0);
                    newPane.getElement().getStyle().setOpacity(1.0);

                    new TransitionEndHandler() {
                        private Timer fallbackTimer = new Timer() {
                            @Override
                            public void run() {
                                if (fallbackTimer == this) {
                                    fallbackTimer = null;
                                    execute();
                                }
                            }
                        };

                        {
                            fallbackTimer.schedule(FADE_TRANSITION_DURATION_MILLIS + 50);
                        }

                        private HandlerRegistration transitionEndRegistration = oldPane.getElement()
                                .<SuperElement>cast().addTransitionEndHandler(this);

                        @Override
                        public void onTransitionEnd(TransitionEndEvent event) {
                            if (transitionEndRegistration != null && oldPane.equals(event.getEventTarget())) {
                                execute();
                            }
                        }

                        private void execute() {
                            if (fallbackTimer != null) {
                                fallbackTimer.cancel();
                                fallbackTimer = null;
                            }
                            if (transitionEndRegistration != null) {
                                transitionEndRegistration.removeHandler();
                                transitionEndRegistration = null;
                            }

                            if (removeOldPane) {
                                if (container instanceof Layout) {
                                    ((Layout) container).removeMember(oldPane);
                                } else {
                                    container.removeChild(oldPane);
                                }
                            } else
                                oldPane.setVisible(false);

                            // Clear the transitions and opacity values.
                            // This is particularly important for oldPane because it may be subsequently added to
                            // a Layout, but the uncleared opacity would keep it invisible.
                            final Style oldPaneStyle = oldPane.getElement().getStyle();
                            oldPaneStyle.setProperty(DOMConstants.INSTANCE.getTransitionShorthandPropertyName(),
                                    "none");
                            oldPaneStyle
                                    .clearProperty(DOMConstants.INSTANCE.getTransitionShorthandPropertyName());
                            oldPaneStyle.clearOpacity();
                            final Style newPaneStyle = newPane.getElement().getStyle();
                            newPaneStyle.setProperty(DOMConstants.INSTANCE.getTransitionShorthandPropertyName(),
                                    "none");
                            newPaneStyle
                                    .clearProperty(DOMConstants.INSTANCE.getTransitionShorthandPropertyName());
                            newPaneStyle.clearOpacity();

                            if (callback != null) {
                                new Timer() {
                                    @Override
                                    public void run() {
                                        assert callback != null;
                                        callback.execute();
                                    }
                                }.schedule(1);
                            }
                        }
                    };
                }
            }.schedule(1);
        }
    }.schedule(1);
}