/*
 * This is a programm to translate latin letters, words, sentences, books and so on, to morse or written morse or backwards.
 * 
 * Copyright (C) 2014 - 2015  Marcel Michael Kapfer (marcelmichaelkapfer@yahoo.co.nz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation  version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/.
 * 
 * Version: 1.1.0
 * 
 */

package de.marcel_kapfer.c.morseconverter;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JMenu;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JScrollPane;
import javax.swing.JPopupMenu;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ResourceBundle;

public class Morse extends JFrame {
	private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("de.marcel_kapfer.c.morseconverter.strings"); //$NON-NLS-1$

	/**
	 * 
	 */
	private static final long serialVersionUID = -4342211364299252760L;
	private JPanel contentPane;
	private JTextArea tfOutput;
	private JTextArea tfInput;
	private JRadioButton rdbtnwmll;
	private JRadioButton rdbtnllwm;
	private JRadioButton rdbtnmll;
	private JRadioButton rdbtnllm;
	private JButton btnConvert;
	private JButton btnSelectInput;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JCheckBoxMenuItem chckbxmntmClipboardAutomatic;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args){ 
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Morse frame = new Morse();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Morse() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(Morse.class.getResource("/de/marcel_kapfer/c/morseconverter/icon.png")));
		setResizable(false);
		setTitle(BUNDLE.getString("Morse.this.title")); //$NON-NLS-1$
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 792, 443);
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JMenuBar menuBar = new JMenuBar();
		menuBar.setBorderPainted(false);
		menuBar.setBounds(0, 0, 820, 21);
		contentPane.add(menuBar);

		JMenu mnMain = new JMenu(BUNDLE.getString("Morse.mnMain.text")); //$NON-NLS-1$
		menuBar.add(mnMain);

		JMenuItem mntmAbout = new JMenuItem(BUNDLE.getString("Morse.mntmAbout.text")); //$NON-NLS-1$
		mntmAbout.setToolTipText(BUNDLE.getString("Morse.mntmAbout.toolTipText")); //$NON-NLS-1$
		mntmAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				About.main(null);
			}
		});
		mnMain.add(mntmAbout);

		JMenuItem mntmBug = new JMenuItem(BUNDLE.getString("Morse.mntmBug.text")); //$NON-NLS-1$
		mntmBug.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Bug.main(null);
			}
		});
		mntmBug.setToolTipText(BUNDLE.getString("Morse.mntmBug.toolTipText")); //$NON-NLS-1$
		mnMain.add(mntmBug);

		JMenuItem mntmHowTo = new JMenuItem(BUNDLE.getString("Morse.mntmHowTo.text")); //$NON-NLS-1$
		mntmHowTo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				HowTo.main(null);
			}
		});
		mntmHowTo.setToolTipText(BUNDLE.getString("Morse.mntmHowTo.toolTipText")); //$NON-NLS-1$
		mnMain.add(mntmHowTo);
		JMenuItem mntmMissingCode = new JMenuItem(BUNDLE.getString("Morse.mntmMissingCode.text")); //$NON-NLS-1$
		mntmMissingCode
				.setToolTipText(BUNDLE.getString("Morse.mntmMissingCode.toolTipText")); //$NON-NLS-1$
		mnMain.add(mntmMissingCode);
		
		JMenuItem mntmExit = new JMenuItem(BUNDLE.getString("Morse.mntmExit.text")); //$NON-NLS-1$
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				dispose();
			}
		});
		mnMain.add(mntmExit);
		mntmMissingCode.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MissingCode.main(null);
			}
		});

		JMenu mnClipboard = new JMenu(BUNDLE.getString("Morse.mnClipboard.text")); //$NON-NLS-1$
		menuBar.add(mnClipboard);

		JMenuItem mntnPaste = new JMenuItem(BUNDLE.getString("Morse.mntnPaste.text")); //$NON-NLS-1$
		mntnPaste
				.setToolTipText(BUNDLE.getString("Morse.mntnPaste.toolTipText")); //$NON-NLS-1$
		mntnPaste.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onPaste(tfInput);
			}
		});
		mnClipboard.add(mntnPaste);

		JMenuItem mntmCopy = new JMenuItem(BUNDLE.getString("Morse.mntmCopy.text")); //$NON-NLS-1$
		mntmCopy.setToolTipText(BUNDLE.getString("Morse.mntmCopy.toolTipText")); //$NON-NLS-1$
		mntmCopy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onCopy(tfOutput);
			}
		});
		mnClipboard.add(mntmCopy);

		chckbxmntmClipboardAutomatic = new JCheckBoxMenuItem(
				BUNDLE.getString("Morse.chckbxmntmClipboardAutomatic.text")); //$NON-NLS-1$
		chckbxmntmClipboardAutomatic
				.setToolTipText(BUNDLE.getString("Morse.chckbxmntmClipboardAutomatic.toolTipText")); //$NON-NLS-1$
		mnClipboard.add(chckbxmntmClipboardAutomatic);

		JScrollPane scrollPaneInput = new JScrollPane();
		scrollPaneInput.setBounds(12, 60, 758, 140);
		contentPane.add(scrollPaneInput);

		tfInput = new JTextArea();
		tfInput.setToolTipText(BUNDLE.getString("Morse.tfInput.toolTipText")); //$NON-NLS-1$
		scrollPaneInput.setViewportView(tfInput);
		tfInput.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					ChooseProcess();
				}
			}

			@Override
			public void keyTyped(KeyEvent e) {
			}
		});
		tfInput.setLineWrap(true);

		JPopupMenu popupMenuIp = new JPopupMenu();
		addPopup(tfInput, popupMenuIp);

		JMenuItem mntmIpPaste = new JMenuItem(BUNDLE.getString("Morse.mntmIpPaste.text")); //$NON-NLS-1$
		mntmIpPaste
				.setToolTipText(BUNDLE.getString("Morse.mntmIpPaste.toolTipText")); //$NON-NLS-1$
		mntmIpPaste.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onPaste(tfInput);
			}
		});
		popupMenuIp.add(mntmIpPaste);

		JMenuItem mntnIpSelect = new JMenuItem(BUNDLE.getString("Morse.mntnIpSelect.text")); //$NON-NLS-1$
		mntnIpSelect.setToolTipText(BUNDLE.getString("Morse.mntnIpSelect.toolTipText")); //$NON-NLS-1$
		mntnIpSelect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tfInput.requestFocus();
				tfInput.selectAll();
			}
		});
		popupMenuIp.add(mntnIpSelect);

		JMenuItem mntmIpDelete = new JMenuItem(BUNDLE.getString("Morse.mntmIpDelete.text")); //$NON-NLS-1$
		mntmIpDelete.setToolTipText(BUNDLE.getString("Morse.mntmIpDelete.toolTipText")); //$NON-NLS-1$
		mntmIpDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tfInput.setText(null);
			}
		});
		popupMenuIp.add(mntmIpDelete);

		JLabel lblEnterYourText = new JLabel(BUNDLE.getString("Morse.lblEnterYourText.text")); //$NON-NLS-1$
		lblEnterYourText.setBounds(10, 33, 178, 15);
		contentPane.add(lblEnterYourText);

		rdbtnwmll = new JRadioButton(BUNDLE.getString("Morse.rdbtnwmll.text")); //$NON-NLS-1$
		rdbtnwmll
				.setToolTipText(BUNDLE.getString("Morse.rdbtnwmll.toolTipText")); //$NON-NLS-1$
		buttonGroup.add(rdbtnwmll);
		rdbtnwmll.setBounds(12, 208, 250, 23);
		contentPane.add(rdbtnwmll);

		rdbtnllwm = new JRadioButton(BUNDLE.getString("Morse.rdbtnllwm.text")); //$NON-NLS-1$
		rdbtnllwm
				.setToolTipText(BUNDLE.getString("Morse.rdbtnllwm.toolTipText")); //$NON-NLS-1$
		buttonGroup.add(rdbtnllwm);
		rdbtnllwm.setBounds(266, 208, 250, 23);
		contentPane.add(rdbtnllwm);

		rdbtnmll = new JRadioButton(BUNDLE.getString("Morse.rdbtnmll.text")); //$NON-NLS-1$
		rdbtnmll.setToolTipText(BUNDLE.getString("Morse.rdbtnmll.toolTipText")); //$NON-NLS-1$
		buttonGroup.add(rdbtnmll);
		rdbtnmll.setBounds(12, 235, 250, 23);
		contentPane.add(rdbtnmll);

		rdbtnllm = new JRadioButton(BUNDLE.getString("Morse.rdbtnllm.text")); //$NON-NLS-1$
		rdbtnllm.setToolTipText(BUNDLE.getString("Morse.rdbtnllm.toolTipText")); //$NON-NLS-1$
		buttonGroup.add(rdbtnllm);
		rdbtnllm.setBounds(266, 235, 250, 23);
		contentPane.add(rdbtnllm);

		btnConvert = new JButton(BUNDLE.getString("Morse.btnConvert.text")); //$NON-NLS-1$
		btnConvert
				.setToolTipText(BUNDLE.getString("Morse.btnConvert.toolTipText")); //$NON-NLS-1$
		btnConvert.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ChooseProcess();
			}
		});
		btnConvert.setBounds(612, 218, 160, 25);
		contentPane.add(btnConvert);

		JScrollPane scrollPaneOutput = new JScrollPane();
		scrollPaneOutput.setBounds(12, 266, 758, 140);
		contentPane.add(scrollPaneOutput);

		tfOutput = new JTextArea();
		tfOutput.setToolTipText(BUNDLE.getString("Morse.tfOutput.toolTipText")); //$NON-NLS-1$
		scrollPaneOutput.setViewportView(tfOutput);
		tfOutput.setEditable(false);
		tfOutput.setLineWrap(true);

		JPopupMenu popupMenuOp = new JPopupMenu();
		addPopup(tfOutput, popupMenuOp);

		JMenuItem mntmOpClipboardCopy = new JMenuItem(BUNDLE.getString("Morse.mntmOpClipboardCopy.text")); //$NON-NLS-1$
		mntmOpClipboardCopy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onCopy(tfOutput);
			}
		});
		popupMenuOp.add(mntmOpClipboardCopy);

		JMenuItem mntmOpSelect = new JMenuItem(BUNDLE.getString("Morse.mntmOpSelect.text")); //$NON-NLS-1$
		mntmOpSelect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tfOutput.requestFocus();
				tfOutput.selectAll();
			}
		});
		popupMenuOp.add(mntmOpSelect);

		btnSelectInput = new JButton(BUNDLE.getString("Morse.btnSelectInput.text")); //$NON-NLS-1$
		btnSelectInput.setToolTipText(BUNDLE.getString("Morse.btnSelectInput.toolTipText")); //$NON-NLS-1$
		btnSelectInput.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tfInput.requestFocus();
				tfInput.selectAll();
				btnSelectInput.setVisible(false);
			}
		});
		btnSelectInput.setBounds(569, 28, 201, 25);
		btnSelectInput.setVisible(false);
		contentPane.add(btnSelectInput);
	}

	/*
	 * Choose the convert process
	 */
	private void ChooseProcess() {
		if (rdbtnwmll.isSelected()) {
			tfOutput.setText("Process not available");
			WmLl();
		} else if (rdbtnllwm.isSelected()) {
			LlWm();
		} else if (rdbtnmll.isSelected()) {
			MLl();
		} else if (rdbtnllm.isSelected()) {
			LlM();
		} else {
			tfOutput.setText("Please select a convert process");
			rdbtnwmll.requestFocus();
		}
	}

	/*
	 * Tasks to do after the converting process
	 */
	private void After() {
		tfOutput.requestFocus();
		tfOutput.selectAll();
		btnSelectInput.setVisible(true);
		if (chckbxmntmClipboardAutomatic.isSelected()) {
			onCopy(tfOutput);
		}
	}

	/*
	 * Code for converting latin letters into written morse
	 */
	private void LlWm() {
		try {
			// Variables
			StringBuffer input = new StringBuffer();
			input = input.replace(0, input.length(), tfInput.getText()
					.toUpperCase());
			StringBuffer output = new StringBuffer();
			if (input.toString().equals("")) {
				tfOutput.setText("Please enter at least one character");
				After();
			} else if (input.toString().equals("LETTERSPACE")) {
				tfOutput.setText("#");
				After();
			} else if (input.toString().equals("END OF WORK")) {
				tfOutput.setText("000101");
				After();
			} else if (input.toString().equals("ERROR")) {
				tfOutput.setText("00000000");
				After();
			} else if (input.toString().equals("STARTING SIGNAL")) {
				tfOutput.setText("10101");
				After();
			} else if (input.toString().equals("ENDING SIGNAL")) {
				tfOutput.setText("01010");
				After();
			} else if (input.toString().equals("UNDERSTOOD")) {
				tfOutput.setText("00010");
				After();
			} else if (input.toString().equals("WAIT")) {
				tfOutput.setText("01000");
				After();
			} else if (input.toString().equals("SOS")) {
				tfOutput.setText("000111000");
				After();
			} else if (input.toString().equals("LETTER SPACE")) {
				tfOutput.setText("#");
				After();
			} else if (input.toString().equals("WORD SPACE")) {
				tfOutput.setText("+");
				After();
			} else {
				for (int c = input.length(); c > 0; c--) {
					if (input.toString().startsWith(" ")) {
						if(output.toString().endsWith("#")){
							output.delete(output.length() -1 , output.length());
						}
						output.append("+");
						input.delete(0, 1);
					} else if (input.toString().startsWith("A")) {
						output.append("01#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("B")) {
						output.append("1000#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("C")) {
						output.append("1010#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("D")) {
						output.append("100#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("E")) {
						output.append("0#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("F")) {
						output.append("0010#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("G")) {
						output.append("110#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("H")) {
						output.append("0000#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("I")) {
						output.append("00#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("J")) {
						output.append("0111#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("K")) {
						output.append("101#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("L")) {
						output.append("0100#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("M")) {
						output.append("11#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("N")) {
						output.append("10#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("O")) {
						output.append("111#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("P")) {
						output.append("0110#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("Q")) {
						output.append("1101#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("R")) {
						output.append("010#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("S")) {
						output.append("000#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("T")) {
						output.append("1#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("U")) {
						output.append("001#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("V")) {
						output.append("0001#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("W")) {
						output.append("011#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("X")) {
						output.append("1001#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("Y")) {
						output.append("1011#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("Z")) {
						output.append("1100#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("0")) {
						output.append("11111#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("1")) {
						output.append("01111#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("2")) {
						output.append("00111#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("3")) {
						output.append("00011#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("4")) {
						output.append("00001#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("5")) {
						output.append("00000#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("6")) {
						output.append("10000#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("7")) {
						output.append("11000#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("8")) {
						output.append("11100#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("9")) {
						output.append("11110#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("Ä")) {
						output.append("0101#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("Ö")) {
						output.append("1110#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("Ü")) {
						output.append("0011#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("ß")) {
						output.append("00011000#");
						input.delete(0, 1);
					} else if (input.toString().startsWith(".")) {
						output.append("010101#");
						input.delete(0, 1);
					} else if (input.toString().startsWith(",")) {
						output.append("110011#");
						input.delete(0, 1);
					} else if (input.toString().startsWith(":")) {
						output.append("111000#");
						input.delete(0, 1);
					} else if (input.toString().startsWith(";")) {
						output.append("101010#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("?")) {
						output.append("001100#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("!")) {
						output.append("101011#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("-")) {
						output.append("100001#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("_")) {
						output.append("001101#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("(")) {
						output.append("10110#");
						input.delete(0, 1);
					} else if (input.toString().startsWith(")")) {
						output.append("101101#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("=")) {
						output.append("10001#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("+")) {
						output.append("01010#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("/")) {
						output.append("10010#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("@")) {
						output.append("011010#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("'")) {
						output.append("011110#");
						input.delete(0, 1);
					} else if (input.toString().startsWith("$")) {
						output.append("0001001#");
						input.delete(0, 1);
					} else {
						tfOutput.setText("Code not listed or wrong.");
					}				
				}
				if(output.toString().endsWith("#")){
					output.delete(output.length() -1 , output.length());
				}
				tfOutput.setText(output.toString());
				After();
			}
		} catch (Exception e) {
			tfOutput.setText("Some error occured. I'm sorry!");
		}
	}

	/*
	 * Code for converting writtenMorse to latin letters
	 */
	private void WmLl() {
		try {
			// Variables
			String input;
			StringBuffer output = new StringBuffer();
			input = tfInput.getText().toUpperCase() + "#";
			StringBuffer inputToSign = new StringBuffer(input);
			while (inputToSign.toString().equals("#") == false) {
				int d = 0;
				boolean signFull = true;
				StringBuffer sign = new StringBuffer();
				while (signFull) {
					if (inputToSign.toString().charAt(d) == '+'
							|| inputToSign.toString().charAt(d) == '#') {
						if (d == 0) {
							if (inputToSign.toString().startsWith("+")) {
								output.append(" ");
							}
							inputToSign.deleteCharAt(0);
						} else {
							sign.replace(0, sign.length(), inputToSign
									.toString().substring(0, d));
							inputToSign.delete(0, d);
							signFull = false;
						}
					} else {
						d++;
					}
				}
				if (input.equals("°")) {
					break;
				} else if (sign.toString().equals("01")) {
					output.append("A");
				} else if (sign.toString().equals("1000")) {
					output.append("B");
				} else if (sign.toString().equals("1010")) {
					output.append("C");
				} else if (sign.toString().equals("100")) {
					output.append("D");
				} else if (sign.toString().equals("0")) {
					output.append("E");
				} else if (sign.toString().equals("0010")) {
					output.append("F");
				} else if (sign.toString().equals("110")) {
					output.append("G");
				} else if (sign.toString().equals("0000")) {
					output.append("H");
				} else if (sign.toString().equals("00")) {
					output.append("I");
				} else if (sign.toString().equals("0111")) {
					output.append("J");
				} else if (sign.toString().equals("101")) {
					output.append("K");
				} else if (sign.toString().equals("0100")) {
					output.append("L");
				} else if (sign.toString().equals("11")) {
					output.append("M");
				} else if (sign.toString().equals("10")) {
					output.append("N");
				} else if (sign.toString().equals("111")) {
					output.append("O");
				} else if (sign.toString().equals("0110")) {
					output.append("P");
				} else if (sign.toString().equals("1101")) {
					output.append("Q");
				} else if (sign.toString().equals("010")) {
					output.append("R");
				} else if (sign.toString().equals("000")) {
					output.append("S");
				} else if (sign.toString().equals("1")) {
					output.append("T");
				} else if (sign.toString().equals("001")) {
					output.append("U");
				} else if (sign.toString().equals("0001")) {
					output.append("V");
				} else if (sign.toString().equals("011")) {
					output.append("W");
				} else if (sign.toString().equals("1001")) {
					output.append("X");
				} else if (sign.toString().equals("1011")) {
					output.append("Y");
				} else if (sign.toString().equals("1100")) {
					output.append("Z");
				} else if (sign.toString().equals("11111")) {
					output.append("0 (zero)");
				} else if (sign.toString().equals("01111")) {
					output.append("1");
				} else if (sign.toString().equals("00111")) {
					output.append("2");
				} else if (sign.toString().equals("00011")) {
					output.append("3");
				} else if (sign.toString().equals("00001")) {
					output.append("4");
				} else if (sign.toString().equals("00000")) {
					output.append("5");
				} else if (sign.toString().equals("10000")) {
					output.append("6");
				} else if (sign.toString().equals("11000")) {
					output.append("7");
				} else if (sign.toString().equals("11100")) {
					output.append("8");
				} else if (sign.toString().equals("11110")) {
					output.append("9");
				} else if (sign.toString().equals("0101")) {
					output.append("Ä");
				} else if (sign.toString().equals("1110")) {
					output.append("Ö");
				} else if (sign.toString().equals("0011")) {
					output.append("Ü");
				} else if (sign.toString().equals("00011000")) {
					output.append("ß");
				} else if (sign.toString().equals("1111")) {
					output.append("CH");
				} else if (sign.toString().equals("010101")) {
					output.append(".");
				} else if (sign.toString().equals("110011")) {
					output.append(",");
				} else if (sign.toString().equals("111000")) {
					output.append(":");
				} else if (sign.toString().equals("101010")) {
					output.append(";");
				} else if (sign.toString().equals("001100")) {
					output.append("?");
				} else if (sign.toString().equals("101011")) {
					output.append("!");
				} else if (sign.toString().equals("100001")) {
					output.append("-");
				} else if (sign.toString().equals("001101")) {
					output.append("_");
				} else if (sign.toString().equals("10110")) {
					output.append("(");
				} else if (sign.toString().equals("101101")) {
					output.append(")");
				} else if (sign.toString().equals("011110")) {
					output.append("'");
				} else if (sign.toString().equals("10001")) {
					output.append("=");
				} else if (sign.toString().equals("01010")) {
					output.append("+ or End of the signal");
				} else if (sign.toString().equals("10010")) {
					output.append("/");
				} else if (sign.toString().equals("011010")) {
					output.append("@");
				} else if (sign.toString().equals("10101")) {
					output.append("Begin of the signal");
				} else if (sign.toString().equals("10001")) {
					output.append("Wait");
				} else if (sign.toString().equals("00010")) {
					output.append("Understood");
				} else if (sign.toString().equals("000101")) {
					output.append("End of work");
				} else if (sign.toString().equals("000111000")) {
					output.append("SOS");
				} else if (sign.toString().equals("00000000")) {
					output.append("Error");
				} else {
					tfOutput.setText("Code not listed or wrong.");
				}
			}
			tfOutput.setText(output.toString());
			After();
		} catch (Exception e) {
			tfOutput.setText("Some error occured. I'm sorry!");
		}
	}

	/*
	 * Code for converting normal letters into morse code
	 */

	private void LlM() {
		try {
			// Variables
			StringBuffer input = new StringBuffer();
			input = input.replace(0, input.length(), tfInput.getText()
					.toUpperCase());
			StringBuffer output = new StringBuffer();
			if (input.toString().equals("")) {
				tfOutput.setText("Please enter at least one character");
				After();
			} else if (input.toString().equals("LETTERSPACE")) {
				tfOutput.setText("   ");
				After();
			} else if (input.toString().equals("END OF WORK")) {
				tfOutput.setText("...-.-");
				After();
			} else if (input.toString().equals("ERROR")) {
				tfOutput.setText("........");
				After();
			} else if (input.toString().equals("STARTING SIGNAL")) {
				tfOutput.setText("-.-.-");
				After();
			} else if (input.toString().equals("ENDING SIGNAL")) {
				tfOutput.setText(".-.-.");
				After();
			} else if (input.toString().equals("UNDERSTOOD")) {
				tfOutput.setText("...-.");
				After();
			} else if (input.toString().equals("WAIT")) {
				tfOutput.setText(".-...");
				After();
			} else if (input.toString().equals("SOS")) {
				tfOutput.setText("...---...");
				After();
			} else if (input.toString().equals("LETTER SPACE")) {
				tfOutput.setText("   ");
				After();
			} else if (input.toString().equals("WORD SPACE")) {
				tfOutput.setText("       ");
				After();
			} else {
				for (int c = input.length(); c > 0; c--) {
					if (input.toString().startsWith(" ")) {
						if(output.toString().endsWith("   ")){
							output.delete(output.length() - 3 , output.length());
						}
						output.append("       ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("A")) {
						output.append(".-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("B")) {
						output.append("-...   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("C")) {
						output.append("-.-.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("D")) {
						output.append("-..   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("E")) {
						output.append(".   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("F")) {
						output.append("..-.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("G")) {
						output.append("--.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("H")) {
						output.append("....   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("I")) {
						output.append("..   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("J")) {
						output.append(".---   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("K")) {
						output.append("-.-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("L")) {
						output.append(".-..   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("M")) {
						output.append("--   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("N")) {
						output.append("-.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("O")) {
						output.append("---   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("P")) {
						output.append(".--.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("Q")) {
						output.append("--.-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("R")) {
						output.append(".-.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("S")) {
						output.append("...   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("T")) {
						output.append("-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("U")) {
						output.append("..-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("V")) {
						output.append("...-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("W")) {
						output.append(".--   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("X")) {
						output.append("-..-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("Y")) {
						output.append("-.--   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("Z")) {
						output.append("--..   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("0")) {
						output.append("-----   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("1")) {
						output.append(".----   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("2")) {
						output.append("..---   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("3")) {
						output.append("...--   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("4")) {
						output.append("....-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("5")) {
						output.append(".....   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("6")) {
						output.append("-....   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("7")) {
						output.append("--...   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("8")) {
						output.append("---..   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("9")) {
						output.append("----.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("Ä")) {
						output.append(".-.-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("Ö")) {
						output.append("---.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("Ü")) {
						output.append("..--   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("ß")) {
						output.append("...--...   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith(".")) {
						output.append(".-.-.-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith(",")) {
						output.append("--..--   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith(":")) {
						output.append("---...   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith(";")) {
						output.append("-.-.-.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("?")) {
						output.append("..--..   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("!")) {
						output.append("-.-.--   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("-")) {
						output.append("-....-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("_")) {
						output.append("..--.-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("(")) {
						output.append("-.--.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith(")")) {
						output.append("-.--.-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("=")) {
						output.append("-...-   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("+")) {
						output.append(".-.-.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("/")) {
						output.append("-..-.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("@")) {
						output.append(".--.-.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("'")) {
						output.append(".----.   ");
						input.delete(0, 1);
					} else if (input.toString().startsWith("$")) {
						output.append("...-..-   ");
						input.delete(0, 1);
					} else {
						tfOutput.setText("Code not listed or wrong.");
					}
				}
				if(output.toString().endsWith("   ")){
					output.delete(output.length() - 3 , output.length());
				}
				tfOutput.setText(output.toString());
				After();
			}
		} catch (Exception e) {
			tfOutput.setText("Some error occured. I'm sorry!");
		}
	}

	/*
	 * Code for converting morse code into latin letters
	 */
	private void MLl() {
		try {
			// Variables
			String input;
			StringBuffer output = new StringBuffer();
			input = tfInput.getText() + "   ";
			StringBuffer inputToSign = new StringBuffer(input);
			while (inputToSign.toString().equals("   ") == false) {
				int d = 0;
				boolean signFull = true;
				StringBuffer sign = new StringBuffer();
				while (signFull) {
					if (inputToSign.toString().startsWith("       ")) {
						output.append(" ");
						inputToSign.delete(d, d + 7);
					} else if (inputToSign.toString().substring(d, d + 3)
							.equals("   ")) {
						if (d == 0) {
							inputToSign.delete(0, 3);
						} else {
							sign.replace(0, sign.length(), inputToSign
									.toString().substring(0, d));
							inputToSign.delete(0, d);
							signFull = false;
						}
					} else {
						d++;
					}
				}
				if (sign.toString().equals(".-")) {
					output.append("A");
				} else if (sign.toString().equals("-...")) {
					output.append("B");
				} else if (sign.toString().equals("-.-.")) {
					output.append("C");
				} else if (sign.toString().equals("-..")) {
					output.append("D");
				} else if (sign.toString().equals(".")) {
					output.append("E");
				} else if (sign.toString().equals("..-.")) {
					output.append("F");
				} else if (sign.toString().equals("--.")) {
					output.append("G");
				} else if (sign.toString().equals("....")) {
					output.append("H");
				} else if (sign.toString().equals("..")) {
					output.append("I");
				} else if (sign.toString().equals(".---")) {
					output.append("J");
				} else if (sign.toString().equals("-.-")) {
					output.append("K");
				} else if (sign.toString().equals(".-..")) {
					output.append("L");
				} else if (sign.toString().equals("--")) {
					output.append("M");
				} else if (sign.toString().equals("-.")) {
					output.append("N");
				} else if (sign.toString().equals("---")) {
					output.append("O");
				} else if (sign.toString().equals(".--.")) {
					output.append("P");
				} else if (sign.toString().equals("--.-")) {
					output.append("Q");
				} else if (sign.toString().equals(".-.")) {
					output.append("R");
				} else if (sign.toString().equals("...")) {
					output.append("S");
				} else if (sign.toString().equals("-")) {
					output.append("T");
				} else if (sign.toString().equals("..-")) {
					output.append("U");
				} else if (sign.toString().equals("...-")) {
					output.append("V");
				} else if (sign.toString().equals(".--")) {
					output.append("W");
				} else if (sign.toString().equals("-..-")) {
					output.append("X");
				} else if (sign.toString().equals("-.--")) {
					output.append("Y");
				} else if (sign.toString().equals("--..")) {
					output.append("Z");
				} else if (sign.toString().equals("-----")) {
					output.append(". (zero)");
				} else if (sign.toString().equals(".----")) {
					output.append("-");
				} else if (sign.toString().equals("..---")) {
					output.append("2");
				} else if (sign.toString().equals("...--")) {
					output.append("3");
				} else if (sign.toString().equals("....-")) {
					output.append("4");
				} else if (sign.toString().equals(".....")) {
					output.append("5");
				} else if (sign.toString().equals("-....")) {
					output.append("6");
				} else if (sign.toString().equals("--...")) {
					output.append("7");
				} else if (sign.toString().equals("---..")) {
					output.append("8");
				} else if (sign.toString().equals("----.")) {
					output.append("9");
				} else if (sign.toString().equals(".-.-")) {
					output.append("Ä");
				} else if (sign.toString().equals("---.")) {
					output.append("Ö");
				} else if (sign.toString().equals("..--")) {
					output.append("Ü");
				} else if (sign.toString().equals("...--...")) {
					output.append("ß");
				} else if (sign.toString().equals("----")) {
					output.append("CH");
				} else if (sign.toString().equals(".-.-.-")) {
					output.append(".");
				} else if (sign.toString().equals("--..--")) {
					output.append(",");
				} else if (sign.toString().equals("---...")) {
					output.append(":");
				} else if (sign.toString().equals("-.-.-.")) {
					output.append(";");
				} else if (sign.toString().equals("..--..")) {
					output.append("?");
				} else if (sign.toString().equals("-.-.--")) {
					output.append("!");
				} else if (sign.toString().equals("-....-")) {
					output.append("-");
				} else if (sign.toString().equals("..--.-")) {
					output.append("_");
				} else if (sign.toString().equals("-.--.")) {
					output.append("(");
				} else if (sign.toString().equals("-.--.-")) {
					output.append(")");
				} else if (sign.toString().equals(".----.")) {
					output.append("'");
				} else if (sign.toString().equals("-...-")) {
					output.append("=");
				} else if (sign.toString().equals(".-.-.")) {
					output.append("+ or End of the signal");
				} else if (sign.toString().equals("-..-.")) {
					output.append("/");
				} else if (sign.toString().equals(".--.-.")) {
					output.append("@");
				} else if (sign.toString().equals("-.-.-")) {
					output.append("Begin of the signal");
				} else if (sign.toString().equals("-...-")) {
					output.append("Wait");
				} else if (sign.toString().equals("...-.")) {
					output.append("Understood");
				} else if (sign.toString().equals("...-.-")) {
					output.append("End of work");
				} else if (sign.toString().equals("...---...")) {
					output.append("SOS");
				} else if (sign.toString().equals("........")) {
					output.append("Error");
				} else {
					tfOutput.setText("Code not listed or wrong.");
				}
			}
			tfOutput.setText(output.toString());
			After();
		} catch (Exception e) {
			tfOutput.setText("Some error occured. I'm sorry!");
		}
	}

	/*
	 * popup menu
	 */
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}

	/*
	 * Past function
	 */
	private void onPaste(JTextArea tf) {
		try{
		Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
		Transferable t = c.getContents(this);
		if (t == null)
			return;
		try {
			tf.setText((String) t.getTransferData(DataFlavor.stringFlavor));
		} catch (Exception e) {
			e.printStackTrace();
		}// try
		} catch (Exception e){
			tfOutput.setText("Some error occured. I'm sorry! \n Are you sure that your clipboard isn't empty?");
		}
	}// onPaste

	/*
	 * Copy function
	 */
	private void onCopy(JTextArea tf) {
		StringSelection selection = new StringSelection(tf.getText());
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		clipboard.setContents(selection, selection);
	}
}
