001package org.gwtbootstrap3.extras.slider.client.ui;
002
003/*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 - 2015 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.extras.slider.client.ui.base.SliderBase;
024
025import com.google.gwt.core.client.JavaScriptObject;
026import com.google.gwt.dom.client.Element;
027import com.google.gwt.uibinder.client.UiConstructor;
028import com.google.gwt.user.client.Event;
029
030/**
031 * This slider simply takes a numeric value.
032 *
033 * @author Xiaodong SUN
034 */
035public class Slider extends SliderBase<Double> {
036
037    /**
038     * Creates a numerical slider.
039     */
040    public Slider() {
041        setRange(false);
042    }
043
044    /**
045     * Creates a numerical slider with min, max, and value.
046     *
047     * @param min
048     * @param max
049     * @param value
050     */
051    @UiConstructor
052    public Slider(final double min, final double max, final double value) {
053        this();
054        setMin(min);
055        setMax(max);
056        setValue(value);
057    }
058
059    @Override
060    protected native void setValue(Element e, Double value) /*-{
061        var doubleValue = value.@java.lang.Double::doubleValue()();
062        if (this.@org.gwtbootstrap3.extras.slider.client.ui.Slider::isSliderNamespaceAvailable()())
063            $wnd.jQuery(e).slider(@org.gwtbootstrap3.extras.slider.client.ui.base.SliderCommand::SET_VALUE, doubleValue);
064        else
065            $wnd.jQuery(e).bootstrapSlider(@org.gwtbootstrap3.extras.slider.client.ui.base.SliderCommand::SET_VALUE, doubleValue);
066    }-*/;
067
068    @Override
069    protected native Double getValue(Element e) /*-{
070        var value;
071        if (this.@org.gwtbootstrap3.extras.slider.client.ui.Slider::isSliderNamespaceAvailable()())
072            value = $wnd.jQuery(e).slider(@org.gwtbootstrap3.extras.slider.client.ui.base.SliderCommand::GET_VALUE);
073        else
074            value = $wnd.jQuery(e).bootstrapSlider(@org.gwtbootstrap3.extras.slider.client.ui.base.SliderCommand::GET_VALUE);
075        return @java.lang.Double::new(D)(value);
076    }-*/;
077
078    @Override
079    protected native void setFormatterOption(JavaScriptObject options) /*-{
080        var slider = this;
081        options.formatter = function(value) {
082            var val = @java.lang.Double::new(D)(value);
083            return slider.@org.gwtbootstrap3.extras.slider.client.ui.Slider::formatTooltip(Ljava/lang/Double;)(val);
084        };
085    }-*/;
086
087    @Override
088    protected native void setFormatter(Element e) /*-{
089        var slider = this;
090        var attr = @org.gwtbootstrap3.extras.slider.client.ui.base.SliderOption::FORMATTER;
091        var formatter = function(value) {
092            var val = @java.lang.Double::new(D)(value);
093            return slider.@org.gwtbootstrap3.extras.slider.client.ui.Slider::formatTooltip(Ljava/lang/Double;)(val);
094        };
095        if (this.@org.gwtbootstrap3.extras.slider.client.ui.Slider::isSliderNamespaceAvailable()())
096            $wnd.jQuery(e).slider(@org.gwtbootstrap3.extras.slider.client.ui.base.SliderCommand::SET_ATTRIBUTE, attr, formatter);
097        else
098            $wnd.jQuery(e).bootstrapSlider(@org.gwtbootstrap3.extras.slider.client.ui.base.SliderCommand::SET_ATTRIBUTE, attr, formatter);
099    }-*/;
100
101    @Override
102    protected String format(Double value) {
103        return value.toString();
104    }
105
106    @Override
107    protected Double convertValue(String value) {
108        if (value == null || value.isEmpty())
109            return null;
110        return Double.valueOf(value);
111    }
112
113    @Override
114    protected native void onSlide(Event event) /*-{
115        var value = @java.lang.Double::new(D)(event.value);
116        this.@org.gwtbootstrap3.extras.slider.client.ui.Slider::fireSlideEvent(Ljava/lang/Double;)(value);
117    }-*/;
118
119    @Override
120    protected native void onSlideStart(Event event) /*-{
121        var value = @java.lang.Double::new(D)(event.value);
122        this.@org.gwtbootstrap3.extras.slider.client.ui.Slider::fireSlideStartEvent(Ljava/lang/Double;)(value);
123    }-*/;
124
125    @Override
126    protected native void onSlideStop(Event event) /*-{
127        var value = @java.lang.Double::new(D)(event.value);
128        this.@org.gwtbootstrap3.extras.slider.client.ui.Slider::fireSlideStopEvent(Ljava/lang/Double;)(value);
129    }-*/;
130
131    @Override
132    protected native void onSlideChange(Event event) /*-{
133        var value = @java.lang.Double::new(D)(event.value.newValue);
134        this.@org.gwtbootstrap3.extras.slider.client.ui.Slider::fireChangeEvent(Ljava/lang/Double;)(value);
135    }-*/;
136
137}