Coverage Report - org.kuali.student.common.ui.client.widgets.field.layout.layouts.FieldLayout
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldLayout
0%
0/133
0%
0/62
1.943
 
 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.layouts;
 17  
 
 18  
 import java.util.HashMap;
 19  
 import java.util.Iterator;
 20  
 import java.util.LinkedHashMap;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle;
 24  
 import org.kuali.student.common.ui.client.configurable.mvc.sections.WarnContainer;
 25  
 import org.kuali.student.common.ui.client.widgets.field.layout.button.ButtonLayout;
 26  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton;
 27  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton.AbbrButtonType;
 28  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrPanel;
 29  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.FieldElement;
 30  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.SpanPanel;
 31  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 32  
 
 33  
 import com.google.gwt.user.client.ui.FlowPanel;
 34  
 import com.google.gwt.user.client.ui.HTML;
 35  
 import com.google.gwt.user.client.ui.Widget;
 36  
 
 37  
 /**
 38  
  * Abstract class for field layouts.  Areas which contain fields for user data entry should use an
 39  
  * implementation of this class for visual and behavior consistency
 40  
  * 
 41  
  * @author Kuali Student Team
 42  
  *
 43  
  */
 44  0
 public abstract class FieldLayout extends FlowPanel implements FieldLayoutComponent{
 45  0
         protected Map<String, FieldElement> fieldMap = new HashMap<String, FieldElement>();
 46  0
         protected Map<String, FieldLayout> layoutMap = new HashMap<String, FieldLayout>();
 47  0
         protected LinkedHashMap<String, Widget> drawOrder = new LinkedHashMap<String, Widget>();
 48  0
         protected SpanPanel instructions = new SpanPanel();
 49  0
         protected WarnContainer message = new WarnContainer(false);
 50  
         protected FieldLayout parentLayout;
 51  0
         protected boolean hasValidation = false;
 52  
         private HTML messagePanel;
 53  0
         private static int generatedKeyNum = 0;
 54  
         private String key;
 55  
         private ButtonLayout buttonLayout;
 56  0
         protected SectionTitle layoutTitle = null;
 57  0
         private AbbrButton help = null;
 58  
 
 59  
 
 60  
         private static String getNextId() {
 61  0
                 return "fieldComponent" + (generatedKeyNum++);
 62  
         }
 63  
 
 64  
         /**
 65  
          * Add or remove underline from the layout's title
 66  
          * @param underline
 67  
          */
 68  
         public void underlineTitle(boolean underline){
 69  0
                 if(layoutTitle != null){
 70  0
                         if(underline){
 71  0
                                 layoutTitle.addStyleName("header-underline");
 72  
                         }
 73  
                         else{
 74  0
                                 layoutTitle.removeStyleName("header-underline");
 75  
                         }
 76  
                 }
 77  0
         }
 78  
 
 79  
         public FieldLayout getParentLayout() {
 80  0
                 return parentLayout;
 81  
         }
 82  
 
 83  
         protected void setParentLayout(FieldLayout parentLayout) {
 84  0
                 this.parentLayout = parentLayout;
 85  0
         }
 86  
 
 87  
         /**
 88  
          * Adds a field to this layout - how it is added in the ui is up to the implementation
 89  
          * of addFieldToLayout
 90  
          * @param field
 91  
          * @return
 92  
          */
 93  
         public String addField(FieldElement field){
 94  0
                 String key = null;
 95  0
                 if(field != null){
 96  0
                         if(field.getKey() == null){
 97  0
                                 key = getNextId();
 98  0
                                 field.setKey(key);
 99  
                         }
 100  
                         else{
 101  0
                                 key = field.getKey();
 102  
                         }
 103  0
                         fieldMap.put(key, field);
 104  0
                         drawOrder.put(key, field);
 105  0
                         addFieldToLayout(field);
 106  
                 }
 107  0
                 return key;
 108  
         }
 109  
 
 110  
         /**
 111  
          * Adds a layout as a child of this layout - how it is added in the ui is up to the implementation
 112  
          * of addLayoutToLayout
 113  
          * @param layout
 114  
          * @return key to identify the layout added
 115  
          */
 116  
         public String addLayout(FieldLayout layout){
 117  0
                 String key = null;
 118  0
                 if(layout != null){
 119  0
                         if(layout.getKey() == null){
 120  0
                                 key = getNextId();
 121  0
                                 layout.setKey(key);
 122  
                         }
 123  
                         else{
 124  0
                                 key = layout.getKey();
 125  
                         }
 126  0
                         layoutMap.put(key, layout);
 127  0
                         drawOrder.put(key, layout);
 128  0
                         addLayoutToLayout(layout);
 129  
                 }
 130  0
                 return key;
 131  
         }
 132  
 
 133  
         /**
 134  
          * Adds a widget to the layout - how it is added in the ui is up to the implementation of addWidgetToLayout
 135  
          * @param widget
 136  
          * @return key to identify the widget added
 137  
          */
 138  
         public String addWidget(Widget widget){
 139  0
                 return this.addWidget(null, widget);
 140  
         }
 141  
 
 142  
         /**
 143  
          * Adds a widget to the layout - how it is added in the ui is up to the implementation of addWidgetToLayout
 144  
          * @param key
 145  
          * @param widget
 146  
          * @return key to identify the widget added
 147  
          */
 148  
         public String addWidget(String key, Widget widget){
 149  0
                 if(widget != null){
 150  0
                         if(key == null){
 151  0
                                 key = getNextId();
 152  
                         }
 153  0
                         drawOrder.put(key, widget);
 154  0
                         addWidgetToLayout(widget);
 155  
                 }
 156  
                 else{
 157  0
                         key = null;
 158  
                 }
 159  0
                 return key;
 160  
         }
 161  
 
 162  
         /**
 163  
          * Remove an element of this layout by key
 164  
          * @param key
 165  
          * @return
 166  
          */
 167  
         public boolean removeLayoutElement(String key){
 168  0
                 Widget w = drawOrder.get(key);
 169  0
                 if(w != null){
 170  
                         //removeElementFromLayout(w);
 171  0
                         fieldMap.remove(key);
 172  0
                         layoutMap.remove(key);
 173  0
                         drawOrder.remove(key);
 174  0
                         if(w instanceof FieldLayoutComponent){
 175  0
                                 removeFieldLayoutComponentFromLayout((FieldLayoutComponent)w);
 176  
                         }
 177  
                         else{
 178  0
                                 removeWidgetFromLayout(w);
 179  
                         }
 180  0
                         return true;
 181  
                 }
 182  
                 else{
 183  0
                         return false;
 184  
                 }
 185  
         }
 186  
 
 187  
         /**
 188  
          * Remove an element of this layout by widget reference
 189  
          * @param widget
 190  
          * @return
 191  
          */
 192  
         public boolean removeLayoutElement(Widget widget){
 193  0
                 if(drawOrder.containsValue(widget)){
 194  0
                         Iterator<String> it = drawOrder.keySet().iterator();
 195  0
                         String matchedKey = null;
 196  0
                         while(it.hasNext()){
 197  0
                                 String key = it.next();
 198  0
                                 Widget w = drawOrder.get(key);
 199  0
                                 if(w.equals(widget)){
 200  0
                                         matchedKey = key;
 201  0
                                         break;
 202  
                                 }
 203  0
                         }
 204  0
                         fieldMap.remove(matchedKey);
 205  0
                         layoutMap.remove(matchedKey);
 206  0
                         drawOrder.remove(matchedKey);
 207  0
                         if(widget instanceof FieldLayoutComponent){
 208  0
                                 removeFieldLayoutComponentFromLayout((FieldLayoutComponent)widget);
 209  
                         }
 210  
                         else{
 211  0
                                 removeWidgetFromLayout(widget);
 212  
                         }
 213  0
                         return true;
 214  
                 }
 215  
                 else{
 216  0
                         return false;
 217  
                 }
 218  
         }
 219  
 
 220  
         /**
 221  
          * Sets the instructions for this layout, whether or not they appear or where they appear is up
 222  
          * to the layout's implementation
 223  
          * @param instructions
 224  
          */
 225  
         public void setInstructions(String instructions) {
 226  0
                 if(instructions != null && !instructions.equals("")){
 227  0
                         this.instructions.addStyleName("ks-section-instuctions");
 228  0
                         this.instructions.setHTML(instructions);
 229  0
                         this.instructions.setVisible(true);
 230  
                 }
 231  
                 else{
 232  0
                         this.instructions.setVisible(false);
 233  
                 }
 234  0
         }
 235  
 
 236  
         /**
 237  
          * Sets a message for this layout, whether or not it appears or where they appears is up
 238  
          * to the layout's implementation
 239  
          * @param html
 240  
          * @param show
 241  
          */
 242  
         public void setMessage(String html, boolean show) {
 243  0
                 if(messagePanel == null){
 244  0
                         messagePanel = new HTML(html);
 245  0
                         message.addWarnWidget(messagePanel);
 246  
                 }
 247  
                 else{
 248  0
                         messagePanel.setHTML(html);
 249  
                 }
 250  0
                 message.setVisible(show);
 251  0
                 message.showWarningLayout(show);
 252  0
         }
 253  
 
 254  
         public void showMessage(boolean show) {
 255  0
                 message.setVisible(show);
 256  0
                 message.showWarningLayout(show);
 257  0
         }
 258  
         
 259  
         public WarnContainer getMessageWarnContainer(){
 260  0
                 return message;
 261  
         }
 262  
 
 263  
         @Override
 264  
         public String getKey() {
 265  0
                 return key;
 266  
         }
 267  
 
 268  
         @Override
 269  
         public void setKey(String layoutKey) {
 270  0
                 key = layoutKey;
 271  0
         }
 272  
 
 273  
         public FieldElement getFieldElement(String key){
 274  0
                 return fieldMap.get(key);
 275  
         }
 276  
 
 277  
         public FieldLayout getFieldLayout(String key){
 278  0
                 return layoutMap.get(key);
 279  
         }
 280  
 
 281  
         public Widget getWidget(String key){
 282  0
                 return drawOrder.get(key);
 283  
         }
 284  
 
 285  
         public abstract void addFieldToLayout(FieldElement field);
 286  
         public abstract void addLayoutToLayout(FieldLayout layout);
 287  
         public abstract void addWidgetToLayout(Widget widget);
 288  
         public abstract void removeWidgetFromLayout(Widget widget);
 289  
         public abstract void removeFieldLayoutComponentFromLayout(FieldLayoutComponent component);
 290  
 
 291  
         /**
 292  
          * Processes the validation result for the FieldElement specified by key, which will likely show
 293  
          * the validation message next to the field
 294  
          * @param fieldElementKey
 295  
          * @param validationResult
 296  
          */
 297  
         public void processValidationResults(String fieldElementKey, ValidationResultInfo validationResult){
 298  0
                 FieldElement field = fieldMap.get(fieldElementKey);
 299  0
                 if(field != null && hasValidation){
 300  0
                         field.processValidationResult(validationResult);
 301  
                 }
 302  0
         }
 303  
 
 304  
         public void addValidationErrorMessage(String fieldElementKey, String message){
 305  0
                 FieldElement field = fieldMap.get(fieldElementKey);
 306  0
                 if(field != null && hasValidation){
 307  0
                         field.addValidationErrorMessage(message);
 308  
                 }
 309  0
         }
 310  
 
 311  
         /**
 312  
          * Clear all validation errors in this layout and child layouts
 313  
          */
 314  
         public void clearValidationErrors(){
 315  
                 //fieldMap.
 316  0
                 for(FieldElement e: fieldMap.values()){
 317  0
                         e.clearValidationErrors();
 318  
                 }
 319  0
                 for(FieldLayout layout: layoutMap.values()){
 320  0
                         layout.clearValidationErrors();
 321  
                 }
 322  0
         }
 323  
 
 324  
         /**
 325  
          * Clear all validation errors in this layout and child layouts
 326  
          */
 327  
         public void clearValidationWarnings(){
 328  
                 //fieldMap.
 329  0
                 for(FieldElement e: fieldMap.values()){
 330  0
                         e.clearValidationWarnings();
 331  
                 }
 332  0
                 for(FieldLayout layout: layoutMap.values()){
 333  0
                         layout.clearValidationWarnings();
 334  
                 }
 335  0
         }
 336  
         
 337  
         public abstract void setLayoutTitle(SectionTitle layoutTitle);
 338  
 
 339  
         public SectionTitle getLayoutTitle() {
 340  0
                 return layoutTitle;
 341  
         }
 342  
 
 343  
         /**
 344  
          * Adds a button layout to this layout which will appear below all layouts and fields
 345  
          * @param buttonLayout
 346  
          */
 347  
         public void addButtonLayout(ButtonLayout buttonLayout){
 348  0
                 this.buttonLayout = buttonLayout;
 349  0
                 addButtonLayoutToLayout(buttonLayout);
 350  0
         }
 351  
 
 352  
         public ButtonLayout getButtonLayout(){
 353  0
                 return buttonLayout;
 354  
         }
 355  
 
 356  
         public abstract void addButtonLayoutToLayout(ButtonLayout buttonLayout);
 357  
         
 358  
         /**
 359  
          * Sets a help message hover icon to display the html passed in and will appear next to the layout's
 360  
          * title, if shown
 361  
          * @param html
 362  
          */
 363  
         public void setHelp(String html){
 364  0
                 if(layoutTitle != null){
 365  0
                         if(help == null){
 366  0
                                 help = new AbbrButton(AbbrButtonType.HELP);
 367  0
                                 layoutTitle.add(help);
 368  
                         }
 369  
                         
 370  0
                     if(html != null && !html.trim().equals("")){
 371  0
                             help.setVisible(true);
 372  0
                             help.setHoverHTML(html);
 373  
                     }
 374  
                     else{
 375  0
                             help.setVisible(false);
 376  
                     }
 377  
                     
 378  
                     
 379  
                 }
 380  0
         }
 381  
         
 382  
         public void setRequired(AbbrPanel required){
 383  0
                 if(layoutTitle != null && required != null){
 384  0
                         layoutTitle.add(required);
 385  
                 }
 386  0
         }
 387  
 
 388  
 }