001    package org.apache.myfaces.tobago.renderkit.css;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.myfaces.tobago.context.ResourceManagerUtils;
021    import org.apache.myfaces.tobago.layout.Display;
022    import org.apache.myfaces.tobago.layout.LayoutBase;
023    import org.apache.myfaces.tobago.layout.LayoutComponent;
024    import org.apache.myfaces.tobago.layout.LayoutContainer;
025    import org.apache.myfaces.tobago.layout.Measure;
026    import org.apache.myfaces.tobago.layout.TextAlign;
027    
028    import javax.faces.context.FacesContext;
029    import java.io.Serializable;
030    
031    public class Style implements Serializable {
032                                              
033      private static final long serialVersionUID = 4L;
034    
035      private Measure width;
036      private Measure height;
037      private Measure left;
038      private Measure top;
039      private Display display;
040      private Position position;
041      private Overflow overflowX;
042      private Overflow overflowY;
043      private Measure marginLeft;
044      private Measure marginRight;
045      private Measure marginTop;
046      private Measure marginBottom;
047      private Measure margin;
048      private Measure paddingLeft;
049      private Measure paddingRight;
050      private Measure paddingTop;
051      private Measure paddingBottom;
052      private Measure padding;
053      private String backgroundImage;
054      private Integer zIndex;
055      private TextAlign textAlign;
056    
057      public Style() {
058      }
059    
060      public Style(Style map) {
061        this.width = map.width;
062        this.height = map.height;
063        this.left = map.left;
064        this.top = map.top;
065        this.display = map.display;
066        this.position = map.position;
067        this.overflowX = map.overflowX;
068        this.overflowY = map.overflowY;
069        this.marginLeft = map.marginLeft;
070        this.marginRight = map.marginRight;
071        this.marginTop = map.marginTop;
072        this.marginBottom = map.marginBottom;
073        this.margin = map.margin;
074        this.paddingLeft = map.paddingLeft;
075        this.paddingRight = map.paddingRight;
076        this.paddingTop = map.paddingTop;
077        this.paddingBottom = map.paddingBottom;
078        this.padding = map.padding;
079        this.backgroundImage = map.backgroundImage;
080        this.zIndex = map.zIndex;
081        this.textAlign = map.textAlign;
082      }
083    
084      public Style(FacesContext facesContext, LayoutBase layout) {
085    
086        String rendererType = layout.getRendererType();
087        
088        width = layout.getCurrentWidth();
089        if (width != null) {
090          // TODO: Make configurable: this is needed if the box-sizing is border-box, not content-box (see CSS3)
091          width = width.subtractNotNegative(
092              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.border-left-width"));
093          width = width.subtractNotNegative(
094              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.padding-left"));
095          width = width.subtractNotNegative(
096              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.padding-right"));
097          width = width.subtractNotNegative(
098              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.border-right-width"));
099        }
100        height = layout.getCurrentHeight();
101        if (height != null) {
102          // TODO: Make configurable: this is needed if the box-sizing is border-box, not content-box (see CSS3)
103          height = height.subtractNotNegative(
104              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.border-top-width"));
105          height = height.subtractNotNegative(
106              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.padding-top"));
107          height = height.subtractNotNegative(
108              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.padding-bottom"));
109          height = height.subtractNotNegative(
110              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.border-bottom-width"));
111        }
112        this.left = layout.getLeft();
113        this.top = layout.getTop();
114    
115        // if there are a position coordinates, activate absolute positioning
116        // XXX String "Page" is not nice here
117        if ((left != null || top != null) && !rendererType.contains("Page")) {
118          position = Position.ABSOLUTE;
119        }
120    
121        if (layout instanceof LayoutComponent) { // fixme
122          display = ((LayoutComponent) layout).getDisplay();
123        }
124    
125        if (layout instanceof LayoutContainer) {
126          overflowX = ((LayoutContainer) layout).isOverflowX() ? Overflow.AUTO : null;
127          overflowY = ((LayoutContainer) layout).isOverflowY() ? Overflow.AUTO : null;
128        }
129      }
130    
131      public String encode() {
132        StringBuilder buf = new StringBuilder();
133        if (width != null) {
134          buf.append("width:");
135          buf.append(width);
136          buf.append(';');
137        }
138        if (height != null) {
139          buf.append("height:");
140          buf.append(height);
141          buf.append(';');
142        }
143        if (top != null) {
144          buf.append("top:");
145          buf.append(top);
146          buf.append(';');
147        }
148        if (left != null) {
149          buf.append("left:");
150          buf.append(left);
151          buf.append(';');
152        }
153        if (display != null) {
154          buf.append("display:");
155          buf.append(display.getValue());
156          buf.append(';');
157        }
158        if (position != null) {
159          buf.append("position:");
160          buf.append(position.getValue());
161          buf.append(';');
162        }
163        if (overflowX != null) {
164          buf.append("overflow-x:");
165          buf.append(overflowX.getValue());
166          buf.append(';');
167        }
168        if (overflowY != null) {
169          buf.append("overflow-y:");
170          buf.append(overflowY.getValue());
171          buf.append(';');
172        }
173        if (marginLeft != null) {
174          buf.append("margin-left:");
175          buf.append(marginLeft);
176          buf.append(';');
177        }
178        if (marginRight != null) {
179          buf.append("margin-right:");
180          buf.append(marginRight);
181          buf.append(';');
182        }
183        if (marginTop != null) {
184          buf.append("margin-top:");
185          buf.append(marginTop);
186          buf.append(';');
187        }
188        if (marginBottom != null) {
189          buf.append("margin-bottom:");
190          buf.append(marginBottom);
191          buf.append(';');
192        }
193        if (margin != null) {
194          buf.append("margin:");
195          buf.append(margin);
196          buf.append(';');
197        }
198        if (paddingLeft != null) {
199          buf.append("padding-left:");
200          buf.append(paddingLeft);
201          buf.append(';');
202        }
203        if (paddingRight != null) {
204          buf.append("padding-right:");
205          buf.append(paddingRight);
206          buf.append(';');
207        }
208        if (paddingTop != null) {
209          buf.append("padding-top:");
210          buf.append(paddingTop);
211          buf.append(';');
212        }
213        if (paddingBottom != null) {
214          buf.append("padding-bottom:");
215          buf.append(paddingBottom);
216          buf.append(';');
217        }
218        if (padding != null) {
219          buf.append("padding:");
220          buf.append(padding);
221          buf.append(';');
222        }
223        if (backgroundImage != null) {
224          buf.append("background-image:");
225          buf.append(backgroundImage);
226          buf.append(';');
227        }
228        if (zIndex != null) {
229          buf.append("z-index:");
230          buf.append(zIndex);
231          buf.append(';');
232        }
233        if (textAlign != null) {
234          buf.append("text-align:");
235          buf.append(textAlign);
236          buf.append(';');
237        }
238    
239        return buf.toString();
240      }
241    
242      public Measure getWidth() {
243        return width;
244      }
245    
246      public void setWidth(Measure width) {
247        this.width = width;
248      }
249    
250      public Measure getHeight() {
251        return height;
252      }
253    
254      public void setHeight(Measure height) {
255        this.height = height;
256      }
257    
258      public Measure getLeft() {
259        return left;
260      }
261    
262      public void setLeft(Measure left) {
263        this.left = left;
264      }
265    
266      public Measure getTop() {
267        return top;
268      }
269    
270      public void setTop(Measure top) {
271        this.top = top;
272      }
273    
274      public Display getDisplay() {
275        return display;
276      }
277    
278      public void setDisplay(Display display) {
279        this.display = display;
280      }
281    
282      public Position getPosition() {
283        return position;
284      }
285    
286      public void setPosition(Position position) {
287        this.position = position;
288      }
289    
290      public Overflow getOverflowX() {
291        return overflowX;
292      }
293    
294      public void setOverflowX(Overflow overflowX) {
295        this.overflowX = overflowX;
296      }
297    
298      public Overflow getOverflowY() {
299        return overflowY;
300      }
301    
302      public void setOverflowY(Overflow overflowY) {
303        this.overflowY = overflowY;
304      }
305    
306      public Measure getMarginLeft() {
307        return marginLeft;
308      }
309    
310      public void setMarginLeft(Measure marginLeft) {
311        this.marginLeft = marginLeft;
312      }
313    
314      public Measure getMarginRight() {
315        return marginRight;
316      }
317    
318      public void setMarginRight(Measure marginRight) {
319        this.marginRight = marginRight;
320      }
321    
322      public Measure getMarginTop() {
323        return marginTop;
324      }
325    
326      public void setMarginTop(Measure marginTop) {
327        this.marginTop = marginTop;
328      }
329    
330      public Measure getMarginBottom() {
331        return marginBottom;
332      }
333    
334      public void setMarginBottom(Measure marginBottom) {
335        this.marginBottom = marginBottom;
336      }
337    
338      public Measure getMargin() {
339        return margin;
340      }
341    
342      public void setMargin(Measure margin) {
343        this.margin = margin;
344      }
345    
346      public Measure getPaddingLeft() {
347        return paddingLeft;
348      }
349    
350      public void setPaddingLeft(Measure paddingLeft) {
351        this.paddingLeft = paddingLeft;
352      }
353    
354      public Measure getPaddingRight() {
355        return paddingRight;
356      }
357    
358      public void setPaddingRight(Measure paddingRight) {
359        this.paddingRight = paddingRight;
360      }
361    
362      public Measure getPaddingTop() {
363        return paddingTop;
364      }
365    
366      public void setPaddingTop(Measure paddingTop) {
367        this.paddingTop = paddingTop;
368      }
369    
370      public Measure getPaddingBottom() {
371        return paddingBottom;
372      }
373    
374      public void setPaddingBottom(Measure paddingBottom) {
375        this.paddingBottom = paddingBottom;
376      }
377    
378      public Measure getPadding() {
379        return padding;
380      }
381    
382      public void setPadding(Measure padding) {
383        this.padding = padding;
384      }
385    
386      public String getBackgroundImage() {
387        return backgroundImage;
388      }
389    
390      public void setBackgroundImage(String backgroundImage) {
391        this.backgroundImage = backgroundImage;
392      }
393    
394      public Integer getZIndex() {
395        return zIndex;
396      }
397    
398      public void setZIndex(Integer zIndex) {
399        this.zIndex = zIndex;
400      }
401    
402      public TextAlign getTextAlign() {
403        return textAlign;
404      }
405    
406      public void setTextAlign(TextAlign textAlign) {
407        this.textAlign = textAlign;
408      }
409    }