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.internal.component;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.Visual;
24  import org.apache.myfaces.tobago.internal.util.StringUtils;
25  import org.apache.myfaces.tobago.layout.Display;
26  import org.apache.myfaces.tobago.layout.GridSpan;
27  import org.apache.myfaces.tobago.layout.Measure;
28  import org.apache.myfaces.tobago.layout.Overflow;
29  import org.apache.myfaces.tobago.layout.Position;
30  import org.apache.myfaces.tobago.layout.TextAlign;
31  import org.apache.myfaces.tobago.renderkit.css.CustomClass;
32  import org.apache.myfaces.tobago.util.FacesVersion;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  import javax.el.ValueExpression;
37  import javax.faces.component.UIComponent;
38  import javax.faces.component.UIComponentBase;
39  import javax.faces.component.UIViewRoot;
40  import javax.faces.context.FacesContext;
41  import javax.faces.event.ComponentSystemEvent;
42  import javax.faces.event.ListenerFor;
43  import javax.faces.event.PostAddToViewEvent;
44  import javax.faces.event.PreRenderViewEvent;
45  import java.lang.invoke.MethodHandles;
46  
47  /**
48   * {@link org.apache.myfaces.tobago.internal.taglib.component.StyleTagDeclaration}
49   *
50   * @since 4.0.0
51   */
52  @ListenerFor(systemEventClass = PostAddToViewEvent.class)
53  public abstract class AbstractUIStyle extends UIComponentBase {
54  
55    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
56  
57    @Override
58    public void processEvent(final ComponentSystemEvent event) {
59      super.processEvent(event);
60      final FacesContext facesContext = getFacesContext();
61  
62      if (event instanceof PreRenderViewEvent) {
63        // attribute file
64        if (StringUtils.isNotBlank(getFile())) {
65          final UIViewRoot root = facesContext.getViewRoot();
66          root.addComponentResource(facesContext, this);
67        }
68      } else if (event instanceof PostAddToViewEvent) {
69        if (StringUtils.isNotBlank(getFile())) {
70          // MyFaces core is removing the component resources in head if the view will be recreated before rendering.
71          // The view will be recreated because of expressions. For example  expressins in the ui:include src attribute
72          // The PostAddToViewEvent will not be broadcasted in this case again.
73          // A subscription to the PreRenderViewEvent avoids this problem
74          // NOTE: PreRenderViewEvent can not used in myfaces prior 2.0.3 using PostAddToView for all myfaces 2.0 versions
75          if (FacesVersion.supports21() || !FacesVersion.isMyfaces()) {
76            facesContext.getViewRoot().subscribeToEvent(PreRenderViewEvent.class, this);
77          } else {
78            final UIViewRoot root = facesContext.getViewRoot();
79            root.addComponentResource(facesContext, this);
80          }
81        }
82        // attribute customClass
83        final ValueExpression valueExpression = getValueExpression(Attributes.customClass.getName());
84        if (valueExpression != null) {
85          final UIComponent parent = getParent();
86          if (parent instanceof Visual) {
87            parent.setValueExpression(Attributes.customClass.getName(), valueExpression);
88          } else {
89            LOG.warn("The parent '{}' of a style component doesn't support styling!", parent.getClientId(facesContext));
90          }
91        } else {
92          final CustomClass customClass = getCustomClass();
93          if (customClass != null) {
94            final UIComponent parent = getParent();
95            if (parent instanceof Visual) {
96              ((Visual) parent).setCustomClass(customClass);
97            } else {
98              LOG.warn("The parent '{}' of a style component doesn't support styling!", parent.getClientId(facesContext));
99            }
100         }
101       }
102     }
103   }
104 
105   public abstract String getSelector();
106   public abstract void setSelector(String selector);
107 
108   public abstract Measure getPaddingRight();
109 
110   public abstract Measure getMinHeight();
111 
112   public abstract Measure getMarginRight();
113 
114   public abstract String getFile();
115   public abstract void setFile(String file);
116 
117   public abstract Measure getPaddingBottom();
118 
119   public abstract Measure getTop();
120 
121   public abstract Measure getMaxHeight();
122 
123   public abstract Measure getPaddingTop();
124 
125   public abstract Measure getHeight();
126   public abstract void setHeight(Measure height);
127 
128   public abstract Measure getMaxWidth();
129 
130   public abstract TextAlign getTextAlign();
131 
132   public abstract Measure getBottom();
133 
134   public abstract Display getDisplay();
135 
136   public abstract Measure getMinWidth();
137 
138   public abstract Measure getRight();
139 
140   public abstract Measure getMarginLeft();
141 
142   public abstract Overflow getOverflowX();
143 
144   public abstract Overflow getOverflowY();
145 
146   public abstract Measure getLeft();
147 
148   public abstract Measure getWidth();
149   public abstract void setWidth(Measure width);
150 
151   public abstract CustomClass getCustomClass();
152 
153   public abstract Measure getMarginBottom();
154 
155   public abstract Position getPosition();
156 
157   public abstract Measure getPaddingLeft();
158 
159   public abstract Measure getMarginTop();
160 
161   public abstract String getBackgroundImage();
162   public abstract void setBackgroundImage(String backgroundImage);
163 
164   public abstract Number getFlexGrow();
165   public abstract void setFlexGrow(Number flexGrow);
166 
167   public abstract Number getFlexShrink();
168   public abstract void setFlexShrink(Number flexShrink);
169 
170   public abstract Measure getFlexBasis();
171   public abstract void setFlexBasis(Measure flexBasis);
172 
173   public abstract String getGridTemplateColumns();
174   public abstract void setGridTemplateColumns(String gridTemplateColumns);
175 
176   public abstract String getGridTemplateRows();
177   public abstract void setGridTemplateRows(String gridTemplateRows);
178 
179   public abstract GridSpan getGridColumn();
180   public abstract void setGridColumn(GridSpan gridColumn);
181 
182   public abstract GridSpan getGridRow();
183   public abstract void setGridRow(GridSpan gridRow);
184 
185 }