<commit_before>package main.java.author.view.tabs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.JSpinner.DefaultEditor;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.plaf.SpinnerUI;
import javax.swing.table.DefaultTableModel;

import main.java.author.controller.TabController;
import main.java.author.util.ObjectUtilFunctions;
import main.java.author.view.components.ImageCanvas;
import main.java.author.view.components.BehaviorTogglingRadioButton;
import main.java.author.view.global_constants.FontConstants;
import main.java.author.view.global_constants.ObjectEditorConstants;
import main.java.engine.objects.tower.TowerBehaviors;
import main.java.schema.tdobjects.TDObjectSchema;

public abstract class ObjectEditorTab extends EditorTab {

	protected DefaultTableModel listModel;
	protected JTable list;
	protected JSplitPane splitPane;
	protected List<JSpinner> spinnerFields;
	protected JButton createObjectButton;
	protected List<BehaviorTogglingRadioButton> behaviorTogglingButtons;
	protected List<ImageCanvas> imageCanvases;
	protected JTextField createObjectField;

	protected JButton deleteObjectButton;
	protected JButton towerImageButton;
	protected Border originalCreateObjectFieldBorder;
	protected ObjectTabViewBuilder myBuilder;
	protected HashMap<String, TDObjectSchema> objectMap;
	protected String objectName = "Default Object Name";

	public ObjectEditorTab(TabController towerController, String objectName) {
		super(towerController);
		this.objectName = objectName;
		init();
	}

	private void replaceKeysInObjectMap(String originalKey, String newKey) {
		TDObjectSchema objectSchema = objectMap.get(originalKey);
		objectMap.remove(originalKey);
		objectMap.put(newKey, objectSchema);
	}

	protected void addListeners() {

		deleteObjectButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (list.getRowCount() == 1) {
					JOptionPane.showMessageDialog(null,
							"Please design at least one " + objectName,
							"Alert!", JOptionPane.ERROR_MESSAGE);

					return;
				}
				String objectNameToDelete = getSelectedObjectName();
				objectMap.remove(objectNameToDelete);
				int rowToDelete = list.getSelectedRow();
				listModel.removeRow(rowToDelete);
				int newNumOfRow = listModel.getRowCount();
				if (newNumOfRow > 0) {
					list.setRowSelectionInterval(0, 0);
				}

			}
		});

		list.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {

					@Override
					public void valueChanged(ListSelectionEvent e) {
						if (list.getSelectedRow() != -1) {
							updateFieldDataUponNewSelection();
						}

					}
				});

		list.setDefaultEditor(Object.class, new TDObjectCellEditor());
		for (JSpinner field : spinnerFields) {
			field.addChangeListener(new ChangeListener() {

				@Override
				public void stateChanged(ChangeEvent e) {

					updateSchemaDataFromView();
				}
			});
		}
		if (behaviorTogglingButtons != null) {
			for (BehaviorTogglingRadioButton button : behaviorTogglingButtons) {
				button.addItemListener(new ItemListener() {

					@Override
					public void itemStateChanged(ItemEvent e) {
						BehaviorTogglingRadioButton button = (BehaviorTogglingRadioButton) e
								.getSource();
						button.toggle();
						updateSchemaDataFromView();

					}

				});
			}
		}

		createObjectButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String objectName = createObjectField.getText();

				objectName = objectName.trim().replaceAll(" +", " ");
				if (ObjectUtilFunctions.newObjectNameIsValid(objectName,
						objectMap)) {

					addObjectNameToList(objectName);
					createObjectField.setText("");
					createObjectField
							.setBorder(originalCreateObjectFieldBorder);
				} else {
					createObjectField.setBorder(new LineBorder(Color.red));
					createObjectField.selectAll();
					createObjectField.requestFocusInWindow();
					showInvalidObjectNameDialog();
				}
			}
		});

	}

	protected void addObjectNameToList(String objectName) {
		int indexToPlace = listModel.getRowCount();
		listModel.addRow(new String[] { objectName });
		list.setRowSelectionInterval(indexToPlace, indexToPlace);<commit_msg>added simple key listener<commit_after>package main.java.author.view.tabs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.JSpinner.DefaultEditor;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.plaf.SpinnerUI;
import javax.swing.table.DefaultTableModel;

import main.java.author.controller.TabController;
import main.java.author.util.ObjectUtilFunctions;
import main.java.author.view.components.ImageCanvas;
import main.java.author.view.components.BehaviorTogglingRadioButton;
import main.java.author.view.global_constants.FontConstants;
import main.java.author.view.global_constants.ObjectEditorConstants;
import main.java.engine.objects.tower.TowerBehaviors;
import main.java.schema.tdobjects.TDObjectSchema;

public abstract class ObjectEditorTab extends EditorTab {

	protected DefaultTableModel listModel;
	protected JTable list;
	protected JSplitPane splitPane;
	protected List<JSpinner> spinnerFields;
	protected JButton createObjectButton;
	protected List<BehaviorTogglingRadioButton> behaviorTogglingButtons;
	protected List<ImageCanvas> imageCanvases;
	protected JTextField createObjectField;

	protected JButton deleteObjectButton;
	protected JButton towerImageButton;
	protected Border originalCreateObjectFieldBorder;
	protected ObjectTabViewBuilder myBuilder;
	protected HashMap<String, TDObjectSchema> objectMap;
	protected String objectName = "Default Object Name";

	public ObjectEditorTab(TabController towerController, String objectName) {
		super(towerController);
		this.objectName = objectName;
		init();
	}

	private void replaceKeysInObjectMap(String originalKey, String newKey) {
		TDObjectSchema objectSchema = objectMap.get(originalKey);
		objectMap.remove(originalKey);
		objectMap.put(newKey, objectSchema);
	}

	private void createObject() {
		String objectName = createObjectField.getText();

		objectName = objectName.trim().replaceAll(" +", " ");
		if (ObjectUtilFunctions.newObjectNameIsValid(objectName, objectMap)) {

			addObjectNameToList(objectName);
			createObjectField.setText("");
			createObjectField.setBorder(originalCreateObjectFieldBorder);
		} else {
			createObjectField.setBorder(new LineBorder(Color.red));
			createObjectField.selectAll();
			createObjectField.requestFocusInWindow();
			showInvalidObjectNameDialog();
		}
	}

	protected void addListeners() {

		createObjectField.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					createObject();
				}
			}
		});

		deleteObjectButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (list.getRowCount() == 1) {
					JOptionPane.showMessageDialog(null,
							"Please design at least one " + objectName,
							"Alert!", JOptionPane.ERROR_MESSAGE);

					return;
				}
				String objectNameToDelete = getSelectedObjectName();
				objectMap.remove(objectNameToDelete);
				int rowToDelete = list.getSelectedRow();
				listModel.removeRow(rowToDelete);
				int newNumOfRow = listModel.getRowCount();
				if (newNumOfRow > 0) {
					list.setRowSelectionInterval(0, 0);
				}

			}
		});

		list.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {

					@Override
					public void valueChanged(ListSelectionEvent e) {
						if (list.getSelectedRow() != -1) {
							updateFieldDataUponNewSelection();
						}

					}
				});

		list.setDefaultEditor(Object.class, new TDObjectCellEditor());
		for (JSpinner field : spinnerFields) {
			field.addChangeListener(new ChangeListener() {

				@Override
				public void stateChanged(ChangeEvent e) {

					updateSchemaDataFromView();
				}
			});
		}
		if (behaviorTogglingButtons != null) {
			for (BehaviorTogglingRadioButton button : behaviorTogglingButtons) {
				button.addItemListener(new ItemListener() {

					@Override
					public void itemStateChanged(ItemEvent e) {
						BehaviorTogglingRadioButton button = (BehaviorTogglingRadioButton) e
								.getSource();
						button.toggle();
						updateSchemaDataFromView();

					}

				});
			}
		}

		createObjectButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				createObject();
			}

		});

	}

	protected void addObjectNameToList(String objectName) {
		int indexToPlace = listModel.getRowCount();
		listModel.addRow(new String[] { objectName });
		list.setRowSelectionInterval(indexToPlace, indexToPlace);
{'fraction_non_alphanumeric': 0.09667673716012085, 'fraction_numerical': 0.0008631851532153647, 'mean_word_length': 5.113984168865436, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '6266190', 'n_tokens_mistral': 3835, 'n_tokens_neox': 3346, 'n_words': 647}