001/*
002 * #%L
003 * GwtMaterial
004 * %%
005 * Copyright (C) 2015 - 2017 GwtMaterialDesign
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package gwt.material.design.client.ui;
021
022import com.google.gwt.dom.client.Element;
023import com.google.gwt.dom.client.Style.Display;
024import com.google.gwt.i18n.client.HasDirection.Direction;
025import com.google.gwt.i18n.shared.DirectionEstimator;
026import com.google.gwt.safehtml.shared.SafeHtml;
027import com.google.gwt.user.client.DOM;
028import gwt.material.design.client.base.BaseCheckBox;
029import gwt.material.design.client.base.HasGrid;
030import gwt.material.design.client.base.mixin.GridMixin;
031import gwt.material.design.client.base.mixin.ToggleStyleMixin;
032import gwt.material.design.client.constants.CheckBoxType;
033import gwt.material.design.client.constants.CssName;
034
035//@formatter:off
036
037/**
038 * Checkbox component provides two types
039 * - FILLED
040 * - INTERMEDIATE
041 * <p>
042 * <h3>UiBinder Usage:</h3>
043 * <pre>
044 * {@code
045 * // Default
046 * <m:MaterialCheckBox text="Option 1"/>
047 *
048 * // Filled
049 * <m:MaterialCheckBox text="Option 1" type="FILLED"/>
050 * }
051 * </pre>
052 * </p>
053 *
054 * @author kevzlou7979
055 * @author Ben Dol
056 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#checkbox">CheckBox</a>
057 * @see <a href="https://material.io/guidelines/components/selection-controls.html#selection-controls-checkbox">Material Design Specification</a>
058 */
059public class MaterialCheckBox extends BaseCheckBox implements HasGrid {
060
061    private Object object;
062
063    private GridMixin<MaterialCheckBox> gridMixin;
064    private ToggleStyleMixin<MaterialCheckBox> toggleOldMixin;
065
066    private CheckBoxType type;
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        setType(type);
107    }
108
109    @Override
110    protected void onLoad() {
111        super.onLoad();
112
113        getElement().getStyle().setDisplay(isVisible() ? Display.BLOCK : Display.NONE);
114    }
115
116    @Override
117    public void setGrid(String grid) {
118        getGridMixin().setGrid(grid);
119    }
120
121    @Override
122    public void setOffset(String offset) {
123        getGridMixin().setOffset(offset);
124    }
125
126    public Object getObject() {
127        return object;
128    }
129
130    public void setObject(Object object) {
131        this.object = object;
132    }
133
134    /**
135     * Used the old checkbox.
136     */
137    public void setOld(boolean old) {
138        getToggleOldMixin().setOn(old);
139    }
140
141    public boolean isOld() {
142        return getToggleOldMixin().isOn();
143    }
144
145    /**
146     * Setting the type of Checkbox.
147     */
148    public void setType(CheckBoxType type) {
149        this.type = type;
150        switch (type) {
151            case FILLED:
152                Element input = DOM.getChild(getElement(), 0);
153                input.setAttribute("class", CssName.FILLED_IN);
154                break;
155            case INTERMEDIATE:
156                addStyleName(type.getCssName() + "-checkbox");
157                break;
158            default:
159                addStyleName(type.getCssName());
160                break;
161        }
162    }
163
164    public CheckBoxType getType() {
165        return type;
166    }
167
168    public GridMixin<MaterialCheckBox> getGridMixin() {
169        if (gridMixin == null) {
170            gridMixin = new GridMixin<>(this);
171        }
172        return gridMixin;
173    }
174
175    public ToggleStyleMixin<MaterialCheckBox> getToggleOldMixin() {
176        if (toggleOldMixin == null) {
177            toggleOldMixin = new ToggleStyleMixin<>(this, CssName.OLD_CHECKBOX);
178        }
179        return toggleOldMixin;
180    }
181}