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.Visual;
24  import org.apache.myfaces.tobago.internal.util.Deprecation;
25  import org.apache.myfaces.tobago.internal.util.FacesContextUtils;
26  import org.apache.myfaces.tobago.internal.util.StringUtils;
27  import org.apache.myfaces.tobago.layout.Display;
28  import org.apache.myfaces.tobago.layout.Measure;
29  import org.apache.myfaces.tobago.layout.Overflow;
30  import org.apache.myfaces.tobago.layout.Position;
31  import org.apache.myfaces.tobago.layout.TextAlign;
32  import org.apache.myfaces.tobago.renderkit.css.CustomClass;
33  import org.apache.myfaces.tobago.renderkit.css.Style;
34  
35  import javax.el.ELException;
36  import javax.faces.component.UIComponent;
37  import javax.faces.view.facelets.ComponentHandler;
38  import javax.faces.view.facelets.FaceletContext;
39  import javax.faces.view.facelets.TagAttribute;
40  import javax.faces.view.facelets.TagConfig;
41  import javax.faces.view.facelets.TagException;
42  import javax.faces.view.facelets.TagHandler;
43  
44  public final class StyleHandler extends TagHandler {
45  
46    private final TagAttribute file;
47  
48    private final TagAttribute customClass;
49  
50    private final TagAttribute width;
51    private final TagAttribute height;
52    private final TagAttribute minWidth;
53    private final TagAttribute minHeight;
54    private final TagAttribute maxWidth;
55    private final TagAttribute maxHeight;
56    private final TagAttribute left;
57    private final TagAttribute right;
58    private final TagAttribute top;
59    private final TagAttribute bottom;
60    private final TagAttribute paddingLeft;
61    private final TagAttribute paddingRight;
62    private final TagAttribute paddingTop;
63    private final TagAttribute paddingBottom;
64    private final TagAttribute marginLeft;
65    private final TagAttribute marginRight;
66    private final TagAttribute marginTop;
67    private final TagAttribute marginBottom;
68    private final TagAttribute overflowX;
69    private final TagAttribute overflowY;
70    private final TagAttribute display;
71    private final TagAttribute position;
72    private final TagAttribute textAlign;
73  
74  
75    public StyleHandler(final TagConfig config) {
76      super(config);
77  
78      this.file = getAttribute(Attributes.file.getName());
79  
80      this.customClass = getAttribute(Attributes.customClass.getName());
81  
82      this.width = getAttribute(Attributes.width.getName());
83      this.height = getAttribute(Attributes.height.getName());
84      this.minWidth = getAttribute(Attributes.minWidth.getName());
85      this.minHeight = getAttribute(Attributes.minHeight.getName());
86      this.maxWidth = getAttribute(Attributes.maxWidth.getName());
87      this.maxHeight = getAttribute(Attributes.maxHeight.getName());
88      this.left = getAttribute(Attributes.left.getName());
89      this.right = getAttribute(Attributes.right.getName());
90      this.top = getAttribute(Attributes.top.getName());
91      this.bottom = getAttribute(Attributes.bottom.getName());
92      this.paddingLeft = getAttribute(Attributes.paddingLeft.getName());
93      this.paddingRight = getAttribute(Attributes.paddingRight.getName());
94      this.paddingTop = getAttribute(Attributes.paddingTop.getName());
95      this.paddingBottom = getAttribute(Attributes.paddingBottom.getName());
96      this.marginLeft = getAttribute(Attributes.marginLeft.getName());
97      this.marginRight = getAttribute(Attributes.marginRight.getName());
98      this.marginTop = getAttribute(Attributes.marginTop.getName());
99      this.marginBottom = getAttribute(Attributes.marginBottom.getName());
100     this.overflowX = getAttribute(Attributes.overflowX.getName());
101     this.overflowY = getAttribute(Attributes.overflowY.getName());
102     this.display = getAttribute(Attributes.display.getName());
103     this.position = getAttribute(Attributes.position.getName());
104     this.textAlign = getAttribute(Attributes.textAlign.getName());
105   }
106 
107   @Override
108   public void apply(final FaceletContext faceletContext, final UIComponent parent) throws ELException {
109 
110     boolean renderedSet = getAttribute(Attributes.rendered.getName()) != null;
111     if (renderedSet) {
112       Deprecation.LOG.warn("Attribute 'rendered' is deprecated for tc:style.");
113     }
114     boolean rendered = !renderedSet || getAttribute(Attributes.rendered.getName()).getBoolean(faceletContext);
115 
116     if (ComponentHandler.isNew(parent) && rendered) {
117 
118       // file
119       if (file != null) {
120         final String value = file.getValue(faceletContext);
121         if (StringUtils.isNotBlank(value)) {
122           FacesContextUtils.addStyleFile(faceletContext.getFacesContext(), value);
123         }
124       }
125 
126       // custom class
127       if (customClass != null) {
128         final CustomClass customClassAttribute;
129         if (customClass.isLiteral()) {
130           final String value = customClass.getValue(faceletContext);
131           if (StringUtils.isNotBlank(value)) {
132             customClassAttribute = new CustomClass(value);
133           } else {
134             customClassAttribute = null;
135           }
136         } else {
137           customClassAttribute = new CustomClass(customClass.getValueExpression(faceletContext, Object.class));
138         }
139         ((Visual) parent).setCustomClass(customClassAttribute);
140       }
141 
142       // style
143       Style style = null;
144       style = applyAttributes1(faceletContext, style);
145       style = applyAttributes2(faceletContext, style);
146 
147       // XXX the applyAttributes1() and applyAttributes2() methods are only, because the checkstyle check requires
148       // XXX methods smaller than 250 lines. A better resolution might be possible with Java 8 lambdas.
149 
150       if (style != null) {
151         if (parent instanceof Visual) {
152           ((Visual) parent).setStyle(style);
153         } else {
154           throw new TagException(tag, "Parent component is not of type Visual, so no style can be set!");
155         }
156       }
157     }
158   }
159 
160   private Style applyAttributes1(final FaceletContext faceletContext, final Style parameter) {
161 
162     Style style = parameter;
163 
164     if (width != null) {
165       if (width.isLiteral()) {
166         final String value = width.getValue(faceletContext);
167         if (StringUtils.isNotBlank(value)) {
168           if (style == null) {
169             style = new Style();
170           }
171           style.setWidth(Measure.valueOf(value));
172         }
173       } else {
174         if (style == null) {
175           style = new Style();
176         }
177         style.setWidth(width.getValueExpression(faceletContext, Object.class));
178       }
179     }
180 
181     if (height != null) {
182       if (height.isLiteral()) {
183         final String value = height.getValue(faceletContext);
184         if (StringUtils.isNotBlank(value)) {
185           if (style == null) {
186             style = new Style();
187           }
188           style.setHeight(Measure.valueOf(value));
189         }
190       } else {
191         if (style == null) {
192           style = new Style();
193         }
194         style.setHeight(height.getValueExpression(faceletContext, Object.class));
195       }
196     }
197 
198     if (minWidth != null) {
199       if (minWidth.isLiteral()) {
200         final String value = minWidth.getValue(faceletContext);
201         if (StringUtils.isNotBlank(value)) {
202           if (style == null) {
203             style = new Style();
204           }
205           style.setMinWidth(Measure.valueOf(value));
206         }
207       } else {
208         if (style == null) {
209           style = new Style();
210         }
211         style.setMinWidth(minWidth.getValueExpression(faceletContext, Object.class));
212       }
213     }
214 
215     if (minHeight != null) {
216       if (minHeight.isLiteral()) {
217         final String value = minHeight.getValue(faceletContext);
218         if (StringUtils.isNotBlank(value)) {
219           if (style == null) {
220             style = new Style();
221           }
222           style.setMinHeight(Measure.valueOf(value));
223         }
224       } else {
225         if (style == null) {
226           style = new Style();
227         }
228         style.setMinHeight(minHeight.getValueExpression(faceletContext, Object.class));
229       }
230     }
231 
232     if (maxWidth != null) {
233       if (maxWidth.isLiteral()) {
234         final String value = maxWidth.getValue(faceletContext);
235         if (StringUtils.isNotBlank(value)) {
236           if (style == null) {
237             style = new Style();
238           }
239           style.setMaxWidth(Measure.valueOf(value));
240         }
241       } else {
242         if (style == null) {
243           style = new Style();
244         }
245         style.setMaxWidth(maxWidth.getValueExpression(faceletContext, Object.class));
246       }
247     }
248 
249     if (maxHeight != null) {
250       if (maxHeight.isLiteral()) {
251         final String value = maxHeight.getValue(faceletContext);
252         if (StringUtils.isNotBlank(value)) {
253           if (style == null) {
254             style = new Style();
255           }
256           style.setMaxHeight(Measure.valueOf(value));
257         }
258       } else {
259         if (style == null) {
260           style = new Style();
261         }
262         style.setMaxHeight(maxHeight.getValueExpression(faceletContext, Object.class));
263       }
264     }
265 
266     if (left != null) {
267       if (left.isLiteral()) {
268         final String value = left.getValue(faceletContext);
269         if (StringUtils.isNotBlank(value)) {
270           if (style == null) {
271             style = new Style();
272           }
273           style.setLeft(Measure.valueOf(value));
274         }
275       } else {
276         if (style == null) {
277           style = new Style();
278         }
279         style.setLeft(left.getValueExpression(faceletContext, Object.class));
280       }
281     }
282 
283     if (right != null) {
284       if (right.isLiteral()) {
285         final String value = right.getValue(faceletContext);
286         if (StringUtils.isNotBlank(value)) {
287           if (style == null) {
288             style = new Style();
289           }
290           style.setRight(Measure.valueOf(value));
291         }
292       } else {
293         if (style == null) {
294           style = new Style();
295         }
296         style.setRight(right.getValueExpression(faceletContext, Object.class));
297       }
298     }
299 
300     if (top != null) {
301       if (top.isLiteral()) {
302         final String value = top.getValue(faceletContext);
303         if (StringUtils.isNotBlank(value)) {
304           if (style == null) {
305             style = new Style();
306           }
307           style.setTop(Measure.valueOf(value));
308         }
309       } else {
310         if (style == null) {
311           style = new Style();
312         }
313         style.setTop(top.getValueExpression(faceletContext, Object.class));
314       }
315     }
316 
317     if (bottom != null) {
318       if (bottom.isLiteral()) {
319         final String value = bottom.getValue(faceletContext);
320         if (StringUtils.isNotBlank(value)) {
321           if (style == null) {
322             style = new Style();
323           }
324           style.setBottom(Measure.valueOf(value));
325         }
326       } else {
327         if (style == null) {
328           style = new Style();
329         }
330         style.setBottom(bottom.getValueExpression(faceletContext, Object.class));
331       }
332     }
333 
334     if (paddingLeft != null) {
335       if (paddingLeft.isLiteral()) {
336         final String value = paddingLeft.getValue(faceletContext);
337         if (StringUtils.isNotBlank(value)) {
338           if (style == null) {
339             style = new Style();
340           }
341           style.setPaddingLeft(Measure.valueOf(value));
342         }
343       } else {
344         if (style == null) {
345           style = new Style();
346         }
347         style.setPaddingLeft(paddingLeft.getValueExpression(faceletContext, Object.class));
348       }
349     }
350 
351     if (paddingRight != null) {
352       if (paddingRight.isLiteral()) {
353         final String value = paddingRight.getValue(faceletContext);
354         if (StringUtils.isNotBlank(value)) {
355           if (style == null) {
356             style = new Style();
357           }
358           style.setPaddingRight(Measure.valueOf(value));
359         }
360       } else {
361         if (style == null) {
362           style = new Style();
363         }
364         style.setPaddingRight(paddingRight.getValueExpression(faceletContext, Object.class));
365       }
366     }
367 
368     if (paddingTop != null) {
369       if (paddingTop.isLiteral()) {
370         final String value = paddingTop.getValue(faceletContext);
371         if (StringUtils.isNotBlank(value)) {
372           if (style == null) {
373             style = new Style();
374           }
375           style.setPaddingTop(Measure.valueOf(value));
376         }
377       } else {
378         if (style == null) {
379           style = new Style();
380         }
381         style.setPaddingTop(paddingTop.getValueExpression(faceletContext, Object.class));
382       }
383     }
384 
385     if (paddingBottom != null) {
386       if (paddingBottom.isLiteral()) {
387         final String value = paddingBottom.getValue(faceletContext);
388         if (StringUtils.isNotBlank(value)) {
389           if (style == null) {
390             style = new Style();
391           }
392           style.setPaddingBottom(Measure.valueOf(value));
393         }
394       } else {
395         if (style == null) {
396           style = new Style();
397         }
398         style.setPaddingBottom(paddingBottom.getValueExpression(faceletContext, Object.class));
399       }
400     }
401     return style;
402   }
403 
404   private Style applyAttributes2(FaceletContext faceletContext, Style parameter) {
405 
406     Style style = parameter;
407 
408     if (marginLeft != null) {
409       if (marginLeft.isLiteral()) {
410         final String value = marginLeft.getValue(faceletContext);
411         if (StringUtils.isNotBlank(value)) {
412           if (style == null) {
413             style = new Style();
414           }
415           style.setMarginLeft(Measure.valueOf(value));
416         }
417       } else {
418         if (style == null) {
419           style = new Style();
420         }
421         style.setMarginLeft(marginLeft.getValueExpression(faceletContext, Object.class));
422       }
423     }
424 
425     if (marginRight != null) {
426       if (marginRight.isLiteral()) {
427         final String value = marginRight.getValue(faceletContext);
428         if (StringUtils.isNotBlank(value)) {
429           if (style == null) {
430             style = new Style();
431           }
432           style.setMarginRight(Measure.valueOf(value));
433         }
434       } else {
435         if (style == null) {
436           style = new Style();
437         }
438         style.setMarginRight(marginRight.getValueExpression(faceletContext, Object.class));
439       }
440     }
441 
442     if (marginTop != null) {
443       if (marginTop.isLiteral()) {
444         final String value = marginTop.getValue(faceletContext);
445         if (StringUtils.isNotBlank(value)) {
446           if (style == null) {
447             style = new Style();
448           }
449           style.setMarginTop(Measure.valueOf(value));
450         }
451       } else {
452         if (style == null) {
453           style = new Style();
454         }
455         style.setMarginTop(marginTop.getValueExpression(faceletContext, Object.class));
456       }
457     }
458 
459     if (marginBottom != null) {
460       if (marginBottom.isLiteral()) {
461         final String value = marginBottom.getValue(faceletContext);
462         if (StringUtils.isNotBlank(value)) {
463           if (style == null) {
464             style = new Style();
465           }
466           style.setMarginBottom(Measure.valueOf(value));
467         }
468       } else {
469         if (style == null) {
470           style = new Style();
471         }
472         style.setMarginBottom(marginBottom.getValueExpression(faceletContext, Object.class));
473       }
474     }
475 
476     if (overflowX != null) {
477       if (overflowX.isLiteral()) {
478         final String value = overflowX.getValue(faceletContext);
479         if (StringUtils.isNotBlank(value)) {
480           if (style == null) {
481             style = new Style();
482           }
483           style.setOverflowX(Overflow.valueOf(value));
484         }
485       } else {
486         if (style == null) {
487           style = new Style();
488         }
489         style.setOverflowX(overflowX.getValueExpression(faceletContext, Object.class));
490       }
491     }
492 
493     if (overflowY != null) {
494       if (overflowY.isLiteral()) {
495         final String value = overflowY.getValue(faceletContext);
496         if (StringUtils.isNotBlank(value)) {
497           if (style == null) {
498             style = new Style();
499           }
500           style.setOverflowY(Overflow.valueOf(value));
501         }
502       } else {
503         if (style == null) {
504           style = new Style();
505         }
506         style.setOverflowY(overflowY.getValueExpression(faceletContext, Object.class));
507       }
508     }
509 
510     if (display != null) {
511       if (display.isLiteral()) {
512         final String value = display.getValue(faceletContext);
513         if (StringUtils.isNotBlank(value)) {
514           if (style == null) {
515             style = new Style();
516           }
517           style.setDisplay(Display.valueOf(value));
518         }
519       } else {
520         if (style == null) {
521           style = new Style();
522         }
523         style.setDisplay(display.getValueExpression(faceletContext, Object.class));
524       }
525     }
526 
527     if (position != null) {
528       if (position.isLiteral()) {
529         final String value = position.getValue(faceletContext);
530         if (StringUtils.isNotBlank(value)) {
531           if (style == null) {
532             style = new Style();
533           }
534           style.setPosition(Position.valueOf(value));
535         }
536       } else {
537         if (style == null) {
538           style = new Style();
539         }
540         style.setPosition(position.getValueExpression(faceletContext, Object.class));
541       }
542     }
543 
544     if (textAlign != null) {
545       if (textAlign.isLiteral()) {
546         final String value = textAlign.getValue(faceletContext);
547         if (StringUtils.isNotBlank(value)) {
548           if (style == null) {
549             style = new Style();
550           }
551           style.setTextAlign(TextAlign.valueOf(value));
552         }
553       } else {
554         if (style == null) {
555           style = new Style();
556         }
557         style.setTextAlign(textAlign.getValueExpression(faceletContext, Object.class));
558       }
559     }
560     return style;
561   }
562 }