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.ComplexWidget;
024 import org.gwtbootstrap3.client.ui.base.HasAlignment;
025 import org.gwtbootstrap3.client.ui.base.HasEmphasis;
026 import org.gwtbootstrap3.client.ui.base.HasSubText;
027 import org.gwtbootstrap3.client.ui.base.helper.StyleHelper;
028 import org.gwtbootstrap3.client.ui.constants.Alignment;
029 import org.gwtbootstrap3.client.ui.constants.Emphasis;
030 import org.gwtbootstrap3.client.ui.constants.HeadingSize;
031 import org.gwtbootstrap3.client.ui.constants.Styles;
032 import org.gwtbootstrap3.client.ui.html.Small;
033 import org.gwtbootstrap3.client.ui.html.Text;
034
035 import com.google.gwt.dom.client.Document;
036 import com.google.gwt.uibinder.client.UiConstructor;
037 import com.google.gwt.user.client.ui.HasText;
038 import com.google.gwt.user.client.ui.HasWidgets;
039
040 /**
041 * Represents a Heading tag, has an optional subtext.
042 * <p/>
043 * <h3>Bootstrap's Documentation</h3>
044 * <a href="http://getbootstrap.com/css/#type">Typography</a>
045 * <p/>
046 * <h3>Usage in UiBinder</h3>
047 * <p/>
048 * <pre>
049 * {@code
050 * <b:Heading size="H1">
051 * <b:Text text="Heading"/>
052 * <b:Small text=" subtext"/>
053 * </b:Heading>
054 *
055 * <b:Heading size="H1" text="Heading Text" subText="Subtext Text"/>
056 * <b:Heading size="H1" subText="Subtext Text" text="Heading Text"/>
057 *
058 * <b:Heading size="H1">
059 * <b:Icon type="..."/>
060 * <b:Text text="Heading with icon"/>
061 * </b:Heading>
062 *
063 * <b:Heading size="H1">
064 * <b:Icon type="..."/>
065 * <b:Text text="Heading with icon"/>
066 * <b:Small text=" subtext"/>
067 * </b:Heading>
068 * }
069 * </pre>
070 * <p/>
071 * <h3>Usage in Java</h3>
072 * <p/>
073 * <pre>
074 * Heading h1 = new Heading(1, "Heading Text");
075 * h1.setSubText("Subtext Text"); // optional
076 * </pre>
077 *
078 * @author Sven Jacobs
079 * @author Joshua Godi
080 */
081 public class Heading extends ComplexWidget implements HasWidgets, HasText, HasEmphasis, HasAlignment, HasSubText {
082
083 private final Small subText = new Small();
084 private final Text text = new Text();
085
086 /**
087 * Creates a Heading with the passed in size.
088 *
089 * @param size size of the heading
090 */
091 @UiConstructor
092 public Heading(final HeadingSize size) {
093 setElement(Document.get().createHElement(size.getHeadingSize()));
094 }
095
096 /**
097 * Creates a Heading with the passed in size and text.
098 *
099 * @param size size of the heading
100 * @param text text for the heading
101 */
102 public Heading(final HeadingSize size, final String text) {
103 this(size);
104 setText(text);
105 }
106
107 /**
108 * Creates a Heading with the passed in size and text.
109 *
110 * @param size size of the heading
111 * @param text text for the heading
112 * @param subText subtext for the heading
113 */
114 public Heading(final HeadingSize size, final String text, final String subText) {
115 this(size, text);
116 setSubText(subText);
117 }
118
119 /**
120 * Sets the subtext for the heading (wrapped in a Small tag).
121 * <p/>
122 * When using the setter for this, the subtext will be added after the text
123 *
124 * @param subText the subtext of the heading
125 */
126 @Override
127 public void setSubText(final String subText) {
128 // Force a space between the heading and the subText
129 this.subText.setText(" " + subText);
130 add(this.subText);
131 }
132
133 /**
134 * Returns the subtext of the heading.
135 *
136 * @return subtext of the heading
137 */
138 @Override
139 public String getSubText() {
140 return subText.getText();
141 }
142
143 /**
144 * {@inheritDoc}
145 */
146 @Override
147 public String getText() {
148 return text.getText();
149 }
150
151 /**
152 * {@inheritDoc}
153 */
154 @Override
155 public void setText(final String text) {
156 this.text.setText(text);
157 insert(this.text, 0);
158 }
159
160 /**
161 * {@inheritDoc}
162 */
163 @Override
164 public void setEmphasis(final Emphasis emphasis) {
165 StyleHelper.addUniqueEnumStyleName(this, Emphasis.class, emphasis);
166 }
167
168 /**
169 * {@inheritDoc}
170 */
171 @Override
172 public Emphasis getEmphasis() {
173 return Emphasis.fromStyleName(getStyleName());
174 }
175
176 /**
177 * {@inheritDoc}
178 */
179 @Override
180 public void setAlignment(final Alignment alignment) {
181 StyleHelper.addUniqueEnumStyleName(this, Alignment.class, alignment);
182 }
183
184 /**
185 * {@inheritDoc}
186 */
187 @Override
188 public Alignment getAlignment() {
189 return Alignment.fromStyleName(getStyleName());
190 }
191
192 /**
193 * {@inheritDoc}
194 */
195 @Override
196 protected void onAttach() {
197 super.onAttach();
198
199 // Adding styles to the heading depending on the parent
200 if (getParent() != null) {
201 if (getParent() instanceof LinkedGroupItem) {
202 addStyleName(Styles.LIST_GROUP_ITEM_HEADING);
203 } else if (getParent() instanceof PanelHeader) {
204 addStyleName(Styles.PANEL_TITLE);
205 } else if (getParent() instanceof MediaBody) {
206 addStyleName(Styles.MEDIA_HEADING);
207 }
208 }
209 }
210 }