001 /** 002 * Copyright 2005-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krad.uif.field; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.kuali.rice.core.api.exception.RiceRemoteServiceConnectionException; 020 import org.kuali.rice.core.api.exception.RiceRuntimeException; 021 import org.kuali.rice.krad.uif.UifConstants.Position; 022 import org.kuali.rice.krad.uif.component.ComponentSecurity; 023 import org.kuali.rice.krad.uif.view.View; 024 import org.kuali.rice.krad.uif.component.Component; 025 import org.kuali.rice.krad.uif.component.ComponentBase; 026 import org.kuali.rice.krad.uif.util.ComponentFactory; 027 028 import java.util.List; 029 030 /** 031 * Base class for <code>Field</code> implementations 032 * 033 * <p> 034 * Sets the component type name so that all field templates have a fixed 035 * contract 036 * </p> 037 * 038 * <p> 039 * Holds a nested <code>LabelField</code> with configuration for rendering the 040 * label and configuration on label placement. 041 * </p> 042 * 043 * @author Kuali Rice Team (rice.collab@kuali.org) 044 */ 045 public class FieldBase extends ComponentBase implements Field { 046 private static final long serialVersionUID = -5888414844802862760L; 047 048 private String shortLabel; 049 private LabelField labelField; 050 051 private Position labelPlacement; 052 053 private boolean labelFieldRendered; 054 055 public FieldBase() { 056 labelFieldRendered = false; 057 labelPlacement = Position.LEFT; 058 } 059 060 /** 061 * The following initialization is performed: 062 * 063 * <ul> 064 * </ul> 065 * 066 * @see org.kuali.rice.krad.uif.component.ComponentBase#performInitialization(org.kuali.rice.krad.uif.view.View, java.lang.Object) 067 */ 068 @Override 069 public void performInitialization(View view, Object model) { 070 super.performInitialization(view, model); 071 } 072 073 /** 074 * The following finalization is performed: 075 * 076 * <ul> 077 * <li>Set the labelForComponentId to this component id</li> 078 * <li>Set the label text on the label field from the field's label property 079 * </li> 080 * <li>Set the render property on the label's required message field if this 081 * field is marked as required</li> 082 * <li>If label placement is right, set render colon to false</li> 083 * </ul> 084 * 085 * @see org.kuali.rice.krad.uif.component.ComponentBase#performFinalize(org.kuali.rice.krad.uif.view.View, 086 * java.lang.Object, org.kuali.rice.krad.uif.component.Component) 087 */ 088 @Override 089 public void performFinalize(View view, Object model, Component parent) { 090 super.performFinalize(view, model, parent); 091 092 if (labelField != null) { 093 labelField.setLabelForComponentId(this.getId()); 094 095 if ((getRequired() != null) && getRequired().booleanValue()) { 096 labelField.getRequiredMessageField().setRender(true); 097 } else { 098 setRequired(new Boolean(false)); 099 labelField.getRequiredMessageField().setRender(true); 100 String prefixStyle = ""; 101 if (StringUtils.isNotBlank(labelField.getRequiredMessageField().getStyle())) { 102 prefixStyle = labelField.getRequiredMessageField().getStyle(); 103 } 104 labelField.getRequiredMessageField().setStyle(prefixStyle + ";" + "display: none;"); 105 } 106 107 if (labelPlacement.equals(Position.RIGHT)) { 108 labelField.setRenderColon(false); 109 } 110 111 if (labelPlacement.equals(Position.TOP) || labelPlacement.equals(Position.BOTTOM)){ 112 labelField.addStyleClass("uif-labelBlock"); 113 } 114 } 115 } 116 117 /** 118 * @see org.kuali.rice.krad.uif.component.Component#getComponentTypeName() 119 */ 120 @Override 121 public final String getComponentTypeName() { 122 return "field"; 123 } 124 125 /** 126 * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle() 127 */ 128 @Override 129 public List<Component> getComponentsForLifecycle() { 130 List<Component> components = super.getComponentsForLifecycle(); 131 132 components.add(labelField); 133 134 return components; 135 } 136 137 /** 138 * @see org.kuali.rice.krad.uif.field.Field#getLabel() 139 */ 140 public String getLabel() { 141 if (labelField != null) { 142 return labelField.getLabelText(); 143 } 144 145 return null; 146 } 147 148 /** 149 * @see org.kuali.rice.krad.uif.field.Field#setLabel(java.lang.String) 150 */ 151 public void setLabel(String label) { 152 if (StringUtils.isNotBlank(label) && labelField == null) { 153 labelField = ComponentFactory.getLabelField(); 154 } 155 156 if (labelField != null) { 157 labelField.setLabelText(label); 158 } 159 } 160 161 /** 162 * @see org.kuali.rice.krad.uif.field.Field#getShortLabel() 163 */ 164 public String getShortLabel() { 165 return this.shortLabel; 166 } 167 168 /** 169 * @see org.kuali.rice.krad.uif.field.Field#setShortLabel(java.lang.String) 170 */ 171 public void setShortLabel(String shortLabel) { 172 this.shortLabel = shortLabel; 173 } 174 175 /** 176 * Sets whether the label should be displayed 177 * 178 * <p> 179 * Convenience method for configuration that sets the render indicator on 180 * the fields <code>LabelField</code> instance 181 * </p> 182 * 183 * @param showLabel boolean true if label should be displayed, false if the label 184 * should not be displayed 185 */ 186 public void setShowLabel(boolean showLabel) { 187 if (labelField != null) { 188 labelField.setRender(showLabel); 189 } 190 } 191 192 /** 193 * @see org.kuali.rice.krad.uif.field.Field#getLabelField() 194 */ 195 public LabelField getLabelField() { 196 return this.labelField; 197 } 198 199 /** 200 * @see org.kuali.rice.krad.uif.field.Field#setLabelField(org.kuali.rice.krad.uif.field.LabelField) 201 */ 202 public void setLabelField(LabelField labelField) { 203 this.labelField = labelField; 204 } 205 206 /** 207 * Indicates where the label is placed in relation to the field (valid options are 208 * LEFT, RIGHT, BOTTOM, and TOP 209 * 210 * @return Position position of label 211 */ 212 public Position getLabelPlacement() { 213 return this.labelPlacement; 214 } 215 216 /** 217 * Setter for the label's position in relation to the field (control if editable) 218 * 219 * @param labelPlacement 220 */ 221 public void setLabelPlacement(Position labelPlacement) { 222 this.labelPlacement = labelPlacement; 223 } 224 225 /** 226 * @see org.kuali.rice.krad.uif.field.Field#isLabelFieldRendered() 227 */ 228 public boolean isLabelFieldRendered() { 229 return this.labelFieldRendered; 230 } 231 232 /** 233 * @see org.kuali.rice.krad.uif.field.Field#setLabelFieldRendered(boolean) 234 */ 235 public void setLabelFieldRendered(boolean labelFieldRendered) { 236 this.labelFieldRendered = labelFieldRendered; 237 } 238 239 /** 240 * Field Security object that indicates what authorization (permissions) exist for the field 241 * 242 * @return FieldSecurity instance 243 */ 244 @Override 245 public FieldSecurity getComponentSecurity() { 246 return (FieldSecurity) super.getComponentSecurity(); 247 } 248 249 /** 250 * Override to assert a {@link FieldSecurity} instance is set 251 * 252 * @param componentSecurity - instance of FieldSecurity 253 */ 254 @Override 255 public void setComponentSecurity(ComponentSecurity componentSecurity) { 256 if (!(componentSecurity instanceof FieldSecurity)) { 257 throw new RiceRuntimeException("Component security for Field should be instance of FieldSecurity"); 258 } 259 260 super.setComponentSecurity(componentSecurity); 261 } 262 263 @Override 264 protected Class<? extends ComponentSecurity> getComponentSecurityClass() { 265 return FieldSecurity.class; 266 } 267 }