Hi,
I would like to make my own layout class called GridLayoutPanel for
laying out grids with special constraints.
The paradigm I build on is DockLayoutPanel. I have adopted nearly
everything:
- create a local Layout object
- attach the GridLayoutPanel to a div element
- attach the child widgets to layout objects
- implement doLayout
- ...
However, the children are not positioned at all:
I started with a grid of width 2 and height 1, i. e. a pair of
SimplePanels side by side. Each of them should consume half of the
width. But both of them consume the whole width.
I already verified that the computed sizes I pass to
layer.setLeftWidth are correct...
How can I become more familiar with the Layout class, or what's the
problem with my code?
Thanks
Magnus
-----
package myLayout;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.layout.client.Layout;
import com.google.gwt.layout.client.Layout.Layer;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.AnimatedLayout;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.LayoutCommand;
import com.google.gwt.user.client.ui.ProvidesResize;
import com.google.gwt.user.client.ui.RequiresResize;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.WidgetCollection;
public class GridLayoutPanel extends ComplexPanel implements
AnimatedLayout,RequiresResize,ProvidesResize
{
// adopted from DockLayoutPanel
public enum Direction
{
NORTH, EAST, SOUTH, WEST, CENTER, LINE_START, LINE_END
}
/**
* Layout data associated with each widget.
*/
protected static class LayoutData
{
public Direction direction;
public double oldSize, size;
public double originalSize;
public boolean hidden;
public Layer layer;
public LayoutData(Direction direction, double size, Layer layer) {
this.direction = direction;
this.size = size;
this.layer = layer;
}
}
private class AnimateCommand extends LayoutCommand {
public AnimateCommand(Layout layout) {
super(layout);
}
@Override
protected void doBeforeLayout() {
doLayout();
}
}
public class Cell extends SimplePanel
{
public Cell ()
{
super ();
}
int x;
}
private final Unit unit = Unit.PX;
///////////////////////////////////////////////////////////////////////////////
// Public
Members //
///////////////////////////////////////////////////////////////////////////////
private final Layout layout;
private final LayoutCommand layoutCmd;
private int xs;
private int ys;
private Cell [] cll = null;
///////////////////////////////////////////////////////////////////////////////
//
Public //
///////////////////////////////////////////////////////////////////////////////
public GridLayoutPanel ()
{
DivElement e = Document.get().createDivElement();
setElement(e);
layout = new Layout(e);
layoutCmd = new AnimateCommand(layout);
}
///////////////////////////////////////////////////////////////////////////////
public GridLayoutPanel (int xs,int ys)
{
this ();
setSize (xs,ys);
}
///////////////////////////////////////////////////////////////////////////////
//
Public //
///////////////////////////////////////////////////////////////////////////////
public void setSize (int xs,int ys)
{
this.xs = xs;
this.ys = ys;
cll = null;
int n = xs * ys;
cll = new Cell[n];
for (int i = 0;i < cll.length;i++) // (Cell c: cll)
{
cll[i] = new Cell ();
insert (cll[i],null);
}
}
///////////////////////////////////////////////////////////////////////////////
private void insert (Widget wgt,Widget before)
{
// gui.styleBorder(wgt,"#FF0000"); // own method to draw a border
around it
// detach new child
wgt.removeFromParent();
// attach logical
WidgetCollection c = getChildren();
if (before == null)
{
c.add (wgt);
}
else
{
int i = c.indexOf (before);
c.insert (wgt,i);
}
// attach physical
Direction direction = Direction.CENTER;
double size = 10;
Layer layer = layout.attachChild (wgt.getElement(),(before !=
null) ? before.getElement() : null,wgt);
LayoutData data = new LayoutData(direction, size, layer);
wgt.setLayoutData(data);
// adopt
adopt (wgt);
// update layout
animate(0);
}
///////////////////////////////////////////////////////////////////////////////
public void animate(int duration) {
animate(duration, null);
}
///////////////////////////////////////////////////////////////////////////////
public void animate(int duration, final Layout.AnimationCallback
callback) {
layoutCmd.schedule(duration, callback);
}
///////////////////////////////////////////////////////////////////////////////
public void forceLayout() {
layoutCmd.cancel();
doLayout();
layout.layout();
onResize();
}
///////////////////////////////////////////////////////////////////////////////
public void onResize()
{
//doLayout (); //
for (Widget child : getChildren()) {
if (child instanceof RequiresResize) {
((RequiresResize) child).onResize();
}
}
}
///////////////////////////////////////////////////////////////////////////////
private void doLayout()
{
double left = 0;
double top = 0;
double right = 0;
double bottom = 0;
if (xs == 0 || ys == 0)
return;
int parent_xs = getParent().getOffsetWidth ();
int parent_ys = getParent().getOffsetHeight ();
int client_xs = parent_xs / xs;
int client_ys = parent_ys / ys;
for (Widget child : getChildren())
{
LayoutData data = (LayoutData) child.getLayoutData();
Layer layer = data.layer;
right = left + client_xs;
bottom = top + client_ys;
// the sizes are computed correctly:
// Window.alert ("left: " + left + ", top: " + top + ", right: "
+ right + ", bottom: " + bottom);
layer.setLeftWidth (left, unit, client_xs, unit);
layer.setTopHeight (top, unit, client_ys, unit);
left += client_xs;
}
}
}
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.