001 package org.gwtbootstrap3.client.ui;
002
003 /*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 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.ElementTags;
025 import org.gwtbootstrap3.client.ui.constants.IconType;
026 import org.gwtbootstrap3.client.ui.constants.Styles;
027
028 import com.google.gwt.dom.client.Document;
029 import com.google.gwt.dom.client.Element;
030 import com.google.gwt.dom.client.Style.Unit;
031 import com.google.gwt.event.dom.client.ChangeEvent;
032 import com.google.gwt.event.dom.client.ChangeHandler;
033 import com.google.gwt.event.dom.client.DomEvent;
034
035 /**
036 * @author Joshua Godi
037 * @author Steven Jardine
038 */
039 public class HelpBlock extends AbstractTextWidget {
040
041 private Element iconElement = null;
042
043 private IconType iconType = null;
044
045 public HelpBlock() {
046 super(Document.get().createSpanElement());
047 setStyleName(Styles.HELP_BLOCK);
048 addHandler(new ChangeHandler() {
049 @Override
050 public void onChange(ChangeEvent event) {
051 if (iconElement != null) {
052 iconElement.removeFromParent();
053 }
054 String html = getHTML();
055 if (html != null && !"".equals(html) && iconType != null) {
056 iconElement = createIconElement();
057 getElement().insertFirst(iconElement);
058 }
059 }
060 }, ChangeEvent.getType());
061 }
062
063 /**
064 * @return a new icon element. We only create this when {@link #iconElement} is null or the
065 * {@link #iconType} has changed.
066 */
067 protected Element createIconElement() {
068 Element e = Document.get().createElement(ElementTags.I);
069 e.addClassName(Styles.FONT_AWESOME_BASE);
070 e.addClassName(iconType.getCssName());
071 e.getStyle().setPaddingRight(5, Unit.PX);
072 return e;
073 }
074
075 /**
076 * @return the icon type
077 */
078 public IconType getIconType() {
079 return iconType;
080 }
081
082 /** {@inheritDoc} */
083 @Override
084 public void setHTML(String html) {
085 super.setHTML(html);
086 DomEvent.fireNativeEvent(Document.get().createChangeEvent(), this);
087 }
088
089 /**
090 * Sets the icon type. If the icon type changes programatically then the icon is removed from the dom and
091 * recreated.
092 *
093 * @param type the new icon type
094 */
095 public void setIconType(IconType type) {
096 IconType prevType = iconType;
097 iconType = type;
098 if (iconType != prevType && iconElement != null) {
099 iconElement.removeFromParent();
100 iconElement = null;
101 DomEvent.fireNativeEvent(Document.get().createChangeEvent(), this);
102 }
103 }
104
105 /** {@inheritDoc} */
106 @Override
107 public void setText(String text) {
108 super.setText(text);
109 DomEvent.fireNativeEvent(Document.get().createChangeEvent(), this);
110 }
111
112 }