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.HasDataToggle;
025import org.gwtbootstrap3.client.ui.base.HasHref;
026import org.gwtbootstrap3.client.ui.base.HasTarget;
027import org.gwtbootstrap3.client.ui.base.HasTargetHistoryToken;
028import org.gwtbootstrap3.client.ui.base.mixin.AttributeMixin;
029import org.gwtbootstrap3.client.ui.base.mixin.DataToggleMixin;
030import org.gwtbootstrap3.client.ui.base.mixin.FocusableMixin;
031import org.gwtbootstrap3.client.ui.constants.Attributes;
032import org.gwtbootstrap3.client.ui.constants.Toggle;
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;
044import com.google.gwt.user.client.ui.Focusable;
045
046/**
047 * Widget representing the Bootstrap Thumbnail as a clickable image
048 * <p/>
049 * <a href="http://getbootstrap.com/components/#thumbnails">Bootstrap Documentation</a>
050 * <p/>
051 * <h3>UiBinder example</h3>
052 * <p/>
053 * <pre>
054 * {@code
055 * <b:ThumbnailLink>
056 *    Image
057 * </b:ThumbnailLink>
058 * }
059 * </pre>
060 *
061 * @author Joshua Godi
062 */
063public class ThumbnailLink extends ComplexWidget implements HasClickHandlers, HasDoubleClickHandlers, HasHref,
064        HasDataToggle, HasTargetHistoryToken, Focusable, HasTarget {
065
066    private final DataToggleMixin<ThumbnailLink> toggleMixin = new DataToggleMixin<ThumbnailLink>(this);
067    private final AttributeMixin<ThumbnailLink> attributeMixin = new AttributeMixin<ThumbnailLink>(this);
068    private final FocusableMixin<ThumbnailLink> focusableMixin;
069    private String targetHistoryToken;
070
071    /**
072     * Creates the default ThumbnailLink with the specified HREF
073     *
074     * @param href String href to use
075     */
076    public ThumbnailLink(final String href) {
077        setElement(Document.get().createAnchorElement());
078        setHref(href);
079        focusableMixin = new FocusableMixin<ThumbnailLink>(this);
080    }
081
082    /**
083     * Creates the default ThumbnailLink with a default HREF
084     */
085    public ThumbnailLink() {
086        this(EMPTY_HREF);
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    @Override
093    public HandlerRegistration addClickHandler(final ClickHandler handler) {
094        return addDomHandler(handler, ClickEvent.getType());
095    }
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    public HandlerRegistration addDoubleClickHandler(final DoubleClickHandler handler) {
102        return addDomHandler(handler, DoubleClickEvent.getType());
103    }
104
105    /**
106     * {@inheritDoc}
107     */
108    @Override
109    public void setHref(final String href) {
110        AnchorElement.as(getElement()).setHref(href);
111    }
112
113    /**
114     * {@inheritDoc}
115     */
116    @Override
117    public String getHref() {
118        return AnchorElement.as(getElement()).getHref();
119    }
120
121    /**
122     * {@inheritDoc}
123     */
124    @Override
125    public void setTargetHistoryToken(final String targetHistoryToken) {
126        this.targetHistoryToken = targetHistoryToken;
127        final String hash = History.encodeHistoryToken(targetHistoryToken);
128        setHref("#" + hash);
129    }
130
131    /**
132     * {@inheritDoc}
133     */
134    @Override
135    public String getTargetHistoryToken() {
136        return targetHistoryToken;
137    }
138
139    /**
140     * {@inheritDoc}
141     */
142    @Override
143    public void setDataToggle(final Toggle toggle) {
144        toggleMixin.setDataToggle(toggle);
145    }
146
147    /**
148     * {@inheritDoc}
149     */
150    @Override
151    public Toggle getDataToggle() {
152        return toggleMixin.getDataToggle();
153    }
154
155    /**
156     * {@inheritDoc}
157     */
158    @Override
159    public int getTabIndex() {
160        return focusableMixin.getTabIndex();
161    }
162
163    /**
164     * {@inheritDoc}
165     */
166    @Override
167    public void setTabIndex(final int index) {
168        focusableMixin.setTabIndex(index);
169    }
170
171    /**
172     * {@inheritDoc}
173     */
174    @Override
175    public void setAccessKey(final char key) {
176        focusableMixin.setAccessKey(key);
177    }
178
179    /**
180     * {@inheritDoc}
181     */
182    @Override
183    public void setFocus(final boolean focused) {
184        focusableMixin.setFocus(focused);
185    }
186
187    /**
188     * {@inheritDoc}
189     */
190    @Override
191    public void setTarget(final String target) {
192        attributeMixin.setAttribute(Attributes.TARGET, target);
193    }
194
195    /**
196     * {@inheritDoc}
197     */
198    @Override
199    public String getTarget() {
200        return attributeMixin.getAttribute(Attributes.TARGET);
201    }
202}