001package org.gwtbootstrap3.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 org.gwtbootstrap3.client.ui.base.HasFormValue; 024import org.gwtbootstrap3.client.ui.base.HasId; 025import org.gwtbootstrap3.client.ui.base.HasPull; 026import org.gwtbootstrap3.client.ui.base.HasResponsiveness; 027import org.gwtbootstrap3.client.ui.base.helper.StyleHelper; 028import org.gwtbootstrap3.client.ui.base.mixin.EnabledMixin; 029import org.gwtbootstrap3.client.ui.base.mixin.IdMixin; 030import org.gwtbootstrap3.client.ui.base.mixin.PullMixin; 031import org.gwtbootstrap3.client.ui.constants.DeviceSize; 032import org.gwtbootstrap3.client.ui.constants.Pull; 033import org.gwtbootstrap3.client.ui.gwt.Widget; 034import org.gwtbootstrap3.client.ui.impl.SimpleRadioButtonImpl; 035 036import com.google.gwt.core.client.GWT; 037import com.google.gwt.dom.client.Document; 038import com.google.gwt.dom.client.Element; 039import com.google.gwt.dom.client.InputElement; 040import com.google.gwt.event.dom.client.ChangeEvent; 041import com.google.gwt.event.dom.client.ChangeHandler; 042import com.google.gwt.event.dom.client.HasChangeHandlers; 043import com.google.gwt.event.shared.HandlerRegistration; 044import com.google.gwt.uibinder.client.UiConstructor; 045import com.google.gwt.user.client.ui.RootPanel; 046 047/** 048 * A simple radio button widget, with no label. 049 */ 050public class SimpleRadioButton extends com.google.gwt.user.client.ui.SimpleRadioButton implements HasResponsiveness, 051 HasId, HasPull, HasFormValue, HasChangeHandlers { 052 053 private static final SimpleRadioButtonImpl impl = GWT.create(SimpleRadioButtonImpl.class); 054 055 /** 056 * Creates a SimpleRadioButton widget that wraps an existing <input 057 * type='radio'> element. 058 * 059 * This element must already be attached to the document. If the element is 060 * removed from the document, you must call 061 * {@link RootPanel#detachNow(Widget)}. 062 * 063 * @param element 064 * the element to be wrapped 065 */ 066 public static SimpleRadioButton wrap(Element element) { 067 // Assert that the element is attached. 068 assert Document.get().getBody().isOrHasChild(element); 069 070 SimpleRadioButton radioButton = new SimpleRadioButton(InputElement.as(element)); 071 072 // Mark it attached and remember it for cleanup. 073 radioButton.onAttach(); 074 RootPanel.detachOnWindowClose(radioButton); 075 076 return radioButton; 077 } 078 079 private final IdMixin<SimpleRadioButton> idMixin = new IdMixin<SimpleRadioButton>(this); 080 private final PullMixin<SimpleRadioButton> pullMixin = new PullMixin<SimpleRadioButton>(this); 081 private final EnabledMixin<SimpleRadioButton> enabledMixin = new EnabledMixin<SimpleRadioButton>(this); 082 083 /** 084 * Creates a new radio associated with a particular group name. All radio 085 * buttons associated with the same group name belong to a 086 * mutually-exclusive set. 087 * 088 * Radio buttons are grouped by their name attribute, so changing their name 089 * using the setName() method will also change their associated group. 090 * 091 * @param name 092 * the group name with which to associate the radio button 093 */ 094 @UiConstructor 095 public SimpleRadioButton(String name) { 096 this(Document.get().createRadioInputElement(name)); 097 } 098 099 /** 100 * This constructor may be used by subclasses to explicitly use an existing 101 * element. This element must be an <input> element whose type is 102 * 'radio'. 103 * 104 * @param element 105 * the element to be used 106 */ 107 protected SimpleRadioButton(InputElement element) { 108 super(element); 109 } 110 111 @Override 112 public HandlerRegistration addChangeHandler(ChangeHandler handler) { 113 return addDomHandler(handler, ChangeEvent.getType()); 114 } 115 116 @Override 117 public void setEnabled(boolean enabled) { 118 enabledMixin.setEnabled(enabled); 119 } 120 121 @Override 122 public boolean isEnabled() { 123 return enabledMixin.isEnabled(); 124 } 125 126 /** 127 * {@inheritDoc} 128 */ 129 @Override 130 public void setId(final String id) { 131 idMixin.setId(id); 132 } 133 134 /** 135 * {@inheritDoc} 136 */ 137 @Override 138 public String getId() { 139 return idMixin.getId(); 140 } 141 142 /** 143 * {@inheritDoc} 144 */ 145 @Override 146 public void setVisibleOn(final DeviceSize deviceSize) { 147 StyleHelper.setVisibleOn(this, deviceSize); 148 } 149 150 /** 151 * {@inheritDoc} 152 */ 153 @Override 154 public void setHiddenOn(final DeviceSize deviceSize) { 155 StyleHelper.setHiddenOn(this, deviceSize); 156 } 157 158 /** 159 * {@inheritDoc} 160 */ 161 @Override 162 public void setPull(final Pull pull) { 163 pullMixin.setPull(pull); 164 } 165 166 /** 167 * {@inheritDoc} 168 */ 169 @Override 170 public Pull getPull() { 171 return pullMixin.getPull(); 172 } 173 174 @Override 175 protected void ensureDomEventHandlers() { 176 impl.ensureDomEventHandlers(this); 177 } 178 179}