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.renderkit.css;
21  
22  import org.apache.myfaces.tobago.component.UIStyle;
23  import org.apache.myfaces.tobago.layout.Display;
24  import org.apache.myfaces.tobago.layout.Measure;
25  import org.apache.myfaces.tobago.layout.Overflow;
26  import org.apache.myfaces.tobago.layout.Position;
27  import org.apache.myfaces.tobago.layout.TextAlign;
28  
29  import java.io.Serializable;
30  
31  /**
32   * A subset of the CSS style used by Tobago. It's more or less the layout specific part.
33   */
34  public class Style implements Serializable {
35  
36    private static final long serialVersionUID = 5L;
37  
38    private Measure width;
39    private Measure height;
40  
41    private Measure minWidth;
42    private Measure minHeight;
43    private Measure maxWidth;
44    private Measure maxHeight;
45  
46    private Measure left;
47    private Measure right;
48    private Measure top;
49    private Measure bottom;
50  
51    private Measure paddingLeft;
52    private Measure paddingRight;
53    private Measure paddingTop;
54    private Measure paddingBottom;
55  
56    private Measure marginLeft;
57    private Measure marginRight;
58    private Measure marginTop;
59    private Measure marginBottom;
60  
61    private Overflow overflowX;
62    private Overflow overflowY;
63    private Display display;
64    private Position position;
65  
66    private String backgroundImage;
67    private String backgroundPosition;
68    private Integer zIndex; // TBD
69    private TextAlign textAlign;
70  
71    private Boolean empty;
72  
73    public Style() {
74    }
75  
76    public Style(final UIStyle style) {
77  
78      width = style.getWidth();
79      height = style.getHeight();
80  
81      minWidth = style.getMinWidth();
82      minHeight = style.getMinHeight();
83      maxWidth = style.getMaxWidth();
84      maxHeight = style.getMaxHeight();
85  
86      left = style.getLeft();
87      right = style.getRight();
88      top = style.getTop();
89      bottom = style.getBottom();
90  
91      paddingLeft = style.getPaddingLeft();
92      paddingRight = style.getPaddingRight();
93      paddingTop = style.getPaddingTop();
94      paddingBottom = style.getPaddingBottom();
95  
96      marginLeft = style.getMarginLeft();
97      marginRight = style.getMarginRight();
98      marginTop = style.getMarginTop();
99      marginBottom = style.getMarginBottom();
100 
101     overflowX = style.getOverflowX();
102     overflowY = style.getOverflowY();
103     display = style.getDisplay();
104     position = style.getPosition();
105 
106     // tbd: backgroundImage from UIStyle?
107     // tbd: backgroundPosition from UIStyle?
108     // tbd: zIndex from UIStyle?
109     textAlign = style.getTextAlign();
110 
111     checkEmptiness();
112   }
113 
114   /**
115    * Checks if the encode string holds free text, which must be escaped.
116    * This is the case for image URLs.
117    * For {@link Measure}, and enum types like {@link Display} no escaping is needed.
118    */
119   public boolean needsToBeEscaped() {
120     return backgroundImage != null;
121   }
122 
123   public boolean isEmpty() {
124     if (empty == null) {
125       checkEmptiness();
126     }
127     return empty;
128   }
129 
130   public void checkEmptiness() {
131     empty
132         = width == null
133         && height == null
134 
135         && minWidth == null
136         && minHeight == null
137         && maxWidth == null
138         && maxHeight == null
139 
140         && left == null
141         && right == null
142         && top == null
143         && bottom == null
144 
145         && marginLeft == null
146         && marginRight == null
147         && marginTop == null
148         && marginBottom == null
149 
150         && paddingLeft == null
151         && paddingRight == null
152         && paddingTop == null
153         && paddingBottom == null
154 
155         && display == null
156         && position == null
157         && overflowX == null
158         && overflowY == null
159 
160         && backgroundImage == null
161         && backgroundPosition == null
162         && zIndex == null
163         && textAlign == null;
164   }
165 
166   public String encode() {
167     final StringBuilder buf = new StringBuilder();
168     if (width != null) {
169       buf.append("width:");
170       buf.append(width.serialize());
171       buf.append(';');
172     }
173     if (height != null) {
174       buf.append("height:");
175       buf.append(height.serialize());
176       buf.append(';');
177     }
178     if (minWidth != null) {
179       buf.append("min-width:");
180       buf.append(minWidth.serialize());
181       buf.append(';');
182     }
183     if (minHeight != null) {
184       buf.append("min-height:");
185       buf.append(minHeight.serialize());
186       buf.append(';');
187     }
188     if (maxWidth != null) {
189       buf.append("max-width:");
190       buf.append(maxWidth.serialize());
191       buf.append(';');
192     }
193     if (maxHeight != null) {
194       buf.append("max-height:");
195       buf.append(maxHeight.serialize());
196       buf.append(';');
197     }
198     if (left != null) {
199       buf.append("left:");
200       buf.append(left.serialize());
201       buf.append(';');
202     }
203     if (right != null) {
204       buf.append("right:");
205       buf.append(right.serialize());
206       buf.append(';');
207     }
208     if (top != null) {
209       buf.append("top:");
210       buf.append(top.serialize());
211       buf.append(';');
212     }
213     if (bottom != null) {
214       buf.append("bottom:");
215       buf.append(bottom.serialize());
216       buf.append(';');
217     }
218     if (paddingLeft != null) {
219       buf.append("padding-left:");
220       buf.append(paddingLeft.serialize());
221       buf.append(';');
222     }
223     if (paddingRight != null) {
224       buf.append("padding-right:");
225       buf.append(paddingRight.serialize());
226       buf.append(';');
227     }
228     if (paddingTop != null) {
229       buf.append("padding-top:");
230       buf.append(paddingTop.serialize());
231       buf.append(';');
232     }
233     if (paddingBottom != null) {
234       buf.append("padding-bottom:");
235       buf.append(paddingBottom.serialize());
236       buf.append(';');
237     }
238     if (marginLeft != null) {
239       buf.append("margin-left:");
240       buf.append(marginLeft.serialize());
241       buf.append(';');
242     }
243     if (marginRight != null) {
244       buf.append("margin-right:");
245       buf.append(marginRight.serialize());
246       buf.append(';');
247     }
248     if (marginTop != null) {
249       buf.append("margin-top:");
250       buf.append(marginTop.serialize());
251       buf.append(';');
252     }
253     if (marginBottom != null) {
254       buf.append("margin-bottom:");
255       buf.append(marginBottom.serialize());
256       buf.append(';');
257     }
258     if (overflowX != null) {
259       buf.append("overflow-x:");
260       buf.append(overflowX.name());
261       buf.append(';');
262     }
263     if (overflowY != null) {
264       buf.append("overflow-y:");
265       buf.append(overflowY.name());
266       buf.append(';');
267     }
268     if (display != null) {
269       buf.append("display:");
270       buf.append(display.name());
271       buf.append(';');
272     }
273     if (position != null) {
274       buf.append("position:");
275       buf.append(position.name());
276       buf.append(';');
277     }
278     if (backgroundImage != null) {
279       buf.append("background-image:");
280       buf.append(backgroundImage);
281       buf.append(';');
282     }
283     if (backgroundPosition != null) {
284       buf.append("background-position:");
285       buf.append(backgroundPosition);
286       buf.append(';');
287     }
288     if (zIndex != null) {
289       buf.append("z-index:");
290       buf.append(zIndex);
291       buf.append(';');
292     }
293     if (textAlign != null) {
294       buf.append("text-align:");
295       buf.append(textAlign.name());
296       buf.append(';');
297     }
298 
299     return buf.toString();
300   }
301 
302   public String encodeJson() {
303     final StringBuilder buf = new StringBuilder("{");
304     if (width != null) {
305       buf.append("\"width\":\"");
306       buf.append(width.serialize());
307       buf.append("\",");
308     }
309     if (height != null) {
310       buf.append("\"height\":\"");
311       buf.append(height.serialize());
312       buf.append("\",");
313     }
314     if (minWidth != null) {
315       buf.append("\"minWidth\":\"");
316       buf.append(minWidth.serialize());
317       buf.append("\",");
318     }
319     if (minHeight != null) {
320       buf.append("\"minHeight\":\"");
321       buf.append(minHeight.serialize());
322       buf.append("\",");
323     }
324     if (maxWidth != null) {
325       buf.append("\"maxWidth\":\"");
326       buf.append(maxWidth.serialize());
327       buf.append("\",");
328     }
329     if (maxHeight != null) {
330       buf.append("\"maxHeight\":\"");
331       buf.append(maxHeight.serialize());
332       buf.append("\",");
333     }
334     if (left != null) {
335       buf.append("\"left\":\"");
336       buf.append(left.serialize());
337       buf.append("\",");
338     }
339     if (right != null) {
340       buf.append("\"right\":\"");
341       buf.append(right.serialize());
342       buf.append("\",");
343     }
344     if (top != null) {
345       buf.append("\"top\":\"");
346       buf.append(top.serialize());
347       buf.append("\",");
348     }
349     if (bottom != null) {
350       buf.append("\"bottom\":\"");
351       buf.append(bottom.serialize());
352       buf.append("\",");
353     }
354     if (paddingLeft != null) {
355       buf.append("\"paddingLeft\":\"");
356       buf.append(paddingLeft.serialize());
357       buf.append("\",");
358     }
359     if (paddingRight != null) {
360       buf.append("\"paddingRight\":\"");
361       buf.append(paddingRight.serialize());
362       buf.append("\",");
363     }
364     if (paddingTop != null) {
365       buf.append("\"paddingTop\":\"");
366       buf.append(paddingTop.serialize());
367       buf.append("\",");
368     }
369     if (paddingBottom != null) {
370       buf.append("\"paddingBottom\":\"");
371       buf.append(paddingBottom.serialize());
372       buf.append("\",");
373     }
374     if (marginLeft != null) {
375       buf.append("\"marginLeft\":\"");
376       buf.append(marginLeft.serialize());
377       buf.append("\",");
378     }
379     if (marginRight != null) {
380       buf.append("\"marginRight\":\"");
381       buf.append(marginRight.serialize());
382       buf.append("\",");
383     }
384     if (marginTop != null) {
385       buf.append("\"marginTop\":\"");
386       buf.append(marginTop.serialize());
387       buf.append("\",");
388     }
389     if (marginBottom != null) {
390       buf.append("\"marginBottom\":\"");
391       buf.append(marginBottom.serialize());
392       buf.append("\",");
393     }
394     if (overflowX != null) {
395       buf.append("\"overflowX\":\"");
396       buf.append(overflowX.name());
397       buf.append("\",");
398     }
399     if (overflowY != null) {
400       buf.append("\"overflowY\":\"");
401       buf.append(overflowY.name());
402       buf.append("\",");
403     }
404     if (display != null) {
405       buf.append("\"display\":\"");
406       buf.append(display.name());
407       buf.append("\",");
408     }
409     if (position != null) {
410       buf.append("\"position\":\"");
411       buf.append(position.name());
412       buf.append("\",");
413     }
414     if (backgroundImage != null) {
415       buf.append("\"backgroundImage\":\"");
416       buf.append(backgroundImage);
417       buf.append("\",");
418     }
419     if (backgroundPosition != null) {
420       buf.append("\"backgroundPosition\":\"");
421       buf.append(backgroundPosition);
422       buf.append("\",");
423     }
424     if (zIndex != null) {
425       buf.append("\"zIndex\":");
426       buf.append(zIndex);
427       buf.append(",");
428     }
429     if (textAlign != null) {
430       buf.append("\"textAlign\":\"");
431       buf.append(textAlign.name());
432       buf.append("\",");
433     }
434 
435     if (buf.length() > 1) {
436       buf.deleteCharAt(buf.length() - 1);
437     }
438 
439     buf.append('}');
440     return buf.toString();
441   }
442 
443   public Measure getWidth() {
444     return width;
445   }
446 
447   public void setWidth(final Measure width) {
448     empty = null;
449     this.width = width;
450   }
451 
452   public Measure getHeight() {
453     return height;
454   }
455 
456   public void setHeight(final Measure height) {
457     empty = null;
458     this.height = height;
459   }
460 
461   public Measure getMinWidth() {
462     return minWidth;
463   }
464 
465   public void setMinWidth(Measure minWidth) {
466     empty = null;
467     this.minWidth = minWidth;
468   }
469 
470   public Measure getMinHeight() {
471     return minHeight;
472   }
473 
474   public void setMinHeight(Measure minHeight) {
475     empty = null;
476     this.minHeight = minHeight;
477   }
478 
479   public Measure getMaxWidth() {
480     return maxWidth;
481   }
482 
483   public void setMaxWidth(Measure maxWidth) {
484     empty = null;
485     this.maxWidth = maxWidth;
486   }
487 
488   public Measure getMaxHeight() {
489     return maxHeight;
490   }
491 
492   public void setMaxHeight(Measure maxHeight) {
493     empty = null;
494     this.maxHeight = maxHeight;
495   }
496 
497   public Measure getLeft() {
498     return left;
499   }
500 
501   public void setLeft(final Measure left) {
502     empty = null;
503     this.left = left;
504   }
505 
506   public Measure getRight() {
507     return right;
508   }
509 
510   public void setRight(Measure right) {
511     empty = null;
512     this.right = right;
513   }
514 
515   public Measure getTop() {
516     return top;
517   }
518 
519   public void setTop(final Measure top) {
520     empty = null;
521     this.top = top;
522   }
523 
524   public Measure getBottom() {
525     return bottom;
526   }
527 
528   public void setBottom(Measure bottom) {
529     empty = null;
530     this.bottom = bottom;
531   }
532 
533   public Display getDisplay() {
534     return display;
535   }
536 
537   public void setDisplay(final Display display) {
538     empty = null;
539     this.display = display;
540   }
541 
542   public Position getPosition() {
543     return position;
544   }
545 
546   public void setPosition(final Position position) {
547     empty = null;
548     this.position = position;
549   }
550 
551   public Overflow getOverflowX() {
552     return overflowX;
553   }
554 
555   public void setOverflowX(final Overflow overflowX) {
556     empty = null;
557     this.overflowX = overflowX;
558   }
559 
560   public Overflow getOverflowY() {
561     return overflowY;
562   }
563 
564   public void setOverflowY(final Overflow overflowY) {
565     empty = null;
566     this.overflowY = overflowY;
567   }
568 
569   public Measure getMarginLeft() {
570     return marginLeft;
571   }
572 
573   public void setMarginLeft(final Measure marginLeft) {
574     empty = null;
575     this.marginLeft = marginLeft;
576   }
577 
578   public Measure getMarginRight() {
579     return marginRight;
580   }
581 
582   public void setMarginRight(final Measure marginRight) {
583     empty = null;
584     this.marginRight = marginRight;
585   }
586 
587   public Measure getMarginTop() {
588     return marginTop;
589   }
590 
591   public void setMarginTop(final Measure marginTop) {
592     empty = null;
593     this.marginTop = marginTop;
594   }
595 
596   public Measure getMarginBottom() {
597     return marginBottom;
598   }
599 
600   public void setMarginBottom(final Measure marginBottom) {
601     empty = null;
602     this.marginBottom = marginBottom;
603   }
604 
605   public Measure getPaddingLeft() {
606     return paddingLeft;
607   }
608 
609   public void setPaddingLeft(final Measure paddingLeft) {
610     empty = null;
611     this.paddingLeft = paddingLeft;
612   }
613 
614   public Measure getPaddingRight() {
615     return paddingRight;
616   }
617 
618   public void setPaddingRight(final Measure paddingRight) {
619     empty = null;
620     this.paddingRight = paddingRight;
621   }
622 
623   public Measure getPaddingTop() {
624     return paddingTop;
625   }
626 
627   public void setPaddingTop(final Measure paddingTop) {
628     empty = null;
629     this.paddingTop = paddingTop;
630   }
631 
632   public Measure getPaddingBottom() {
633     return paddingBottom;
634   }
635 
636   public void setPaddingBottom(final Measure paddingBottom) {
637     empty = null;
638     this.paddingBottom = paddingBottom;
639   }
640 
641   public String getBackgroundImage() {
642     return backgroundImage;
643   }
644 
645   public void setBackgroundImage(String backgroundImage) {
646     this.backgroundImage = backgroundImage;
647   }
648 
649   public String getBackgroundPosition() {
650     return backgroundPosition;
651   }
652 
653   public void setBackgroundPosition(String backgroundPosition) {
654     this.backgroundPosition = backgroundPosition;
655   }
656 
657   public Integer getZIndex() {
658     return zIndex;
659   }
660 
661   public void setZIndex(final Integer zIndex) {
662     empty = null;
663     this.zIndex = zIndex;
664   }
665 
666   public TextAlign getTextAlign() {
667     return textAlign;
668   }
669 
670   public void setTextAlign(final TextAlign textAlign) {
671     empty = null;
672     this.textAlign = textAlign;
673   }
674 
675   @Override
676   public String toString() {
677     return encode();
678   }
679 }