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.shared.event.HiddenEvent;
024import org.gwtbootstrap3.client.shared.event.HiddenHandler;
025import org.gwtbootstrap3.client.shared.event.HideEvent;
026import org.gwtbootstrap3.client.shared.event.HideHandler;
027import org.gwtbootstrap3.client.shared.event.ShowEvent;
028import org.gwtbootstrap3.client.shared.event.ShowHandler;
029import org.gwtbootstrap3.client.shared.event.ShownEvent;
030import org.gwtbootstrap3.client.shared.event.ShownHandler;
031import org.gwtbootstrap3.client.ui.base.helper.StyleHelper;
032import org.gwtbootstrap3.client.ui.constants.Styles;
033import org.gwtbootstrap3.client.ui.html.Div;
034
035import com.google.gwt.event.shared.HandlerRegistration;
036import com.google.gwt.user.client.Event;
037
038/**
039 * @author Grant Slender
040 * @author Joshua Godi
041 */
042public class PanelCollapse extends Div {
043
044    public PanelCollapse() {
045        setStyleName(Styles.PANEL_COLLAPSE);
046        addStyleName(Styles.COLLAPSE);
047    }
048
049    public void setIn(final boolean in) {
050        if (in) {
051            addStyleName(Styles.IN);
052        } else {
053            removeStyleName(Styles.IN);
054        }
055    }
056
057    public boolean isIn() {
058        return StyleHelper.containsStyle(getStyleName(), Styles.IN);
059    }
060
061    @Override
062    protected void onLoad() {
063        super.onLoad();
064
065        // Bind jquery events
066        bindJavaScriptEvents(getElement());
067    }
068
069    @Override
070    protected void onUnload() {
071        super.onUnload();
072
073        // Unbind the events
074        unbindJavaScriptEvents(getElement());
075    }
076
077    public HandlerRegistration addShowHandler(final ShowHandler showHandler) {
078        return addHandler(showHandler, ShowEvent.getType());
079    }
080
081    public HandlerRegistration addShownHandler(final ShownHandler shownHandler) {
082        return addHandler(shownHandler, ShownEvent.getType());
083    }
084
085    public HandlerRegistration addHideHandler(final HideHandler hideHandler) {
086        return addHandler(hideHandler, HideEvent.getType());
087    }
088
089    public HandlerRegistration addHiddenHandler(final HiddenHandler hiddenHandler) {
090        return addHandler(hiddenHandler, HiddenEvent.getType());
091    }
092
093    /**
094     * Fired when the collapse is starting to show
095     */
096    private void onShow(final Event evt) {
097        fireEvent(new ShowEvent(evt));
098    }
099
100    /**
101     * Fired when the collapse has shown
102     */
103    private void onShown(final Event evt) {
104        fireEvent(new ShownEvent(evt));
105    }
106
107    /**
108     * Fired when the collapse is starting to hide
109     */
110    private void onHide(final Event evt) {
111        fireEvent(new HideEvent(evt));
112    }
113
114    /**
115     * Fired when the collapse has hidden
116     */
117    private void onHidden(final Event evt) {
118        fireEvent(new HiddenEvent(evt));
119    }
120
121    private native void bindJavaScriptEvents(final com.google.gwt.dom.client.Element e) /*-{
122        var target = this;
123        var $collapse = $wnd.jQuery(e);
124
125        $collapse.on('show.bs.collapse', function (evt) {
126            target.@org.gwtbootstrap3.client.ui.PanelCollapse::onShow(Lcom/google/gwt/user/client/Event;)(evt);
127        });
128
129        $collapse.on('shown.bs.collapse', function (evt) {
130            target.@org.gwtbootstrap3.client.ui.PanelCollapse::onShown(Lcom/google/gwt/user/client/Event;)(evt);
131        });
132
133        $collapse.on('hide.bs.collapse', function (evt) {
134            target.@org.gwtbootstrap3.client.ui.PanelCollapse::onHide(Lcom/google/gwt/user/client/Event;)(evt);
135        });
136
137        $collapse.on('hidden.bs.collapse', function (evt) {
138            target.@org.gwtbootstrap3.client.ui.PanelCollapse::onHidden(Lcom/google/gwt/user/client/Event;)(evt);
139        });
140    }-*/;
141
142    private native void unbindJavaScriptEvents(final com.google.gwt.dom.client.Element e) /*-{
143        $wnd.jQuery(e).off('show.bs.collapse');
144        $wnd.jQuery(e).off('shown.bs.collapse');
145        $wnd.jQuery(e).off('hide.bs.collapse');
146        $wnd.jQuery(e).off('hidden.bs.collapse');
147    }-*/;
148}