-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFXMainPane.java
More file actions
225 lines (197 loc) · 8.56 KB
/
FXMainPane.java
File metadata and controls
225 lines (197 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
public class FXMainPane extends BorderPane {
private Button decryption, exitButton, encryption, test, clearButton;
private TextField plainTextTextField, inputForEncryptionTextField, encryptedStringTextField3, decryptedTextField4;
private Label plainTextLabel, descriptionForInputLabel, encryptedLabel3, decriptedLabel4, blankLabel1, blankLabel2, blankLabel3, blankLabel4;
private RadioButton radioButton1, radioButton2;
private int shiftInt = 0;
FXMainPane() {
Insets inset = new Insets(10); //for setting margins
plainTextTextField = new TextField();
plainTextLabel = new Label("Enter plain-text string to encrypt");
inputForEncryptionTextField = new TextField();
descriptionForInputLabel = new Label("Cyber Key - enter an integer for Caesar Cipher");
encryptedStringTextField3 = new TextField();
encryptedLabel3 = new Label("Encrypted string");
decryptedTextField4 = new TextField();
decriptedLabel4 = new Label("Decrypted string");
blankLabel1 = new Label(" ");
blankLabel2 = new Label(" ");
blankLabel3 = new Label(" ");
blankLabel4 = new Label(" ");
//create three radio button instances
radioButton1 = new RadioButton("Use Caesar cipher");
radioButton2 = new RadioButton("Use Bellaso cipher");
//create a group to make the radio buttons mutually exclusive
ToggleGroup radioButtonGroup = new ToggleGroup();
radioButton1.setToggleGroup(radioButtonGroup);
radioButton2.setToggleGroup(radioButtonGroup);
radioButton1.setSelected(true);
RadioButtonListener radioButtonListener = new RadioButtonListener();
radioButton1.setOnAction(radioButtonListener);
radioButton2.setOnAction(radioButtonListener);
radioButton1.setAlignment(Pos.CENTER);
radioButton2.setAlignment(Pos.CENTER);
HBox topBox = new HBox();
HBox.setMargin(radioButton1, inset);
topBox.setAlignment(Pos.CENTER);
topBox.getChildren().addAll(radioButton1, radioButton2);
topBox.setStyle("-fx-border-color: gray;");
//create the leftPanel
VBox centerBox = new VBox(10);
centerBox.getChildren().addAll(plainTextLabel, plainTextTextField, encryptedLabel3, encryptedStringTextField3,
decriptedLabel4, decryptedTextField4, descriptionForInputLabel, inputForEncryptionTextField);
setCenter(centerBox);
setRight(blankLabel1);
setLeft(blankLabel2);
setTop(topBox);
//create the exit Button
exitButton = new Button("E_xit");
//_ in label specifies that the next character is the mnemonic, ie, type Alt-m as a shortcut
//this activates the mnemonic on exitButton when the Alt key is pressed
exitButton.setMnemonicParsing(true);
exitButton.setTooltip(new Tooltip("Select to close the application"));
//use a lambda expression for the EventHandler class for exitButton
exitButton.setOnAction(
event -> {
Platform.exit();
System.exit(0);
}
);
//create the clear Button
clearButton = new Button("_Clear");
clearButton.setMnemonicParsing(true);
clearButton.setTooltip(new Tooltip("Select this to clear the text fields"));
//create a listener for the other button using a lambda expression
clearButton.setOnAction(event -> {
System.out.println("...clearing text fields...");
plainTextTextField.setText("");
inputForEncryptionTextField.setText("");
encryptedStringTextField3.setText("");
decryptedTextField4.setText("");
});
//create the decryption Button
decryption = new Button("_Decrypt a string");
decryption.setMnemonicParsing(true);
decryption.setTooltip(new Tooltip("Select this to decrypt a string"));
//create a listener for the other button using a lambda expression
decryption.setOnAction(event -> {
String encryptedText = "";
String bellasoStr = "";
String decryptedText;
System.out.println("...decrypting...");
encryptedText = encryptedStringTextField3.getText();
if (radioButton1.isSelected()) {
shiftInt = Integer.parseInt(inputForEncryptionTextField.getText());
decryptedText = CryptoManager.decryptCaesar(encryptedText, shiftInt);
}
else {
bellasoStr = inputForEncryptionTextField.getText().toUpperCase();
inputForEncryptionTextField.setText(bellasoStr);
decryptedText = CryptoManager.decryptBellaso(encryptedText, bellasoStr);
}
decryptedTextField4.setText(decryptedText);
});
//create the encryption Button
encryption = new Button("Encrypt a string");
encryption.setMnemonicParsing(true);
encryption.setTooltip(new Tooltip("Encrypt the string in the upper textfield"));
encryption.setVisible(true);
//create a listener for the exit button using a lambda expression
encryption.setOnAction(event -> {
try {
System.out.println("...encrypting...");
String bellasoStr = "";
String encryptedStr = "";
if (radioButton1.isSelected()) {
shiftInt = Integer.parseInt(inputForEncryptionTextField.getText());
encryptedStr = CryptoManager.encryptCaesar(plainTextTextField.getText().toUpperCase(), shiftInt);
}
else {
bellasoStr = inputForEncryptionTextField.getText().toUpperCase();
inputForEncryptionTextField.setText(bellasoStr);
encryptedStr = CryptoManager.encryptBellaso(plainTextTextField.getText().toUpperCase(), bellasoStr);
}
plainTextTextField.setText(plainTextTextField.getText().toUpperCase());
if (encryptedStr.equals(""))
encryptedStringTextField3.setText("encryption failed");
else
encryptedStringTextField3.setText(encryptedStr);
} catch (NumberFormatException e) {
System.out.println(inputForEncryptionTextField.getText()+" should be an integer");
}
});
//create the encryption Button
test = new Button("Test toStudent File");
test.setMnemonicParsing(true);
test.setTooltip(new Tooltip("Test the toStudent java file"));
test.setVisible(true);
//create a listener for the exit button using a lambda expression
test.setOnAction(event -> {
System.out.println("...testing...");
try {
CryptoManager.stringInBounds("TEST");
} catch (RuntimeException e) {
System.out.println("stringInBounds "+e.getMessage());
}try {
CryptoManager.encryptCaesar("TEST", 3);
} catch (RuntimeException e) {
System.out.println("encryptCaesar "+e.getMessage());
}
try {
CryptoManager.encryptBellaso("TEST", "CMSC");
} catch (RuntimeException e) {
System.out.println("encryptBellaso "+e.getMessage());
}
try {
CryptoManager.decryptCaesar("TEST", 3);
} catch (RuntimeException e) {
System.out.println("decryptCaesar "+e.getMessage());
}
try {
CryptoManager.decryptBellaso("TEST", "CMSC");
} catch (RuntimeException e) {
System.out.println("decryptBellaso "+e.getMessage());
}
});
HBox bottomBox = new HBox();
//HBox.setMargin(breakEncryption, inset);
HBox.setMargin(decryption, inset);
HBox.setMargin(encryption, inset);
HBox.setMargin(clearButton, inset);
HBox.setMargin(exitButton, inset);
//bottomBox.getChildren().addAll(encryption, decryption, clearButton, test, exitButton); //
bottomBox.getChildren().addAll(encryption, decryption, clearButton, exitButton);
setBottom(bottomBox);
bottomBox.setAlignment(Pos.CENTER);
}
class RadioButtonListener implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent event) {
Object source = event.getTarget();
if (source == radioButton1)
{
descriptionForInputLabel.setText("Cyber Key - enter an integer for Caesar Cipher");
}
else if(source == radioButton2)
{
descriptionForInputLabel.setText("Cyber Key - enter a string for Bellaso Cipher");
}
}
}
}