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