View Javadoc

1   package org.kuali.student.common.ui.client.configurable.mvc;
2   
3   import org.kuali.student.common.ui.client.application.Application;
4   import org.kuali.student.common.ui.client.configurable.mvc.sections.Section;
5   import org.kuali.student.common.ui.client.widgets.field.layout.element.MessageKeyInfo;
6   import org.kuali.student.core.assembly.data.Metadata;
7   import org.kuali.student.core.assembly.data.ModelDefinition;
8   import org.kuali.student.core.assembly.data.QueryPath;
9   
10  import com.google.gwt.user.client.ui.Widget;
11  
12  /**
13   * A configurer defines the configuration of screens in KS, in particular this is where you add views,
14   * sections, and fields to a LayoutController preferably passed in in an configurer's implementation.
15   * This abstract class contains helper methods to do this.
16   * 
17   * @author Kuali Student Team
18   *
19   */
20  public abstract class Configurer {
21      protected ModelDefinition modelDefinition;
22      protected String type = "";
23      protected String state = "";
24      protected String groupName = "";
25  //    public abstract void configure(ConfigurableLayout layout);
26      /**
27       * Sets the modelDefinition which is the metadata backing the fields to be configured,
28       * this needs to be set before adding any fields in the configurer
29       * @param modelDefinition
30       */
31      public void setModelDefinition(ModelDefinition modelDefinition){
32          this.modelDefinition = modelDefinition;
33      }
34  
35      public ModelDefinition getModelDefinition() {
36          return modelDefinition;
37      }
38  
39      /**
40       * Generates a message info to be used in your field descriptor to get the label for the field.
41       * Used by the field descriptor with the application context to determine the label to show based on
42       * the labelKey
43       * @param labelKey key of the message - must match a message in your messages (stored in the db)
44       * @return
45       */
46      protected MessageKeyInfo generateMessageInfo(String labelKey) {
47          return new MessageKeyInfo(groupName, type, state, labelKey);
48      }
49      
50      /**
51       * Gets the string corresponding to the label key passed in from the application messages
52       * @param labelKey
53       * @return
54       */
55      protected String getLabel(String labelKey) {
56          return Application.getApplicationContext().getUILabel(groupName, type, state, labelKey);
57      }
58  
59      /**
60       * Gets a section title which is an h1 element using the label key passed to retrieve the corresponding
61       * message
62       * @param labelKey
63       * @return
64       */
65      protected SectionTitle getH1Title(String labelKey) {
66          return SectionTitle.generateH1Title(getLabel(labelKey));
67      }
68  
69      /**
70       * Gets a section title which is an h1 element using the label key passed to retrieve the corresponding
71       * message
72       * @param labelKey
73       * @return
74       */
75      protected SectionTitle getH2Title(String labelKey) {
76          return SectionTitle.generateH2Title(getLabel(labelKey));
77      }
78  
79      /**
80       * Gets a section title which is an h1 element using the label key passed to retrieve the corresponding
81       * message
82       * @param labelKey
83       * @return
84       */
85      protected SectionTitle getH3Title(String labelKey) {
86          return SectionTitle.generateH3Title(getLabel(labelKey));
87      }
88  
89      /**
90       * Gets a section title which is an h1 element using the label key passed to retrieve the corresponding
91       * message
92       * @param labelKey
93       * @return
94       */
95      protected SectionTitle getH4Title(String labelKey) {
96          return SectionTitle.generateH4Title(getLabel(labelKey));
97      }
98  
99      /**
100      * Gets a section title which is an h1 element using the label key passed to retrieve the corresponding
101      * message
102      * @param labelKey
103      * @return
104      */
105     protected SectionTitle getH5Title(String labelKey) {
106         return SectionTitle.generateH5Title(getLabel(labelKey));
107     }
108 
109     /**
110      * Adds a field with the field key specified to the passed in section.  
111      * Returns the generated FieldDescriptor.  The field will have no label in this case.
112      * The widget will be auto generated in field descriptor based on the field's metadata in the
113      * model definition defined in this configurer - using the default widget factory.  The fieldKey passed
114      * in MUST match a logical path in the metadata (ex. proposal/proposalTitle, etc.)
115      * 
116      * Note: It also is acceptable to not use this helper method and add the FieldDescriptor directly
117      * to the section
118      * 
119      * @param section
120      * @param fieldKey
121      * @return
122      */
123     public FieldDescriptor addField(Section section, String fieldKey) {
124         return addField(section, fieldKey, null, null, null);
125     }
126     
127     /**
128      * Adds a field with the field key specified to the passed in section.  
129      * Returns the generated FieldDescriptor.  Uses the message key provided to generate the field label.
130      * The widget will be auto generated in field descriptor based on the field's metadata in the
131      * model definition defined in this configurer - using the default widget factory.  The fieldKey passed
132      * in MUST match a logical path in the metadata (ex. proposal/proposalTitle, etc.)
133      * 
134      * Note: It also is acceptable to not use this helper method and add the FieldDescriptor directly
135      * to the section
136      * 
137      * @param section
138      * @param fieldKey
139      * @param messageKey
140      * @return
141      */
142     public FieldDescriptor addField(Section section, String fieldKey, MessageKeyInfo messageKey) {
143         return addField(section, fieldKey, messageKey, null, null);
144     }
145     
146     /**
147      * Adds a field with the field key specified to the passed in section.  
148      * Returns the generated FieldDescriptor.  Uses the message key provided to generate the field label.
149      * The widget is not autogenerated based on the metadata in the defined model definition, but instead
150      * the passed in widget is used.  The widget must be a type that is compatible with the default bindings
151      * (such as being a widget which implements HasText or HasValue), unless a custom binding is set on the
152      * returned FieldDescriptor from this method manually.
153      * 
154      * Note: It also is acceptable to not use this helper method and add the FieldDescriptor directly
155      * to the section
156      * 
157      * @param section
158      * @param fieldKey
159      * @param messageKey
160      * @param widget
161      * @return
162      */
163     public FieldDescriptor addField(Section section, String fieldKey, MessageKeyInfo messageKey, Widget widget) {
164         return addField(section, fieldKey, messageKey, widget, null);
165     }
166     
167     /**
168      * Adds a field with the field key specified to the passed in section.  
169      * Returns the generated FieldDescriptor.  Uses the message key provided to generate the field label.
170      * The widget will be auto generated in field descriptor based on the field's metadata in the
171      * model definition defined in this configurer - using the default widget factory.  The fieldKey passed
172      * in MUST match a logical path in the metadata (ex. proposal/proposalTitle, etc.).  The parentPath
173      * will be concatenated onto the front of the fieldKey.
174      * 
175      * Note: It also is acceptable to not use this helper method and add the FieldDescriptor directly
176      * to the section
177      * 
178      * @param section
179      * @param fieldKey
180      * @param messageKey
181      * @param parentPath
182      * @return
183      */
184     public FieldDescriptor addField(Section section, String fieldKey, MessageKeyInfo messageKey, String parentPath) {
185         return addField(section, fieldKey, messageKey, null, parentPath);
186     }
187     
188     /**
189      * Adds a field with the field key specified to the passed in section.  
190      * Returns the generated FieldDescriptor.  Uses the message key provided to generate the field label.
191      * The widget is not autogenerated based on the metadata in the defined model definition, but instead
192      * the passed in widget is used.  The widget must be a type that is compatible with the default bindings
193      * (such as being a widget which implements HasText or HasValue), unless a custom binding is set on the
194      * returned FieldDescriptor from this method manually.  The parentPath
195      * will be concatenated onto the front of the fieldKey.
196      * 
197      * Note: It also is acceptable to not use this helper method and add the FieldDescriptor directly
198      * to the section
199      * 
200      * @param section
201      * @param fieldKey
202      * @param messageKey
203      * @param widget
204      * @param parentPath
205      * @return
206      */
207     public FieldDescriptor addField(Section section, String fieldKey, MessageKeyInfo messageKey, Widget widget, String parentPath) {
208         QueryPath path = QueryPath.concat(parentPath, fieldKey);
209         Metadata meta = modelDefinition.getMetadata(path);
210 
211         FieldDescriptor fd = new FieldDescriptor(path.toString(), messageKey, meta);
212         if (widget != null) {
213             fd.setFieldWidget(widget);
214         }
215         section.addField(fd);
216         return fd;
217     }
218     
219     /**
220      * Read only variant of the corresponding addField method.  This method will generate the read only version
221      * of the widget from the metadata found in the model definition that matches the fieldKey.
222      * 
223      * @param section
224      * @param fieldKey
225      * @return
226      */
227     public FieldDescriptor addReadOnlyField(Section section, String fieldKey) {
228         return addReadOnlyField(section, fieldKey, null, null, null);
229     }    
230     
231     /**
232      * Read only variant of the corresponding addField method.  This method will generate the read only version
233      * of the widget from the metadata found in the model definition that matches the fieldKey.
234      * 
235      * @param section
236      * @param fieldKey
237      * @param messageKey
238      * @return
239      */
240     public FieldDescriptor addReadOnlyField(Section section, String fieldKey, MessageKeyInfo messageKey) {
241         return addReadOnlyField(section, fieldKey, messageKey, null, null);
242     }
243     
244     /**
245      * Read only variant of the corresponding addField method.  Uses widget passed in.
246      * 
247      * @param section
248      * @param fieldKey
249      * @param messageKey
250      * @param widget
251      * @return
252      */
253     public FieldDescriptor addReadOnlyField(Section section, String fieldKey, MessageKeyInfo messageKey, Widget widget) {
254         return addReadOnlyField(section, fieldKey, messageKey, widget, null);
255     }
256     
257     /**
258      * Read only variant of the corresponding addField method.  This method will generate the read only version
259      * of the widget from the metadata found in the model definition that matches the fieldKey.
260      * 
261      * @param section
262      * @param fieldKey
263      * @param messageKey
264      * @param parentPath
265      * @return
266      */
267     public FieldDescriptor addReadOnlyField(Section section, String fieldKey, MessageKeyInfo messageKey, String parentPath) {
268         return addReadOnlyField(section, fieldKey, messageKey, null, parentPath);
269     }
270     
271     /**
272      * Read only variant of the corresponding addField method.  Uses widget passed in.
273      * 
274      * @param section
275      * @param fieldKey
276      * @param messageKey
277      * @param widget
278      * @param parentPath
279      * @return
280      */
281     public FieldDescriptor addReadOnlyField(Section section, String fieldKey, MessageKeyInfo messageKey, Widget widget, String parentPath) {
282         QueryPath path = QueryPath.concat(parentPath, fieldKey);
283         Metadata meta = modelDefinition.getMetadata(path);
284 
285         FieldDescriptor fd = new FieldDescriptorReadOnly(path.toString(), messageKey, meta);
286         if (widget != null) {                                               
287             fd.setFieldWidget(widget);
288         }
289         section.addField(fd);
290         return fd;
291     }
292 }