Initialize
[sdk/ide/product.git] / org.eclipse.jst.pagedesigner / src / org / eclipse / jst / pagedesigner / css2 / layout / CSSWidgetLayout.java
1 /*******************************************************************************\r
2  * Copyright (c) 2006 Sybase, Inc. and others.\r
3  *\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Sybase, Inc. - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.eclipse.jst.pagedesigner.css2.layout;\r
13 \r
14 import org.eclipse.draw2d.Graphics;\r
15 import org.eclipse.draw2d.geometry.Dimension;\r
16 import org.eclipse.draw2d.geometry.Rectangle;\r
17 import org.eclipse.jst.pagedesigner.css2.provider.DimensionInfo;\r
18 import org.eclipse.jst.pagedesigner.css2.provider.ICSSWidgetProvider;\r
19 \r
20 /**\r
21  * @author mengbo\r
22  */\r
23 public class CSSWidgetLayout extends CSSBlockFlowLayout implements ICSSPainter {\r
24         private WidgetBox _widgetBox;\r
25 \r
26         private ICSSWidgetProvider _provider;\r
27 \r
28         /**\r
29          * @param flowfigure\r
30          * @param provider \r
31          */\r
32         public CSSWidgetLayout(CSSFigure flowfigure, ICSSWidgetProvider provider) {\r
33                 super(flowfigure);\r
34                 _provider = provider;\r
35         }\r
36 \r
37         /**\r
38          * normally this method is called directly after constructor\r
39          * \r
40          * @param provider\r
41          */\r
42         public void setProvider(ICSSWidgetProvider provider) {\r
43                 _provider = provider;\r
44         }\r
45 \r
46         /**\r
47          * @return the provider\r
48          */\r
49         public ICSSWidgetProvider getProvider() {\r
50                 // return ((CSSWidgetFigure)this.getFlowFigure()).getProvider();\r
51                 return _provider;\r
52         }\r
53 \r
54         /*\r
55          * (non-Javadoc)\r
56          * \r
57          * @see org.eclipse.jst.pagedesigner.css2.layout.CSSBlockFlowLayout#isInlineBlock()\r
58          */\r
59         public boolean isInlineBlock() {\r
60                 ICSSWidgetProvider provider = getProvider();\r
61                 return provider.isInline();\r
62         }\r
63 \r
64         /*\r
65          * (non-Javadoc)\r
66          * \r
67          * @see org.eclipse.jst.pagedesigner.css2.layout.FlowFigureLayout#layout()\r
68          */\r
69         protected void layoutChildren() {\r
70                 ICSSWidgetProvider provider = getProvider();\r
71 \r
72                 // if we did endLine, then will result in context create a new line, so\r
73                 // we may in the new line now.\r
74                 // passing in the top margin, and context will consider that when\r
75                 // creating the new line.\r
76 \r
77                 int suggestedWith = _blockBox.getContentWidth();\r
78                 int suggestedHeight = _blockBox.getContentHeight();\r
79                 // int suggestedWith = getSuggestedWidth(line, style, provider);\r
80                 // int suggestedHeight = getSuggestedHeight(line, style, provider);\r
81 \r
82                 DimensionInfo resultInfo = provider.getPreferredDimension(\r
83                                 suggestedWith, suggestedHeight);\r
84                 Dimension resultSize = resultInfo.getDimension();\r
85 \r
86                 _widgetBox = new WidgetBox(); // ((CSSWidgetFigure)getFlowFigure()).getWidgetBox();\r
87                 // if (provider.isHandlingBorder() || style == null)\r
88                 // {\r
89                 _widgetBox.setWidth(resultSize.width);\r
90                 _widgetBox.setHeight(resultSize.height);\r
91                 _widgetBox.setAscent(resultInfo.getAscent());\r
92                 // }\r
93                 // else\r
94                 // {\r
95                 // widgetBox.setWidth(resultSize.width +\r
96                 // style.getBorderInsets().getWidth());\r
97                 // widgetBox.setHeight(resultSize.height +\r
98                 // style.getBorderInsets().getHeight());\r
99                 // widgetBox.setAscent(resultInfo.getAscent()+style.getBorderInsets().top);\r
100                 // }\r
101                 this.addToCurrentLine(_widgetBox);\r
102                 // if (!provider.isInline())\r
103                 // {\r
104                 // context.endLine();\r
105                 // }\r
106         }\r
107 \r
108         /*\r
109          * (non-Javadoc)\r
110          * \r
111          * @see org.eclipse.jst.pagedesigner.css2.layout.FlowFigureLayout#dispose()\r
112          */\r
113         public void dispose() {\r
114         // TODO: anything to dispose?\r
115         }\r
116 \r
117         // public int getSuggestedWidth(LineBox line, ICSSStyle style,\r
118         // ICSSWidgetProvider provider)\r
119         // {\r
120         // if (style == null) return -1;\r
121         //\r
122         // Object width = style.getStyleProperty(ICSSPropertyID.ATTR_WIDTH);\r
123         // Length recommendedWidth = (width instanceof Length) ? (Length) width :\r
124         // null;\r
125         //\r
126         // int rw = 0;\r
127         // if (recommendedWidth == null || recommendedWidth.getValue() <= 0)\r
128         // {\r
129         // return -1;\r
130         // }\r
131         // else\r
132         // {\r
133         // if (recommendedWidth.isPercentage())\r
134         // {\r
135         // rw = line.getAvailableWidth() * recommendedWidth.getValue() / 100;\r
136         // }\r
137         // else\r
138         // {\r
139         // rw = recommendedWidth.getValue();\r
140         // }\r
141         //\r
142         // if (!style.isSizeIncludeBorderPadding() && provider.isHandlingBorder())\r
143         // {\r
144         // rw += style.getBorderInsets().getWidth() +\r
145         // style.getPaddingInsets().getWidth();\r
146         // }\r
147         // else if (style.isSizeIncludeBorderPadding() &&\r
148         // !provider.isHandlingBorder())\r
149         // {\r
150         // rw -= style.getBorderInsets().getWidth() +\r
151         // style.getPaddingInsets().getWidth();\r
152         // }\r
153         // }\r
154         //\r
155         // return rw;\r
156         // }\r
157         //\r
158         // public int getSuggestedHeight(LineBox line, ICSSStyle style,\r
159         // ICSSWidgetProvider provider)\r
160         // {\r
161         // if (style == null) return -1;\r
162         //\r
163         // Object height = style.getStyleProperty(ICSSPropertyID.ATTR_HEIGHT);\r
164         // Length recommendedHeight = (height instanceof Length) ? (Length) height :\r
165         // null;\r
166         //\r
167         // int rh = 0;\r
168         // if (recommendedHeight == null || recommendedHeight.getValue() <= 0)\r
169         // {\r
170         // return -1;\r
171         // }\r
172         // else\r
173         // {\r
174         // if (recommendedHeight.isPercentage())\r
175         // {\r
176         // // we don't support percentage height for this version, ignore\r
177         // return -1;\r
178         // }\r
179         // else\r
180         // {\r
181         // rh = recommendedHeight.getValue();\r
182         // }\r
183         //\r
184         // if (!style.isSizeIncludeBorderPadding() && provider.isHandlingBorder())\r
185         // {\r
186         // rh += style.getBorderInsets().getHeight() +\r
187         // style.getPaddingInsets().getHeight();\r
188         // }\r
189         // else if (style.isSizeIncludeBorderPadding() &&\r
190         // !provider.isHandlingBorder())\r
191         // {\r
192         // rh -= style.getBorderInsets().getHeight() +\r
193         // style.getPaddingInsets().getHeight();\r
194         // }\r
195         // }\r
196         //\r
197         // return rh;\r
198         // }\r
199 \r
200         /*\r
201          * (non-Javadoc)\r
202          * \r
203          * @see org.eclipse.jst.pagedesigner.css2.layout.ICSSPainter#paintFigure(org.eclipse.draw2d.Graphics)\r
204          */\r
205         public void paintFigure(Graphics g) {\r
206                 ICSSWidgetProvider provider = this.getProvider();\r
207                 if (provider != null && _widgetBox != null) {\r
208                         provider.paintFigure(g, new Rectangle(_widgetBox._x, _widgetBox._y,\r
209                                         _widgetBox.getWidth(), _widgetBox.getHeight()));\r
210                 }\r
211         }\r
212 \r
213         /*\r
214          * (non-Javadoc)\r
215          * \r
216          * @see org.eclipse.jst.pagedesigner.css2.layout.CSSLayout#handlingBorderForBlock()\r
217          */\r
218         public boolean handlingBorderForBlock() {\r
219                 ICSSWidgetProvider provider = this.getProvider();\r
220                 if (provider != null) {\r
221                         return provider.isHandlingBorder();\r
222                 }\r
223                 return super.handlingBorderForBlock();\r
224         }\r
225 }\r