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.ComplexWidget;
024import org.gwtbootstrap3.client.ui.base.HasActive;
025import org.gwtbootstrap3.client.ui.base.HasHref;
026import org.gwtbootstrap3.client.ui.base.HasTargetHistoryToken;
027import org.gwtbootstrap3.client.ui.base.HasType;
028import org.gwtbootstrap3.client.ui.base.helper.StyleHelper;
029import org.gwtbootstrap3.client.ui.base.mixin.ActiveMixin;
030import org.gwtbootstrap3.client.ui.constants.ListGroupItemType;
031import org.gwtbootstrap3.client.ui.constants.Styles;
032import org.gwtbootstrap3.client.ui.html.Span;
033
034import com.google.gwt.dom.client.AnchorElement;
035import com.google.gwt.dom.client.Document;
036import com.google.gwt.event.dom.client.ClickEvent;
037import com.google.gwt.event.dom.client.ClickHandler;
038import com.google.gwt.event.dom.client.DoubleClickEvent;
039import com.google.gwt.event.dom.client.DoubleClickHandler;
040import com.google.gwt.event.dom.client.HasClickHandlers;
041import com.google.gwt.event.dom.client.HasDoubleClickHandlers;
042import com.google.gwt.event.shared.HandlerRegistration;
043import com.google.gwt.user.client.History;
044
045/**
046 * @author Joshua Godi
047 */
048public class LinkedGroupItem extends ComplexWidget implements HasClickHandlers, HasDoubleClickHandlers, HasHref,
049        HasTargetHistoryToken, HasActive, HasType<ListGroupItemType> {
050
051    private final ActiveMixin<LinkedGroupItem> activeMixin = new ActiveMixin<LinkedGroupItem>(this);
052
053    private final Span span = new Span();
054
055    private String targetHistoryToken;
056
057    public LinkedGroupItem(final String href) {
058        setElement(Document.get().createAnchorElement());
059        setStyleName(Styles.LIST_GROUP_ITEM);
060        setHref(href);
061        add(span);
062    }
063
064    public LinkedGroupItem(final String text, final String href) {
065        this(href);
066        setText(text);
067    }
068
069    public LinkedGroupItem() {
070        this(EMPTY_HREF);
071    }
072
073    @Override
074    public HandlerRegistration addClickHandler(final ClickHandler handler) {
075        return addDomHandler(handler, ClickEvent.getType());
076    }
077
078    @Override
079    public HandlerRegistration addDoubleClickHandler(final DoubleClickHandler handler) {
080        return addDomHandler(handler, DoubleClickEvent.getType());
081    }
082
083    public void setText(final String text) {
084        span.setText(text);
085    }
086
087    public String getText() {
088        return span.getText();
089    }
090
091    @Override
092    public void setHref(final String href) {
093        AnchorElement.as(getElement()).setHref(href);
094    }
095
096    @Override
097    public String getHref() {
098        return AnchorElement.as(getElement()).getHref();
099    }
100
101    @Override
102    public void setTargetHistoryToken(final String targetHistoryToken) {
103        this.targetHistoryToken = targetHistoryToken;
104        final String hash = History.encodeHistoryToken(targetHistoryToken);
105        setHref("#" + hash);
106    }
107
108    @Override
109    public String getTargetHistoryToken() {
110        return targetHistoryToken;
111    }
112
113    @Override
114    public void setActive(final boolean active) {
115        activeMixin.setActive(active);
116    }
117
118    @Override
119    public boolean isActive() {
120        return activeMixin.isActive();
121    }
122
123    @Override
124    public void setType(final ListGroupItemType type) {
125        StyleHelper.addUniqueEnumStyleName(this, ListGroupItemType.class, type);
126    }
127
128    @Override
129    public ListGroupItemType getType() {
130        return ListGroupItemType.fromStyleName(getStyleName());
131    }
132}