com.codon.dev.client.wigets.SliderWithValBox.java Source code

Java tutorial

Introduction

Here is the source code for com.codon.dev.client.wigets.SliderWithValBox.java

Source

package com.codon.dev.client.wigets;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.kiouri.sliderbar.client.event.BarValueChangedEvent;
import com.kiouri.sliderbar.client.event.BarValueChangedHandler;

import java.util.logging.Logger;

/**
 * UI element, Slider with value box,
 *  - on slider movement changes value in valuebox
 *  - on value change in valuebox, moves slider position
 *
 *  WARNING !
 *  component uses third part code, and licensed under open source  (http://code.google.com/p/gwt-slider-bar/)
 *  all code under "com.kiouri.sliderbar" is work of another author
 *
 */

public class SliderWithValBox extends Composite {
    private static final Logger logger = Logger.getLogger(SliderWithValBox.class.getName());

    private SliderHorizontal slider;
    private TextBox textBox;
    private Integer sliderMaxVal;
    private Integer sliderDefaultVal;

    public SliderWithValBox(int maxVal) {
        this.sliderMaxVal = maxVal;
        onInit();
    }

    public SliderWithValBox(int maxVal, int sliderCurrentVal) {
        this.sliderMaxVal = maxVal;
        this.sliderDefaultVal = sliderCurrentVal;
        onInit();
    }

    private void onInit() {
        HorizontalPanel panel = new HorizontalPanel();

        slider = new SliderHorizontal(sliderMaxVal);
        if (sliderDefaultVal != null)
            slider.setValue(sliderDefaultVal);

        panel.add(slider);

        textBox = new TextBox();
        textBox.setStyleName("valueInput");
        textBox.setValue(String.valueOf(SliderHorizontal.MINVAL));
        panel.add(textBox);

        initHandlers();
        initWidget(panel);
    }

    private void initHandlers() {
        slider.addBarValueChangedHandler(new BarValueChangedHandler() {
            public void onBarValueChanged(BarValueChangedEvent event) {
                textBox.setValue("" + event.getValue());
            }
        });

        textBox.addValueChangeHandler(new ValueChangeHandler<String>() {
            public void onValueChange(ValueChangeEvent<String> stringValueChangeEvent) {
                String valStr = stringValueChangeEvent.getValue();
                int val = parseString(valStr);

                if (val > getSliderMaxVal()) {
                    textBox.setValue(String.valueOf(getSliderMaxVal()));
                    slider.setValue(getSliderMaxVal());
                } else if (val < SliderHorizontal.MINVAL) {
                    textBox.setValue(String.valueOf(SliderHorizontal.MINVAL));
                    slider.setValue(SliderHorizontal.MINVAL);
                } else {
                    slider.setValue(val);
                }
            }
        });
    }

    public int getSliderValue() {
        int value = this.slider.getValue();
        return value;
    }

    private int parseString(String strVal) {
        int val = 1;
        try {
            val = Integer.parseInt(strVal);
        } catch (NumberFormatException nfe) {
            logger.severe("Error casting str(" + strVal + ") to int : " + nfe.getMessage());
            val = 1;
        }
        return val;
    }

    public int getSliderMaxVal() {
        return sliderMaxVal;
    }

    public void clearVal() {
        if (sliderDefaultVal != null) {
            this.slider.setValue(sliderDefaultVal);
        } else {
            this.slider.setValue(SliderHorizontal.MINVAL);
        }
    }

}