import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
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.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Creator {
private final MainView mView = new MainView();
private int i, length = 5, x, y;
private Random r = new Random();
private StringBuffer output;
/***************
*hier**********
*wird**********
*der***********
*Mouse*********
*Motion********
*Listener******
*hinzugefügt***
*!!!***********
**************/
private final MouseMotionListener mml = new MouseMotionAdapter() {
public void mouseMoved(MouseEvent evt) {
if (mView.generate.contains(evt.getX(), evt.getY()) == false) {
System.out.println("Hallo");
} else {
System.out.println("Buhhh");
}
}
};
public static void main(String args[]) {
Creator myCreator = new Creator();
myCreator.Controller();
}
public void Controller() {
mView.loadView();
load();
}
public void load() {
while (true) {
mView.loadWindow.show();
mView.loadProgress.setValue(20);
mView.setValue();
mView.loadProgress.setValue(50);
mView.setProperties();
mView.addComponents();
mView.loadProgress.setValue(80);
addListener();
mView.loadProgress.setValue(100);
mView.loadWindow.hide();
mView.mainFrame.show();
return;
}
}
public void addListener() {
mView.loadWindow.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
System.exit(0);
}
}
});
mView.mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
mView.mainFrame.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
if (evt.getKeyCode() == evt.VK_ESCAPE) {
System.exit(0);
}
}
});
mView.amountScroll.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent evt) {
mView.amount.setText(
"Anzahl der Zeichen: " + mView.amountScroll.getValue());
}
});
mView.spokenRadio.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
if (evt.getStateChange() == ItemEvent.SELECTED) {
mView.numbers.setEnabled(false);
mView.numbers.setSelected(false);
} else {
mView.numbers.setEnabled(true);
}
}
});
mView.exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
mView.generate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
generate();
}
});
mView.endError.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
mView.errorDialog.hide();
}
});
mView.errorDialog.addWindowListener(new WindowAdapter() {
public void closingWindow(WindowEvent evt) {
mView.errorDialog.hide();
}
});
mView.endSelectError.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
mView.selectErrorDialog.hide();
}
});
mView.selectErrorDialog.addWindowListener(new WindowAdapter() {
public void closingWindow(WindowEvent evt) {
mView.selectErrorDialog.hide();
}
});
mView.save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
Clipboard clip =
Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection newString =
new StringSelection(mView.password.getText());
clip.setContents(newString, newString);
} catch (Exception e) {
mView.errorDialog.show();
}
}
});
mView.generateItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
generate();
}
});
mView.closeItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
mView.infoItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
mView.infoDialog.show();
}
});
mView.exitInfo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
mView.infoDialog.hide();
}
});
mView.infoDialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
mView.infoDialog.hide();
}
});
mView.infoDialog.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
mView.infoDialog.hide();
}
}
});
/**************
**hier********
**kommt*******
**mein********
**Probelm*****
**!!!*********
*************/
mView.helpItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (mView.helpItem.isSelected() == true) {
mView.mainFrame.addMouseMotionListener(mml);
} else {
mView.mainFrame.removeMouseMotionListener(mml);
}
}
});
}
public void generateUnspoken() {
String signs[][] =
{
{
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z" },
{
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z" },
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }
};
output = new StringBuffer("");
for (i = 0; i != length; i++) {
if (mView.capitalLetters.isSelected() == true) {
y = 0;
if (mView.littleLetters.isSelected() == true) {
y = Math.abs(r.nextInt()) % 2;
if (mView.numbers.isSelected() == true) {
y = Math.abs(r.nextInt()) % 3;
}
}
if (mView.numbers.isSelected() == true
&& mView.littleLetters.isSelected() == false) {
y = Math.abs(r.nextInt()) % 3;
if (y == 1) {
y = 2;
}
}
}
if (mView.littleLetters.isSelected() == true
&& mView.capitalLetters.isSelected() == false) {
y = 1;
if (mView.numbers.isSelected() == true) {
y = (Math.abs(r.nextInt()) % 2) + 1;
}
}
if (mView.numbers.isSelected() == true
&& mView.littleLetters.isSelected() == false
&& mView.capitalLetters.isSelected() == false) {
y = 2;
}
if (y > 1) {
x = Math.abs(r.nextInt()) % 10;
} else {
x = Math.abs(r.nextInt()) % 26;
}
output.append(signs[y][x]);
}
}
public void generateSpoken() {
String vowels[][] = { { "a", "e", "i", "o", "u" }, {
"A", "E", "I", "O", "U" }
},
consonants[][] =
{
{
"b",
"c",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
"m",
"n",
"p",
"q",
"r",
"s",
"t",
"v",
"w",
"x",
"y",
"z" },
{
"B",
"C",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
"M",
"N",
"P",
"Q",
"R",
"S",
"T",
"V",
"W",
"X",
"Y",
"Z" }
};
int begin = Math.abs(r.nextInt()) % 2 + 2;
output = new StringBuffer("");
for (i = 0; i != length; i++) {
if (mView.littleLetters.isSelected() == true) {
y = 0;
if (mView.capitalLetters.isSelected() == true) {
y = Math.abs(r.nextInt()) % 2;
}
} else {
y = 1;
}
if (begin % 2 == 0) {
x = Math.abs(r.nextInt()) % 5;
output.append(vowels[y][x]);
} else {
x = Math.abs(r.nextInt()) % 21;
output.append(consonants[y][x]);
}
begin++;
}
}
public void generate() {
length = mView.amountScroll.getValue();
if (mView.littleLetters.isSelected() == false
&& mView.capitalLetters.isSelected() == false
&& mView.numbers.isSelected() == false) {
mView.selectErrorDialog.show();
} else {
if (mView.spokenRadio.isSelected() == true) {
generateSpoken();
mView.password.setText(output.toString());
} else {
generateUnspoken();
mView.password.setText(output.toString());
}
}
}
}