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