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.context.ResourceManagerUtils;
23  import org.apache.myfaces.tobago.layout.Display;
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.apache.myfaces.tobago.layout.TextAlign;
29  
30  import javax.faces.context.FacesContext;
31  import java.io.Serializable;
32  
33  public class Style implements Serializable {
34                                            
35    private static final long serialVersionUID = 4L;
36  
37    private Measure width;
38    private Measure height;
39    private Measure left;
40    private Measure top;
41    private Display display;
42    private Position position;
43    private Overflow overflowX;
44    private Overflow overflowY;
45    private Measure marginLeft;
46    private Measure marginRight;
47    private Measure marginTop;
48    private Measure marginBottom;
49    private Measure margin;
50    private Measure paddingLeft;
51    private Measure paddingRight;
52    private Measure paddingTop;
53    private Measure paddingBottom;
54    private Measure padding;
55    private String backgroundImage;
56    private String backgroundPosition;
57    private Integer zIndex;
58    private TextAlign textAlign;
59  
60    public Style() {
61    }
62  
63    public Style(final Style map) {
64      this.width = map.width;
65      this.height = map.height;
66      this.left = map.left;
67      this.top = map.top;
68      this.display = map.display;
69      this.position = map.position;
70      this.overflowX = map.overflowX;
71      this.overflowY = map.overflowY;
72      this.marginLeft = map.marginLeft;
73      this.marginRight = map.marginRight;
74      this.marginTop = map.marginTop;
75      this.marginBottom = map.marginBottom;
76      this.margin = map.margin;
77      this.paddingLeft = map.paddingLeft;
78      this.paddingRight = map.paddingRight;
79      this.paddingTop = map.paddingTop;
80      this.paddingBottom = map.paddingBottom;
81      this.padding = map.padding;
82      this.backgroundImage = map.backgroundImage;
83      this.backgroundPosition = map.backgroundPosition;
84      this.zIndex = map.zIndex;
85      this.textAlign = map.textAlign;
86    }
87  
88    public Style(final FacesContext facesContext, final LayoutBase layout) {
89  
90      final String rendererType = layout.getRendererType();
91      
92      width = layout.getCurrentWidth();
93      if (width != null) {
94        // TODO: Make configurable: this is needed if the box-sizing is border-box, not content-box (see CSS3)
95        width = width.subtractNotNegative(
96            ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.border-left-width"));
97        width = width.subtractNotNegative(
98            ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.padding-left"));
99        width = width.subtractNotNegative(
100           ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.padding-right"));
101       width = width.subtractNotNegative(
102           ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.border-right-width"));
103     }
104     height = layout.getCurrentHeight();
105     if (height != null) {
106       // TODO: Make configurable: this is needed if the box-sizing is border-box, not content-box (see CSS3)
107       height = height.subtractNotNegative(
108           ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.border-top-width"));
109       height = height.subtractNotNegative(
110           ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.padding-top"));
111       height = height.subtractNotNegative(
112           ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.padding-bottom"));
113       height = height.subtractNotNegative(
114           ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.border-bottom-width"));
115     }
116     this.left = layout.getLeft();
117     this.top = layout.getTop();
118 
119     // if there are a position coordinates, activate absolute positioning
120     // XXX String "Page" is not nice here
121     if ((left != null || top != null) && !rendererType.contains("Page")) {
122       position = Position.ABSOLUTE;
123     }
124 
125     if (layout instanceof LayoutComponent) { // fixme
126       display = ((LayoutComponent) layout).getDisplay();
127     }
128 
129     if (layout instanceof LayoutContainer) {
130       overflowX = ((LayoutContainer) layout).isOverflowX() ? Overflow.AUTO : null;
131       overflowY = ((LayoutContainer) layout).isOverflowY() ? Overflow.AUTO : null;
132     }
133   }
134 
135   /**
136    * Checks if the encode string holds free text, which must be escaped.
137    * This is the case for image URLs.
138    * For {@link Measure}, and enum types like {@link Display} no escaping is needed.
139    */
140   public boolean needsToBeEscaped() {
141     return backgroundImage != null;
142   }
143 
144   public String encode() {
145     final StringBuilder buf = new StringBuilder();
146     if (width != null) {
147       buf.append("width:");
148       buf.append(width.serialize());
149       buf.append(';');
150     }
151     if (height != null) {
152       buf.append("height:");
153       buf.append(height.serialize());
154       buf.append(';');
155     }
156     if (top != null) {
157       buf.append("top:");
158       buf.append(top.serialize());
159       buf.append(';');
160     }
161     if (left != null) {
162       buf.append("left:");
163       buf.append(left.serialize());
164       buf.append(';');
165     }
166     if (display != null) {
167       buf.append("display:");
168       buf.append(display.getValue());
169       buf.append(';');
170     }
171     if (position != null) {
172       buf.append("position:");
173       buf.append(position.getValue());
174       buf.append(';');
175     }
176     if (overflowX != null) {
177       buf.append("overflow-x:");
178       buf.append(overflowX.getValue());
179       buf.append(';');
180     }
181     if (overflowY != null) {
182       buf.append("overflow-y:");
183       buf.append(overflowY.getValue());
184       buf.append(';');
185     }
186     if (marginLeft != null) {
187       buf.append("margin-left:");
188       buf.append(marginLeft.serialize());
189       buf.append(';');
190     }
191     if (marginRight != null) {
192       buf.append("margin-right:");
193       buf.append(marginRight.serialize());
194       buf.append(';');
195     }
196     if (marginTop != null) {
197       buf.append("margin-top:");
198       buf.append(marginTop.serialize());
199       buf.append(';');
200     }
201     if (marginBottom != null) {
202       buf.append("margin-bottom:");
203       buf.append(marginBottom.serialize());
204       buf.append(';');
205     }
206     if (margin != null) {
207       buf.append("margin:");
208       buf.append(margin.serialize());
209       buf.append(';');
210     }
211     if (paddingLeft != null) {
212       buf.append("padding-left:");
213       buf.append(paddingLeft.serialize());
214       buf.append(';');
215     }
216     if (paddingRight != null) {
217       buf.append("padding-right:");
218       buf.append(paddingRight.serialize());
219       buf.append(';');
220     }
221     if (paddingTop != null) {
222       buf.append("padding-top:");
223       buf.append(paddingTop.serialize());
224       buf.append(';');
225     }
226     if (paddingBottom != null) {
227       buf.append("padding-bottom:");
228       buf.append(paddingBottom.serialize());
229       buf.append(';');
230     }
231     if (padding != null) {
232       buf.append("padding:");
233       buf.append(padding.serialize());
234       buf.append(';');
235     }
236     if (backgroundImage != null) {
237       buf.append("background-image:");
238       buf.append(backgroundImage);
239       buf.append(';');
240     }
241     if (backgroundPosition != null) {
242       buf.append("background-position:");
243       buf.append(backgroundPosition);
244       buf.append(';');
245     }
246     if (zIndex != null) {
247       buf.append("z-index:");
248       buf.append(zIndex);
249       buf.append(';');
250     }
251     if (textAlign != null) {
252       buf.append("text-align:");
253       buf.append(textAlign.getValue());
254       buf.append(';');
255     }
256 
257     return buf.toString();
258   }
259 
260   public String encodeJson() {
261     final StringBuilder buf = new StringBuilder("{");
262     if (width != null) {
263       buf.append("\"width\":\"");
264       buf.append(width.serialize());
265       buf.append("\",");
266     }
267     if (height != null) {
268       buf.append("\"height\":\"");
269       buf.append(height.serialize());
270       buf.append("\",");
271     }
272     if (top != null) {
273       buf.append("\"top\":\"");
274       buf.append(top.serialize());
275       buf.append("\",");
276     }
277     if (left != null) {
278       buf.append("\"left\":\"");
279       buf.append(left.serialize());
280       buf.append("\",");
281     }
282     if (display != null) {
283       buf.append("\"display\":\"");
284       buf.append(display.getValue());
285       buf.append("\",");
286     }
287     if (position != null) {
288       buf.append("\"position\":\"");
289       buf.append(position.getValue());
290       buf.append("\",");
291     }
292     if (overflowX != null) {
293       buf.append("\"overflowX\":\"");
294       buf.append(overflowX.getValue());
295       buf.append("\",");
296     }
297     if (overflowY != null) {
298       buf.append("\"overflowY\":\"");
299       buf.append(overflowY.getValue());
300       buf.append("\",");
301     }
302     if (marginLeft != null) {
303       buf.append("\"marginLeft\":\"");
304       buf.append(marginLeft.serialize());
305       buf.append("\",");
306     }
307     if (marginRight != null) {
308       buf.append("\"marginRight\":\"");
309       buf.append(marginRight.serialize());
310       buf.append("\",");
311     }
312     if (marginTop != null) {
313       buf.append("\"marginTop\":\"");
314       buf.append(marginTop.serialize());
315       buf.append("\",");
316     }
317     if (marginBottom != null) {
318       buf.append("\"marginBottom\":\"");
319       buf.append(marginBottom.serialize());
320       buf.append("\",");
321     }
322     if (margin != null) {
323       buf.append("\"margin\":\"");
324       buf.append(margin.serialize());
325       buf.append("\",");
326     }
327     if (paddingLeft != null) {
328       buf.append("\"paddingLeft\":\"");
329       buf.append(paddingLeft.serialize());
330       buf.append("\",");
331     }
332     if (paddingRight != null) {
333       buf.append("\"paddingRight\":\"");
334       buf.append(paddingRight.serialize());
335       buf.append("\",");
336     }
337     if (paddingTop != null) {
338       buf.append("\"paddingTop\":\"");
339       buf.append(paddingTop.serialize());
340       buf.append("\",");
341     }
342     if (paddingBottom != null) {
343       buf.append("\"paddingBottom\":\"");
344       buf.append(paddingBottom.serialize());
345       buf.append("\",");
346     }
347     if (padding != null) {
348       buf.append("\"padding\":\"");
349       buf.append(padding.serialize());
350       buf.append("\",");
351     }
352     if (backgroundImage != null) {
353       buf.append("\"backgroundImage\":\"");
354       buf.append(backgroundImage);
355       buf.append("\",");
356     }
357     if (backgroundPosition != null) {
358       buf.append("\"backgroundPosition\":\"");
359       buf.append(backgroundPosition);
360       buf.append("\",");
361     }
362     if (zIndex != null) {
363       buf.append("\"zIndex\":");
364       buf.append(zIndex);
365       buf.append(";");
366     }
367     if (textAlign != null) {
368       buf.append("\"textAlign\":\"");
369       buf.append(textAlign.getValue());
370       buf.append("\",");
371     }
372 
373     if (buf.length() > 1) {
374       buf.deleteCharAt(buf.length() - 1);
375     }
376 
377     buf.append('}');
378     return buf.toString();
379   }
380 
381   public Measure getWidth() {
382     return width;
383   }
384 
385   public void setWidth(final Measure width) {
386     this.width = width;
387   }
388 
389   public Measure getHeight() {
390     return height;
391   }
392 
393   public void setHeight(final Measure height) {
394     this.height = height;
395   }
396 
397   public Measure getLeft() {
398     return left;
399   }
400 
401   public void setLeft(final Measure left) {
402     this.left = left;
403   }
404 
405   public Measure getTop() {
406     return top;
407   }
408 
409   public void setTop(final Measure top) {
410     this.top = top;
411   }
412 
413   public Display getDisplay() {
414     return display;
415   }
416 
417   public void setDisplay(final Display display) {
418     this.display = display;
419   }
420 
421   public Position getPosition() {
422     return position;
423   }
424 
425   public void setPosition(final Position position) {
426     this.position = position;
427   }
428 
429   public Overflow getOverflowX() {
430     return overflowX;
431   }
432 
433   public void setOverflowX(final Overflow overflowX) {
434     this.overflowX = overflowX;
435   }
436 
437   public Overflow getOverflowY() {
438     return overflowY;
439   }
440 
441   public void setOverflowY(final Overflow overflowY) {
442     this.overflowY = overflowY;
443   }
444 
445   public Measure getMarginLeft() {
446     return marginLeft;
447   }
448 
449   public void setMarginLeft(final Measure marginLeft) {
450     this.marginLeft = marginLeft;
451   }
452 
453   public Measure getMarginRight() {
454     return marginRight;
455   }
456 
457   public void setMarginRight(final Measure marginRight) {
458     this.marginRight = marginRight;
459   }
460 
461   public Measure getMarginTop() {
462     return marginTop;
463   }
464 
465   public void setMarginTop(final Measure marginTop) {
466     this.marginTop = marginTop;
467   }
468 
469   public Measure getMarginBottom() {
470     return marginBottom;
471   }
472 
473   public void setMarginBottom(final Measure marginBottom) {
474     this.marginBottom = marginBottom;
475   }
476 
477   public Measure getMargin() {
478     return margin;
479   }
480 
481   public void setMargin(final Measure margin) {
482     this.margin = margin;
483   }
484 
485   public Measure getPaddingLeft() {
486     return paddingLeft;
487   }
488 
489   public void setPaddingLeft(final Measure paddingLeft) {
490     this.paddingLeft = paddingLeft;
491   }
492 
493   public Measure getPaddingRight() {
494     return paddingRight;
495   }
496 
497   public void setPaddingRight(final Measure paddingRight) {
498     this.paddingRight = paddingRight;
499   }
500 
501   public Measure getPaddingTop() {
502     return paddingTop;
503   }
504 
505   public void setPaddingTop(final Measure paddingTop) {
506     this.paddingTop = paddingTop;
507   }
508 
509   public Measure getPaddingBottom() {
510     return paddingBottom;
511   }
512 
513   public void setPaddingBottom(final Measure paddingBottom) {
514     this.paddingBottom = paddingBottom;
515   }
516 
517   public Measure getPadding() {
518     return padding;
519   }
520 
521   public void setPadding(final Measure padding) {
522     this.padding = padding;
523   }
524 
525   public String getBackgroundImage() {
526     return backgroundImage;
527   }
528 
529   public void setBackgroundImage(final String backgroundImage) {
530     this.backgroundImage = backgroundImage;
531   }
532 
533   public String getBackgroundPosition() {
534     return backgroundPosition;
535   }
536 
537   public void setBackgroundPosition(String backgroundPosition) {
538     this.backgroundPosition = backgroundPosition;
539   }
540 
541   public Integer getZIndex() {
542     return zIndex;
543   }
544 
545   public void setZIndex(final Integer zIndex) {
546     this.zIndex = zIndex;
547   }
548 
549   public TextAlign getTextAlign() {
550     return textAlign;
551   }
552 
553   public void setTextAlign(final TextAlign textAlign) {
554     this.textAlign = textAlign;
555   }
556 }