Advertisement
Guest User

Untitled

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