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.el.ELException;
32  import javax.faces.FacesException;
33  import javax.faces.component.UIComponent;
34  import javax.faces.view.facelets.FaceletContext;
35  import javax.faces.view.facelets.TagAttribute;
36  import javax.faces.view.facelets.TagConfig;
37  import javax.faces.view.facelets.TagHandler;
38  import java.io.IOException;
39  
40  public class GridLayoutConstraintHandler extends TagHandler {
41  
42    private static final Logger LOG = LoggerFactory.getLogger(GridLayoutConstraintHandler.class);
43  
44    private final TagAttribute columnSpan;
45    private final TagAttribute rowSpan;
46  
47    private final TagAttribute width;
48    private final TagAttribute height;
49  
50    private final TagAttribute minimumWidth;
51    private final TagAttribute minimumHeight;
52  
53    private final TagAttribute preferredWidth;
54    private final TagAttribute preferredHeight;
55  
56    private final TagAttribute maximumWidth;
57    private final TagAttribute maximumHeight;
58  
59    private final TagAttribute marginLeft;
60    private final TagAttribute marginRight;
61    private final TagAttribute marginTop;
62    private final TagAttribute marginBottom;
63  
64    private final TagAttribute borderLeft;
65    private final TagAttribute borderRight;
66    private final TagAttribute borderTop;
67    private final TagAttribute borderBottom;
68  
69    private final TagAttribute paddingLeft;
70    private final TagAttribute paddingRight;
71    private final TagAttribute paddingTop;
72    private final TagAttribute paddingBottom;
73  
74    public GridLayoutConstraintHandler(TagConfig config) {
75      super(config);
76      columnSpan = getAttribute(Attributes.COLUMN_SPAN);
77      rowSpan = getAttribute(Attributes.ROW_SPAN);
78      width = getAttribute(Attributes.WIDTH);
79      height = getAttribute(Attributes.HEIGHT);
80      minimumWidth = getAttribute(Attributes.MINIMUM_WIDTH);
81      minimumHeight = getAttribute(Attributes.MINIMUM_HEIGHT);
82      preferredWidth = getAttribute(Attributes.PREFERRED_WIDTH);
83      preferredHeight = getAttribute(Attributes.PREFERRED_HEIGHT);
84      maximumWidth = getAttribute(Attributes.MAXIMUM_WIDTH);
85      maximumHeight = getAttribute(Attributes.MAXIMUM_HEIGHT);
86      marginLeft = getAttribute(Attributes.MARGIN_LEFT);
87      marginRight = getAttribute(Attributes.MARGIN_RIGHT);
88      marginTop = getAttribute(Attributes.MARGIN_TOP);
89      marginBottom = getAttribute(Attributes.MARGIN_BOTTOM);
90      borderLeft = getAttribute(Attributes.BORDER_LEFT);
91      borderRight = getAttribute(Attributes.BORDER_RIGHT);
92      borderTop = getAttribute(Attributes.BORDER_TOP);
93      borderBottom = getAttribute(Attributes.BORDER_BOTTOM);
94      paddingLeft = getAttribute(Attributes.PADDING_LEFT);
95      paddingRight = getAttribute(Attributes.PADDING_RIGHT);
96      paddingTop = getAttribute(Attributes.PADDING_TOP);
97      paddingBottom = getAttribute(Attributes.PADDING_BOTTOM);
98    }
99  
100   public void apply(FaceletContext faceletContext, UIComponent parent)
101       throws IOException, FacesException, ELException {
102     if (parent.getParent() != null && parent.getParent() instanceof UIExtensionPanel) {
103        parent = parent.getParent();
104     } else if (parent.getAttributes().get("tobago.panel") != null
105          && parent.getAttributes().get("tobago.panel") instanceof UIExtensionPanel) {
106        parent = (UIComponent) parent.getAttributes().get("tobago.panel");
107     }
108     if (parent instanceof LayoutBase) {
109       LayoutBase component = (LayoutBase) parent;
110 
111       if (parent instanceof LayoutComponent && columnSpan != null) {
112         if (columnSpan.isLiteral()) {
113           ((LayoutComponent) component).setColumnSpan(Integer.valueOf(columnSpan.getValue()));
114         } else {
115           parent.setValueBinding(Attributes.COLUMN_SPAN,
116               faceletContext.getFacesContext().getApplication().createValueBinding(columnSpan.getValue()));
117         }
118       }
119 
120       if (parent instanceof LayoutComponent && rowSpan != null) {
121         if (rowSpan.isLiteral()) {
122           ((LayoutComponent) component).setRowSpan(Integer.valueOf(rowSpan.getValue()));
123         } else {
124           parent.setValueBinding(Attributes.ROW_SPAN,
125               faceletContext.getFacesContext().getApplication().createValueBinding(rowSpan.getValue()));
126         }
127       }
128 
129       if (width != null) {
130         if (width.isLiteral()) {
131           component.setWidth(Measure.parse(width.getValue()));
132         } else {
133           parent.setValueBinding(Attributes.WIDTH,
134               faceletContext.getFacesContext().getApplication().createValueBinding(width.getValue()));
135         }
136       }
137 
138       if (height != null) {
139         if (height.isLiteral()) {
140           component.setHeight(Measure.parse(height.getValue()));
141         } else {
142           parent.setValueBinding(Attributes.HEIGHT,
143               faceletContext.getFacesContext().getApplication().createValueBinding(height.getValue()));
144         }
145       }
146 
147       if (minimumWidth != null) {
148         if (minimumWidth.isLiteral()) {
149           component.setMinimumWidth(Measure.parse(minimumWidth.getValue()));
150         } else {
151           parent.setValueBinding(Attributes.MINIMUM_WIDTH,
152               faceletContext.getFacesContext().getApplication().createValueBinding(minimumWidth.getValue()));
153         }
154       }
155 
156       if (minimumHeight != null) {
157         if (minimumHeight.isLiteral()) {
158           component.setMinimumHeight(Measure.parse(minimumHeight.getValue()));
159         } else {
160           parent.setValueBinding(Attributes.MINIMUM_HEIGHT,
161               faceletContext.getFacesContext().getApplication().createValueBinding(minimumHeight.getValue()));
162         }
163       }
164 
165       if (preferredWidth != null) {
166         if (preferredWidth.isLiteral()) {
167           component.setPreferredWidth(Measure.parse(preferredWidth.getValue()));
168         } else {
169           parent.setValueBinding(Attributes.PREFERRED_WIDTH,
170               faceletContext.getFacesContext().getApplication().createValueBinding(preferredWidth.getValue()));
171         }
172       }
173 
174       if (preferredHeight != null) {
175         if (preferredHeight.isLiteral()) {
176           component.setPreferredHeight(Measure.parse(preferredHeight.getValue()));
177         } else {
178           parent.setValueBinding(Attributes.PREFERRED_HEIGHT,
179               faceletContext.getFacesContext().getApplication().createValueBinding(preferredHeight.getValue()));
180         }
181       }
182 
183       if (maximumWidth != null) {
184         if (maximumWidth.isLiteral()) {
185           component.setMaximumWidth(Measure.parse(maximumWidth.getValue()));
186         } else {
187           parent.setValueBinding(Attributes.MAXIMUM_WIDTH,
188               faceletContext.getFacesContext().getApplication().createValueBinding(maximumWidth.getValue()));
189         }
190       }
191 
192       if (maximumHeight != null) {
193         if (maximumHeight.isLiteral()) {
194           component.setMaximumHeight(Measure.parse(maximumHeight.getValue()));
195         } else {
196           parent.setValueBinding(Attributes.MAXIMUM_HEIGHT,
197               faceletContext.getFacesContext().getApplication().createValueBinding(maximumHeight.getValue()));
198         }
199       }
200 
201       if (marginLeft != null) {
202         if (marginLeft.isLiteral()) {
203           component.setMarginLeft(Measure.parse(marginLeft.getValue()));
204         } else {
205           parent.setValueBinding(Attributes.MARGIN_LEFT,
206               faceletContext.getFacesContext().getApplication().createValueBinding(marginLeft.getValue()));
207         }
208       }
209 
210       if (marginRight != null) {
211         if (marginRight.isLiteral()) {
212           component.setMarginRight(Measure.parse(marginRight.getValue()));
213         } else {
214           parent.setValueBinding(Attributes.MARGIN_RIGHT,
215               faceletContext.getFacesContext().getApplication().createValueBinding(marginRight.getValue()));
216         }
217       }
218 
219       if (marginTop != null) {
220         if (marginTop.isLiteral()) {
221           component.setMarginTop(Measure.parse(marginTop.getValue()));
222         } else {
223           parent.setValueBinding(Attributes.MARGIN_TOP,
224               faceletContext.getFacesContext().getApplication().createValueBinding(marginTop.getValue()));
225         }
226       }
227 
228       if (marginBottom != null) {
229         if (marginBottom.isLiteral()) {
230           component.setMarginBottom(Measure.parse(marginBottom.getValue()));
231         } else {
232           parent.setValueBinding(Attributes.MARGIN_BOTTOM,
233               faceletContext.getFacesContext().getApplication().createValueBinding(marginBottom.getValue()));
234         }
235       }
236 
237     } else {
238       LOG.warn("Layout Constraints inside of a non LayoutBase component!");
239     }
240 
241     if (parent instanceof LayoutContainer) {
242       LayoutContainer container = (LayoutContainer) parent;
243 
244       if (borderLeft != null) {
245         if (borderLeft.isLiteral()) {
246           container.setBorderLeft(Measure.parse(borderLeft.getValue()));
247         } else {
248           parent.setValueBinding(Attributes.BORDER_LEFT,
249               faceletContext.getFacesContext().getApplication().createValueBinding(borderLeft.getValue()));
250         }
251       }
252 
253       if (borderRight != null) {
254         if (borderRight.isLiteral()) {
255           container.setBorderRight(Measure.parse(borderRight.getValue()));
256         } else {
257           parent.setValueBinding(Attributes.BORDER_RIGHT,
258               faceletContext.getFacesContext().getApplication().createValueBinding(borderRight.getValue()));
259         }
260       }
261 
262       if (borderTop != null) {
263         if (borderTop.isLiteral()) {
264           container.setBorderTop(Measure.parse(borderTop.getValue()));
265         } else {
266           parent.setValueBinding(Attributes.BORDER_TOP,
267               faceletContext.getFacesContext().getApplication().createValueBinding(borderTop.getValue()));
268         }
269       }
270 
271       if (borderBottom != null) {
272         if (borderBottom.isLiteral()) {
273           container.setBorderBottom(Measure.parse(borderBottom.getValue()));
274         } else {
275           parent.setValueBinding(Attributes.BORDER_BOTTOM,
276               faceletContext.getFacesContext().getApplication().createValueBinding(borderBottom.getValue()));
277         }
278       }
279 
280       if (paddingLeft != null) {
281         if (paddingLeft.isLiteral()) {
282           container.setPaddingLeft(Measure.parse(paddingLeft.getValue()));
283         } else {
284           parent.setValueBinding(Attributes.PADDING_LEFT,
285               faceletContext.getFacesContext().getApplication().createValueBinding(paddingLeft.getValue()));
286         }
287       }
288 
289       if (paddingRight != null) {
290         if (paddingRight.isLiteral()) {
291           container.setPaddingRight(Measure.parse(paddingRight.getValue()));
292         } else {
293           parent.setValueBinding(Attributes.PADDING_RIGHT,
294               faceletContext.getFacesContext().getApplication().createValueBinding(paddingRight.getValue()));
295         }
296       }
297 
298       if (paddingTop != null) {
299         if (paddingTop.isLiteral()) {
300           container.setPaddingTop(Measure.parse(paddingTop.getValue()));
301         } else {
302           parent.setValueBinding(Attributes.PADDING_TOP,
303               faceletContext.getFacesContext().getApplication().createValueBinding(paddingTop.getValue()));
304         }
305       }
306 
307       if (paddingBottom != null) {
308         if (paddingBottom.isLiteral()) {
309           container.setPaddingBottom(Measure.parse(paddingBottom.getValue()));
310         } else {
311           parent.setValueBinding(Attributes.PADDING_BOTTOM,
312               faceletContext.getFacesContext().getApplication().createValueBinding(paddingBottom.getValue()));
313         }
314       }
315 
316     } else {
317       if (LOG.isWarnEnabled()) {
318         if (borderLeft != null) {
319           LOG.warn("Ignoring border left, because the parent is not a LayoutContainer!");
320         }
321         if (borderRight != null) {
322           LOG.warn("Ignoring border right, because the parent is not a LayoutContainer!");
323         }
324         if (borderTop != null) {
325           LOG.warn("Ignoring border top, because the parent is not a LayoutContainer!");
326         }
327         if (borderBottom != null) {
328           LOG.warn("Ignoring border bottom, because the parent is not a LayoutContainer!");
329         }
330         if (paddingLeft != null) {
331           LOG.warn("Ignoring padding left, because the parent is not a LayoutContainer!");
332         }
333         if (paddingRight != null) {
334           LOG.warn("Ignoring padding right, because the parent is not a LayoutContainer!");
335         }
336         if (paddingTop != null) {
337           LOG.warn("Ignoring padding top, because the parent is not a LayoutContainer!");
338         }
339         if (paddingBottom != null) {
340           LOG.warn("Ignoring padding bottom, because the parent is not a LayoutContainer!");
341         }
342       }
343     }
344   }
345 }