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.r1.common.assembly.data.Metadata; 7 import org.kuali.student.r1.common.assembly.data.ModelDefinition; 8 import org.kuali.student.r1.common.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 //FIXME: WJG: I think state should be removed from the configurer 24 protected String state = ""; 25 protected String nextState = ""; 26 protected String groupName = ""; 27 28 /** 29 * Sets the modelDefinition which is the metadata backing the fields to be configured, 30 * this needs to be set before adding any fields in the configurer 31 * @param modelDefinition 32 */ 33 public void setModelDefinition(ModelDefinition modelDefinition){ 34 this.modelDefinition = modelDefinition; 35 } 36 37 public ModelDefinition getModelDefinition() { 38 return modelDefinition; 39 } 40 41 /** 42 * Generates a message info to be used in your field descriptor to get the label for the field. 43 * Used by the field descriptor with the application context to determine the label to show based on 44 * the labelKey 45 * @param labelKey key of the message - must match a message in your messages (stored in the db) 46 * @return 47 */ 48 public MessageKeyInfo generateMessageInfo(String labelKey) { 49 return new MessageKeyInfo(groupName, type, state, labelKey); 50 } 51 52 /** 53 * Gets the string corresponding to the label key passed in from the application messages 54 * @param labelKey 55 * @return 56 */ 57 public String getLabel(String labelKey) { 58 return Application.getApplicationContext().getUILabel(groupName, type, state, labelKey); 59 } 60 61 public String getLabel(String labelKey, String fieldKey) { 62 String parentPath = Application.getApplicationContext().getParentPath(); 63 QueryPath path = QueryPath.concat(parentPath, fieldKey); 64 Metadata metadata = modelDefinition.getMetadata(path); 65 66 return Application.getApplicationContext().getUILabel(groupName, type, state, labelKey, metadata); 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 getH1Title(String labelKey) { 76 return SectionTitle.generateH1Title(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 getH2Title(String labelKey) { 86 return SectionTitle.generateH2Title(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 getH3Title(String labelKey) { 96 return SectionTitle.generateH3Title(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 getH4Title(String labelKey) { 106 return SectionTitle.generateH4Title(getLabel(labelKey)); 107 } 108 109 /** 110 * Gets a section title which is an h1 element using the label key passed to retrieve the corresponding 111 * message 112 * @param labelKey 113 * @return 114 */ 115 protected SectionTitle getH5Title(String labelKey) { 116 return SectionTitle.generateH5Title(getLabel(labelKey)); 117 } 118 119 /** 120 * Adds a field with the field key specified to the passed in section. 121 * Returns the generated FieldDescriptor. The field will have no label in this case. 122 * The widget will be auto generated in field descriptor based on the field's metadata in the 123 * model definition defined in this configurer - using the default widget factory. The fieldKey passed 124 * in MUST match a logical path in the metadata (ex. proposal/proposalTitle, etc.) 125 * 126 * Note: It also is acceptable to not use this helper method and add the FieldDescriptor directly 127 * to the section 128 * 129 * @param section 130 * @param fieldKey 131 * @return 132 */ 133 public FieldDescriptor addField(Section section, String fieldKey) { 134 return addField(section, fieldKey, null, null, null); 135 } 136 137 /** 138 * Adds a field with the field key specified to the passed in section. 139 * Returns the generated FieldDescriptor. Uses the message key provided to generate the field label. 140 * The widget will be auto generated in field descriptor based on the field's metadata in the 141 * model definition defined in this configurer - using the default widget factory. The fieldKey passed 142 * in MUST match a logical path in the metadata (ex. proposal/proposalTitle, etc.) 143 * 144 * Note: It also is acceptable to not use this helper method and add the FieldDescriptor directly 145 * to the section 146 * 147 * @param section 148 * @param fieldKey 149 * @param messageKey 150 * @return 151 */ 152 public FieldDescriptor addField(Section section, String fieldKey, MessageKeyInfo messageKey) { 153 return addField(section, fieldKey, messageKey, null, null); 154 } 155 156 /** 157 * Adds a field with the field key specified to the passed in section. 158 * Returns the generated FieldDescriptor. Uses the message key provided to generate the field label. 159 * The widget is not autogenerated based on the metadata in the defined model definition, but instead 160 * the passed in widget is used. The widget must be a type that is compatible with the default bindings 161 * (such as being a widget which implements HasText or HasValue), unless a custom binding is set on the 162 * returned FieldDescriptor from this method manually. 163 * 164 * Note: It also is acceptable to not use this helper method and add the FieldDescriptor directly 165 * to the section 166 * 167 * @param section 168 * @param fieldKey 169 * @param messageKey 170 * @param widget 171 * @return 172 */ 173 public FieldDescriptor addField(Section section, String fieldKey, MessageKeyInfo messageKey, Widget widget) { 174 return addField(section, fieldKey, messageKey, widget, null); 175 } 176 177 /** 178 * Adds a field with the field key specified to the passed in section. 179 * Returns the generated FieldDescriptor. Uses the message key provided to generate the field label. 180 * The widget will be auto generated in field descriptor based on the field's metadata in the 181 * model definition defined in this configurer - using the default widget factory. The fieldKey passed 182 * in MUST match a logical path in the metadata (ex. proposal/proposalTitle, etc.). The parentPath 183 * will be concatenated onto the front of the fieldKey. 184 * 185 * Note: It also is acceptable to not use this helper method and add the FieldDescriptor directly 186 * to the section 187 * 188 * @param section 189 * @param fieldKey 190 * @param messageKey 191 * @param parentPath 192 * @return 193 */ 194 public FieldDescriptor addField(Section section, String fieldKey, MessageKeyInfo messageKey, String parentPath) { 195 return addField(section, fieldKey, messageKey, null, parentPath); 196 } 197 198 /** 199 * Adds a field with the field key specified to the passed in section. 200 * Returns the generated FieldDescriptor. Uses the message key provided to generate the field label. 201 * The widget is not autogenerated based on the metadata in the defined model definition, but instead 202 * the passed in widget is used. The widget must be a type that is compatible with the default bindings 203 * (such as being a widget which implements HasText or HasValue), unless a custom binding is set on the 204 * returned FieldDescriptor from this method manually. The parentPath 205 * will be concatenated onto the front of the fieldKey. 206 * 207 * Note: It also is acceptable to not use this helper method and add the FieldDescriptor directly 208 * to the section 209 * 210 * @param section 211 * @param fieldKey 212 * @param messageKey 213 * @param widget 214 * @param parentPath 215 * @return 216 */ 217 public FieldDescriptor addField(Section section, String fieldKey, MessageKeyInfo messageKey, Widget widget, String parentPath) { 218 QueryPath path = QueryPath.concat(parentPath, fieldKey); 219 Metadata meta = modelDefinition.getMetadata(path); 220 221 FieldDescriptor fd = new FieldDescriptor(path.toString(), messageKey, meta); 222 if (widget != null) { 223 fd.setFieldWidget(widget); 224 } 225 section.addField(fd); 226 return fd; 227 } 228 229 /** 230 * Read only variant of the corresponding addField method. This method will generate the read only version 231 * of the widget from the metadata found in the model definition that matches the fieldKey. 232 * 233 * @param section 234 * @param fieldKey 235 * @return 236 */ 237 public FieldDescriptor addReadOnlyField(Section section, String fieldKey) { 238 return addReadOnlyField(section, fieldKey, null, null, null); 239 } 240 241 /** 242 * Read only variant of the corresponding addField method. This method will generate the read only version 243 * of the widget from the metadata found in the model definition that matches the fieldKey. 244 * 245 * @param section 246 * @param fieldKey 247 * @param messageKey 248 * @return 249 */ 250 public FieldDescriptor addReadOnlyField(Section section, String fieldKey, MessageKeyInfo messageKey) { 251 return addReadOnlyField(section, fieldKey, messageKey, null, null); 252 } 253 254 /** 255 * Read only variant of the corresponding addField method. Uses widget passed in. 256 * 257 * @param section 258 * @param fieldKey 259 * @param messageKey 260 * @param widget 261 * @return 262 */ 263 public FieldDescriptor addReadOnlyField(Section section, String fieldKey, MessageKeyInfo messageKey, Widget widget) { 264 return addReadOnlyField(section, fieldKey, messageKey, widget, null); 265 } 266 267 /** 268 * Read only variant of the corresponding addField method. This method will generate the read only version 269 * of the widget from the metadata found in the model definition that matches the fieldKey. 270 * 271 * @param section 272 * @param fieldKey 273 * @param messageKey 274 * @param parentPath 275 * @return 276 */ 277 public FieldDescriptor addReadOnlyField(Section section, String fieldKey, MessageKeyInfo messageKey, String parentPath) { 278 return addReadOnlyField(section, fieldKey, messageKey, null, parentPath); 279 } 280 281 /** 282 * Read only variant of the corresponding addField method. Uses widget passed in. 283 * 284 * @param section 285 * @param fieldKey 286 * @param messageKey 287 * @param widget 288 * @param parentPath 289 * @return 290 */ 291 public FieldDescriptor addReadOnlyField(Section section, String fieldKey, MessageKeyInfo messageKey, Widget widget, String parentPath) { 292 QueryPath path = QueryPath.concat(parentPath, fieldKey); 293 Metadata meta = modelDefinition.getMetadata(path); 294 295 FieldDescriptor fd = new FieldDescriptorReadOnly(path.toString(), messageKey, meta); 296 if (widget != null) { 297 fd.setFieldWidget(widget); 298 } 299 section.addField(fd); 300 return fd; 301 } 302 303 /** 304 * The initial state of the objects for the screen 305 * 306 * @return 307 */ 308 public String getState() { 309 return state; 310 } 311 312 public void setState(String state) { 313 this.state = state; 314 } 315 316 public String getNextState() { 317 return nextState; 318 } 319 320 public void setNextState(String nextState) { 321 this.nextState = nextState; 322 } 323 }