Coverage Report - org.kuali.student.common.ui.client.widgets.field.layout.element.FieldElement
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldElement
0%
0/203
0%
0/98
2.316
FieldElement$1
0%
0/1
N/A
2.316
FieldElement$LineNum
0%
0/1
N/A
2.316
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.common.ui.client.widgets.field.layout.element;
 17  
 
 18  
 import org.kuali.student.common.ui.client.application.Application;
 19  
 import org.kuali.student.common.ui.client.configurable.mvc.sections.ValidationMessagePanel;
 20  
 import org.kuali.student.common.ui.client.widgets.HasInputWidget;
 21  
 import org.kuali.student.common.ui.client.widgets.HasWatermark;
 22  
 import org.kuali.student.common.ui.client.widgets.KSLabel;
 23  
 import org.kuali.student.common.ui.client.widgets.KSTitleDescPanel;
 24  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton.AbbrButtonType;
 25  
 import org.kuali.student.common.ui.client.widgets.field.layout.layouts.FieldLayoutComponent;
 26  
 import org.kuali.student.core.validation.dto.ValidationResultInfo;
 27  
 import org.kuali.student.core.validation.dto.ValidationResultInfo.ErrorLevel;
 28  
 
 29  
 import com.google.gwt.user.client.Element;
 30  
 import com.google.gwt.user.client.ui.Composite;
 31  
 import com.google.gwt.user.client.ui.FlowPanel;
 32  
 import com.google.gwt.user.client.ui.HTMLPanel;
 33  
 import com.google.gwt.user.client.ui.Panel;
 34  
 import com.google.gwt.user.client.ui.Widget;
 35  
 
 36  
 public class FieldElement extends Composite implements FieldLayoutComponent{
 37  
 
 38  
         //Layout
 39  0
         private KSTitleDescPanel titlePanel = new KSTitleDescPanel();
 40  0
         private FlowPanel layout = new FlowPanel();
 41  
         private FieldTitle fieldTitle;
 42  0
         private SpanPanel instructions = new SpanPanel();
 43  0
         private SpanPanel constraints = new SpanPanel();
 44  0
         private AbbrPanel required = new AbbrPanel("Required", "ks-form-module-elements-required", " * ");
 45  0
         private AbbrButton help = new AbbrButton(AbbrButtonType.HELP);
 46  
         private Widget fieldWidget;
 47  0
         private SpanPanel widgetSpan = new SpanPanel();
 48  
         private String fieldHTMLId;
 49  
         private LineNum margin;
 50  0
         private String watermarkText = null;
 51  0
         public static enum LineNum{SINGLE, DOUBLE, TRIPLE}
 52  
 
 53  
         private ValidationMessagePanel validationPanel;
 54  
         private String fieldKey;
 55  0
         private boolean labelShown = true;
 56  
 
 57  
         public void setValidationPanel(ValidationMessagePanel validationPanel) {
 58  0
                 this.validationPanel = validationPanel;
 59  0
         }
 60  
 
 61  
         private Panel parentPanel;
 62  
         private Element parentElement;
 63  
         public Panel getParentPanel() {
 64  0
                 return parentPanel;
 65  
         }
 66  
 
 67  
         public void setParentPanel(Panel parentPanel) {
 68  0
                 this.parentPanel = parentPanel;
 69  0
                 this.parentElement = parentPanel.getElement();
 70  0
         }
 71  
 
 72  
         public void setParentElement(Element element){
 73  0
                 parentElement = element;
 74  0
         }
 75  
 
 76  
         private String fieldName;
 77  
         private String instructionText;
 78  
 
 79  
     public String getInstructionText() {
 80  0
                 return instructionText;
 81  
         }
 82  
 
 83  0
         public FieldElement(String title, Widget widget) {
 84  0
             generateLayout(null, title, null, null, null, widget);
 85  
 
 86  0
     }
 87  
 
 88  0
     public FieldElement(String key, String title, Widget widget){
 89  0
             generateLayout(key, title, null, null, null, widget);
 90  0
     }
 91  
 
 92  0
     public FieldElement(String key, MessageKeyInfo info){
 93  0
             init(key, info, null);
 94  0
     }
 95  
 
 96  0
     public FieldElement(String key, MessageKeyInfo info, Widget widget){
 97  0
             init(key, info, widget);
 98  
 
 99  0
     }
 100  
     
 101  
     private void init(String key, MessageKeyInfo info, Widget widget){
 102  0
             String title = Application.getApplicationContext().getUILabel(info.getGroup(), info.getType(), info.getState(), info.getId());
 103  
             
 104  0
             String help = Application.getApplicationContext().getUILabel(info.getGroup(), info.getType(), info.getState(),
 105  
                             info.getId() + HELP_MESSAGE_KEY);
 106  0
             if(help.equals(info.getId() + HELP_MESSAGE_KEY)){
 107  0
                     help = null;
 108  
             }
 109  
             
 110  0
             String instructions = Application.getApplicationContext().getUILabel(info.getGroup(), info.getType(), info.getState(),
 111  
                             info.getId() + INSTRUCT_MESSAGE_KEY);
 112  0
             if(instructions.equals(info.getId() + INSTRUCT_MESSAGE_KEY)){
 113  0
                     instructions = null;
 114  
             }
 115  
             
 116  0
             String constraints = Application.getApplicationContext().getUILabel(info.getGroup(), info.getType(), info.getState(),
 117  
                             info.getId() + CONSTRAINT_MESSAGE_KEY);
 118  0
             if(constraints.equals(info.getId() + CONSTRAINT_MESSAGE_KEY)){
 119  0
                     constraints = null;
 120  
             }
 121  
             
 122  0
             watermarkText = Application.getApplicationContext().getUILabel(info.getGroup(), info.getType(), info.getState(),
 123  
                             info.getId() + WATERMARK_MESSAGE_KEY);
 124  0
             if(watermarkText.equals(info.getId() + WATERMARK_MESSAGE_KEY)){
 125  0
                     watermarkText = null;
 126  
             }
 127  
             
 128  0
             generateLayout(key, title, help, instructions, constraints, widget);
 129  0
     }
 130  
 
 131  
     private void generateLayout(String key, String title, String helpText, String instructText, String constraintText, Widget widget){
 132  0
             this.setKey(key);
 133  0
                 fieldName = title;
 134  
 
 135  0
                 fieldHTMLId = HTMLPanel.createUniqueId();
 136  0
                 fieldTitle = new LabelPanel(title, fieldHTMLId);
 137  
 
 138  0
                 required.setVisible(false);
 139  0
                 fieldTitle.add(required);
 140  0
                 if(helpText != null){
 141  0
                         this.setHelp(helpText);
 142  
                 }
 143  
                 else{
 144  0
                         help.setVisible(false);
 145  
                 }
 146  
 
 147  0
                 fieldTitle.add(help);
 148  0
                 layout.add(fieldTitle);
 149  0
                 if(instructText != null){
 150  0
                         this.setInstructions(instructText);
 151  
                 }
 152  
                 else{
 153  0
                         instructions.setVisible(false);
 154  
                 }
 155  
                 
 156  0
                 if(constraintText != null){
 157  0
                         this.setConstraintText(constraintText);
 158  
                 }
 159  
                 else{
 160  0
                         constraints.setVisible(false);
 161  
                 }
 162  0
                 instructions.setStyleName("ks-form-module-elements-instruction");
 163  0
                 layout.add(instructions);
 164  0
                 layout.add(widgetSpan);
 165  0
                 if(widget != null){
 166  0
                         this.setWidget(widget);
 167  
                 }
 168  0
                 constraints.setStyleName("ks-form-module-elements-help-text");
 169  0
                 layout.add(constraints);
 170  
 
 171  
 
 172  0
         initWidget(layout);
 173  0
         layout.addStyleName("ks-form-module-elements");
 174  0
         layout.addStyleName("ks-form-module-single-line-margin");
 175  0
     }
 176  
 
 177  
     public void setWidget(Widget w){
 178  0
             if(fieldWidget != null){
 179  0
                     widgetSpan.remove(fieldWidget);
 180  
             }
 181  0
             fieldWidget = w;
 182  
             //TODO Do a check here to change the type of label based on widget type eventually
 183  0
             if(fieldWidget != null){
 184  
                     
 185  
                     //Checks for input widgets that may be incased in a more complex widget layout
 186  0
                     if(fieldWidget instanceof HasInputWidget){
 187  0
                             Widget input = ((HasInputWidget)fieldWidget).getInputWidget();
 188  0
                             if(input != null){
 189  0
                                     if(input instanceof HasWatermark && watermarkText != null){
 190  0
                                             ((HasWatermark)input).setWatermarkText(watermarkText);
 191  
                                     }
 192  0
                                     input.getElement().setAttribute("id", fieldHTMLId);
 193  
                             }
 194  
                             else{
 195  0
                                     fieldWidget.getElement().setAttribute("id", fieldHTMLId);
 196  
                             }
 197  0
                     }
 198  
                     else{
 199  0
                         if(fieldWidget instanceof HasWatermark && watermarkText != null){
 200  0
                                 ((HasWatermark)fieldWidget).setWatermarkText(watermarkText);
 201  
                         }
 202  0
                             fieldWidget.getElement().setAttribute("id", fieldHTMLId);
 203  
                     }
 204  
                     
 205  0
                     widgetSpan.add(fieldWidget);
 206  
             }
 207  0
     }
 208  
     
 209  
     public void setRequiredString(String requiredKey){
 210  0
             String requiredText = Application.getApplicationContext().getMessage(requiredKey);
 211  0
             required.setText(requiredText);
 212  0
             required.setVisible(true);
 213  0
     }
 214  
     
 215  
     public void setRequiredString(String requiredKey, String style){
 216  0
             String requiredText = Application.getApplicationContext().getMessage(requiredKey);
 217  0
             required.setText(requiredText);
 218  0
             required.setStyleName(style);
 219  0
             required.setVisible(true);
 220  0
     }
 221  
 
 222  
     public void clearRequiredText(){
 223  0
         required.setText("");
 224  0
     }
 225  
 
 226  
     public Widget getFieldWidget(){
 227  0
             return fieldWidget;
 228  
     }
 229  
 
 230  
     public FlowPanel getFieldDetailsLayout(){
 231  0
             FlowPanel div = new FlowPanel();
 232  0
                 div.add(fieldTitle);
 233  0
                 div.add(instructions);
 234  0
                 div.addStyleName("ks-form-module-elements");
 235  0
                 return div;
 236  
     }
 237  
 
 238  
     public FlowPanel getFieldWidgetAreaLayout(){
 239  0
             FlowPanel div = new FlowPanel();
 240  0
             div.add(fieldWidget);
 241  0
             div.add(constraints);
 242  0
             div.addStyleName("ks-form-module-elements");
 243  0
             return div;
 244  
     }
 245  
     
 246  
     public boolean isRequired() {
 247  0
         return required.isVisible();
 248  
     }
 249  
 
 250  
     public void setRequired(boolean isRequired){
 251  0
             required.setVisible(isRequired);
 252  0
     }
 253  
 
 254  
     public void setInstructions(String text){
 255  0
             instructionText = text;
 256  0
             if(instructionText != null && !instructionText.trim().equals("")){
 257  0
                     instructions.setHTML(text);
 258  0
                     instructions.setVisible(true);
 259  
             }
 260  0
     }
 261  
 
 262  
     public void setConstraintText(String text){
 263  0
             if(text != null && !text.trim().equals("")){
 264  0
                     constraints.setHTML(text);
 265  0
                     constraints.setVisible(true);
 266  
             }
 267  0
     }
 268  
 
 269  
     public void setHelp(final String html){
 270  0
             if(html != null && !html.trim().equals("")){
 271  0
                     help.setVisible(true);
 272  0
                     help.setHoverHTML(html);
 273  
                     /*help.addClickHandler(new ClickHandler(){
 274  
 
 275  
                                 @Override
 276  
                                 public void onClick(ClickEvent event) {
 277  
                                         //TODO show actual help window
 278  
                                         Window.alert(html);
 279  
 
 280  
                                 }
 281  
                         });*/
 282  
             }
 283  
             else{
 284  0
                     help.setVisible(false);
 285  
             }
 286  0
     }
 287  
 
 288  
         public Widget getTitleWidget() {
 289  0
             return titlePanel.getTitleWidget();
 290  
         }
 291  
 
 292  
         public ValidationMessagePanel getValidationPanel() {
 293  0
                 return validationPanel;
 294  
         }
 295  
 
 296  
         public Panel getEncapsulatingPanel() {
 297  0
                 return parentPanel;
 298  
         }
 299  
 
 300  
         public String getFieldName() {
 301  0
                 return fieldName;
 302  
         }
 303  
 
 304  
         public void setErrorState(boolean error){
 305  0
                 if(error){
 306  0
                         fieldTitle.addStyleName("invalid");
 307  0
                         if(parentPanel != null){
 308  0
                                 parentPanel.addStyleName("error");
 309  
                         }
 310  0
                         else if(parentElement != null){
 311  0
                                 parentElement.setClassName("error");
 312  
                         }
 313  
 
 314  
                 }
 315  
                 else{
 316  0
                         fieldTitle.removeStyleName("invalid");
 317  0
                         if(parentPanel != null){
 318  0
                                 parentPanel.removeStyleName("error");
 319  
                         }
 320  0
                         else if(parentElement != null){
 321  0
                                 parentElement.setClassName("");
 322  
                         }
 323  
                 }
 324  
 
 325  0
         }
 326  
 
 327  
         public ErrorLevel processValidationResult(ValidationResultInfo vr) {
 328  0
                 ErrorLevel status = ErrorLevel.OK;
 329  
 
 330  0
                 if(vr.getLevel() == ErrorLevel.ERROR){
 331  0
                         String message = Application.getApplicationContext().getUILabel("validation", vr.getMessage());
 332  0
                         this.addValidationErrorMessage(message);
 333  
                         
 334  0
                         if(status.getLevel() < ErrorLevel.ERROR.getLevel()){
 335  0
                                 status = vr.getLevel();
 336  
                         }
 337  0
                 }
 338  0
                 else if(vr.getLevel() == ErrorLevel.WARN){
 339  0
                         if(status.getLevel() < ErrorLevel.WARN.getLevel()){
 340  0
                                 status = vr.getLevel();
 341  
                         }
 342  
                         //TODO does nothing on warn, warn is not currently used
 343  
                 }
 344  
                 else{
 345  
                         //TODO does nothing on ok, ok is not currently used
 346  
                 }
 347  0
                 return status;
 348  
         }
 349  
 
 350  
         public void addValidationErrorMessage(String text){
 351  0
                 if(validationPanel != null){
 352  
                         KSLabel message;
 353  0
                         if(fieldName != null && !fieldName.trim().equals("")){
 354  0
                                 message = new KSLabel(fieldName + " - " + text);
 355  
                         }
 356  
                         else{
 357  0
                                 message = new KSLabel(text);
 358  
                         }
 359  0
                         message.setStyleName("ks-form-validation-label");
 360  0
                         this.setErrorState(true);
 361  0
                         this.validationPanel.addMessage(message);
 362  
                 }
 363  0
         }
 364  
 
 365  
         public void clearValidationPanel(){
 366  0
                 this.setErrorState(false);
 367  0
                 if(validationPanel != null){
 368  0
                         this.validationPanel.clear();
 369  
                 }
 370  0
         }
 371  
 
 372  
         @Override
 373  
         public String getKey() {
 374  0
                 return fieldKey;
 375  
         }
 376  
 
 377  
         @Override
 378  
         public void setKey(String key) {
 379  0
             if(key == null){
 380  
                         //TODO better way to generate unique id here?
 381  0
                         key = HTMLPanel.createUniqueId();
 382  
                 }
 383  0
                 this.fieldKey = key;
 384  0
         }
 385  
 
 386  
         public void setTitleDescLineHeight(LineNum margin) {
 387  0
                 layout.removeStyleName("ks-form-module-single-line-margin");
 388  0
                 switch(margin){
 389  
                         case TRIPLE:
 390  0
                                 if(firstLineExists() && secondLineExists()){
 391  0
                                         layout.addStyleName("ks-form-module-single-line-margin");
 392  
                                 }
 393  0
                                 else if((firstLineExists() || secondLineExists())){
 394  0
                                         layout.addStyleName("ks-form-module-double-line-margin");
 395  
                                 }
 396  
                                 else{
 397  0
                                         layout.addStyleName("ks-form-module-triple-line-margin");
 398  
                                 }
 399  
 
 400  0
                                 break;
 401  
                         case DOUBLE:
 402  0
                                 if((firstLineExists() || secondLineExists())){
 403  0
                                         layout.addStyleName("ks-form-module-single-line-margin");
 404  
                                 }
 405  
                                 else{
 406  0
                                         layout.addStyleName("ks-form-module-double-line-margin");
 407  
                                 }
 408  0
                                 break;
 409  
                         case SINGLE:
 410  0
                                 layout.addStyleName("ks-form-module-single-line-margin");
 411  
                                 break;
 412  
                 }
 413  
 
 414  0
         }
 415  
 
 416  
         private boolean firstLineExists(){
 417  0
                 boolean exists = false;
 418  0
                 if((fieldName != null && !fieldName.equals("")) || required.isVisible() || help.isVisible()){
 419  0
                         exists = true;
 420  
                 }
 421  0
                 return exists;
 422  
         }
 423  
 
 424  
         private boolean secondLineExists(){
 425  0
                 boolean exists = false;
 426  0
                 if(instructions.isVisible()){
 427  0
                         exists = true;
 428  
                 }
 429  0
                 return exists;
 430  
         }
 431  
 
 432  
         public void hideLabel() {
 433  0
                 layout.removeStyleName("ks-form-module-double-line-margin");
 434  0
                 layout.removeStyleName("ks-form-module-triple-line-margin");
 435  0
                 layout.addStyleName("ks-form-module-single-line-margin");
 436  0
                 fieldTitle.setStyleName("accessibility-hidden");
 437  0
                 instructions.setVisible(false);
 438  0
                 labelShown = false;
 439  0
         }
 440  
         
 441  
         public boolean isLabelShown(){
 442  0
                 return labelShown;
 443  
         }
 444  
 }