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.base.mixin;
021
022import com.google.gwt.user.client.ui.UIObject;
023import gwt.material.design.client.base.HasType;
024import gwt.material.design.client.constants.CssType;
025
026/**
027 * @author Ben Dol
028 */
029public class CssTypeMixin<T extends CssType, H extends UIObject & HasType<T>> extends AbstractMixin<H> implements HasType<T> {
030
031    private T type;
032    private UIObject target;
033
034    public CssTypeMixin(final H widget) {
035        super(widget);
036    }
037
038    public CssTypeMixin(final H widget, UIObject target) {
039        super(widget);
040        this.target = target;
041    }
042
043    @Override
044    public void setType(T type) {
045        if (this.type != null && !this.type.getCssName().isEmpty()) {
046            if (target != null) {
047                target.removeStyleName(this.type.getCssName());
048            } else {
049                uiObject.removeStyleName(this.type.getCssName());
050            }
051        }
052        this.type = type;
053
054        if (type != null && !type.getCssName().isEmpty()) {
055            if (target != null) {
056                target.addStyleName(type.getCssName());
057            } else {
058                uiObject.addStyleName(type.getCssName());
059            }
060        }
061    }
062
063    @Override
064    public T getType() {
065        return type;
066    }
067}