All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.solovyev.android.prefs.RangeSeekBarPreference Maven / Gradle / Ivy

There is a newer version: 1.1.18
Show newest version
package org.solovyev.android.prefs;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.view.AbstractRangeSeekBar;
import org.solovyev.android.view.NumberRangeSeekBar;
import org.solovyev.common.interval.Interval;
import org.solovyev.common.interval.IntervalImpl;
import org.solovyev.common.text.NumberIntervalMapper;

/**
 * User: serso
 * Date: 9/19/11
 * Time: 12:27 PM
 */
public abstract class RangeSeekBarPreference> extends AbstractDialogPreference> implements AbstractRangeSeekBar.OnRangeSeekBarChangeListener {

	@NotNull
	private final Interval boundaries;

    @NotNull
	private final T step;

	public RangeSeekBarPreference(@NotNull Context context, AttributeSet attrs, @NotNull NumberIntervalMapper mapper) {
		super(context, attrs, null, true, mapper);

		//noinspection ConstantConditions
		boundaries = mapper.parseValue(attrs.getAttributeValue(AbstractDialogPreference.localNameSpace, "boundaries"));

		final String stepValue = attrs.getAttributeValue(AbstractDialogPreference.localNameSpace, "step");
        if ( stepValue == null ) {
           step = getDefaultStep();
        } else {
           step = mapper.getMapper().parseValue(stepValue);
        }

	}

    @NotNull
    protected abstract T getDefaultStep();

    @NotNull
	protected View createPreferenceView(@NotNull Context context) {
        int count = 0;
        for ( T t = boundaries.getLeftLimit(); t.compareTo(boundaries.getRightLimit()) <= 0; t = add(t, step) ) {
            count += 1;
        }
        final NumberRangeSeekBar result = new NumberRangeSeekBar(boundaries, count, context);

        result.setNotifyWhileDragging(true);
		result.setOnRangeSeekBarChangeListener(this);

		return result;
	}

    @NotNull
    protected abstract T add(@NotNull T l, @NotNull T r);

    @Override
	protected LinearLayout.LayoutParams getParams() {
		return null;
	}

	@Override
	protected void initPreferenceView(@NotNull View v, Interval value) {
		if (value != null) {
			((NumberRangeSeekBar) v).setSelectedMinValue(value.getLeftLimit());
			((NumberRangeSeekBar) v).setSelectedMaxValue(value.getRightLimit());
			setValueText(value);
		}
	}

	@Override
	public void rangeSeekBarValuesChanged(T minValue, T maxValue, boolean changeComplete) {
		final Interval interval = IntervalImpl.newClosed(minValue, maxValue);

		if (changeComplete) {
			persistValue(interval);
		}

		setValueText(interval);
	}

	private void setValueText(@NotNull Interval interval) {
		final String t = String.valueOf(interval);
        final String valueText = getValueText();
		updateValueText(valueText == null ? t : t.concat(valueText));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy