001package gwt.material.design.client.base.mixin;
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.user.client.ui.UIObject;
024
025/**
026 * @author Ben Dol
027 */
028public class AttributeMixin<T extends UIObject> extends AbstractMixin<T> {
029
030    private String attribute;
031
032    public AttributeMixin(final T widget, String attribute) {
033        super(widget);
034
035        this.attribute = attribute;
036    }
037
038    public void setAttribute(String value) {
039        if(value != null) {
040            uiObject.getElement().setAttribute(attribute, value);
041        } else {
042            uiObject.getElement().removeAttribute(attribute);
043        }
044    }
045
046    public void setAttribute(int value) {
047       setAttribute(String.valueOf(value));
048    }
049
050    public void setAttribute(double value) {
051        setAttribute(String.valueOf(value));
052    }
053
054    public void setAttribute(boolean value) {
055        setAttribute(String.valueOf(value));
056    }
057
058    public String getAttribute() {
059        return uiObject.getElement().getAttribute(attribute);
060    }
061
062    public int getAttributeAsInteger() {
063        return Integer.parseInt(getAttribute());
064    }
065
066    public double getAttributeAsDouble() {
067        return Double.parseDouble(getAttribute());
068    }
069
070    public boolean getAttributeAsBoolean() {
071        return Boolean.parseBoolean(getAttribute());
072    }
073}