View Javadoc

1   /**
2    * Copyright 2005-2012 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.control;
17  
18  import org.kuali.rice.core.api.util.KeyValue;
19  import org.kuali.rice.krad.uif.component.Component;
20  import org.kuali.rice.krad.uif.container.Container;
21  import org.kuali.rice.krad.uif.element.Message;
22  import org.kuali.rice.krad.uif.field.InputField;
23  import org.kuali.rice.krad.uif.util.ComponentFactory;
24  import org.kuali.rice.krad.uif.util.KeyMessage;
25  import org.kuali.rice.krad.uif.view.View;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  /**
31   * Base class for controls that accept/display multiple values
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public abstract class MultiValueControlBase extends ControlBase implements MultiValueControl {
36      private static final long serialVersionUID = -8691367056245775455L;
37  
38      private List<KeyValue> options;
39      private List<KeyMessage> richOptions;
40      private List<Component> inlineComponents;
41  
42      public MultiValueControlBase() {
43          super();
44      }
45  
46      /**
47       * Process rich message content that may be in the options, by creating and initializing the richOptions
48       *
49       * @see org.kuali.rice.krad.uif.component.ComponentBase#performApplyModel(org.kuali.rice.krad.uif.view.View,
50       *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
51       */
52      @Override
53      public void performApplyModel(View view, Object model, Component parent) {
54          super.performApplyModel(view, model, parent);
55  
56          if (options != null && richOptions == null) {
57              richOptions = new ArrayList<KeyMessage>();
58  
59              for (KeyValue option : options) {
60                  Message message = ComponentFactory.getMessage();
61                  view.assignComponentIds(message);
62                  message.setMessageText(option.getValue());
63                  message.setInlineComponents(inlineComponents);
64                  message.setGenerateSpan(false);
65  
66                  view.getViewHelperService().performComponentInitialization(view, model, message);
67                  richOptions.add(new KeyMessage(option.getKey(), option.getValue(), message));
68              }
69          }
70      }
71  
72      /**
73       * Adds appropriate parent data to inputs internal to the controls that may be in rich content of options
74       *
75       * @see Component#performFinalize(org.kuali.rice.krad.uif.view.View, Object, org.kuali.rice.krad.uif.component.Component)
76       */
77      @Override
78      public void performFinalize(View view, Object model, Component parent) {
79          super.performFinalize(view, model, parent);
80  
81          if (richOptions.isEmpty()) {
82              return;
83          }
84  
85          //Messages included in options which have have rich message content need to be aware of their parent for
86          //validation purposes
87          for (KeyMessage richOption : richOptions) {
88              List<Component> components = richOption.getMessage().getMessageComponentStructure();
89  
90              if (components != null && !components.isEmpty()) {
91                  for (Component c : components) {
92                      if (c instanceof Container || c instanceof InputField) {
93                          c.addDataAttribute("parent", parent.getId());
94                      }
95                  }
96              }
97          }
98  
99      }
100 
101     /**
102      * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
103      */
104     @Override
105     public List<Component> getComponentsForLifecycle() {
106         List<Component> components = super.getComponentsForLifecycle();
107 
108         if (richOptions != null) {
109             for (KeyMessage richOption : richOptions) {
110                 components.add(richOption.getMessage());
111             }
112         }
113         return components;
114     }
115 
116     /**
117      * @see org.kuali.rice.krad.uif.control.MultiValueControl#getOptions()
118      */
119     public List<KeyValue> getOptions() {
120         return this.options;
121     }
122 
123     /**
124      * @see org.kuali.rice.krad.uif.control.MultiValueControl#setOptions(java.util.List<org.kuali.rice.core.api.util.KeyValue>)
125      */
126     public void setOptions(List<KeyValue> options) {
127         this.options = options;
128     }
129 
130     /**
131      * Gets the inlineComponents which represent components that can be referenced in an option's value
132      * by index
133      *
134      * @return the components that can be used in rich values of options
135      */
136     public List<Component> getInlineComponents() {
137         return inlineComponents;
138     }
139 
140     /**
141      * Sets the inlineComponents which represent components that can be referenced in an option's value
142      * by index
143      *
144      * @param inlineComponents
145      */
146     public void setInlineComponents(List<Component> inlineComponents) {
147         this.inlineComponents = inlineComponents;
148     }
149 
150     /**
151      * Gets the richOptions which contain Message objects with the translated rich message structures, which then can
152      * be used by templates to output the appropriate content.
153      *
154      * @return richOptions which include a message object with the translated value content
155      */
156     public List<KeyMessage> getRichOptions() {
157         return richOptions;
158     }
159 
160     /**
161      * Sets the richOptions.  This will always override/ignore options if set.
162      *
163      * <p><b>Messages MUST be defined</b> when using this setter, do not use this setter for most cases
164      * as setting options through setOptions, with a richMessage value, is appropriate in MOST cases.  This
165      * setter is only available for full control.</p>
166      *
167      * @param richOptions with their messages predefined
168      */
169     public void setRichOptions(List<KeyMessage> richOptions) {
170         this.richOptions = richOptions;
171     }
172 }