View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.layout;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
20  import org.kuali.rice.krad.datadictionary.uif.UifDictionaryBeanBase;
21  import org.kuali.rice.krad.uif.component.Component;
22  import org.kuali.rice.krad.uif.component.PropertyReplacer;
23  import org.kuali.rice.krad.uif.component.ReferenceCopy;
24  import org.kuali.rice.krad.uif.container.Container;
25  import org.kuali.rice.krad.uif.view.View;
26  
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.Collections;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * Base class for all layout managers
36   *
37   * <p>
38   * Provides general properties of all layout managers, such as the unique id,
39   * rendering template, and style settings
40   * </p>
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public abstract class LayoutManagerBase extends UifDictionaryBeanBase implements LayoutManager {
45      private static final long serialVersionUID = -2657663560459456814L;
46  
47      private String id;
48      private String template;
49      private String templateName;
50  
51      private String style;
52      private List<String> libraryCssClasses;
53      private List<String> cssClasses;
54      private List<String> additionalCssClasses;
55  
56      @ReferenceCopy(newCollectionInstance = true)
57      private Map<String, Object> context;
58      private Map<String, Object> unmodifiableContext;
59  
60      private List<PropertyReplacer> propertyReplacers;
61  
62      public LayoutManagerBase() {
63          super();
64          unmodifiableContext = context = Collections.emptyMap();
65      }
66  
67      /**
68       * @see org.kuali.rice.krad.uif.layout.LayoutManager#performInitialization(org.kuali.rice.krad.uif.view.View,
69       *      java.lang.Object, org.kuali.rice.krad.uif.container.Container)
70       */
71      @Override
72      public void performInitialization(View view, Object model, Container container) {
73          // set id of layout manager from container
74          if (StringUtils.isBlank(id)) {
75              id = container.getId() + "_layout";
76          }
77      }
78  
79      /**
80       * @see org.kuali.rice.krad.uif.layout.LayoutManager#performApplyModel(org.kuali.rice.krad.uif.view.View,
81       *      java.lang.Object, org.kuali.rice.krad.uif.container.Container)
82       */
83      @Override
84      public void performApplyModel(View view, Object model, Container container) {
85  
86      }
87  
88      /**
89       * @see org.kuali.rice.krad.uif.layout.LayoutManager#performFinalize(org.kuali.rice.krad.uif.view.View,
90       *      java.lang.Object, org.kuali.rice.krad.uif.container.Container)
91       */
92      @Override
93      public void performFinalize(View view, Object model, Container container) {
94          // put together all css class names for this component, in order
95          List<String> finalCssClasses = new ArrayList<String>();
96  
97          if (this.libraryCssClasses != null && view.isUseLibraryCssClasses()) {
98              finalCssClasses.addAll(libraryCssClasses);
99          }
100 
101         if (this.cssClasses != null) {
102             finalCssClasses.addAll(cssClasses);
103         }
104 
105         if (this.additionalCssClasses != null) {
106             finalCssClasses.addAll(additionalCssClasses);
107         }
108 
109         cssClasses = finalCssClasses;
110     }
111 
112     /**
113      * Default Impl
114      *
115      * @see org.kuali.rice.krad.uif.layout.LayoutManager#getSupportedContainer()
116      */
117     @Override
118     public Class<? extends Container> getSupportedContainer() {
119         return Container.class;
120     }
121 
122     /**
123      * @see org.kuali.rice.krad.uif.layout.LayoutManager#getComponentsForLifecycle()
124      */
125     @Override
126     public List<Component> getComponentsForLifecycle() {
127         return new ArrayList<Component>();
128     }
129 
130     /**
131      * @see org.kuali.rice.krad.uif.layout.LayoutManager#getComponentPrototypes()
132      */
133     @Override
134     public List<Component> getComponentPrototypes() {
135         List<Component> components = new ArrayList<Component>();
136 
137         return components;
138     }
139 
140     /**
141      * @see org.kuali.rice.krad.uif.layout.LayoutManager#getId()
142      */
143     @Override
144     @BeanTagAttribute(name = "id")
145     public String getId() {
146         return this.id;
147     }
148 
149     /**
150      * @see org.kuali.rice.krad.uif.layout.LayoutManager#setId(java.lang.String)
151      */
152     @Override
153     public void setId(String id) {
154         this.id = id;
155     }
156 
157     /**
158      * @see org.kuali.rice.krad.uif.layout.LayoutManager#getTemplate()
159      */
160     @Override
161     @BeanTagAttribute(name = "template")
162     public String getTemplate() {
163         return this.template;
164     }
165 
166     /**
167      * @see org.kuali.rice.krad.uif.layout.LayoutManager#setTemplate(java.lang.String)
168      */
169     @Override
170     public void setTemplate(String template) {
171         this.template = template;
172     }
173 
174     /**
175      * @see org.kuali.rice.krad.uif.layout.LayoutManager#getTemplateName()
176      */
177     @BeanTagAttribute(name = "tempateName")
178     public String getTemplateName() {
179         return templateName;
180     }
181 
182     /**
183      * @see org.kuali.rice.krad.uif.layout.LayoutManager#setTemplateName(java.lang.String)
184      */
185     public void setTemplateName(String templateName) {
186         this.templateName = templateName;
187     }
188 
189     /**
190      * @see org.kuali.rice.krad.uif.layout.LayoutManager#getStyle()
191      */
192     @Override
193     @BeanTagAttribute(name = "Style")
194     public String getStyle() {
195         return this.style;
196     }
197 
198     /**
199      * @see org.kuali.rice.krad.uif.layout.LayoutManager#setStyle(java.lang.String)
200      */
201     @Override
202     public void setStyle(String style) {
203         this.style = style;
204     }
205 
206     /**
207      * @see LayoutManager#getLibraryCssClasses()
208      */
209     @Override
210     @BeanTagAttribute(name = "libraryCssClasses", type = BeanTagAttribute.AttributeType.LISTVALUE)
211     public List<String> getLibraryCssClasses() {
212         return libraryCssClasses;
213     }
214 
215     /**
216      * @see LayoutManager#setLibraryCssClasses(java.util.List)
217      */
218     @Override
219     public void setLibraryCssClasses(List<String> libraryCssClasses) {
220         this.libraryCssClasses = libraryCssClasses;
221     }
222 
223     /**
224      * @see org.kuali.rice.krad.uif.layout.LayoutManager#getCssClasses()
225      */
226     @Override
227     @BeanTagAttribute(name = "cssClasses", type = BeanTagAttribute.AttributeType.LISTVALUE)
228     public List<String> getCssClasses() {
229         return this.cssClasses;
230     }
231 
232     /**
233      * @see LayoutManager#getAdditionalCssClasses()
234      */
235     @Override
236     @BeanTagAttribute(name = "additionalCssClasses", type = BeanTagAttribute.AttributeType.LISTVALUE)
237     public List<String> getAdditionalCssClasses() {
238         return additionalCssClasses;
239     }
240 
241     /**
242      * @see LayoutManager#setAdditionalCssClasses(java.util.List)
243      */
244     @Override
245     public void setAdditionalCssClasses(List<String> additionalCssClasses) {
246         this.additionalCssClasses = additionalCssClasses;
247     }
248 
249     /**
250      * @see org.kuali.rice.krad.uif.layout.LayoutManager#setCssClasses(java.util.List)
251      */
252     @Override
253     public void setCssClasses(List<String> cssClasses) {
254         this.cssClasses = cssClasses;
255     }
256 
257     /**
258      * Builds the HTML class attribute string by combining the styleClasses list
259      * with a space delimiter
260      *
261      * @return class attribute string
262      */
263     public String getStyleClassesAsString() {
264         if (cssClasses != null) {
265             return StringUtils.join(cssClasses, " ");
266         }
267 
268         return "";
269     }
270 
271     /**
272      * Sets the styleClasses list from the given string that has the classes
273      * delimited by space. This is a convenience for configuration. If a child
274      * bean needs to inherit the classes from the parent, it should configure as
275      * a list and use merge="true"
276      *
277      * @param styleClasses
278      */
279     public void setStyleClasses(String styleClasses) {
280         String[] classes = StringUtils.split(styleClasses);
281         this.cssClasses = Arrays.asList(classes);
282     }
283 
284     /**
285      * @see org.kuali.rice.krad.uif.layout.LayoutManager#addStyleClass(java.lang.String)
286      */
287     @Override
288     public void addStyleClass(String styleClass) {
289         if (cssClasses == null || cssClasses.isEmpty()) {
290             cssClasses = new ArrayList<String>();
291         }
292         
293         if (!cssClasses.contains(styleClass)) {
294             cssClasses.add(styleClass);
295         }
296     }
297 
298     /**
299      * @see org.kuali.rice.krad.uif.layout.LayoutManager#appendToStyle(java.lang.String)
300      */
301     @Override
302     public void appendToStyle(String styleRules) {
303         if (style == null) {
304             style = "";
305         }
306         style = style + styleRules;
307     }
308 
309     /**
310      * @see org.kuali.rice.krad.uif.layout.LayoutManager#getContext()
311      */
312     @Override
313     @BeanTagAttribute(name = "context", type = BeanTagAttribute.AttributeType.MAPBEAN)
314     public Map<String, Object> getContext() {
315         return this.unmodifiableContext;
316     }
317 
318     /**
319      * @see org.kuali.rice.krad.uif.layout.LayoutManager#setContext(java.util.Map)
320      */
321     @Override
322     public void setContext(Map<String, Object> context) {
323         if (context == null || context.isEmpty()) {
324             this.unmodifiableContext = this.context = Collections.emptyMap();
325         } else {
326             this.context = context;
327             this.unmodifiableContext = Collections.unmodifiableMap(this.context);
328         }
329     }
330 
331     /**
332      * @see org.kuali.rice.krad.uif.layout.LayoutManager#pushObjectToContext(java.lang.String,
333      *      java.lang.Object)
334      */
335     @Override
336     public void pushObjectToContext(String objectName, Object object) {
337         if (this.context.isEmpty()) {
338             this.context = new HashMap<String, Object>();
339             this.unmodifiableContext = Collections.unmodifiableMap(this.context);
340         }
341 
342         this.context.put(objectName, object);
343     }
344 
345     /**
346      * @see org.kuali.rice.krad.uif.layout.LayoutManager#pushAllToContext(java.util.Map)
347      */
348     @Override
349     public void pushAllToContext(Map<String, Object> sourceContext) {
350         if (sourceContext == null || sourceContext.isEmpty()) {
351             return;
352         }
353         
354         if (this.context.isEmpty()) {
355             this.context = new HashMap<String, Object>();
356             this.unmodifiableContext = Collections.unmodifiableMap(this.context);
357         }
358 
359         this.context.putAll(sourceContext);
360     }
361 
362     /**
363      * @see org.kuali.rice.krad.uif.layout.LayoutManager#getPropertyReplacers()
364      */
365     @Override
366     @BeanTagAttribute(name = "propertyReplacers", type = BeanTagAttribute.AttributeType.LISTBEAN)
367     public List<PropertyReplacer> getPropertyReplacers() {
368         return this.propertyReplacers;
369     }
370 
371     /**
372      * @see org.kuali.rice.krad.uif.layout.LayoutManager#setPropertyReplacers(java.util.List)
373      */
374     @Override
375     public void setPropertyReplacers(List<PropertyReplacer> propertyReplacers) {
376         this.propertyReplacers = propertyReplacers;
377     }
378 
379     @Override
380     public <T> T copy() {
381         T copiedClass = null;
382         try {
383             copiedClass = (T) this.getClass().newInstance();
384         } catch (Exception exception) {
385             throw new RuntimeException();
386         }
387 
388         copyProperties(copiedClass);
389 
390         return copiedClass;
391     }
392 
393     protected <T> void copyProperties(T layoutManager) {
394         super.copyProperties(layoutManager);
395 
396         LayoutManagerBase layoutManagerBaseCopy = (LayoutManagerBase) layoutManager;
397 
398         layoutManagerBaseCopy.setId(this.id);
399         layoutManagerBaseCopy.setTemplate(this.template);
400         layoutManagerBaseCopy.setTemplateName(this.templateName);
401         layoutManagerBaseCopy.setStyle(this.style);
402 
403         if (libraryCssClasses != null) {
404             layoutManagerBaseCopy.setLibraryCssClasses(new ArrayList<String>(libraryCssClasses));
405         }
406 
407         if (cssClasses != null && !cssClasses.isEmpty()) {
408             layoutManagerBaseCopy.setCssClasses(new ArrayList<String>(cssClasses));
409         }
410 
411         if (additionalCssClasses != null) {
412             layoutManagerBaseCopy.setAdditionalCssClasses(new ArrayList<String>(additionalCssClasses));
413         }
414 
415         if (propertyReplacers != null) {
416             List<PropertyReplacer> propertyReplacersCopy = new ArrayList<PropertyReplacer>();
417             for (PropertyReplacer propertyReplacer : propertyReplacers) {
418                 propertyReplacersCopy.add((PropertyReplacer) propertyReplacer.copy());
419             }
420 
421             layoutManagerBaseCopy.setPropertyReplacers(propertyReplacersCopy);
422         }
423     }
424 }