001package 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
023import org.gwtbootstrap3.client.ui.base.AbstractListItem;
024import org.gwtbootstrap3.client.ui.constants.Styles;
025import org.gwtbootstrap3.client.ui.html.Text;
026
027import com.google.gwt.event.dom.client.ClickEvent;
028import com.google.gwt.event.dom.client.ClickHandler;
029import com.google.gwt.event.dom.client.HasClickHandlers;
030import com.google.gwt.event.shared.HandlerRegistration;
031import com.google.gwt.user.client.ui.HasText;
032import com.google.gwt.user.client.ui.HasWidgets;
033
034/**
035 * Represents a list item with text contents
036 *
037 * @author Sven Jacobs
038 * @see DropDownMenu
039 * @see NavTabs
040 * @see NavPills
041 * @see Navbar
042 */
043public class ListItem extends AbstractListItem implements HasWidgets, HasText, HasClickHandlers {
044
045    private Text text = null;
046
047    /**
048     * Creates a default list item element
049     */
050    public ListItem() {
051    }
052
053    /**
054     * Creates a default list item element with the desired text
055     *
056     * @param text desired text for list item
057     */
058    public ListItem(final String text) {
059        this();
060        setText(text);
061    }
062
063    /** {@inheritDoc} */
064    @Override
065    public HandlerRegistration addClickHandler(final ClickHandler handler) {
066        return addDomHandler(handler, ClickEvent.getType());
067    }
068
069    /** {@inheritDoc} */
070    @Override
071    public String getText() {
072        return text.getText();
073    }
074
075    /** {@inheritDoc} */
076    @Override
077    protected void onAttach() {
078        super.onAttach();
079        // Adding styles to the list item depending on the parent
080        if (getParent() != null) {
081            if (getParent() instanceof MediaList) {
082                addStyleName(Styles.MEDIA);
083            }
084        }
085    }
086
087    /** {@inheritDoc} */
088    @Override
089    public void setText(final String text) {
090        if (this.text == null) {
091            this.text = new Text(text);
092            add(this.text);
093        } else {
094            this.text.setText(text);
095        }
096    }
097
098}