com.centretown.planets.client.application.widget.SizeableView.java Source code

Java tutorial

Introduction

Here is the source code for com.centretown.planets.client.application.widget.SizeableView.java

Source

/**
 * Copyright 2015 Centretown Software (Dave Marsh)
 *
 * 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.centretown.planets.client.application.widget;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.gwtplatform.mvp.client.UiHandlers;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;

public abstract class SizeableView<T extends UiHandlers> extends ViewWithUiHandlers<T> implements ResizeHandler {

    private final static int CRAZY = 60;
    private int sanityCheck = 0;
    private Element element;

    protected int sizeWidth = 300;
    protected int sizeHeight = 300;

    public SizeableView() {
        Window.addResizeHandler(this);
        sizeWidth = Window.getClientWidth();
        sizeHeight = Window.getClientHeight();
        sanityCheck = 0;
    }

    protected void initialize(Element element) {
        this.element = element;
        if (++sanityCheck < CRAZY) {
            Scheduler.get().scheduleDeferred(new ScheduledCommand() {
                @Override
                public void execute() {
                    //GWT.log("sanityCheck=" + sanityCheck);
                    doResize();
                }
            });
        }
    }

    @Override
    protected void onAttach() {
        super.onAttach();
        doResize();
    }

    protected void doResize() {
        int top = element.getAbsoluteTop();
        if (top > 0) {
            sizeWidth = Window.getClientWidth();
            sizeHeight = Window.getClientHeight() - top;
            element.getStyle().setWidth(sizeWidth, Unit.PX);
            element.getStyle().setHeight(sizeHeight, Unit.PX);
            sanityCheck = 0;
        } else {
            initialize(element);
        }
    }

    private static native void size(Element element, String width, String height) /*-{
                                                                                  $wnd.jQuery(element).animate({
                                                                                  width : width,
                                                                                  height : height,   
                                                                                  }, 500);
                                                                                  }-*/;

    @Override
    public void onResize(ResizeEvent event) {
        doResize();
    }

}