<commit_before>package edu.kit.iks.Cryptographics.Caesar.Experiment;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JTextField;

import edu.kit.iks.Cryptographics.VisualizationContainerController;
import edu.kit.iks.CryptographicsLib.AbstractVisualizationController;
import edu.kit.iks.CryptographicsLib.AbstractVisualizationInfo;

/**
 * Controller for the first and second step of the experiment phase. When user has to put input and
 * encrypt it and in the second step to decrypt a given cipher. Controls the needed elements from
 * CaesarUpperView, !!see CGeneralView for more details!!.
 * 
 * @author <NAME>.
 * 
 */
public class CryptoController extends AbstractVisualizationController {

	/**
	 * Model that is needed for computations.
	 */
	private CryptoModel model;

	/**
	 * Needed
	 */
	private int editableFields;

	private boolean decryptionPhase = false;

	/**
	 * Constructor.
	 * 
	 * @param visualizationInfo
	 */
	public CryptoController(AbstractVisualizationInfo visualizationInfo) {
		super(visualizationInfo);

	}

	@Override
	public void loadView() {
		this.view = new CryptoView();
		this.model = new CryptoModel();

		// Create all needed ActionListener.
		this.getView().getGenerator().addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				char[] string = getModel().generatePlainText();
				int key = getModel().generateKey();
				setEditableFields(string.length);

				// TODO: String and Key generator!
				getView().start(string, key);

				// generate ActionListener.
				generateListener(string);

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

		});
		this.getView().getKey().addFocusListener(new FocusListener() {
			// TODO: make the strings to set dynamically. Avoid hard code.
			@Override
			public void focusLost(FocusEvent e) {
				if (getView().getKey().isEditable()) {
					getView().getKey().setText("Key");
				}

			}

			// TODO: make the strings to set dynamically. Avoid hard code.
			@Override
			public void focusGained(FocusEvent e) {
				if (getView().getKey().isEditable()) {
					getView().getKey().setText("");
					popupKeyboard();
				}
			}
		});
		this.getView().getKey().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					// TODO: Need to take the explanation from other resources. At the moment it
					// leads to invalid behaviour.
					String explanationContent = getView().getExplanations()
							.getText();
					int key = Integer.parseInt(getView().getKey().getText());
					if (getModel().handleKeyInput(key)) {
						getView()
								.getExplanations()
								.setText(
										explanationContent
												+ "<br>"
												+ "<br>This key is ok. Now put your name into the bigger box to the left.");
						getView().getKey().setBorder(
								BorderFactory.createLineBorder(Color.green));
						getView().getKey().setEditable(false);
					} else {
						getView()
								.getExplanations()
								.setText(
										explanationContent
												+ "<br>"
												+ "<br>This key is not valid. Please put a number between 1 and 25.<br>"
												+ "For demonstration purposes the keys between -1 and -25 are not necessary<br>"
												+ "therefore not possible, but could be used as keys too. And 0 as key has no<br>"
												+ " sense, if you dont understand why, then go back to Introduction.");
						getView().getKey().setBorder(
								BorderFactory.createLineBorder(Color.red));
					}
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
		this.getView().getInput().addFocusListener(new FocusListener() {
			// TODO: make the strings to set dynamically. Avoid hard code.
			@Override
			public void focusGained(FocusEvent e) {
				getView().getInput().setText("");
				popupKeyboard();

			}

			// TODO: make the strings to set dynamically. Avoid hard code.
			@Override
			public void focusLost(FocusEvent e) {
				getView().getInput().setText("Put your name here.");

			}

		});
		this.getView().getInput().addActionListener(new ActionListener() {

			/*
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event. ActionEvent)
			 */
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO: At the moment the user has to put first the key and then his name. It is
				// invalid behaviour. There must be no order what to put in first. Need some more
				// checks.

				String input = getView().getInput().getText();
				// TODO: check input for validity!
				if (getModel().handleInput(input)) {
					// refactor the input into an character array.
					try {
						int key = Integer
								.parseInt(getView().getKey().getText());
						char[] inputChars = new char[input.length()];
						input = input.toUpperCase();
						input.getChars(0, input.length(), inputChars, 0);
						setEditableFields(inputChars.length);
						getView().getKey().setBorder(null);

						// load the view!
						getView().start(inputChars, key);

						// Generate Listener for the userOutput JTextfield
						generateListener(inputChars);
					} catch (NumberFormatException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else {
					// TODO: Input was invalid. Pls make another one.
					getView().getExplanations().setText(
							"Your input is invalid. Please try another one!");
				}

			}<commit_msg>Fixed the input phase in Experiment. Now it is possible to make input without considering the right order. (not any more First the key and then the input.)<commit_after>package edu.kit.iks.Cryptographics.Caesar.Experiment;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JTextField;

import edu.kit.iks.Cryptographics.VisualizationContainerController;
import edu.kit.iks.CryptographicsLib.AbstractVisualizationController;
import edu.kit.iks.CryptographicsLib.AbstractVisualizationInfo;

/**
 * Controller for the first and second step of the experiment phase. When user has to put input and
 * encrypt it and in the second step to decrypt a given cipher. Controls the needed elements from
 * CaesarUpperView, !!see CGeneralView for more details!!.
 * 
 * @author <NAME>.
 * 
 */
public class CryptoController extends AbstractVisualizationController {

	/**
	 * Model that is needed for computations.
	 */
	private CryptoModel model;

	/**
	 * Needed
	 */
	private int editableFields;

	private boolean decryptionPhase = false;

	/**
	 * Constructor.
	 * 
	 * @param visualizationInfo
	 */
	public CryptoController(AbstractVisualizationInfo visualizationInfo) {
		super(visualizationInfo);

	}

	@Override
	public void loadView() {
		this.view = new CryptoView();
		this.model = new CryptoModel();
		this.editableFields = 2;

		// Create all needed ActionListener.
		this.getView().getGenerator().addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				char[] string = getModel().generatePlainText();
				int key = getModel().generateKey();
				setEditableFields(string.length);

				// TODO: String and Key generator!
				getView().start(string, key);

				// generate ActionListener.
				generateListener(string);

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

		});
		this.getView().getKey().addFocusListener(new FocusListener() {
			// TODO: make the strings to set dynamically. Avoid hard code.
			@Override
			public void focusLost(FocusEvent e) {
				if (getView().getKey().isEditable()) {
					getView().getKey().setText("Key");
					getView().remove(getView().getKeyboard());
				}

			}

			// TODO: make the strings to set dynamically. Avoid hard code.
			@Override
			public void focusGained(FocusEvent e) {
				if (getView().getKey().isEditable()) {
					getView().getKey().setText("");
					popupKeyboard();
				}
			}
		});
		this.getView().getKey().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					// TODO: Need to take the explanation from other resources. At the moment it
					// leads to invalid behaviour.
					String explanationContent = getView().getExplanations()
							.getText();
					int key = Integer.parseInt(getView().getKey().getText());
					if (getModel().handleKeyInput(key)
							&& (getEditableFields() - 1) != 0) {
						getView()
								.getExplanations()
								.setText(
										explanationContent
												+ "<br>"
												+ "<br>This key is ok. Now put your name into the bigger box to the left.");
						getView().getKey().setBorder(
								BorderFactory.createLineBorder(Color.green));
						getView().getKey().setEditable(false);
						setEditableFields((getEditableFields() - 1));
					} else if (getModel().handleKeyInput(key)
							&& (getEditableFields() - 1) == 0) {
						try {
							String input = getView().getInput().getText();
							char[] inputChars = new char[input.length()];
							input = input.toUpperCase();
							input.getChars(0, input.length(), inputChars, 0);
							setEditableFields(inputChars.length);
							getView().getKey().setBorder(null);

							// load the view!
							getView().start(inputChars, key);

							// Generate Listener for the userOutput JTextfield
							generateListener(inputChars);
						} catch (NumberFormatException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

					} else {
						getView()
								.getExplanations()
								.setText(
										explanationContent
												+ "<br>"
												+ "<br>This key is not valid. Please put a number between 1 and 25.<br>"
												+ "For demonstration purposes the keys between -1 and -25 are not necessary<br>"
												+ "therefore not possible, but could be used as keys too. And 0 as key has no<br>"
												+ " sense, if you dont understand why, then go back to Introduction.");
						getView().getKey().setBorder(
								BorderFactory.createLineBorder(Color.red));
					}
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
		this.getView().getInput().addFocusListener(new FocusListener() {
			// TODO: make the strings to set dynamically. Avoid hard code.
			@Override
			public void focusGained(FocusEvent e) {
				if (getView().getInput().isEditable()) {
					getView().getInput().setText("");
					popupKeyboard();
				}

			}

			// TODO: make the strings to set dynamically. Avoid hard code.
			@Override
			public void focusLost(FocusEvent e) {
				if (getView().getInput().isEditable()) {
					getView().getInput().setText("Put your name here.");
					getView().remove(getView().getKeyboard());
				}
			}

		});
		this.getView().getInput().addActionListener(new ActionListener() {

			/*
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event. ActionEvent)
			 */
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO: At the moment the user has to put first the key and then his name. It is
				// invalid behaviour. There must be no order what to put in first. Need some more
				// checks.
				String explanationContent = getView().getExplanations()
						.getText();
				String input = getView().getInput().getText();

				// TODO: check input for validity!
				if (getModel().handleInput(input) && (getEditableFields() - 1) != 0) {
					getView()
							.getExplanations()
							.setText(
									explanationContent
											+ "<br>"
											+ "<br>This input is ok. Now only the key is left.");
					getView().getInput().setBorder(
							BorderFactory.createLineBorder(Color.green));
					getView().getInput().setEditable(false);
					setEditableFields((getEditableFields() - 1));
				} else if (getModel().handleInput(input)
						&& (getEditableFields() - 1) == 0) {

					// refactor the input into an character array.
					try {
						int key = Integer
								.parseInt(getView().getKey().getText());
						char[] inputChars = new char[input.length()];
						input = input.toUpperCase();
						input.getChars(0, input.length(), inputChars, 0);
						setEditableFields(inputChars.length);
						getView().getKey().setBorder(null);

						// load the view!
						getView().start(inputChars, key);

						// Generate Listener for the userOutput JTextfield
						generateListener(inputChars);
					} catch (NumberFormatException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else {
					// TODO: Input was invalid. Pls make another one.
					getView().getExplanations().setText(
							"Your input is invalid. Please try another one!");
				}

			}
{'fraction_non_alphanumeric': 0.11932144910868316, 'fraction_numerical': 0.0029470960322024154, 'mean_word_length': 2.9581792318634426, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '1895483', 'n_tokens_mistral': 5215, 'n_tokens_neox': 3883, 'n_words': 1099}