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