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.HasSubText;
024 import org.gwtbootstrap3.client.ui.constants.Styles;
025 import org.gwtbootstrap3.client.ui.html.Div;
026
027 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
028 import com.google.gwt.user.client.ui.HasText;
029
030 /**
031 * Page header with optional subtext
032 * <p/>
033 * <h3>UiBinder example</h3>
034 * <p/>
035 * <pre>
036 * {@code
037 * <b:PageHeader subText="Some subtext">Page header title</b:PageHeader>
038 * }
039 * </pre>
040 *
041 * @author Sven Jacobs
042 * @author Joshua Godi
043 */
044 public class PageHeader extends Div implements HasText, HasSubText {
045
046 private String heading;
047 private String subText;
048
049 public PageHeader() {
050 setStyleName(Styles.PAGE_HEADER);
051 }
052
053 @Override
054 public void setText(final String text) {
055 heading = text;
056 render();
057 }
058
059 @Override
060 public String getText() {
061 return heading;
062 }
063
064 @Override
065 public void setSubText(final String subText) {
066 this.subText = subText;
067 render();
068 }
069
070 @Override
071 public String getSubText() {
072 return subText;
073 }
074
075 private void render() {
076 final SafeHtmlBuilder builder = new SafeHtmlBuilder();
077
078 builder.appendHtmlConstant("<h1>");
079 builder.appendEscaped(heading == null ? "" : heading);
080
081 if (subText != null && !subText.isEmpty()) {
082 builder.appendEscaped(" ");
083 builder.appendHtmlConstant("<small>");
084 builder.appendEscaped(subText);
085 builder.appendHtmlConstant("</small>");
086 }
087
088 builder.appendHtmlConstant("</h1>");
089
090 getElement().setInnerSafeHtml(builder.toSafeHtml());
091 }
092 }