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.student.core.krms;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.uif.UifConstants;
20  import org.kuali.rice.krad.uif.component.Component;
21  import org.kuali.rice.krad.uif.component.DataBinding;
22  import org.kuali.rice.krad.uif.container.CollectionGroup;
23  import org.kuali.rice.krad.uif.container.Container;
24  import org.kuali.rice.krad.uif.container.Group;
25  import org.kuali.rice.krad.uif.element.Action;
26  import org.kuali.rice.krad.uif.field.Field;
27  import org.kuali.rice.krad.uif.field.FieldGroup;
28  import org.kuali.rice.krad.uif.layout.StackedLayoutManager;
29  import org.kuali.rice.krad.uif.util.ComponentUtils;
30  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
31  import org.kuali.rice.krad.uif.view.View;
32  import org.kuali.rice.krad.web.form.UifFormBase;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  import java.util.Map;
37  
38  /**
39   * @author Kuali Student Team
40   */
41  public class HorizontalActionsStackedLayoutManager extends StackedLayoutManager {
42  
43      @Override
44      public void buildLine(View view, Object model, CollectionGroup collectionGroup, List<Field> lineFields,
45                            List<FieldGroup> subCollectionFields, String bindingPath, List<Action> actions, String idSuffix,
46                            Object currentLine, int lineIndex) {
47  
48          boolean isAddLine = lineIndex == -1;
49  
50          // construct new group
51          Group lineGroup = null;
52          if (isAddLine) {
53              setStackedGroups(new ArrayList<Group>());
54  
55              if (this.getAddLineGroup() == null) {
56                  lineGroup = ComponentUtils.copy(this.getLineGroupPrototype(), idSuffix);
57              } else {
58                  lineGroup = ComponentUtils.copy(getAddLineGroup(), idSuffix);
59                  lineGroup.addStyleClass(collectionGroup.getAddItemCssClass());
60              }
61  
62              if (collectionGroup.isAddViaLightBox()) {
63                  String actionScript = "showLightboxComponent('" + lineGroup.getId() + "');";
64                  if (StringUtils.isNotBlank(collectionGroup.getAddViaLightBoxAction().getActionScript())) {
65                      actionScript = collectionGroup.getAddViaLightBoxAction().getActionScript() + actionScript;
66                  }
67                  collectionGroup.getAddViaLightBoxAction().setActionScript(actionScript);
68                  lineGroup.setStyle("display: none");
69              }
70          } else {
71              lineGroup = ComponentUtils.copy(this.getLineGroupPrototype(), idSuffix);
72          }
73  
74          if (((UifFormBase) model).isAddedCollectionItem(currentLine)) {
75              lineGroup.addStyleClass(collectionGroup.getNewItemsCssClass());
76          }
77  
78          ComponentUtils.updateContextForLine(lineGroup, currentLine, lineIndex, idSuffix);
79  
80          // build header text for group
81          String headerText = "";
82          if (isAddLine) {
83              headerText = collectionGroup.getAddLabel();
84          } else {
85              // get the collection for this group from the model
86              List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model,
87                      ((DataBinding) collectionGroup).getBindingInfo().getBindingPath());
88  
89              headerText = buildLineHeaderText(view, modelCollection.get(lineIndex), lineGroup);
90          }
91  
92          // don't set header if text is blank (could already be set by other means)
93          if (StringUtils.isNotBlank(headerText) && lineGroup.getHeader() != null) {
94              lineGroup.getHeader().setHeaderText(headerText);
95          }
96  
97          // stack all fields (including sub-collections) for the group
98          List<Component> groupFields = new ArrayList<Component>();
99          groupFields.addAll(lineFields);
100 
101         // set line actions on group footer
102         if (collectionGroup.isRenderLineActions() && !collectionGroup.isReadOnly() && (lineGroup.getFooter() != null)) {
103 
104             // add the actions to the line group if isActionsInLineGroup flag is true
105             if (isActionsInLineGroup()) {
106                 groupFields.addAll(actions);
107                 groupFields.addAll(subCollectionFields);
108                 lineGroup.setRenderFooter(false);
109             }else{
110                 List<Component> footerFields = new ArrayList<Component>();
111                 footerFields.addAll(actions);
112                 footerFields.addAll(subCollectionFields);
113                 lineGroup.getFooter().setItems(footerFields);
114             }
115         }
116 
117         lineGroup.setItems(groupFields);
118         this.getStackedGroups().add(lineGroup);
119 
120         if (lineIndex == -1) {
121             String nodePath = this.getNodePath(this.getContext());
122             if (nodePath != null) {
123                 for (Group group : this.getStackedGroups()) {
124                     //Set the nodepath on the add line group so that progressive rendering can use the #np
125                     ComponentUtils.pushObjectToContext(group, UifConstants.ContextVariableNames.NODE_PATH, nodePath);
126                 }
127             }
128         }
129 
130     }
131 
132     private String getNodePath(Map<String, Object> context) {
133         if (context.containsKey(UifConstants.ContextVariableNames.NODE_PATH)) {
134             return (String) context.get(UifConstants.ContextVariableNames.NODE_PATH);
135 
136         } else if (context.containsKey(UifConstants.ContextVariableNames.PARENT)) {
137             Component parent = (Component) context.get(UifConstants.ContextVariableNames.PARENT);
138             return this.getNodePath(parent.getContext());
139         }
140         return null;
141     }
142 
143 }