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.context;
21  
22  import org.apache.myfaces.tobago.internal.config.RendererConfig;
23  import org.apache.myfaces.tobago.internal.config.RenderersConfig;
24  import org.apache.myfaces.tobago.internal.config.RenderersConfigImpl;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import java.io.Serializable;
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.List;
32  
33  public class ThemeImpl implements Theme, Serializable {
34  
35    private static final long serialVersionUID = 1L;
36  
37    private static final Logger LOG = LoggerFactory.getLogger(ThemeImpl.class);
38  
39    private String name;
40  
41    private String displayName;
42  
43    private String resourcePath;
44  
45    private ThemeImpl fallback;
46  
47    private String fallbackName;
48  
49    private List<Theme> fallbackList;
50  
51    private RenderersConfigImpl renderersConfig;
52  
53    private ThemeResources productionResources;
54  
55    private ThemeResources resources;
56  
57    private String[] productionScripts;
58  
59    private String[] productionStyles;
60  
61    private String[] scripts;
62  
63    private String[] styles;
64  
65    private boolean versioned;
66  
67    private String version;
68  
69    public ThemeImpl() {
70      resources = new ThemeResources();
71      productionResources = new ThemeResources();
72      productionResources.setProduction(true);
73    }
74  
75    public String getName() {
76      return name;
77    }
78  
79    public void setName(String name) {
80      this.name = name;
81    }
82  
83    public String getDisplayName() {
84      return displayName;
85    }
86  
87    public void setDisplayName(String displayName) {
88      this.displayName = displayName;
89    }
90  
91    public String getResourcePath() {
92      return resourcePath;
93    }
94  
95    public void setResourcePath(String resourcePath) {
96      this.resourcePath = resourcePath;
97    }
98  
99    public ThemeImpl getFallback() {
100     return fallback;
101   }
102 
103   public void setFallback(ThemeImpl fallback) {
104     this.fallback = fallback;
105   }
106 
107   public String getFallbackName() {
108     return fallbackName;
109   }
110 
111   public void setFallbackName(String fallbackName) {
112     this.fallbackName = fallbackName;
113   }
114 
115   public List<Theme> getFallbackList() {
116     return fallbackList;
117   }
118 
119   public void resolveFallbacks() {
120     fallbackList = new ArrayList<Theme>();
121     ThemeImpl actual = this;
122     while (actual != null) {
123       fallbackList.add(actual);
124       actual = actual.getFallback();
125     }
126     fallbackList = Collections.unmodifiableList(fallbackList);
127     if (LOG.isDebugEnabled()) {
128       for (Theme theme : fallbackList) {
129         LOG.debug("fallbackList: {}", theme.getName());
130       }
131     }
132   }
133 
134   public void resolveRendererConfig(RenderersConfig rendererConfigFromTobagoConfig) {
135     if (renderersConfig == null) {
136       renderersConfig = new RenderersConfigImpl();
137     }
138     if (!renderersConfig.isMerged()) {
139       ThemeImpl fallback = getFallback();
140       if (fallback != null) {
141         fallback.resolveRendererConfig(rendererConfigFromTobagoConfig);
142         RenderersConfigImpl fallbackRenderersConfig = fallback.getRenderersConfigImpl();
143         if (fallbackRenderersConfig != null) {
144           renderersConfig.merge(fallbackRenderersConfig, false);
145           if (LOG.isDebugEnabled()) {
146             LOG.debug("merge markupconfig from {} for {}", fallback.getName(), getName());
147           }
148         }
149       }
150       if (rendererConfigFromTobagoConfig != null) {
151         renderersConfig.merge(rendererConfigFromTobagoConfig, true);
152       }
153       renderersConfig.setMerged(true);
154       if (LOG.isDebugEnabled()) {
155         LOG.debug("{} {}", getName(), renderersConfig);
156       }
157     }
158   }
159 
160   public void resolveResources() {
161     ThemeImpl fallback = getFallback();
162     if (fallback != null) {
163       fallback.resolveResources();
164       addResources(fallback.getProductionResources());
165       addResources(fallback.getResources());
166     }
167   }
168 
169   public String toString() {
170     StringBuilder builder = new StringBuilder();
171     builder.append("Theme: ");
172     builder.append(name);
173     if (renderersConfig != null) {
174       builder.append("\n");
175       for (RendererConfig config : renderersConfig.getRendererConfigs()) {
176         builder.append(config);
177         builder.append("\n");
178       }
179     }
180     return builder.toString();
181   }
182 
183   public void setRenderersConfig(RenderersConfigImpl renderersConfig) {
184     this.renderersConfig = renderersConfig;
185   }
186 
187   public RenderersConfig getRenderersConfig() {
188     return renderersConfig;
189   }
190 
191   RenderersConfigImpl getRenderersConfigImpl() {
192     return renderersConfig;
193   }
194 
195   public ThemeResources getResources() {
196     return resources;
197   }
198 
199   public ThemeResources getProductionResources() {
200     return productionResources;
201   }
202 
203   public void addResources(ThemeResources themeResources) {
204     if (themeResources.isProduction()) {
205       productionResources.merge(themeResources);
206     } else {
207       resources.merge(themeResources);
208     }
209   }
210 
211   public void init() {
212     productionScripts = new String[productionResources.getScriptList().size()];
213     for (int i = 0; i < productionResources.getScriptList().size(); i++) {
214       productionScripts[i] = productionResources.getScriptList().get(i).getName();
215     }
216     productionStyles = new String[productionResources.getStyleList().size()];
217     for (int i = 0; i < productionResources.getStyleList().size(); i++) {
218       productionStyles[i] = productionResources.getStyleList().get(i).getName();
219     }
220 
221     scripts = new String[resources.getScriptList().size()];
222     for (int i = 0; i < resources.getScriptList().size(); i++) {
223       scripts[i] = resources.getScriptList().get(i).getName();
224     }
225     styles = new String[resources.getStyleList().size()];
226     for (int i = 0; i < resources.getStyleList().size(); i++) {
227       styles[i] = resources.getStyleList().get(i).getName();
228     }
229 
230   }
231 
232   public String[] getScriptResources(boolean production) {
233     if (production) {
234       return productionScripts;
235     }
236     return scripts;
237   }
238 
239   public String[] getStyleResources(boolean production) {
240     if (production) {
241       return productionStyles;
242     }
243     return styles;
244   }
245 
246   public boolean isVersioned() {
247     return versioned;
248   }
249 
250   public void setVersioned(boolean versioned) {
251     this.versioned = versioned;
252   }
253 
254   public String getVersion() {
255     return version;
256   }
257 
258   public void setVersion(String version) {
259     this.version = version;
260   }
261 }