/**
 * codage.java - 01/12/01 - 01/05/2016 - 29/08/22
 * Codage par permutation de lettres
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class codage extends JFrame implements ActionListener {
	static final long serialVersionUID = 220829L;
	TextArea ttexte;
	JLabel lalpha;
	JButton ssespace, code, efface;
	Font f;
	String alphabet, separ;
	boolean bcode;
	StringBuffer lettres;
	Random alea;

	private JLabel ajoutl (String s) {
		JLabel l = new JLabel (s);
		l.setBackground (Color.lightGray);
		l.setFont (f);
		return l;
	}

	private JButton ajoutb (String s) {
		JButton b = new JButton (s);
		b.setFont (f);
		b.addActionListener (this);
		return b;
	}

	public codage (String titre) {
		super (titre);
		f = new Font ("Arial", Font.PLAIN, 12);
		alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		lettres = new StringBuffer (alphabet.toLowerCase());
		separ = " \t!\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~€";
		alea = new Random();
		setLayout (new BorderLayout());
		JPanel p = new JPanel();
		p.setBackground (Color.lightGray);
		add (p, BorderLayout.NORTH);
		add (lalpha = ajoutl (alphabet + "--->" + lettres), BorderLayout.SOUTH);
		lalpha.setFont (f);
		add (ttexte = new TextArea (20, 20), BorderLayout.CENTER);
		ttexte.setFont (f);
//ttexte.setBackground (Color.white);
		p.add (efface = ajoutb ("efface"));
		p.add (ssespace = ajoutb ("lettres accolées"));
		p.add (code = ajoutb ("code"));
//setBackground (Color.lightGray);
	}

	private String majuscul (String s) {
		StringBuffer sb = new StringBuffer (s.toUpperCase());
		for (int i = 0; i < sb.length(); i++) {
			char c = sb.charAt (i);
			switch (c) {
				case 'À':
				case 'Á':
				case 'Â':
				case 'Ã':
				case 'Ä':
				case 'Å':
					c = 'A';
					break;
				case 'É':
				case 'È':
				case 'Ê':
				case 'Ë':
					c = 'E';
					break;
				case 'Ì':
				case 'Í':
				case 'Î':
				case 'Ï':
					c = 'I';
					break;
				case 'Ò':
				case 'Ó':
				case 'Ô':
				case 'Õ':
				case 'Ö':
					c = 'O';
					break;
				case 'Ù':
				case 'Ú':
				case 'Û':
				case 'Ü':
					c = 'U';
					break;
				case 'Ÿ':
				case 'Ý':
					c = 'Y';
					break;
				case 'Ç':
					c = 'C';
					break;
				case 'Ñ':
					c = 'N';
			}
			sb.setCharAt (i, c);
		}
		return sb.toString();
	}

	public void actionPerformed (ActionEvent evt) {
		String s = ttexte.getText();
		String s1 = "";
		if (evt.getSource() == efface)
			ttexte.setText ("");
		else if (evt.getSource() == ssespace) {
			s = majuscul (s);
			for (int i = 0; i < s.length (); i++) {
				char c = s.charAt (i);
				if (c == '\n' || separ.indexOf (c) == -1)
					s1 += c;
			}
			ttexte.setText (s1);
		} else if (evt.getSource() == code) {
			s = majuscul (s);
			String lettres1 = lettres.toString();
			int ll = lettres.length();
			for (int i = 0; i < ll; i++) {
				int j = (int) (alea.nextDouble  () * ll);
				char c = lettres.charAt (i);
				lettres.setCharAt (i, lettres.charAt (j));
				lettres.setCharAt (j, c);
			}
			s = s.toLowerCase();
			for (int i = 0; i < s.length(); i++) {
				char c = s.charAt (i);
				int j = lettres1.indexOf (c);
				if (j == -1)
					s1 += c;
				else
					s1 += lettres.charAt (j);
			}
			ttexte.setText (s1);
			lalpha.setText (alphabet + "--->" + lettres);
		}
	}

	public static void main (String args []) {
		int w = 700;
		int h = 400;
		codage f = new codage ("Codage d'un texte");
		f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		f.setSize (w, h);
		f.setVisible (true);
	}

}
