<commit_before>package com.webif.divex.form;

import java.io.PrintWriter;
import java.util.HashMap;

import org.apache.commons.lang.StringEscapeUtils;

import com.webif.divex.DivEx;
import com.webif.divex.Event;
import com.webif.divex.form.validator.IFormElementValidator;

public class SelectFormElementDE extends FormElementDEBase 
{	
	protected String label;
	protected Integer value;
	protected String error;

	protected IFormElementValidator<Integer> validator = null;
	
	HashMap<Integer, String> keyvalues;
	
	public SelectFormElementDE(DivEx parent, HashMap<Integer, String> _keyvalues) {
		super(parent);
		keyvalues = _keyvalues;
	}
	
	public void render(PrintWriter out) 
	{
		out.print("<div id=\""+getNodeID()+"\">");
		if(label != null) {
			out.print("<label>"+StringEscapeUtils.escapeHtml(label)+"</label><br/>");
		}
		out.print("<select onchange='divex(\""+getNodeID()+"\", \"change\", this.value);'>");
		out.print("<option value=\"\">(Please Select)</option>");

		for(Integer v : keyvalues.keySet()) {
			String name = keyvalues.get(v);
			String selected = "";
			if(v == value) {
				selected = "selected=\"selected\"";
			}
			out.print("<option value=\""+v+"\" "+selected+">"+StringEscapeUtils.escapeHtml(name)+"</option>");
		}
		out.print("</select>");
		if(isRequired()) {
			out.print(" * Required");
		}
		if(error != null) {
			out.print("<p class='elementerror round'>"+StringEscapeUtils.escapeHtml(error)+"</p>");
		}
		out.print("</div>");
	}
	
	public void setLabel(String _label) { label = _label; }
	public void setValidator(IFormElementValidator<Integer> _validator) { validator = _validator; }
	
	public void setValue(Integer _value)	
	{ 
		value = _value; 
	}
	public Integer getValue()
	{
		return value;
	}

	public void validate()
	{
		redraw();
		
		//if required, run RequiredValidator
		if(isRequired()) {
			if(value == null) {
				error = "Please select an item.";
				setValid(false);
				return;
			}
		}
		
		//then run the optional validation
		if(validator != null) {
			if(!validator.isValid(value)) {
				//bad..
				error = validator.getMessage();
				setValid(false);
				return;
			}
		}
		
		//all good..
		error = null;
		setValid(true);
	}
	
	public void onEvent(Event event) {
		try {
			value = Integer.parseInt(event.getValue());
		} catch (NumberFormatException e) {
			value = null;
		}
		validate();
	}
}
<commit_msg>Checking if element is selected using obj.compareTo ().<commit_after>package com.webif.divex.form;

import java.io.PrintWriter;
import java.util.HashMap;

import org.apache.commons.lang.StringEscapeUtils;

import com.webif.divex.DivEx;
import com.webif.divex.Event;
import com.webif.divex.form.validator.IFormElementValidator;

public class SelectFormElementDE extends FormElementDEBase 
{	
	protected String label;
	protected Integer value;
	protected String error;

	protected IFormElementValidator<Integer> validator = null;
	
	HashMap<Integer, String> keyvalues;
	
	public SelectFormElementDE(DivEx parent, HashMap<Integer, String> _keyvalues) {
		super(parent);
		keyvalues = _keyvalues;
	}
	
	public void render(PrintWriter out) 
	{
		out.print("<div id=\""+getNodeID()+"\">");
		if(label != null) {
			out.print("<label>"+StringEscapeUtils.escapeHtml(label)+"</label><br/>");
		}
		out.print("<select onchange='divex(\""+getNodeID()+"\", \"change\", this.value);'>");
		out.print("<option value=\"\">(Please Select)</option>");

		for(Integer v : keyvalues.keySet()) {
			String name = keyvalues.get(v);
			String selected = "";
			if (value != null) {
				if(v.compareTo(value) == 0) {
					selected = "selected=\"selected\"";
				}
			}
			out.print("<option value=\""+v+"\" "+selected+">"+StringEscapeUtils.escapeHtml(name)+"</option>");
		}
		out.print("</select>");
		if(isRequired()) {
			out.print(" * Required");
		}
		if(error != null) {
			out.print("<p class='elementerror round'>"+StringEscapeUtils.escapeHtml(error)+"</p>");
		}
		out.print("</div>");
	}
	
	public void setLabel(String _label) { label = _label; }
	public void setValidator(IFormElementValidator<Integer> _validator) { validator = _validator; }
	
	public void setValue(Integer _value)	
	{ 
		value = _value; 
	}
	public Integer getValue()
	{
		return value;
	}

	public void validate()
	{
		redraw();
		
		//if required, run RequiredValidator
		if(isRequired()) {
			if(value == null) {
				error = "Please select an item.";
				setValid(false);
				return;
			}
		}
		
		//then run the optional validation
		if(validator != null) {
			if(!validator.isValid(value)) {
				//bad..
				error = validator.getMessage();
				setValid(false);
				return;
			}
		}
		
		//all good..
		error = null;
		setValid(true);
	}
	
	public void onEvent(Event event) {
		try {
			value = Integer.parseInt(event.getValue());
		} catch (NumberFormatException e) {
			value = null;
		}
		validate();
	}
}

{'fraction_non_alphanumeric': 0.15872702115042497, 'fraction_numerical': 0.000197667523225934, 'mean_word_length': 3.216666666666667, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '3011864', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1654, 'n_words': 324}