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     */
016    package com.github.gwtbootstrap.client.ui;
017    
018    import com.github.gwtbootstrap.client.ui.base.AbstractTypography;
019    import com.github.gwtbootstrap.client.ui.constants.Alignment;
020    import com.google.gwt.user.client.DOM;
021    
022    //@formatter:off
023    /**
024     * Left or right aligned blockquote with optional source.
025     *
026     * <p>
027     * <h3>UiBinder Usage:</h3>
028     * 
029     * <pre>
030     * {@code <b:Blockquote source="Leonard McCoy">He's dead, Jim</b:Blockquote>}
031     * </pre>
032     * 
033     * All arguments are optional.
034     * </p>
035     * 
036     * @since 2.0.4.0
037     * 
038     * @author Carlos Alexandro Becker
039     * 
040     * @see <a href="http://twitter.github.com/bootstrap/base-css.html#typography">Bootstrap documentation</a>
041     */
042    //@formatter:on
043    public class Blockquote extends AbstractTypography {
044    
045            /**
046             * Creates an empty Blockquote.
047             */
048            public Blockquote() {
049                    setElement(DOM.createElement("blockquote"));
050            }
051    
052            /**
053             * Creates a Blockquote of the given text.
054             * 
055             * @param text
056             *            the text of the quote
057             */
058            public Blockquote(String text) {
059                    setText(text);
060            }
061    
062            /**
063             * Creates a Blockquote of the given text, attributed to the given source.
064             * 
065             * @param text
066             *            the text of the quote
067             * @param source
068             *            the source of the quote
069             */
070            public Blockquote(String text, String source) {
071                    this(text);
072                    setSource(source);
073            }
074    
075            /**
076             * Creates a Blockquote of the given text and optionally pulls it to the
077             * right side.
078             * 
079             * @param text
080             *            text of the quote
081             * @param pullRight
082             *            <code>true</code> if the quote should be pulled to the right
083             *            side. Default: <code>false</code>
084             */
085            public Blockquote(String text, boolean pullRight) {
086                    setText(text);
087                    setPullRight(pullRight);
088            }
089    
090            /**
091             * Creates a Blockquote of the given text, attributed to the given source
092             * and optionally pulls it to the right side.
093             * 
094             * @param text
095             *            text of the quote
096             * @param source
097             *            source of the quote
098             * @param pullRight
099             *            <code>true</code> if the quote should be pulled to the right
100             *            side. Default: <code>false</code>
101             */
102            public Blockquote(String text, String source, boolean pullRight) {
103                    this(text);
104                    setSource(source);
105                    setPullRight(pullRight);
106            }
107    
108            /**
109             * Sets the source of the quote. This may be a person or the title of a
110             * book, song, ...
111             * 
112             * @param source
113             *            the source of the quote
114             */
115            public void setSource(String source) {
116                    getElement().appendChild(new SmallCite(source).getElement());
117            }
118    
119            /**
120             * Sets whether the quote should be aligned left or right.
121             * 
122             * @param pullright
123             *            <code>true</code> if the quote should be pulled to the right
124             *            side. Default: <code>false</code>
125             */
126            public void setPullRight(boolean pullright) {
127                    if (pullright)
128                            setStyle(Alignment.RIGHT);
129                    else
130                            setStyle(Alignment.NONE);
131            }
132    
133            /**
134             * Element that shows the source of the quote.
135             * 
136             * @since 2.0.4.0
137             * 
138             * @author Carlos Alexandro Becker
139             */
140            private class Cite extends AbstractTypography {
141    
142                    /**
143                     * Creates a Cite element of the given text
144                     * 
145                     * @param text
146                     *            the text to be set
147                     */
148                    public Cite(String text) {
149                            setElement(DOM.createElement("cite"));
150                            setText(text);
151                    }
152            }
153    
154            /**
155             * Element that shows the source of the quote in a smaller font.
156             * 
157             * @since 2.0.4.0
158             * 
159             * @author Carlos Alexandro Becker
160             */
161            private class SmallCite extends AbstractTypography {
162    
163                    private final Cite cite;
164    
165                    public SmallCite(String text) {
166                            setElement(DOM.createElement("small"));
167                            this.cite = new Cite(text);
168                            getElement().appendChild(cite.getElement());
169                    }
170    
171                    @Override
172                    public void setText(String text) {
173                            cite.setText(text);
174                    }
175            }
176    }