001package com.github.gwtbootstrap.client.ui.base;
002
003import com.google.gwt.dom.client.Document;
004import com.google.gwt.dom.client.Element;
005import com.google.gwt.dom.client.Text;
006import com.google.gwt.event.logical.shared.AttachEvent;
007import com.google.gwt.user.client.ui.HasText;
008import com.google.gwt.user.client.ui.Widget;
009
010public class TextNode extends Widget implements HasText {
011
012    private Text baseNode;
013    private boolean attached;
014    
015    public TextNode() {
016    }
017
018    public TextNode(String text) {
019        setText(text);
020    }
021
022
023    @Override
024    public String getText() {
025        
026        return baseNode != null ? baseNode.getData() : null;
027    }
028
029
030    @Override
031    public void setText(String text) {
032        assert baseNode == null : "TextNode can be set once";
033        baseNode = Document.get().createTextNode(text);
034        setElement(baseNode.<Element>cast());
035    }
036    
037    @Override
038    public boolean isAttached() {
039        return attached;
040    }
041    
042    @Override
043    protected void onAttach() {
044        
045        if(isAttached()) {
046            throw new IllegalStateException("already added");
047        }
048        
049        this.attached = true;
050        
051        onLoad();
052        
053        AttachEvent.fire(this, attached);
054    }
055    
056    @Override
057    protected void onDetach() {
058        
059        if(!isAttached()) {
060            throw new IllegalStateException("is not attached");
061        }
062        
063        this.attached = false;
064        
065        AttachEvent.fire(this, attached);
066    }
067    
068    
069}