001    package org.gwtbootstrap3.client.ui.html;
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 com.google.gwt.dom.client.Document;
024    import com.google.gwt.dom.client.Element;
025    import com.google.gwt.event.logical.shared.AttachEvent;
026    import com.google.gwt.user.client.ui.HasText;
027    import com.google.gwt.user.client.ui.Widget;
028    
029    /**
030     * Simple text node.
031     * <p/>
032     * <h3>UiBinder example</h3>
033     * <p/>
034     * <pre>
035     * {@code
036     * <b:Text>
037     *    ...
038     * </b:Text>
039     * }
040     * </pre>
041     *
042     * @author Sven Jacobs
043     */
044    public class Text extends Widget implements HasText {
045    
046        private final com.google.gwt.dom.client.Text text;
047        private boolean isAttached;
048    
049        /**
050         * Creates the default text node with empty text
051         */
052        public Text() {
053            this("");
054        }
055    
056        /**
057         * Creates a text node with the desired text
058         *
059         * @param txt String text to display
060         */
061        public Text(final String txt) {
062            text = Document.get().createTextNode(txt);
063            setElement(text.<Element>cast());
064        }
065    
066        /**
067         * {@inheritDoc}
068         */
069        @Override
070        public void setText(final String txt) {
071            text.setData(txt);
072        }
073    
074        /**
075         * {@inheritDoc}
076         */
077        @Override
078        public String getText() {
079            return text.getData();
080        }
081        
082        @Override
083        public boolean isAttached() {
084            return isAttached;
085        }
086    
087        @Override
088        protected void onAttach() {
089            if (isAttached()) {
090                throw new IllegalStateException("Text is already attached!");
091            }
092            isAttached = true;
093            onLoad();
094            AttachEvent.fire(this, isAttached);
095        }
096    
097        @Override
098        protected void onDetach() {
099            if (!isAttached()) {
100                throw new IllegalStateException("Text is not attached!");
101            }
102            isAttached = false;
103            AttachEvent.fire(this, false);
104        }
105    }