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.Measure;
025    
026    import javax.faces.context.FacesContext;
027    import java.io.Serializable;
028    
029    public class Style implements Serializable {
030                                              
031      private static final long serialVersionUID = 4L;
032    
033      private Measure width;
034      private Measure height;
035      private Measure left;
036      private Measure top;
037      private Display display;
038      private Position position;
039      private Overflow overflow;
040      private Measure marginLeft;
041      private Measure marginRight;
042      private Measure marginTop;
043      private Measure marginBottom;
044      private Measure margin;
045      private Measure paddingLeft;
046      private Measure paddingRight;
047      private Measure paddingTop;
048      private Measure paddingBottom;
049      private Measure padding;
050      private String backgroundImage;
051      private Integer zIndex;
052      private String textAlign;
053    
054      public Style() {
055      }
056    
057      public Style(Style map) {
058        this.width = map.width;
059        this.height = map.height;
060        this.left = map.left;
061        this.top = map.top;
062        this.display = map.display;
063        this.position = map.position;
064        this.overflow = map.overflow;
065        this.marginLeft = map.marginLeft;
066        this.marginRight = map.marginRight;
067        this.marginTop = map.marginTop;
068        this.marginBottom = map.marginBottom;
069        this.margin = map.margin;
070        this.paddingLeft = map.paddingLeft;
071        this.paddingRight = map.paddingRight;
072        this.paddingTop = map.paddingTop;
073        this.paddingBottom = map.paddingBottom;
074        this.padding = map.padding;
075        this.backgroundImage = map.backgroundImage;
076        this.zIndex = map.zIndex;
077        this.textAlign = map.textAlign;
078      }
079    
080      public Style(FacesContext facesContext, LayoutBase layout) {
081    
082        String rendererType = layout.getRendererType();
083        
084        width = layout.getCurrentWidth();
085        if (width != null) {
086          // TODO: Make configurable: this is needed if the box-sizing is border-box, not content-box (see CSS3)
087          width = width.subtractNotNegative(
088              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.border-left-width"));
089          width = width.subtractNotNegative(
090              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.padding-left"));
091          width = width.subtractNotNegative(
092              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.padding-right"));
093          width = width.subtractNotNegative(
094              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.border-right-width"));
095        }
096        height = layout.getCurrentHeight();
097        if (height != null) {
098          // TODO: Make configurable: this is needed if the box-sizing is border-box, not content-box (see CSS3)
099          height = height.subtractNotNegative(
100              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.border-top-width"));
101          height = height.subtractNotNegative(
102              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.padding-top"));
103          height = height.subtractNotNegative(
104              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.padding-bottom"));
105          height = height.subtractNotNegative(
106              ResourceManagerUtils.getThemeMeasure(facesContext, layout, "css.border-bottom-width"));
107        }
108        this.left = layout.getLeft();
109        this.top = layout.getTop();
110    
111        // if there are a position coordinates, activate absolute positioning
112        // XXX String "Page" is not nice here
113        if ((left != null || top != null) && !rendererType.contains("Page")) {
114          position = Position.ABSOLUTE;
115        }
116    
117        if (layout instanceof LayoutComponent) { // fixme
118          display = ((LayoutComponent) layout).getDisplay();
119        }
120      }
121    
122      public String encode() {
123        StringBuilder buf = new StringBuilder();
124        if (width != null) {
125          buf.append("width:");
126          buf.append(width);
127          buf.append(';');
128        }
129        if (height != null) {
130          buf.append("height:");
131          buf.append(height);
132          buf.append(';');
133        }
134        if (top != null) {
135          buf.append("top:");
136          buf.append(top);
137          buf.append(';');
138        }
139        if (left != null) {
140          buf.append("left:");
141          buf.append(left);
142          buf.append(';');
143        }
144        if (display != null) {
145          buf.append("display:");
146          buf.append(display.getValue());
147          buf.append(';');
148        }
149        if (position != null) {
150          buf.append("position:");
151          buf.append(position.getValue());
152          buf.append(';');
153        }
154        if (overflow != null) {
155          buf.append("overflow:");
156          buf.append(overflow.getValue());
157          buf.append(';');
158        }
159        if (marginLeft != null) {
160          buf.append("margin-left:");
161          buf.append(marginLeft);
162          buf.append(';');
163        }
164        if (marginRight != null) {
165          buf.append("margin-right:");
166          buf.append(marginRight);
167          buf.append(';');
168        }
169        if (marginTop != null) {
170          buf.append("margin-top:");
171          buf.append(marginTop);
172          buf.append(';');
173        }
174        if (marginBottom != null) {
175          buf.append("margin-bottom:");
176          buf.append(marginBottom);
177          buf.append(';');
178        }
179        if (margin != null) {
180          buf.append("margin:");
181          buf.append(margin);
182          buf.append(';');
183        }
184        if (paddingLeft != null) {
185          buf.append("padding-left:");
186          buf.append(paddingLeft);
187          buf.append(';');
188        }
189        if (paddingRight != null) {
190          buf.append("padding-right:");
191          buf.append(paddingRight);
192          buf.append(';');
193        }
194        if (paddingTop != null) {
195          buf.append("padding-top:");
196          buf.append(paddingTop);
197          buf.append(';');
198        }
199        if (paddingBottom != null) {
200          buf.append("padding-bottom:");
201          buf.append(paddingBottom);
202          buf.append(';');
203        }
204        if (padding != null) {
205          buf.append("padding:");
206          buf.append(padding);
207          buf.append(';');
208        }
209        if (backgroundImage != null) {
210          buf.append("background-image:");
211          buf.append(backgroundImage);
212          buf.append(';');
213        }
214        if (zIndex != null) {
215          buf.append("z-index:");
216          buf.append(zIndex);
217          buf.append(';');
218        }
219        if (textAlign != null) {
220          buf.append("text-align:");
221          buf.append(textAlign);
222          buf.append(';');
223        }
224    
225        return buf.toString();
226      }
227    
228      public Measure getWidth() {
229        return width;
230      }
231    
232      public void setWidth(Measure width) {
233        this.width = width;
234      }
235    
236      public Measure getHeight() {
237        return height;
238      }
239    
240      public void setHeight(Measure height) {
241        this.height = height;
242      }
243    
244      public Measure getLeft() {
245        return left;
246      }
247    
248      public void setLeft(Measure left) {
249        this.left = left;
250      }
251    
252      public Measure getTop() {
253        return top;
254      }
255    
256      public void setTop(Measure top) {
257        this.top = top;
258      }
259    
260      public Display getDisplay() {
261        return display;
262      }
263    
264      public void setDisplay(Display display) {
265        this.display = display;
266      }
267    
268      public Position getPosition() {
269        return position;
270      }
271    
272      public void setPosition(Position position) {
273        this.position = position;
274      }
275    
276      public Overflow getOverflow() {
277        return overflow;
278      }
279    
280      public void setOverflow(Overflow overflow) {
281        this.overflow = overflow;
282      }
283    
284      public Measure getMarginLeft() {
285        return marginLeft;
286      }
287    
288      public void setMarginLeft(Measure marginLeft) {
289        this.marginLeft = marginLeft;
290      }
291    
292      public Measure getMarginRight() {
293        return marginRight;
294      }
295    
296      public void setMarginRight(Measure marginRight) {
297        this.marginRight = marginRight;
298      }
299    
300      public Measure getMarginTop() {
301        return marginTop;
302      }
303    
304      public void setMarginTop(Measure marginTop) {
305        this.marginTop = marginTop;
306      }
307    
308      public Measure getMarginBottom() {
309        return marginBottom;
310      }
311    
312      public void setMarginBottom(Measure marginBottom) {
313        this.marginBottom = marginBottom;
314      }
315    
316      public Measure getMargin() {
317        return margin;
318      }
319    
320      public void setMargin(Measure margin) {
321        this.margin = margin;
322      }
323    
324      public Measure getPaddingLeft() {
325        return paddingLeft;
326      }
327    
328      public void setPaddingLeft(Measure paddingLeft) {
329        this.paddingLeft = paddingLeft;
330      }
331    
332      public Measure getPaddingRight() {
333        return paddingRight;
334      }
335    
336      public void setPaddingRight(Measure paddingRight) {
337        this.paddingRight = paddingRight;
338      }
339    
340      public Measure getPaddingTop() {
341        return paddingTop;
342      }
343    
344      public void setPaddingTop(Measure paddingTop) {
345        this.paddingTop = paddingTop;
346      }
347    
348      public Measure getPaddingBottom() {
349        return paddingBottom;
350      }
351    
352      public void setPaddingBottom(Measure paddingBottom) {
353        this.paddingBottom = paddingBottom;
354      }
355    
356      public Measure getPadding() {
357        return padding;
358      }
359    
360      public void setPadding(Measure padding) {
361        this.padding = padding;
362      }
363    
364      public String getBackgroundImage() {
365        return backgroundImage;
366      }
367    
368      public void setBackgroundImage(String backgroundImage) {
369        this.backgroundImage = backgroundImage;
370      }
371    
372      public Integer getZIndex() {
373        return zIndex;
374      }
375    
376      public void setZIndex(Integer zIndex) {
377        this.zIndex = zIndex;
378      }
379    
380      public String getTextAlign() {
381        return textAlign;
382      }
383    
384      public void setTextAlign(String textAlign) {
385        this.textAlign = textAlign;
386      }
387    }