001    /**
002     * Copyright 2012 The Kuali Foundation Licensed under the
003     * Educational Community License, Version 2.0 (the "License"); you may
004     * not use this file except in compliance with the License. You may
005     * obtain a copy of the License at
006     *
007     * http://www.osedu.org/licenses/ECL-2.0
008     *
009     * Unless required by applicable law or agreed to in writing,
010     * software distributed under the License is distributed on an "AS IS"
011     * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012     * or implied. See the License for the specific language governing
013     * permissions and limitations under the License.
014     *
015     * Created by bobhurt on 8/23/12
016     */
017    package org.kuali.student.common.ui.krad.uif.element;
018    
019    import org.apache.commons.lang.StringUtils;
020    import org.kuali.rice.krad.uif.component.Component;
021    import org.kuali.rice.krad.uif.element.Action;
022    import org.kuali.rice.krad.uif.element.Image;
023    import org.kuali.rice.krad.uif.view.View;
024    
025    import java.text.MessageFormat;
026    
027    /**
028     * Field in the form of an image-only button that presents an action that
029     * can be taken on the UI such as submitting the form or invoking a script
030     *
031     * @author Kuali Student Team
032     */
033    public class ActionImageButton extends Action {
034    
035        private String imageSource;
036        private String imageSourceOnHover;
037    
038        @Override
039        public void performFinalize(View view, Object model, Component parent) {
040            super.performFinalize(view, model, parent);
041    
042            Image img = this.getActionImage();
043            if (null == img) {  // Spring config should always include "actionImage" property
044                throw new RuntimeException("Implementation of "+ this.getClass() +" did not instantiate \"actionImage\" property.");
045            }
046    
047            if (StringUtils.isBlank(img.getSource()) && StringUtils.isNotBlank(this.imageSource)) {
048                img.setSource(this.imageSource);
049            }
050    
051            if (StringUtils.isNotBlank(this.imageSourceOnHover)) {
052                String script = MessageFormat.format("ksButtonImageChanger(this,\"{0}\");", this.imageSourceOnHover);
053                if (StringUtils.isNotBlank(this.getOnMouseOverScript())) {
054                    script += this.getOnMouseOverScript();
055                }
056                this.setOnMouseOverScript(script);
057    
058                script = MessageFormat.format("ksButtonImageChanger(this,\"{0}\");", img.getSource());
059                if (StringUtils.isNotBlank(this.getOnMouseOutScript())) {
060                    script += this.getOnMouseOutScript();
061                }
062                this.setOnMouseOutScript(script);
063            }
064        }
065    
066        public String getImageSource() {
067            return imageSource;
068        }
069        public void setImageSource(String imageSource) {
070            this.imageSource = imageSource;
071        }
072    
073        public String getImageSourceOnHover() {
074            return imageSourceOnHover;
075        }
076        public void setImageSourceOnHover(String imageSourceOnHover) {
077            this.imageSourceOnHover = imageSourceOnHover;
078        }
079    
080    }