<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tonegod.gui.controls.lists;

import com.jme3.input.KeyInput;
import com.jme3.input.event.KeyInputEvent;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector4f;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import tonegod.gui.controls.buttons.ButtonAdapter;
import tonegod.gui.controls.menuing.Menu;
import tonegod.gui.controls.menuing.MenuItem;
import tonegod.gui.controls.text.TextField;
import tonegod.gui.core.Screen;

/**
 *
 * @author t0neg0d
 */
public abstract class ComboBox extends TextField {
	private ButtonAdapter btnArrowDown;
	private Menu DDList = null;
	float btnHeight;
	String ddUID;
	private int selectedIndex = -1;
	private Object selectedValue;
	private String selectedCaption;
	
	private int hlIndex;
	private Object hlValue;
	private String hlCaption;
	
	private int ssIndex;
	private Object ssValue;
	private String ssCaption;
	
	/**
	 * Creates a new instance of the ComboBox control
	 * 
	 * @param screen The screen control the Element is to be added to
	 * @param UID A unique String identifier for the Element
	 * @param position A Vector2f containing the x/y position of the Element
	 */
	public ComboBox(Screen screen, String UID, Vector2f position) {
		this(screen, UID, position,
			screen.getStyle("TextField").getVector2f("defaultSize"),
			screen.getStyle("TextField").getVector4f("resizeBorders"),
			screen.getStyle("TextField").getString("defaultImg")
		);
	}
	
	/**
	 * Creates a new instance of the ComboBox control
	 * 
	 * @param screen The screen control the Element is to be added to
	 * @param UID A unique String identifier for the Element
	 * @param position A Vector2f containing the x/y position of the Element
	 * @param dimensions A Vector2f containing the width/height dimensions of the Element
	 */
	public ComboBox(Screen screen, String UID, Vector2f position, Vector2f dimensions) {
		this(screen, UID, position, dimensions,
			screen.getStyle("TextField").getVector4f("resizeBorders"),
			screen.getStyle("TextField").getString("defaultImg")
		);
	}
	
	/**
	 * Creates a new instance of the ComboBox control
	 * 
	 * @param screen The screen control the Element is to be added to
	 * @param UID A unique String identifier for the Element
	 * @param position A Vector2f containing the x/y position of the Element
	 * @param dimensions A Vector2f containing the width/height dimensions of the Element
	 * @param resizeBorders A Vector4f containg the border information used when resizing the default image (x = N, y = W, z = E, w = S)
	 * @param defaultImg The default image to use for the Slider's track
	 */
	public ComboBox(Screen screen, String UID, Vector2f position, Vector2f dimensions, Vector4f resizeBorders, String defaultImg) {
		super(screen, UID, position, dimensions, resizeBorders, defaultImg);
		
		setScaleNS(false);
		setScaleEW(false);
		
		ddUID = UID + ":ddMenu";
		
		btnHeight = getHeight();//(screen.getStyle("Common").getFloat("defaultControlSize");
		
		btnArrowDown = new ButtonAdapter(screen, UID + ":ArrowDown",
			new Vector2f(
				getWidth(),
				0
			),
			new Vector2f(
				btnHeight,
				btnHeight
			)
		) {
			@Override
			public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
				if (validateListSize()) {
					DDList.showMenu(
						null,
						getElementParent().getAbsoluteX(),
						getElementParent().getAbsoluteY()-DDList.getHeight()
					);
				}
				screen.setTabFocusElement((ComboBox)getElementParent());
			}
		};
		btnArrowDown.setButtonIcon(18, 18, screen.getStyle("Common").getString("arrowDown"));
		btnArrowDown.setDockS(true);
		btnArrowDown.setDockW(true);
		this.addChild(btnArrowDown);
	}
	
	/**
	 * Adds a new list item to the drop-down list associated with this control
	 * 
	 * @param caption The String to display as the list item
	 * @param value A String value to associate with this list item
	 */
	public void addListItem(String caption, Object value) {
		if (DDList == null) {
			DDList = new Menu(screen, ddUID, new Vector2f(0,0), true) {
				@Override
				public void onMenuItemClicked(int index, Object value, boolean isToggled) {
					((ComboBox)getCallerElement()).setSelected(index, DDList.getMenuItem(index).getCaption(), value);
					screen.setTabFocusElement(((ComboBox)getCallerElement()));
					hide();
				}
			};
			DDList.setCallerElement(this);
			DDList.setPreferredSize(new Vector2f(getWidth(),DDList.getMenuItemHeight()*5));
		}
		DDList.addMenuItem(caption, value, null);
		
		if (DDList.getParent() == null) {
			screen.addElement(DDList);
		//	DDList.hide();
		}
		pack();
	}
	
	/**
	 * Inserts a new List Item at the specified index
	 * @param index - List index to insert new List Item
	 * @param caption - Caption for new List Item
	 * @param value - Object to store as value
	 */
	public void insertListItem(int index, String caption, Object value) {
		if (DDList != null) {
			DDList.insertMenuItem(index, caption, value, null);
		}
	}
	
	/**
	 * Removes the List Item at the specified index
	 * @param index 
	 */
	public void removeListItem(int index) {
		if (DDList != null) {
			DDList.removeMenuItem(index);
			if (selectedIndex > DDList.getMenuItems().size()-1)
				selectedIndex = DDList.getMenuItems().size()-1;
			if (!DDList.getMenuItems().isEmpty())
				this.setSelectedIndex(selectedIndex);
			else
				setTextFieldText("");
		} else {
			setTextFieldText("");
		}
	}
	
	/**
	 * Removes the first instance of a list item with the specified caption
	 * @param caption 
	 */
	public void removeListItem(String caption) {<commit_msg>Added toggling the drop-down list's visible state when clicking the arrow button<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tonegod.gui.controls.lists;

import com.jme3.input.KeyInput;
import com.jme3.input.event.KeyInputEvent;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector4f;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import tonegod.gui.controls.buttons.ButtonAdapter;
import tonegod.gui.controls.menuing.Menu;
import tonegod.gui.controls.menuing.MenuItem;
import tonegod.gui.controls.text.TextField;
import tonegod.gui.core.Screen;

/**
 *
 * @author t0neg0d
 */
public abstract class ComboBox extends TextField {
	private ButtonAdapter btnArrowDown;
	private Menu DDList = null;
	float btnHeight;
	String ddUID;
	private int selectedIndex = -1;
	private Object selectedValue;
	private String selectedCaption;
	
	private int hlIndex;
	private Object hlValue;
	private String hlCaption;
	
	private int ssIndex;
	private Object ssValue;
	private String ssCaption;
	
	private boolean DDListIsShowing = false;
	
	/**
	 * Creates a new instance of the ComboBox control
	 * 
	 * @param screen The screen control the Element is to be added to
	 * @param UID A unique String identifier for the Element
	 * @param position A Vector2f containing the x/y position of the Element
	 */
	public ComboBox(Screen screen, String UID, Vector2f position) {
		this(screen, UID, position,
			screen.getStyle("TextField").getVector2f("defaultSize"),
			screen.getStyle("TextField").getVector4f("resizeBorders"),
			screen.getStyle("TextField").getString("defaultImg")
		);
	}
	
	/**
	 * Creates a new instance of the ComboBox control
	 * 
	 * @param screen The screen control the Element is to be added to
	 * @param UID A unique String identifier for the Element
	 * @param position A Vector2f containing the x/y position of the Element
	 * @param dimensions A Vector2f containing the width/height dimensions of the Element
	 */
	public ComboBox(Screen screen, String UID, Vector2f position, Vector2f dimensions) {
		this(screen, UID, position, dimensions,
			screen.getStyle("TextField").getVector4f("resizeBorders"),
			screen.getStyle("TextField").getString("defaultImg")
		);
	}
	
	/**
	 * Creates a new instance of the ComboBox control
	 * 
	 * @param screen The screen control the Element is to be added to
	 * @param UID A unique String identifier for the Element
	 * @param position A Vector2f containing the x/y position of the Element
	 * @param dimensions A Vector2f containing the width/height dimensions of the Element
	 * @param resizeBorders A Vector4f containg the border information used when resizing the default image (x = N, y = W, z = E, w = S)
	 * @param defaultImg The default image to use for the Slider's track
	 */
	public ComboBox(Screen screen, String UID, Vector2f position, Vector2f dimensions, Vector4f resizeBorders, String defaultImg) {
		super(screen, UID, position, dimensions, resizeBorders, defaultImg);
		
		setScaleNS(false);
		setScaleEW(false);
		
		ddUID = UID + ":ddMenu";
		
		btnHeight = getHeight();//(screen.getStyle("Common").getFloat("defaultControlSize");
		
		btnArrowDown = new ButtonAdapter(screen, UID + ":ArrowDown",
			new Vector2f(
				getWidth(),
				0
			),
			new Vector2f(
				btnHeight,
				btnHeight
			)
		) {
			@Override
			public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
				if (validateListSize()) {
					if (!DDList.getIsVisible()) {
						DDList.showMenu(
							null,
							getElementParent().getAbsoluteX(),
							getElementParent().getAbsoluteY()-DDList.getHeight()
						);
					}
				}
				screen.setTabFocusElement((ComboBox)getElementParent());
			}
		};
		btnArrowDown.setButtonIcon(18, 18, screen.getStyle("Common").getString("arrowDown"));
		btnArrowDown.setDockS(true);
		btnArrowDown.setDockW(true);
		this.addChild(btnArrowDown);
	}
	
	/**
	 * Adds a new list item to the drop-down list associated with this control
	 * 
	 * @param caption The String to display as the list item
	 * @param value A String value to associate with this list item
	 */
	public void addListItem(String caption, Object value) {
		if (DDList == null) {
			DDList = new Menu(screen, ddUID, new Vector2f(0,0), true) {
				@Override
				public void onMenuItemClicked(int index, Object value, boolean isToggled) {
					((ComboBox)getCallerElement()).setSelected(index, DDList.getMenuItem(index).getCaption(), value);
					screen.setTabFocusElement(((ComboBox)getCallerElement()));
					hide();
				}
			};
			DDList.setCallerElement(this);
			DDList.setPreferredSize(new Vector2f(getWidth(),DDList.getMenuItemHeight()*5));
		}
		DDList.addMenuItem(caption, value, null);
		
		if (DDList.getParent() == null) {
			screen.addElement(DDList);
		//	DDList.hide();
		}
		pack();
	}
	
	/**
	 * Inserts a new List Item at the specified index
	 * @param index - List index to insert new List Item
	 * @param caption - Caption for new List Item
	 * @param value - Object to store as value
	 */
	public void insertListItem(int index, String caption, Object value) {
		if (DDList != null) {
			DDList.insertMenuItem(index, caption, value, null);
		}
	}
	
	/**
	 * Removes the List Item at the specified index
	 * @param index 
	 */
	public void removeListItem(int index) {
		if (DDList != null) {
			DDList.removeMenuItem(index);
			if (selectedIndex > DDList.getMenuItems().size()-1)
				this.setSelectedIndex(DDList.getMenuItems().size()-1);
			if (!DDList.getMenuItems().isEmpty())
				this.setSelectedIndex(selectedIndex);
			else
				setTextFieldText("");
		} else {
			setTextFieldText("");
		}
	}
	
	/**
	 * Removes the first instance of a list item with the specified caption
	 * @param caption 
	 */
	public void removeListItem(String caption) {
{'fraction_non_alphanumeric': 0.10527212332850694, 'fraction_numerical': 0.006643386423643642, 'mean_word_length': 4.02224123182207, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, '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': '7382051', 'n_tokens_mistral': 3966, 'n_tokens_neox': 3468, 'n_words': 1074}