View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.facelets;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.UIExtensionPanel;
24  import org.apache.myfaces.tobago.layout.LayoutBase;
25  import org.apache.myfaces.tobago.layout.LayoutComponent;
26  import org.apache.myfaces.tobago.layout.LayoutContainer;
27  import org.apache.myfaces.tobago.layout.Measure;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import javax.faces.component.UIComponent;
32  import javax.faces.view.facelets.FaceletContext;
33  import javax.faces.view.facelets.TagAttribute;
34  import javax.faces.view.facelets.TagConfig;
35  import javax.faces.view.facelets.TagHandler;
36  import java.io.IOException;
37  
38  public class GridLayoutConstraintHandler extends TagHandler {
39  
40    private static final Logger LOG = LoggerFactory.getLogger(GridLayoutConstraintHandler.class);
41  
42    private final TagAttribute columnSpan;
43    private final TagAttribute rowSpan;
44  
45    private final TagAttribute width;
46    private final TagAttribute height;
47  
48    private final TagAttribute minimumWidth;
49    private final TagAttribute minimumHeight;
50  
51    private final TagAttribute preferredWidth;
52    private final TagAttribute preferredHeight;
53  
54    private final TagAttribute maximumWidth;
55    private final TagAttribute maximumHeight;
56  
57    private final TagAttribute marginLeft;
58    private final TagAttribute marginRight;
59    private final TagAttribute marginTop;
60    private final TagAttribute marginBottom;
61  
62    private final TagAttribute borderLeft;
63    private final TagAttribute borderRight;
64    private final TagAttribute borderTop;
65    private final TagAttribute borderBottom;
66  
67    private final TagAttribute paddingLeft;
68    private final TagAttribute paddingRight;
69    private final TagAttribute paddingTop;
70    private final TagAttribute paddingBottom;
71  
72    public GridLayoutConstraintHandler(TagConfig config) {
73      super(config);
74      columnSpan = getAttribute(Attributes.COLUMN_SPAN);
75      rowSpan = getAttribute(Attributes.ROW_SPAN);
76      width = getAttribute(Attributes.WIDTH);
77      height = getAttribute(Attributes.HEIGHT);
78      minimumWidth = getAttribute(Attributes.MINIMUM_WIDTH);
79      minimumHeight = getAttribute(Attributes.MINIMUM_HEIGHT);
80      preferredWidth = getAttribute(Attributes.PREFERRED_WIDTH);
81      preferredHeight = getAttribute(Attributes.PREFERRED_HEIGHT);
82      maximumWidth = getAttribute(Attributes.MAXIMUM_WIDTH);
83      maximumHeight = getAttribute(Attributes.MAXIMUM_HEIGHT);
84      marginLeft = getAttribute(Attributes.MARGIN_LEFT);
85      marginRight = getAttribute(Attributes.MARGIN_RIGHT);
86      marginTop = getAttribute(Attributes.MARGIN_TOP);
87      marginBottom = getAttribute(Attributes.MARGIN_BOTTOM);
88      borderLeft = getAttribute(Attributes.BORDER_LEFT);
89      borderRight = getAttribute(Attributes.BORDER_RIGHT);
90      borderTop = getAttribute(Attributes.BORDER_TOP);
91      borderBottom = getAttribute(Attributes.BORDER_BOTTOM);
92      paddingLeft = getAttribute(Attributes.PADDING_LEFT);
93      paddingRight = getAttribute(Attributes.PADDING_RIGHT);
94      paddingTop = getAttribute(Attributes.PADDING_TOP);
95      paddingBottom = getAttribute(Attributes.PADDING_BOTTOM);
96    }
97  
98    public void apply(FaceletContext faceletContext, UIComponent parent) throws IOException {
99      if (parent.getParent() != null && parent.getParent() instanceof UIExtensionPanel) {
100        parent = parent.getParent();
101     } else if (parent.getAttributes().get("tobago.panel") != null
102          && parent.getAttributes().get("tobago.panel") instanceof UIExtensionPanel) {
103        parent = (UIComponent) parent.getAttributes().get("tobago.panel");
104     }
105     if (parent instanceof LayoutBase) {
106       LayoutBase component = (LayoutBase) parent;
107 
108       if (parent instanceof LayoutComponent && columnSpan != null) {
109         if (columnSpan.isLiteral()) {
110           ((LayoutComponent) component).setColumnSpan(Integer.valueOf(columnSpan.getValue()));
111         } else {
112           parent.setValueBinding(Attributes.COLUMN_SPAN,
113               faceletContext.getFacesContext().getApplication().createValueBinding(columnSpan.getValue()));
114         }
115       }
116 
117       if (parent instanceof LayoutComponent && rowSpan != null) {
118         if (rowSpan.isLiteral()) {
119           ((LayoutComponent) component).setRowSpan(Integer.valueOf(rowSpan.getValue()));
120         } else {
121           parent.setValueBinding(Attributes.ROW_SPAN,
122               faceletContext.getFacesContext().getApplication().createValueBinding(rowSpan.getValue()));
123         }
124       }
125 
126       if (width != null) {
127         if (width.isLiteral()) {
128           component.setWidth(Measure.parse(width.getValue()));
129         } else {
130           parent.setValueBinding(Attributes.WIDTH,
131               faceletContext.getFacesContext().getApplication().createValueBinding(width.getValue()));
132         }
133       }
134 
135       if (height != null) {
136         if (height.isLiteral()) {
137           component.setHeight(Measure.parse(height.getValue()));
138         } else {
139           parent.setValueBinding(Attributes.HEIGHT,
140               faceletContext.getFacesContext().getApplication().createValueBinding(height.getValue()));
141         }
142       }
143 
144       if (minimumWidth != null) {
145         if (minimumWidth.isLiteral()) {
146           component.setMinimumWidth(Measure.parse(minimumWidth.getValue()));
147         } else {
148           parent.setValueBinding(Attributes.MINIMUM_WIDTH,
149               faceletContext.getFacesContext().getApplication().createValueBinding(minimumWidth.getValue()));
150         }
151       }
152 
153       if (minimumHeight != null) {
154         if (minimumHeight.isLiteral()) {
155           component.setMinimumHeight(Measure.parse(minimumHeight.getValue()));
156         } else {
157           parent.setValueBinding(Attributes.MINIMUM_HEIGHT,
158               faceletContext.getFacesContext().getApplication().createValueBinding(minimumHeight.getValue()));
159         }
160       }
161 
162       if (preferredWidth != null) {
163         if (preferredWidth.isLiteral()) {
164           component.setPreferredWidth(Measure.parse(preferredWidth.getValue()));
165         } else {
166           parent.setValueBinding(Attributes.PREFERRED_WIDTH,
167               faceletContext.getFacesContext().getApplication().createValueBinding(preferredWidth.getValue()));
168         }
169       }
170 
171       if (preferredHeight != null) {
172         if (preferredHeight.isLiteral()) {
173           component.setPreferredHeight(Measure.parse(preferredHeight.getValue()));
174         } else {
175           parent.setValueBinding(Attributes.PREFERRED_HEIGHT,
176               faceletContext.getFacesContext().getApplication().createValueBinding(preferredHeight.getValue()));
177         }
178       }
179 
180       if (maximumWidth != null) {
181         if (maximumWidth.isLiteral()) {
182           component.setMaximumWidth(Measure.parse(maximumWidth.getValue()));
183         } else {
184           parent.setValueBinding(Attributes.MAXIMUM_WIDTH,
185               faceletContext.getFacesContext().getApplication().createValueBinding(maximumWidth.getValue()));
186         }
187       }
188 
189       if (maximumHeight != null) {
190         if (maximumHeight.isLiteral()) {
191           component.setMaximumHeight(Measure.parse(maximumHeight.getValue()));
192         } else {
193           parent.setValueBinding(Attributes.MAXIMUM_HEIGHT,
194               faceletContext.getFacesContext().getApplication().createValueBinding(maximumHeight.getValue()));
195         }
196       }
197 
198       if (marginLeft != null) {
199         if (marginLeft.isLiteral()) {
200           component.setMarginLeft(Measure.parse(marginLeft.getValue()));
201         } else {
202           parent.setValueBinding(Attributes.MARGIN_LEFT,
203               faceletContext.getFacesContext().getApplication().createValueBinding(marginLeft.getValue()));
204         }
205       }
206 
207       if (marginRight != null) {
208         if (marginRight.isLiteral()) {
209           component.setMarginRight(Measure.parse(marginRight.getValue()));
210         } else {
211           parent.setValueBinding(Attributes.MARGIN_RIGHT,
212               faceletContext.getFacesContext().getApplication().createValueBinding(marginRight.getValue()));
213         }
214       }
215 
216       if (marginTop != null) {
217         if (marginTop.isLiteral()) {
218           component.setMarginTop(Measure.parse(marginTop.getValue()));
219         } else {
220           parent.setValueBinding(Attributes.MARGIN_TOP,
221               faceletContext.getFacesContext().getApplication().createValueBinding(marginTop.getValue()));
222         }
223       }
224 
225       if (marginBottom != null) {
226         if (marginBottom.isLiteral()) {
227           component.setMarginBottom(Measure.parse(marginBottom.getValue()));
228         } else {
229           parent.setValueBinding(Attributes.MARGIN_BOTTOM,
230               faceletContext.getFacesContext().getApplication().createValueBinding(marginBottom.getValue()));
231         }
232       }
233 
234     } else {
235       LOG.warn("Layout Constraints inside of a non LayoutBase component!");
236     }
237 
238     if (parent instanceof LayoutContainer) {
239       LayoutContainer container = (LayoutContainer) parent;
240 
241       if (borderLeft != null) {
242         if (borderLeft.isLiteral()) {
243           container.setBorderLeft(Measure.parse(borderLeft.getValue()));
244         } else {
245           parent.setValueBinding(Attributes.BORDER_LEFT,
246               faceletContext.getFacesContext().getApplication().createValueBinding(borderLeft.getValue()));
247         }
248       }
249 
250       if (borderRight != null) {
251         if (borderRight.isLiteral()) {
252           container.setBorderRight(Measure.parse(borderRight.getValue()));
253         } else {
254           parent.setValueBinding(Attributes.BORDER_RIGHT,
255               faceletContext.getFacesContext().getApplication().createValueBinding(borderRight.getValue()));
256         }
257       }
258 
259       if (borderTop != null) {
260         if (borderTop.isLiteral()) {
261           container.setBorderTop(Measure.parse(borderTop.getValue()));
262         } else {
263           parent.setValueBinding(Attributes.BORDER_TOP,
264               faceletContext.getFacesContext().getApplication().createValueBinding(borderTop.getValue()));
265         }
266       }
267 
268       if (borderBottom != null) {
269         if (borderBottom.isLiteral()) {
270           container.setBorderBottom(Measure.parse(borderBottom.getValue()));
271         } else {
272           parent.setValueBinding(Attributes.BORDER_BOTTOM,
273               faceletContext.getFacesContext().getApplication().createValueBinding(borderBottom.getValue()));
274         }
275       }
276 
277       if (paddingLeft != null) {
278         if (paddingLeft.isLiteral()) {
279           container.setPaddingLeft(Measure.parse(paddingLeft.getValue()));
280         } else {
281           parent.setValueBinding(Attributes.PADDING_LEFT,
282               faceletContext.getFacesContext().getApplication().createValueBinding(paddingLeft.getValue()));
283         }
284       }
285 
286       if (paddingRight != null) {
287         if (paddingRight.isLiteral()) {
288           container.setPaddingRight(Measure.parse(paddingRight.getValue()));
289         } else {
290           parent.setValueBinding(Attributes.PADDING_RIGHT,
291               faceletContext.getFacesContext().getApplication().createValueBinding(paddingRight.getValue()));
292         }
293       }
294 
295       if (paddingTop != null) {
296         if (paddingTop.isLiteral()) {
297           container.setPaddingTop(Measure.parse(paddingTop.getValue()));
298         } else {
299           parent.setValueBinding(Attributes.PADDING_TOP,
300               faceletContext.getFacesContext().getApplication().createValueBinding(paddingTop.getValue()));
301         }
302       }
303 
304       if (paddingBottom != null) {
305         if (paddingBottom.isLiteral()) {
306           container.setPaddingBottom(Measure.parse(paddingBottom.getValue()));
307         } else {
308           parent.setValueBinding(Attributes.PADDING_BOTTOM,
309               faceletContext.getFacesContext().getApplication().createValueBinding(paddingBottom.getValue()));
310         }
311       }
312 
313     } else {
314       if (LOG.isWarnEnabled()) {
315         if (borderLeft != null) {
316           LOG.warn("Ignoring border left, because the parent is not a LayoutContainer!");
317         }
318         if (borderRight != null) {
319           LOG.warn("Ignoring border right, because the parent is not a LayoutContainer!");
320         }
321         if (borderTop != null) {
322           LOG.warn("Ignoring border top, because the parent is not a LayoutContainer!");
323         }
324         if (borderBottom != null) {
325           LOG.warn("Ignoring border bottom, because the parent is not a LayoutContainer!");
326         }
327         if (paddingLeft != null) {
328           LOG.warn("Ignoring padding left, because the parent is not a LayoutContainer!");
329         }
330         if (paddingRight != null) {
331           LOG.warn("Ignoring padding right, because the parent is not a LayoutContainer!");
332         }
333         if (paddingTop != null) {
334           LOG.warn("Ignoring padding top, because the parent is not a LayoutContainer!");
335         }
336         if (paddingBottom != null) {
337           LOG.warn("Ignoring padding bottom, because the parent is not a LayoutContainer!");
338         }
339       }
340     }
341   }
342 }