Package net.mygwt.ui.client.widget.layout

Source Code of net.mygwt.ui.client.widget.layout.FillLayout

/*******************************************************************************
* Copyright (c) 2000, 2007 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     IBM Corporation - initial API and implementation
*     Darrell Meyer <darrell@mygwt.net> - derived implementation
*******************************************************************************/
package net.mygwt.ui.client.widget.layout;

import net.mygwt.ui.client.MyDOM;
import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.util.Rectangle;
import net.mygwt.ui.client.widget.Container;
import net.mygwt.ui.client.widget.Layout;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Widget;

/**
* <code>FillLayout</code> is the simplest layout class. It lays out widgets
* in a single row or column, forcing them to be the same size.
*/
public class FillLayout extends Layout {

  /**
   * margin specifies the number of pixels of margin that will be placed along
   * the edges of the layout. The default value is 0.
   */
  public int margin = 0;
  /**
   * spacing specifies the number of pixels between the edge of one cell and the
   * edge of its neighbouring cell. The default value is 0.
   */
  public int spacing = 0;

  /**
   * type specifies how controls will be positioned within the layout. The
   * default value is HORIZONTAL.
   */
  public int type = Style.HORIZONTAL;

  /**
   * Creates a new layout instance.
   */
  public FillLayout() {

  }

  /**
   * Creates a new instance.
   *
   * @param margin the margin in pixels
   */
  public FillLayout(int margin) {
    this.margin = margin;
  }


  protected void onLayout(Container container, Element target) {
    super.onLayout(container, target);

    int count = container.getWidgetCount();
    if (count < 1) {
      return;
    }
   
    for (int i = 0; i < count; i++) {
      Widget w = container.getWidget(i);
      DOM.setStyleAttribute(w.getElement(), "position", "absolute");
    }
   
    Element ct = container.getLayoutTarget();

    Rectangle rect = MyDOM.getBounds(ct, true);

    int width = rect.width - margin * 2;
    int height = rect.height - margin * 2;

    if (type == Style.HORIZONTAL) {
      width -= (count - 1) * spacing;
      int x = rect.x + margin, extra = width % count;
      int y = rect.y + margin, cellWidth = width / count;
      for (int i = 0; i < count; i++) {
        Widget child = container.getWidget(i);
        int childWidth = cellWidth;
        if (i == 0) {
          childWidth += extra / 2;
        } else {
          if (i == count - 1) childWidth += (extra + 1) / 2;
        }
        setBounds(child, x, y, childWidth, height);
        adjustSizeForScroll(target, child, childWidth, height);
        x += childWidth + spacing;
      }
    } else {
      height -= (count - 1) * spacing;
      int x = rect.x + margin, cellHeight = height / count;
      int y = rect.y + margin, extra = height % count;
      for (int i = 0; i < count; i++) {
        Widget child = container.getWidget(i);
        int childHeight = cellHeight;
        if (i == 0) {
          childHeight += extra / 2;
        } else {
          if (i == count - 1) childHeight += (extra + 1) / 2;
        }
        setBounds(child, x, y, width, childHeight);
        adjustSizeForScroll(target, child, width, childHeight);
        y += childHeight + spacing;
      }
    }
  }

}
TOP

Related Classes of net.mygwt.ui.client.widget.layout.FillLayout

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.