001package gwt.material.design.client.ui;
002
003/*
004 * #%L
005 * GwtMaterial
006 * %%
007 * Copyright (C) 2015 GwtMaterialDesign
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.dom.client.Element;
024import com.google.gwt.dom.client.Style.Display;
025import com.google.gwt.event.dom.client.ClickEvent;
026import com.google.gwt.event.dom.client.ClickHandler;
027import com.google.gwt.event.dom.client.HasClickHandlers;
028import com.google.gwt.event.shared.HandlerRegistration;
029import com.google.gwt.i18n.client.HasDirection.Direction;
030import com.google.gwt.i18n.shared.DirectionEstimator;
031import com.google.gwt.safehtml.shared.SafeHtml;
032import com.google.gwt.user.client.DOM;
033import gwt.material.design.client.base.BaseCheckBox;
034import gwt.material.design.client.base.HasGrid;
035import gwt.material.design.client.base.mixin.GridMixin;
036import gwt.material.design.client.constants.CheckBoxType;
037
038//@formatter:off
039
040/**
041 * Checkbox component provides two types
042 * - FILLED
043 * - INTERMEDIATE
044 * <p>
045 * <h3>UiBinder Usage:</h3>
046 * <pre>
047 * {@code
048 * // Default
049 * <m:MaterialCheckBox text="Option 1"/>
050 *
051 * // Filled
052 * <m:MaterialCheckBox text="Option 1" type="FILLED"/>
053 * }
054 * </pre>
055 * </p>
056 *
057 * @author kevzlou7979
058 * @author Ben Dol
059 * @see <a href="http://gwt-material-demo.herokuapp.com/#forms">CheckBox</a>
060 */
061public class MaterialCheckBox extends BaseCheckBox implements HasClickHandlers, HasGrid {
062
063    private Object object;
064    private String old;
065
066    private final GridMixin<MaterialCheckBox> gridMixin = new GridMixin<>(this);
067
068    public MaterialCheckBox() {
069        super();
070    }
071
072    public MaterialCheckBox(Element elem) {
073        super(elem);
074    }
075
076    public MaterialCheckBox(SafeHtml label, Direction dir) {
077        super(label, dir);
078    }
079
080    public MaterialCheckBox(SafeHtml label, DirectionEstimator directionEstimator) {
081        super(label, directionEstimator);
082    }
083
084    public MaterialCheckBox(SafeHtml label) {
085        super(label);
086    }
087
088    public MaterialCheckBox(String label, boolean asHTML) {
089        super(label, asHTML);
090    }
091
092    public MaterialCheckBox(String label, Direction dir) {
093        super(label, dir);
094    }
095
096    public MaterialCheckBox(String label, DirectionEstimator directionEstimator) {
097        super(label, directionEstimator);
098    }
099
100    public MaterialCheckBox(String label) {
101        super(label);
102    }
103
104    public MaterialCheckBox(String label, CheckBoxType type) {
105        super(label);
106
107        setType(type);
108    }
109
110    public Object getObject() {
111        return object;
112    }
113
114    public void setObject(Object object) {
115        this.object = object;
116    }
117
118    @Override
119    public HandlerRegistration addClickHandler(final ClickHandler handler) {
120        return addDomHandler(new ClickHandler() {
121            @Override
122            public void onClick(ClickEvent event) {
123                if(isEnabled()){
124                    handler.onClick(event);
125                }
126            }
127        }, ClickEvent.getType());
128    }
129
130    @Override
131    protected void onLoad() {
132        super.onLoad();
133        if(isVisible()) {
134            this.getElement().getStyle().setDisplay(Display.BLOCK);
135        }else {
136            this.getElement().getStyle().setDisplay(Display.NONE);
137        }
138    }
139
140    public String getOld() {
141        return old;
142    }
143
144    /**
145     * Used the old checkbox.
146     */
147    public void setOld(String old) {
148        this.old = old;
149        this.addStyleName("oldCheckBox");
150    }
151
152    /**
153     * Setting the type of Checkbox.
154     */
155    public void setType(CheckBoxType type) {
156        switch (type) {
157            case FILLED:
158                Element cb = this.getElement();
159                Element input = DOM.getChild(cb, 0);
160                input.setAttribute("class", "filled-in");
161                break;
162            case INTERMEDIATE:
163                this.addStyleName(type + "-checkbox");
164                break;
165            default:
166                this.addStyleName(type.getCssName());
167                break;
168        }
169    }
170
171    @Override
172    public void setGrid(String grid) {
173        gridMixin.setGrid(grid);
174    }
175
176    @Override
177    public void setOffset(String offset) {
178        gridMixin.setOffset(offset);
179    }
180}