| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.common.ui.client.configurable.mvc; |
| 17 | |
|
| 18 | |
import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBinding; |
| 19 | |
import org.kuali.student.common.ui.client.configurable.mvc.binding.MultiplicityCompositeBinding; |
| 20 | |
import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityComposite; |
| 21 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
| 22 | |
import org.kuali.student.common.ui.client.mvc.HasDataValue; |
| 23 | |
import org.kuali.student.common.ui.client.widgets.KSCheckBox; |
| 24 | |
import org.kuali.student.common.ui.client.widgets.KSTextBox; |
| 25 | |
import org.kuali.student.common.ui.client.widgets.RichTextEditor; |
| 26 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.FieldElement; |
| 27 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.MessageKeyInfo; |
| 28 | |
import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract; |
| 29 | |
import org.kuali.student.core.assembly.data.Metadata; |
| 30 | |
import org.kuali.student.core.assembly.data.MetadataInterrogator; |
| 31 | |
|
| 32 | |
import com.google.gwt.user.client.ui.HasText; |
| 33 | |
import com.google.gwt.user.client.ui.HasValue; |
| 34 | |
import com.google.gwt.user.client.ui.Widget; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
public class FieldDescriptor { |
| 43 | |
protected String fieldKey; |
| 44 | |
protected Metadata metadata; |
| 45 | |
@SuppressWarnings("unchecked") |
| 46 | |
private ModelWidgetBinding modelWidgetBinding; |
| 47 | |
private Callback<Boolean> validationRequestCallback; |
| 48 | 0 | private boolean dirty = false; |
| 49 | 0 | private boolean hasHadFocus = false; |
| 50 | |
private final FieldElement fieldElement; |
| 51 | |
private String modelId; |
| 52 | |
private MessageKeyInfo messageKey; |
| 53 | 0 | private boolean optional = false; |
| 54 | |
|
| 55 | 0 | public FieldDescriptor(String fieldKey, MessageKeyInfo messageKey, Metadata metadata) { |
| 56 | 0 | this.fieldKey = fieldKey; |
| 57 | 0 | this.metadata = metadata; |
| 58 | 0 | if(messageKey == null){ |
| 59 | 0 | messageKey = new MessageKeyInfo(""); |
| 60 | |
} |
| 61 | 0 | setMessageKey(messageKey); |
| 62 | 0 | fieldElement = new FieldElement(fieldKey, messageKey, createFieldWidget()); |
| 63 | 0 | setupField(); |
| 64 | 0 | } |
| 65 | |
|
| 66 | 0 | public FieldDescriptor(String fieldKey, MessageKeyInfo messageKey, Metadata metadata, Widget fieldWidget){ |
| 67 | 0 | this.fieldKey = fieldKey; |
| 68 | 0 | this.metadata = metadata; |
| 69 | 0 | if(messageKey == null){ |
| 70 | 0 | messageKey = new MessageKeyInfo(""); |
| 71 | |
} |
| 72 | 0 | setMessageKey(messageKey); |
| 73 | 0 | addStyleToWidget(fieldWidget); |
| 74 | 0 | fieldElement = new FieldElement(fieldKey, messageKey, fieldWidget); |
| 75 | 0 | setupField(); |
| 76 | 0 | } |
| 77 | |
|
| 78 | |
protected void addStyleToWidget(Widget w){ |
| 79 | 0 | if(fieldKey != null && !fieldKey.isEmpty() && w != null){ |
| 80 | 0 | String style = this.fieldKey.replaceAll("/", "-"); |
| 81 | 0 | w.addStyleName(style); |
| 82 | |
} |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
protected void setupField() { |
| 86 | 0 | if(metadata != null){ |
| 87 | 0 | if(MetadataInterrogator.isRequired(metadata)){ |
| 88 | 0 | fieldElement.setRequiredString("requiredMarker", "ks-form-module-elements-required"); |
| 89 | |
} |
| 90 | 0 | else if(MetadataInterrogator.isRequiredForNextState(metadata)){ |
| 91 | 0 | String nextState = MetadataInterrogator.getNextState(metadata); |
| 92 | 0 | if(nextState != null){ |
| 93 | 0 | if(nextState.equalsIgnoreCase("SUBMITTED")){ |
| 94 | 0 | fieldElement.setRequiredString("requiredOnSubmit", "ks-form-required-for-submit"); |
| 95 | |
} |
| 96 | 0 | else if(nextState.equalsIgnoreCase("APPROVED")){ |
| 97 | 0 | fieldElement.setRequiredString("reqApproval", "ks-form-required-for-submit"); |
| 98 | |
} |
| 99 | 0 | else if(nextState.equalsIgnoreCase("ACTIVE")){ |
| 100 | 0 | fieldElement.setRequiredString("reqActivate", "ks-form-required-for-submit"); |
| 101 | |
} |
| 102 | 0 | else if(nextState.equalsIgnoreCase("INACTIVE") || |
| 103 | |
nextState.equalsIgnoreCase("RETIRED")){ |
| 104 | 0 | fieldElement.setRequiredString("reqDeactivate", "ks-form-required-for-submit"); |
| 105 | |
} |
| 106 | |
else { |
| 107 | 0 | fieldElement.setRequiredString("requiredOnSubmit", "ks-form-required-for-submit"); |
| 108 | |
} |
| 109 | |
|
| 110 | |
} |
| 111 | 0 | } else{ |
| 112 | 0 | fieldElement.clearRequiredText(); |
| 113 | |
} |
| 114 | |
} |
| 115 | 0 | } |
| 116 | |
|
| 117 | |
public void hideLabel(){ |
| 118 | 0 | fieldElement.hideLabel(); |
| 119 | 0 | } |
| 120 | |
|
| 121 | |
public boolean isLabelShown(){ |
| 122 | 0 | return fieldElement.isLabelShown(); |
| 123 | |
} |
| 124 | |
|
| 125 | |
public FieldElement getFieldElement(){ |
| 126 | 0 | return fieldElement; |
| 127 | |
} |
| 128 | |
|
| 129 | |
public String getFieldKey() { |
| 130 | 0 | return fieldKey; |
| 131 | |
} |
| 132 | |
|
| 133 | |
public void setFieldKey(String fieldKey) { |
| 134 | 0 | this.fieldKey = fieldKey; |
| 135 | 0 | } |
| 136 | |
|
| 137 | |
public String getFieldLabel() { |
| 138 | 0 | return fieldElement.getFieldName(); |
| 139 | |
} |
| 140 | |
|
| 141 | |
public Widget getFieldWidget(){ |
| 142 | 0 | if (fieldElement.getFieldWidget() == null){ |
| 143 | 0 | Widget w = createFieldWidget(); |
| 144 | 0 | fieldElement.setWidget(w); |
| 145 | |
} |
| 146 | 0 | return fieldElement.getFieldWidget(); |
| 147 | |
} |
| 148 | |
|
| 149 | |
protected Widget createFieldWidget() { |
| 150 | 0 | if (metadata == null) { |
| 151 | |
|
| 152 | |
|
| 153 | 0 | Widget result = new KSTextBox(); |
| 154 | 0 | addStyleToWidget(result); |
| 155 | 0 | return result; |
| 156 | |
} else { |
| 157 | 0 | Widget result = DefaultWidgetFactory.getInstance().getWidget(this); |
| 158 | 0 | addStyleToWidget(result); |
| 159 | 0 | return result; |
| 160 | |
} |
| 161 | |
} |
| 162 | |
|
| 163 | |
public ModelWidgetBinding<?> getModelWidgetBinding() { |
| 164 | 0 | if(modelWidgetBinding == null){ |
| 165 | 0 | if(fieldElement.getFieldWidget() instanceof RichTextEditor){ |
| 166 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.RichTextBinding.INSTANCE; |
| 167 | 0 | } else if (fieldElement.getFieldWidget() instanceof KSCheckBox){ |
| 168 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.HasValueBinding.INSTANCE; |
| 169 | 0 | } else if(fieldElement.getFieldWidget() instanceof MultiplicityComposite){ |
| 170 | 0 | modelWidgetBinding = MultiplicityCompositeBinding.INSTANCE; |
| 171 | 0 | } else if (fieldElement.getFieldWidget()instanceof HasText) { |
| 172 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.HasTextBinding.INSTANCE; |
| 173 | 0 | } else if (fieldElement.getFieldWidget() instanceof KSSelectItemWidgetAbstract){ |
| 174 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.SelectItemWidgetBinding.INSTANCE; |
| 175 | 0 | } else if (fieldElement.getFieldWidget() instanceof HasDataValue){ |
| 176 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.HasDataValueBinding.INSTANCE; |
| 177 | 0 | } else if (fieldElement.getFieldWidget() instanceof HasValue){ |
| 178 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.HasValueBinding.INSTANCE; |
| 179 | |
} |
| 180 | |
} |
| 181 | 0 | return modelWidgetBinding; |
| 182 | |
} |
| 183 | |
|
| 184 | |
public void setValidationCallBack(Callback<Boolean> callback){ |
| 185 | 0 | validationRequestCallback = callback; |
| 186 | 0 | } |
| 187 | |
|
| 188 | |
public Callback<Boolean> getValidationRequestCallback(){ |
| 189 | 0 | return validationRequestCallback; |
| 190 | |
} |
| 191 | |
|
| 192 | |
public boolean isDirty() { |
| 193 | 0 | return dirty; |
| 194 | |
} |
| 195 | |
|
| 196 | |
public void setDirty(boolean dirty) { |
| 197 | 0 | this.dirty = dirty; |
| 198 | 0 | } |
| 199 | |
|
| 200 | |
public boolean hasHadFocus() { |
| 201 | 0 | return hasHadFocus; |
| 202 | |
} |
| 203 | |
|
| 204 | |
public void setHasHadFocus(boolean hasHadFocus) { |
| 205 | 0 | this.hasHadFocus = hasHadFocus; |
| 206 | 0 | } |
| 207 | |
|
| 208 | |
public Metadata getMetadata() { |
| 209 | 0 | return metadata; |
| 210 | |
} |
| 211 | |
|
| 212 | |
public void setMetadata(Metadata metadata) { |
| 213 | 0 | this.metadata = metadata; |
| 214 | 0 | setupField(); |
| 215 | 0 | } |
| 216 | |
|
| 217 | |
public void setFieldWidget(Widget fieldWidget) { |
| 218 | 0 | this.fieldElement.setWidget(fieldWidget); |
| 219 | 0 | } |
| 220 | |
|
| 221 | |
public String getModelId() { |
| 222 | 0 | return modelId; |
| 223 | |
} |
| 224 | |
|
| 225 | |
public void setModelId(String modelId) { |
| 226 | 0 | this.modelId = modelId; |
| 227 | 0 | } |
| 228 | |
|
| 229 | |
public void setWidgetBinding(ModelWidgetBinding widgetBinding) { |
| 230 | 0 | this.modelWidgetBinding = widgetBinding; |
| 231 | 0 | } |
| 232 | |
|
| 233 | |
public MessageKeyInfo getMessageKey() { |
| 234 | 0 | return messageKey; |
| 235 | |
} |
| 236 | |
|
| 237 | |
public void setMessageKey(MessageKeyInfo messageKey) { |
| 238 | 0 | this.messageKey = messageKey; |
| 239 | 0 | } |
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
public void setOptional(boolean optional){ |
| 248 | 0 | this.optional = optional; |
| 249 | 0 | } |
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
public boolean isOptional(){ |
| 256 | 0 | return optional; |
| 257 | |
} |
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
public boolean isVisible() { |
| 263 | 0 | if (metadata != null){ |
| 264 | 0 | return metadata.isCanView(); |
| 265 | |
} else { |
| 266 | 0 | return true; |
| 267 | |
} |
| 268 | |
} |
| 269 | |
|
| 270 | |
} |