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.SimpleCheckBoxImpl; 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.user.client.ui.RootPanel; 045 046/** 047 * A simple checkbox widget, with no label. 048 */ 049public class SimpleCheckBox extends com.google.gwt.user.client.ui.SimpleCheckBox implements HasResponsiveness, HasId, 050 HasPull, HasFormValue, HasChangeHandlers { 051 052 private static final SimpleCheckBoxImpl impl = GWT.create(SimpleCheckBoxImpl.class); 053 054 /** 055 * Creates a SimpleCheckBox widget that wraps an existing <input 056 * type='checkbox'> element. 057 * 058 * This element must already be attached to the document. If the element is 059 * removed from the document, you must call 060 * {@link RootPanel#detachNow(Widget)}. 061 * 062 * @param element 063 * the element to be wrapped 064 */ 065 public static SimpleCheckBox wrap(Element element) { 066 // Assert that the element is attached. 067 assert Document.get().getBody().isOrHasChild(element); 068 069 SimpleCheckBox checkBox = new SimpleCheckBox(InputElement.as(element)); 070 071 // Mark it attached and remember it for cleanup. 072 checkBox.onAttach(); 073 RootPanel.detachOnWindowClose(checkBox); 074 075 return checkBox; 076 } 077 078 private final IdMixin<SimpleCheckBox> idMixin = new IdMixin<SimpleCheckBox>(this); 079 private final PullMixin<SimpleCheckBox> pullMixin = new PullMixin<SimpleCheckBox>(this); 080 private final EnabledMixin<SimpleCheckBox> enabledMixin = new EnabledMixin<SimpleCheckBox>(this); 081 082 /** 083 * Creates a new simple checkbox. 084 */ 085 public SimpleCheckBox() { 086 super(Document.get().createCheckInputElement()); 087 } 088 089 /** 090 * This constructor may be used by subclasses to explicitly use an existing 091 * element. This element must be an <input> element whose type is 092 * 'checkbox'. 093 * 094 * @param element 095 * the element to be used 096 */ 097 protected SimpleCheckBox(InputElement element) { 098 super(element); 099 } 100 101 @Override 102 public HandlerRegistration addChangeHandler(ChangeHandler handler) { 103 return addDomHandler(handler, ChangeEvent.getType()); 104 } 105 106 @Override 107 public void setEnabled(boolean enabled) { 108 enabledMixin.setEnabled(enabled); 109 } 110 111 @Override 112 public boolean isEnabled() { 113 return enabledMixin.isEnabled(); 114 } 115 116 /** 117 * {@inheritDoc} 118 */ 119 @Override 120 public void setId(final String id) { 121 idMixin.setId(id); 122 } 123 124 /** 125 * {@inheritDoc} 126 */ 127 @Override 128 public String getId() { 129 return idMixin.getId(); 130 } 131 132 /** 133 * {@inheritDoc} 134 */ 135 @Override 136 public void setVisibleOn(final DeviceSize deviceSize) { 137 StyleHelper.setVisibleOn(this, deviceSize); 138 } 139 140 /** 141 * {@inheritDoc} 142 */ 143 @Override 144 public void setHiddenOn(final DeviceSize deviceSize) { 145 StyleHelper.setHiddenOn(this, deviceSize); 146 } 147 148 /** 149 * {@inheritDoc} 150 */ 151 @Override 152 public void setPull(final Pull pull) { 153 pullMixin.setPull(pull); 154 } 155 156 /** 157 * {@inheritDoc} 158 */ 159 @Override 160 public Pull getPull() { 161 return pullMixin.getPull(); 162 } 163 164 @Override 165 protected void ensureDomEventHandlers() { 166 impl.ensureDomEventHandlers(this); 167 } 168 169}