<commit_before>package org.cytoscape.filter.internal.view;

import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.JFormattedTextField.AbstractFormatter;

import org.cytoscape.filter.internal.prefuse.NumberRangeModel;

public abstract class RangeChooserController {
	private NumberRangeModel sliderModel;
	private Number low;
	private Number high;
	private Number minimum;
	private Number maximum;
	
	public RangeChooserController() {
		sliderModel = new NumberRangeModel(0, 0, 0, 0);
		low = 0;
		high = 0;
		minimum = 0;
		maximum = 0;
	}
	
	public NumberRangeModel getSliderModel() {
		return sliderModel;
	}
	
	Number clampByFormat(AbstractFormatter format, Number value) {
		if (format == null) {
			return value;
		}
		if (value == null) {
			return null;
		}
		try {
			return (Number) format.stringToValue(format.valueToString(value));
		} catch (ParseException e) {
			return null;
		}
	}
	
	public void sliderChanged(RangeChooser chooser) {
		JFormattedTextField minimumField = chooser.getMinimumField();
		JFormattedTextField maximumField = chooser.getMaximumField();
		
		// Ensure that what we display is what actually makes it into the model
		Number newLow = clampByFormat(minimumField.getFormatter(), (Number) sliderModel.getLowValue());
		Number newHigh = clampByFormat(maximumField.getFormatter(), (Number) sliderModel.getHighValue());
		
		if (newLow != null && newLow.equals(low) && newHigh != null && newHigh.equals(high)) {
			return;
		}
		
		low = newLow ;
		high = newHigh;
		
		minimumField.setValue(low);
		maximumField.setValue(high);
		handleRangeChanged(low, high);
	}
	
	public void minimumChanged(RangeChooser chooser) {
		low = (Number) chooser.getMinimumField().getValue();
		if (low == null) {
			low = minimum;
		}
		sliderModel.setValueRange(low, high, minimum, maximum);
		handleRangeChanged(low, high);
	}

	public void maximumChanged(RangeChooser chooser) {
		high = (Number) chooser.getMaximumField().getValue();
		if (high == null) {
			high = maximum;
		}
		sliderModel.setValueRange(low, high, minimum, maximum);
		handleRangeChanged(low, high);<commit_msg>References #2617: Fixed issue in model event feedback which causes 3x as many filtering operations to happen when sliders are used.<commit_after>package org.cytoscape.filter.internal.view;

import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.JFormattedTextField.AbstractFormatter;

import org.cytoscape.filter.internal.prefuse.NumberRangeModel;

public abstract class RangeChooserController {
	private NumberRangeModel sliderModel;
	private Number low;
	private Number high;
	private Number minimum;
	private Number maximum;
	private boolean disableListeners;
	
	public RangeChooserController() {
		sliderModel = new NumberRangeModel(0, 0, 0, 0);
		low = 0;
		high = 0;
		minimum = 0;
		maximum = 0;
	}
	
	public NumberRangeModel getSliderModel() {
		return sliderModel;
	}
	
	Number clampByFormat(AbstractFormatter format, Number value) {
		if (format == null) {
			return value;
		}
		if (value == null) {
			return null;
		}
		try {
			return (Number) format.stringToValue(format.valueToString(value));
		} catch (ParseException e) {
			return null;
		}
	}
	
	public void sliderChanged(RangeChooser chooser) {
		JFormattedTextField minimumField = chooser.getMinimumField();
		JFormattedTextField maximumField = chooser.getMaximumField();
		
		// Ensure that what we display is what actually makes it into the model
		Number newLow = clampByFormat(minimumField.getFormatter(), (Number) sliderModel.getLowValue());
		Number newHigh = clampByFormat(maximumField.getFormatter(), (Number) sliderModel.getHighValue());
		
		if (newLow != null && newLow.equals(low) && newHigh != null && newHigh.equals(high)) {
			return;
		}
		
		low = newLow ;
		high = newHigh;
		
		disableListeners = true;
		try {
			minimumField.setValue(low);
			maximumField.setValue(high);
		} finally {
			disableListeners = false;
		}
		handleRangeChanged(low, high);
	}
	
	public void minimumChanged(RangeChooser chooser) {
		low = (Number) chooser.getMinimumField().getValue();
		if (low == null) {
			low = minimum;
		}
		sliderModel.setValueRange(low, high, minimum, maximum);
		if (disableListeners) {
			return;
		}
		handleRangeChanged(low, high);
	}

	public void maximumChanged(RangeChooser chooser) {
		high = (Number) chooser.getMaximumField().getValue();
		if (high == null) {
			high = maximum;
		}
		sliderModel.setValueRange(low, high, minimum, maximum);
		if (disableListeners) {
			return;
		}
		handleRangeChanged(low, high);
{'fraction_non_alphanumeric': 0.10615618881879486, 'fraction_numerical': 0.004568196649989123, 'mean_word_length': 4.506586826347306, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6350540', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1373, 'n_words': 352}