001/*
002 *  Copyright 2012 GWT-Bootstrap
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.github.gwtbootstrap.client.ui;
017
018import com.github.gwtbootstrap.client.ui.base.AbstractTypography;
019import com.github.gwtbootstrap.client.ui.base.ComplexWidget;
020import com.google.gwt.uibinder.client.UiConstructor;
021import com.google.gwt.user.client.ui.HasText;
022
023//@formatter:off
024/**
025 * Heading with optional subtext.
026 * 
027 * <p>
028 * <h3>UiBinder Usage:</h3>
029 * 
030 * <pre>
031 * {@code 
032 * <b:Heading size="2" subtext="And I'm the subtext">I'm the heading</b:Heading>}
033 * </pre>
034 * Specifying the <code>size</code> is mandatory.
035 * </p>
036 *
037 * @since 2.0.4.0
038 * 
039 * @author Carlos Alexandro Becker
040 * @author Dominik Mayer
041 * 
042 * @see <a href="http://twitter.github.com/bootstrap/base-css.html#typography">Bootstrap documentation</a>
043 */
044//@formatter:on
045public class Heading extends ComplexWidget implements HasText {
046
047        private static final int HEADER_MINIMUM = 1;
048
049        private static final int HEADER_MAXIMUM = 6;
050
051        private Small small = new Small();
052
053        private String text;
054
055        /**
056         * Creates a new Heading of given size.
057         * 
058         * @param size
059         *            the size of the heading
060         */
061        public @UiConstructor
062        Heading(int size) {
063                super("h" + size);
064                if (size < HEADER_MINIMUM || size > HEADER_MAXIMUM)
065                        throw new IllegalArgumentException(
066                                        "The size of the header must be between 1 and 6.");
067
068                super.add(small);
069        }
070
071        /**
072         * Creates a new Heading of given size and text.
073         * 
074         * @param size
075         *            size of the heading
076         * @param text
077         *            text of the heading
078         */
079        public Heading(int size, String text) {
080                this(size);
081                setText(text);
082        }
083
084        /**
085         * Creates a new Heading of given size, text and subtext.
086         * 
087         * @param size
088         *            size of the heading
089         * @param text
090         *            text of the heading
091         * @param subtext
092         *            subtext of the heading
093         */
094        public Heading(int size, String text, String subtext) {
095                this(size, text);
096                setSubtext(subtext);
097        }
098
099        /**
100         * Sets the heading's subtext.
101         * 
102         * @param subtext
103         *            the heading's subtext
104         */
105        public void setSubtext(String subtext) {
106                small.setText(subtext);
107                redraw();
108        }
109
110        private void redraw() {
111                setText(this.text);
112        }
113
114        /**
115         * {@inheritDoc}
116         */
117        public void setText(String text) {
118                this.text = text;
119
120            // Add a space after the main heading text to get the same effect as Twitter Bootstrap.
121        // <h1>Some text <small>Some subtext</small></h1>
122        //              ^- Note the space
123                getElement().setInnerHTML(text + " " + small.toString());
124        }
125
126        /**
127         * {@inheritDoc}
128         */
129        public String getText() {
130                return text;
131        }
132
133        private class Small extends AbstractTypography {
134
135                public Small() {
136                        super("small");
137                }
138        }
139}