com.gwtm.ui.client.widgets.OverlayPanel.java Source code

Java tutorial

Introduction

Here is the source code for com.gwtm.ui.client.widgets.OverlayPanel.java

Source

/*
 * Copyright 2011 Vancouver Ywebb Consulting Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.gwtm.ui.client.widgets;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtm.ui.client.core.Utils;
import com.gwtm.ui.client.themes.ThemeManager;

/**
 * 
 * <p>
 * <img class='ai' src='../../../../resources/XPopup.png' />
 * </p>
 */
public class OverlayPanel extends SimplePanel {

    private boolean isCentered = false;

    private String top;
    private String right;
    private String bottom;
    private String left;

    public OverlayPanel() {
        Utils.Widgets.setPrimaryCssClass(this, ThemeManager.getTheme().overlaypanel());

        addDomHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                hide();
            }
        }, ClickEvent.getType());
    }

    @Override
    public void onBrowserEvent(Event e) {
        EventTarget target = e.getEventTarget();
        Node node = Node.as(target);

        if (Element.is(node)) {
            Element ele = Element.as(target);
            // maek sure click is invoked from overlay not inner widget
            if (ele.getClassName().contains(ThemeManager.getTheme().overlaypanel())) {
                super.onBrowserEvent(e);
                return;
            }
        }
    }

    public void setTop(String value) {
        top = value;
    }

    public void setRight(String value) {
        right = value;
    }

    public void setBottom(String value) {
        bottom = value;
    }

    public void setLeft(String value) {
        left = value;
    }

    public void center() {
        isCentered = true;
        doPosition(getWidget().getElement().getStyle());
    }

    @Override
    protected void onLoad() {
        super.onLoad();
        Style s = getWidget().getElement().getStyle();
        //getWidget().addStyleName("xpopup");
        doPosition(s);
    }

    @Override
    public void setWidget(Widget w) {
        super.setWidget(w);
    }

    private void doPosition(Style s) {

        if (isCentered) {

            setBottom(null);
            setRight(null);
            setTop("50%");
            setLeft("50%");
            int height = getWidget().getOffsetHeight();
            int width = getWidget().getOffsetWidth();
            int centralPointW = Window.getClientWidth() / 2;
            int centralPointH = Window.getClientHeight() / 2;
            int newLeft = centralPointW - (width / 2);
            int newTop = centralPointH - (height / 2);

            getWidget().getElement().getStyle().setLeft(newLeft, Unit.PX);
            getWidget().getElement().getStyle().setTop(newTop, Unit.PX);
            // XLog.info("height: " + height + ", getOffsetHeight(): " +
            // getOffsetHeight());

            //         int top_ = height >= getOffsetHeight() ? 0 : (height / 2);
            //         int left_ = width >= getOffsetWidth() ? 0 : (width / 2);
            //
            //         s.setProperty("marginTop", "-" + top_ + "px");
            //         s.setProperty("marginLeft", "-" + left_ + "px");

        } else {

            if (top == null) {
                s.clearProperty("top");
            } else {
                s.setProperty("top", top);
            }

            if (right == null) {
                s.clearProperty("right");
            } else {
                s.setProperty("right", right);
            }

            if (bottom == null) {
                s.clearProperty("bottom");
            } else {
                s.setProperty("bottom", bottom);
            }

            if (left == null) {
                s.clearProperty("left");
            } else {
                s.setProperty("left", left);
            }

        }

    }

    public void show() {
        RootLayoutPanel.get().add(this);
        //      if (Utils.isAndroid()) {
        //this.setVisible(true);
        //this.getElement().getStyle().setOpacity(0);
        //      } else {
        Utils.Fx.fadeIn(this, 2000);
        //      }
    }

    public void hide() {
        //      if (Utils.isAndroid()) {
        //         removeFromParent();
        //      } else {
        Utils.Fx.fadeOut(this, 2000, new Command() {
            @Override
            public void execute() {
                // XPopup.super.hide(autoClosed);
                // setVisible(false);
                removeFromParent();
            }
        });
        //      }

    }

}