<commit_before>package org.zaproxy.zap.view.widgets;

import java.awt.Component;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

import org.parosproxy.paros.model.Model;
import org.parosproxy.paros.model.Session;
import org.zaproxy.zap.model.Context;

/**
 * A ComboBox widget that displays the list of existing {@link Context Contexts}.
 * 
 * NOTE: Does not automatically refresh when the Contexts have changed. For this, make sure you
 * manually call {@link #reloadContexts()}.
 * 
 */
public class ContextSelectComboBox extends JComboBox<Context> {

	private static final long serialVersionUID = 6177823947839642740L;

	@SuppressWarnings("unchecked")
	public ContextSelectComboBox() {
		super();
		reloadContexts();
		this.setRenderer(new ContextComboBoxRenderer());
	}

	/**
	 * Reloads/refreshes the list of {@link Context Contexts} from the {@link Session}.
	 */
	public void reloadContexts() {
		Context selected = (Context) getSelectedItem();
		List<Context> contexts = Model.getSingleton().getSession().getContexts();
		Context[] contextsArray = contexts.toArray(new Context[contexts.size()]);
		ComboBoxModel<Context> model = new DefaultComboBoxModel<>(contextsArray);
		this.setModel(model);
		this.setSelectedItem(selected);
	}

	/**
	 * A renderer for properly displaying the name of Context in a ComboBox.
	 */
	private static class ContextComboBoxRenderer extends BasicComboBoxRenderer {

		private static final Border BORDER = new EmptyBorder(2, 8, 2, 8);
		private static final long serialVersionUID = 3272133514462699823L;

		@Override
		@SuppressWarnings("rawtypes")
		public Component getListCellRendererComponent(JList list, Object value, int index,
				boolean isSelected, boolean cellHasFocus) {
			super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

			if (value != null) {
				Context item = (Context) value;
				setText(item.getName());
				setBorder(BORDER);
			}
			return this;
		}
	}
}<commit_msg>Added utility method for quickly obtaining the selected context.<commit_after>package org.zaproxy.zap.view.widgets;

import java.awt.Component;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

import org.parosproxy.paros.model.Model;
import org.parosproxy.paros.model.Session;
import org.zaproxy.zap.model.Context;

/**
 * A ComboBox widget that displays the list of existing {@link Context Contexts}.
 * 
 * NOTE: Does not automatically refresh when the Contexts have changed. For this, make sure you
 * manually call {@link #reloadContexts()}.
 * 
 */
public class ContextSelectComboBox extends JComboBox<Context> {

	private static final long serialVersionUID = 6177823947839642740L;

	@SuppressWarnings("unchecked")
	public ContextSelectComboBox() {
		super();
		reloadContexts();
		this.setRenderer(new ContextComboBoxRenderer());
	}

	/**
	 * Reloads/refreshes the list of {@link Context Contexts} from the {@link Session}.
	 */
	public void reloadContexts() {
		Context selected = (Context) getSelectedItem();
		List<Context> contexts = Model.getSingleton().getSession().getContexts();
		Context[] contextsArray = contexts.toArray(new Context[contexts.size()]);
		ComboBoxModel<Context> model = new DefaultComboBoxModel<>(contextsArray);
		this.setModel(model);
		this.setSelectedItem(selected);
	}

	/**
	 * Returns the selected {@link Context}. Behaves the same as {@link #getSelectedItem()}, but
	 * performs a cast.
	 * 
	 * @see #getSelectedItem()
	 * @return the selected context, if any
	 */
	public Context getSelectedContext() {
		return (Context) getSelectedItem();
	}

	/**
	 * A renderer for properly displaying the name of Context in a ComboBox.
	 */
	private static class ContextComboBoxRenderer extends BasicComboBoxRenderer {

		private static final Border BORDER = new EmptyBorder(2, 8, 2, 8);
		private static final long serialVersionUID = 3272133514462699823L;

		@Override
		@SuppressWarnings("rawtypes")
		public Component getListCellRendererComponent(JList list, Object value, int index,
				boolean isSelected, boolean cellHasFocus) {
			super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

			if (value != null) {
				Context item = (Context) value;
				setText(item.getName());
				setBorder(BORDER);
			}
			return this;
		}
	}
}
{'fraction_non_alphanumeric': 0.10553083280356007, 'fraction_numerical': 0.017800381436745075, 'mean_word_length': 4.997458703939009, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5561478', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1364, 'n_words': 377}