View Javadoc

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