001    package org.gwtbootstrap3.client.ui.impl;
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    
023    import org.gwtbootstrap3.client.ui.Radio;
024    
025    import com.google.gwt.dom.client.Element;
026    import com.google.gwt.event.dom.client.BlurEvent;
027    import com.google.gwt.event.dom.client.BlurHandler;
028    import com.google.gwt.event.dom.client.ClickEvent;
029    import com.google.gwt.event.dom.client.ClickHandler;
030    import com.google.gwt.event.dom.client.KeyDownEvent;
031    import com.google.gwt.event.dom.client.KeyDownHandler;
032    import com.google.gwt.event.dom.client.MouseUpEvent;
033    import com.google.gwt.event.dom.client.MouseUpHandler;
034    import com.google.gwt.event.logical.shared.ValueChangeEvent;
035    import com.google.gwt.user.client.Event;
036    
037    /**
038     * This implementation will work in most cases.
039     *
040     * This case is not supported:
041     *
042     * 1. Given a group of two Radios
043     * 2. Select the first with a click on either input or label
044     * 3. Select the second with a click on either input or label
045     * 4. Select the first using the keyboard
046     *
047     * You will notice that 4 does not trigger a ValueChangeEvent.
048     *
049     */
050    public class RadioImplIE8 extends RadioImpl {
051    
052        private static class Handler implements ClickHandler, MouseUpHandler, BlurHandler, KeyDownHandler {
053    
054            private final Radio radio;
055            private Boolean oldValue;
056    
057            public Handler(Radio radio) {
058                this.radio = radio;
059            }
060    
061            @Override
062            public void onClick(ClickEvent event) {
063                ValueChangeEvent.fireIfNotEqual(radio, oldValue, radio.getValue());
064            }
065    
066            @Override
067            public void onKeyDown(KeyDownEvent event) {
068                oldValue = radio.getValue();
069            }
070    
071            @Override
072            public void onBlur(BlurEvent event) {
073                oldValue = radio.getValue();
074            }
075    
076            @Override
077            public void onMouseUp(MouseUpEvent event) {
078                oldValue = radio.getValue();
079            }
080    
081        }
082    
083        @Override
084        public void ensureDomEventHandlers(final Radio radio) {
085            final Handler handler = new Handler(radio);
086            radio.addClickHandler(handler);
087            radio.addMouseUpHandler(handler);
088            radio.addBlurHandler(handler);
089            radio.addKeyDownHandler(handler);
090        }
091    
092        @Override
093        public void sinkEvents(int eventBitsToAdd, Element inputElem,
094                Element labelElem) {
095            Event.sinkEvents(inputElem,
096                    eventBitsToAdd | Event.getEventsSunk(inputElem));
097            Event.sinkEvents(labelElem,
098                    eventBitsToAdd | Event.getEventsSunk(labelElem));
099        }
100    
101    }