View Javadoc

1   /**********************************************
2    * Copyright (C) 2010 Lukas Laag
3    * This file is part of vectomatic2.
4    * 
5    * vectomatic2 is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * vectomatic2 is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with vectomatic2.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.gxt;
19  
20  import com.extjs.gxt.ui.client.core.El;
21  import com.extjs.gxt.ui.client.util.Point;
22  import com.extjs.gxt.ui.client.util.Size;
23  import com.extjs.gxt.ui.client.widget.Component;
24  import com.extjs.gxt.ui.client.widget.Container;
25  import com.extjs.gxt.ui.client.widget.Window;
26  import com.extjs.gxt.ui.client.widget.layout.FitLayout;
27  import com.google.gwt.core.client.GWT;
28  import com.google.gwt.dom.client.Style.Position;
29  import com.google.gwt.dom.client.Style.Unit;
30  import com.google.gwt.user.client.ui.Widget;
31  
32  /**
33   * GXT layout class to implement a multilayer layout based
34   * on several CSS absolutely positionned divs with different
35   * z-indices.
36   * @author laaglu
37   */
38  public class AbsoluteLayerLayout extends FitLayout {
39  	public static class PositionHack {
40  		protected int getDy(Window window) {
41  			return window.getDraggable().getDragHandle().getOffsetHeight();
42  		}
43  	}
44  	public static class PositionHackMozilla extends PositionHack {
45  		@Override
46  		protected int getDy(Window window) {
47  			return 0;
48  		}		
49  	}
50  	protected void onLayout(Container<?> container, El target) {
51  		super.onLayout(container, target);
52  		// Retrieve the first parent with absolute layout (it
53  		// ought to be the Window)
54  		Widget w = container;
55  		while ((w = w.getParent()) != null)  {
56  			String position = w.getElement().getStyle().getPosition();
57  			if (Position.ABSOLUTE.getCssName().equals(position)) {
58  				break;
59  			}
60  		}
61  		Window window = (Window)w;
62  		Point p = target.getOffsetsTo(w.getElement());
63  		PositionHack ph = GWT.create(PositionHack.class);
64  		p.y -= ph.getDy(window);
65  		GWT.log("p = " + p.toString());
66  		Size windowSize = window.el().getStyleSize();
67  		for (int i = 0, count = container.getItemCount(); i < count; i++) {
68  			Component c = container.getItem(i);
69  			Size containerSize = target.getStyleSize();
70  			containerSize.width -= getSideMargins(c);
71  			containerSize.height -= c.el().getMargins("tb");
72  			c.getElement().getStyle().setPosition(Position.ABSOLUTE);
73  			AbsoluteLayerLayoutData data = (AbsoluteLayerLayoutData) c.getData("layoutData");
74  			int horizontalOffset = data.getHorizontalOffset();
75  			if (data.isAttachedLeft()) {
76  				c.getElement().getStyle().setLeft(p.x + horizontalOffset, Unit.PX);
77  			} else {
78  				c.getElement().getStyle().setRight(windowSize.width - containerSize.width - p.x + horizontalOffset, Unit.PX);
79  			}
80  			int verticalOffset = data.getVerticalOffset();
81  			if (data.isAttachedTop()) {
82  				c.getElement().getStyle().setTop(p.y + verticalOffset, Unit.PX);
83  			} else {
84  				c.getElement().getStyle().setBottom(windowSize.height - containerSize.height - p.y + verticalOffset, Unit.PX);
85  			}
86  			if (c.isRendered()) {
87  				setSize(c, 
88  						data.getWidth() > 0 ? data.getWidth() : containerSize.width, 
89  						data.getHeight() > 0 ? data.getHeight() : containerSize.height);
90  			}
91  			c.getElement().getStyle().setZIndex(data.getZIndex());
92  		}
93  	}
94  }
95