001package org.gwtbootstrap3.extras.gallery.client.ui;
002
003/*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 - 2014 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 com.google.gwt.core.client.Scheduler;
024import com.google.gwt.dom.client.AnchorElement;
025import com.google.gwt.dom.client.Document;
026import com.google.gwt.dom.client.Style;
027import com.google.gwt.dom.client.Style.Display;
028import com.google.gwt.dom.client.Style.Position;
029import com.google.gwt.dom.client.Style.Unit;
030import com.google.gwt.event.dom.client.ClickEvent;
031import com.google.gwt.event.dom.client.ClickHandler;
032import com.google.gwt.event.dom.client.HasClickHandlers;
033import com.google.gwt.event.dom.client.HasLoadHandlers;
034import com.google.gwt.event.dom.client.LoadEvent;
035import com.google.gwt.event.dom.client.LoadHandler;
036import com.google.gwt.event.shared.HandlerRegistration;
037import com.google.gwt.uibinder.client.UiConstructor;
038import com.google.gwt.user.client.Command;
039import com.google.gwt.user.client.ui.Image;
040import com.google.gwt.user.client.ui.Widget;
041import org.gwtbootstrap3.client.ui.base.ComplexWidget;
042import org.gwtbootstrap3.client.ui.base.HasHref;
043
044/**
045 * Gallery Image
046 *
047 * @author Ben Dol
048 */
049public class GalleryImage extends ComplexWidget implements HasHref, HasLoadHandlers {
050
051    private Image image;
052
053    @UiConstructor
054    public GalleryImage(String url) {
055        setElement(Document.get().createAnchorElement());
056        getElement().setAttribute("data-gallery", "data-gallery");
057        getElement().getStyle().setDisplay(Display.INLINE_TABLE);
058
059        setHref(url);
060
061        image = new Image(url);
062        add(image);
063    }
064
065    @Override
066    public void add(final Widget child) {
067        if(child instanceof Image) {
068            if(image != null) {
069                image.removeFromParent();
070            }
071
072            image = (Image) child;
073            setHref(image.getUrl());
074
075            super.add(image);
076        } else if(child instanceof HasClickHandlers) {
077            ((HasClickHandlers) child).addClickHandler(new ClickHandler() {
078                @Override
079                public void onClick(ClickEvent event) {
080                    event.stopPropagation();
081                }
082            });
083
084            Scheduler.get().scheduleDeferred(new Command() {
085                @Override
086                public void execute() {
087                    Style style = child.getElement().getStyle();
088                    style.setPosition(Position.RELATIVE);
089                    style.setBottom((double) image.getHeight(), Unit.PX);
090                    style.setLeft(4, Unit.PX);
091                }
092            });
093
094            super.add(child);
095        } else {
096            super.add(child);
097        }
098    }
099
100    @Override
101    public void setHref(String href) {
102        AnchorElement.as(getElement()).setHref(href);
103    }
104
105    @Override
106    public String getHref() {
107        return AnchorElement.as(getElement()).getHref();
108    }
109
110    @Override
111    public void setWidth(String width) {
112        super.setWidth(width);
113        image.setWidth(width);
114    }
115
116    @Override
117    public void setHeight(String height) {
118        super.setHeight(height);
119        image.setHeight(height);
120
121        Scheduler.get().scheduleDeferred(new Command() {
122            @Override
123            public void execute() {
124                for (Widget child : GalleryImage.this) {
125                    if (child instanceof HasClickHandlers && !(child instanceof Image)) {
126                        Style style = child.getElement().getStyle();
127                        style.setBottom((double) image.getHeight(), Unit.PX);
128                    }
129                }
130            }
131        });
132    }
133
134    @Override
135    public HandlerRegistration addLoadHandler(LoadHandler handler) {
136        return image.addHandler(handler, LoadEvent.getType());
137    }
138}