diff --git a/src/application/Main.java b/src/application/Main.java index c8a2443..a1c62da 100644 --- a/src/application/Main.java +++ b/src/application/Main.java @@ -13,7 +13,7 @@ public class Main extends Application { RootBorderPane root = new RootBorderPane(); Scene scene = new Scene(root, 700, 500); primaryStage.setScene(scene); - primaryStage.setTitle("GUI-Uebung 4"); + primaryStage.setTitle("GUI-Uebung 5"); primaryStage.show(); } catch (Exception e) { showAlert(AlertType.ERROR, e.getMessage()); diff --git a/src/application/RootBorderPane.java b/src/application/RootBorderPane.java index 84f0ce0..52ca457 100644 --- a/src/application/RootBorderPane.java +++ b/src/application/RootBorderPane.java @@ -17,6 +17,8 @@ import javafx.scene.control.TextField; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; +import model.TextException; +import model.Textverbinder; public class RootBorderPane extends BorderPane { @@ -121,22 +123,27 @@ public class RootBorderPane extends BorderPane { private void verbinden() { String text1 = tfText1.getText(); String text2 = tfText2.getText(); - String verbinder = ""; // Direct +// String verbinder = ""; // Direct + String ergebnis = ""; - if (rbLeerzeichen.isSelected()) { - verbinder = " "; // spacebar - } + try { - if (rbLeerzeile.isSelected()) { - verbinder = "\n"; // NewLine - } + if (rbDirekt.isSelected()) { + ergebnis = Textverbinder.texteVerbindenDirekt(text1, text2); + } - if (!text1.isEmpty() && !text2.isEmpty()) { - String ergebnis = text1 + verbinder + text2; + if (rbLeerzeichen.isSelected()) { + ergebnis = Textverbinder.texteVerbindenLeerZeichen(text1, text2); + } - taErgebnis.setText(ergebnis); - taErgebnis.setDisable(false); - } else { + if (rbLeerzeile.isSelected()) { + ergebnis = Textverbinder.texteVerbindenLeerZeile(text1, text2); + } + + if (!text1.isEmpty() && !text2.isEmpty()) { + taErgebnis.setText(ergebnis); + taErgebnis.setDisable(false); + } else { if (text1.isEmpty() && text2.isEmpty()) { Main.showAlert(AlertType.ERROR, "Text1 fehlt\nText2 fehlt"); } else { @@ -145,8 +152,11 @@ public class RootBorderPane extends BorderPane { } else { Main.showAlert(AlertType.ERROR, "Text2 fehlt"); } + } } + } catch (TextException e) { + Main.showAlert(AlertType.ERROR, e.getMessage()); } } diff --git a/src/model/TextException.java b/src/model/TextException.java index a42ab68..2b50d3c 100644 --- a/src/model/TextException.java +++ b/src/model/TextException.java @@ -1,11 +1,10 @@ package model; public class TextException extends Exception { - + private static final long serialVersionUID = 1L; - public TextException (String message) - { + public TextException(String message) { super(message); } diff --git a/src/model/Textverbinder.java b/src/model/Textverbinder.java index 039bd49..22bfb2f 100644 --- a/src/model/Textverbinder.java +++ b/src/model/Textverbinder.java @@ -2,58 +2,55 @@ package model; public class Textverbinder { - public static String texteVerbindenDirekt(String text1, String text2) throws TextException - { + public static String texteVerbindenDirekt(String text1, String text2) throws TextException { if (text1 != null && text2 != null) return texteVerbinden(text1, text2, ""); else - throw new TextException("Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")"); + throw new TextException( + "Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")"); } - - public static String texteVerbindenLeerZeichen(String text1, String text2) throws TextException - { + + public static String texteVerbindenLeerZeichen(String text1, String text2) throws TextException { if (text1 != null && text2 != null) return texteVerbinden(text1, text2, " "); else - throw new TextException("Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")"); - } - - public static String texteVerbindenLeerZeile(String text1, String text2) throws TextException - { - if (text1 != null && text2 != null) - return texteVerbinden(text1, text2, "\n"); - else - throw new TextException("Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")"); - } - - private static String texteVerbinden(String text1, String text2, String verbinder) throws TextException - { - if (text1 != null && text2 != null && verbinder != null) - if (checkTextEmpty(text1) && checkTextEmpty(text2)) - if (checkVerbinder(verbinder)) - { - StringBuilder sb = new StringBuilder().append(text1).append(verbinder).append(text2); - return sb.toString(); - } - else - throw new TextException("Fehler: verbinder ist ungueltig (verbinder: \"" + verbinder + "\")"); - else - throw new TextException("Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")"); - else - throw new TextException("Fehler: null-Referenz fuer text1, text2 oder verbinder erhalten (text1: \"" + text1 + " \", text2: \"" + text2 + "\", " + ", verbinder: \"" + verbinder); + throw new TextException( + "Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")"); } - private static boolean checkTextEmpty(String text) - { + public static String texteVerbindenLeerZeile(String text1, String text2) throws TextException { + if (text1 != null && text2 != null) + return texteVerbinden(text1, text2, "\n"); + else + throw new TextException( + "Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")"); + } + + private static String texteVerbinden(String text1, String text2, String verbinder) throws TextException { + if (text1 != null && text2 != null && verbinder != null) + if (checkTextEmpty(text1) && checkTextEmpty(text2)) + if (checkVerbinder(verbinder)) { + StringBuilder sb = new StringBuilder().append(text1).append(verbinder).append(text2); + return sb.toString(); + } else + throw new TextException("Fehler: verbinder ist ungueltig (verbinder: \"" + verbinder + "\")"); + else + throw new TextException( + "Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")"); + else + throw new TextException("Fehler: null-Referenz fuer text1, text2 oder verbinder erhalten (text1: \"" + text1 + + " \", text2: \"" + text2 + "\", " + ", verbinder: \"" + verbinder); + } + + private static boolean checkTextEmpty(String text) { if (text != null && !text.isEmpty()) return true; else return false; } - - private static boolean checkVerbinder(String text) - { - if (text != null && (text.equals("") || text.equals(" ") || text.equals("\n") )) + + private static boolean checkVerbinder(String text) { + if (text != null && (text.equals("") || text.equals(" ") || text.equals("\n"))) return true; return false; }