001    package org.gwtbootstrap3.client.ui;
002    
003    /*
004     * #%L
005     * GwtBootstrap3
006     * %%
007     * Copyright (C) 2013 - 2015 GwtBootstrap3
008     * %%
009     * Licensed under the Apache License, Version 2.0 (the "License");
010     * you may not use this file except in compliance with the License.
011     * You may obtain a copy of the License at
012     * 
013     *      http://www.apache.org/licenses/LICENSE-2.0
014     * 
015     * Unless required by applicable law or agreed to in writing, software
016     * distributed under the License is distributed on an "AS IS" BASIS,
017     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018     * See the License for the specific language governing permissions and
019     * limitations under the License.
020     * #L%
021     */
022    
023    import org.gwtbootstrap3.client.ui.base.AbstractTextWidget;
024    import org.gwtbootstrap3.client.ui.constants.Attributes;
025    import org.gwtbootstrap3.client.ui.constants.ElementTags;
026    import org.gwtbootstrap3.client.ui.constants.IconType;
027    import org.gwtbootstrap3.client.ui.constants.Styles;
028    
029    import com.google.gwt.dom.client.Document;
030    import com.google.gwt.dom.client.Element;
031    import com.google.gwt.dom.client.Style;
032    import com.google.gwt.dom.client.Style.Unit;
033    import com.google.gwt.event.dom.client.ChangeEvent;
034    import com.google.gwt.event.dom.client.ChangeHandler;
035    import com.google.gwt.event.dom.client.DomEvent;
036    
037    /**
038     * @author Sven Jacobs
039     * @author Steven Jardine
040     */
041    public class FormLabel extends AbstractTextWidget {
042    
043        private Element iconElement = null;
044    
045        private boolean showRequiredIndicator = false;
046    
047        /**
048         * Constructor.
049         */
050        public FormLabel() {
051            super(Document.get().createLabelElement());
052            setStyleName(Styles.CONTROL_LABEL);
053            addHandler(new ChangeHandler() {
054                @Override
055                public void onChange(ChangeEvent event) {
056                    if (iconElement != null) {
057                        iconElement.removeFromParent();
058                    }
059                    String html = getHTML();
060                    if (showRequiredIndicator && html != null && !"".equals(html)) {
061                        iconElement = createIconElement();
062                        getElement().appendChild(iconElement);
063                    }
064                }
065            }, ChangeEvent.getType());
066        }
067    
068        /**
069         * @return a new icon element. We only create this when {@link #iconElement} is null or the
070         *         {@link #showRequiredIndicator} has changed.
071         */
072        protected Element createIconElement() {
073            Element e = Document.get().createElement(ElementTags.I);
074            e.addClassName(Styles.FONT_AWESOME_BASE);
075            e.addClassName(IconType.STAR.getCssName());
076            Style s = e.getStyle();
077            s.setFontSize(6, Unit.PX);
078            s.setPaddingLeft(2, Unit.PX);
079            s.setPaddingRight(5, Unit.PX);
080            s.setColor("#b94a48");
081            Element sup = Document.get().createElement("sup");
082            sup.appendChild(e);
083            return sup;
084        }
085    
086        /**
087         * @return does this label show required?
088         */
089        public boolean getShowRequiredIndicator() {
090            return showRequiredIndicator;
091        }
092    
093        public void setFor(final String f) {
094            if (f != null) {
095                getElement().setAttribute(Attributes.FOR, f);
096            } else {
097                getElement().removeAttribute(Attributes.FOR);
098            }
099        }
100    
101        /** {@inheritDoc} */
102        @Override
103        public void setHTML(final String html) {
104            super.setHTML(html);
105            DomEvent.fireNativeEvent(Document.get().createChangeEvent(), this);
106        }
107    
108        /**
109         * @param should this label show as required?
110         */
111        public void setShowRequiredIndicator(boolean required) {
112            this.showRequiredIndicator = required;
113            DomEvent.fireNativeEvent(Document.get().createChangeEvent(), this);
114        }
115    
116        /** {@inheritDoc} */
117        @Override
118        public void setText(String text) {
119            super.setText(text);
120            DomEvent.fireNativeEvent(Document.get().createChangeEvent(), this);
121        }
122    
123    }