Initial Commit
This commit is contained in:
commit
698796d22d
53
src/application/Main.java
Normal file
53
src/application/Main.java
Normal file
@ -0,0 +1,53 @@
|
||||
package application;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Alert.AlertType;
|
||||
import javafx.scene.control.ButtonType;
|
||||
|
||||
public class Main extends Application
|
||||
{
|
||||
public void start(Stage primaryStage)
|
||||
{
|
||||
try
|
||||
{
|
||||
RootBorderPane root = new RootBorderPane();
|
||||
Scene scene = new Scene(root, 700, 500);
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.setTitle("GUI-Uebung 6 Textverbinder");
|
||||
primaryStage.show();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
showAlert(AlertType.ERROR, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void showAlert(AlertType alertType, String message)
|
||||
{
|
||||
if (message != null)
|
||||
{
|
||||
Alert alert = new Alert(alertType, message, ButtonType.OK);
|
||||
alert.setHeaderText(null);
|
||||
if (alertType == AlertType.WARNING)
|
||||
alert.setTitle("Warnung");
|
||||
else
|
||||
alert.setTitle("Hinweis-Meldung");
|
||||
alert.showAndWait();
|
||||
}
|
||||
else
|
||||
{
|
||||
Alert alert = new Alert(alertType, "Bitte waehlen Sie \"Weiter\" aus", ButtonType.NEXT);
|
||||
alert.setHeaderText(null);
|
||||
alert.setTitle("Hinweis-Meldung");
|
||||
alert.showAndWait();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
launch(args);
|
||||
}
|
||||
}
|
199
src/application/RootBorderPane.java
Normal file
199
src/application/RootBorderPane.java
Normal file
@ -0,0 +1,199 @@
|
||||
package application;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import javafx.geometry.HPos;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.geometry.VPos;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.Menu;
|
||||
import javafx.scene.control.MenuBar;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.scene.control.RadioButton;
|
||||
import javafx.scene.control.SeparatorMenuItem;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.control.ToggleGroup;
|
||||
import javafx.scene.control.Alert.AlertType;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.stage.FileChooser;
|
||||
import model.TextException;
|
||||
import model.Textverbinder;
|
||||
|
||||
public class RootBorderPane extends BorderPane {
|
||||
|
||||
private MenuBar menuBar;
|
||||
private Menu mDatei, mBearbeiten, mHilfe;
|
||||
private MenuItem miBeenden, miZuruecksetzen, miExport;
|
||||
private GridPane gpZentrum;
|
||||
private RadioButton rbDirekt, rbLeerzeichen, rbLeerzeile;
|
||||
private TextField tfText1, tfText2;
|
||||
private TextArea taErgebnis;
|
||||
private Button btVerbinden;
|
||||
private ToggleGroup tgRadios;
|
||||
private Label lbErgebnis;
|
||||
private Textverbinder textverbinder;
|
||||
|
||||
public RootBorderPane() throws TextException {
|
||||
initComponents();
|
||||
addComponents();
|
||||
addHandler();
|
||||
}
|
||||
|
||||
private void initComponents() throws TextException {
|
||||
|
||||
String promptText = "Bitte einen Text eingeben...";
|
||||
|
||||
menuBar = new MenuBar();
|
||||
mDatei = new Menu("Datei");
|
||||
mBearbeiten = new Menu("Bearbeiten");
|
||||
mHilfe = new Menu("Hilfe");
|
||||
miExport = new MenuItem("Texte in Datei exportieren...");
|
||||
miExport.setDisable(true);
|
||||
miBeenden = new MenuItem("Beenden");
|
||||
miZuruecksetzen = new MenuItem("Zuruecksetzen");
|
||||
|
||||
gpZentrum = new GridPane();
|
||||
gpZentrum.setAlignment(Pos.CENTER);
|
||||
gpZentrum.setHgap(10);
|
||||
gpZentrum.setVgap(10);
|
||||
gpZentrum.setPadding(new Insets(10));
|
||||
|
||||
tfText1 = new TextField();
|
||||
//tfText1.setPromptText("Bitte einen Text eingeben...");
|
||||
tfText1.setPromptText(promptText);
|
||||
tfText2 = new TextField();
|
||||
tfText2.setPromptText(promptText);
|
||||
taErgebnis = new TextArea();
|
||||
taErgebnis.setDisable(true);
|
||||
taErgebnis.setEditable(false);
|
||||
|
||||
rbDirekt = new RadioButton("Direkt:");
|
||||
rbLeerzeichen = new RadioButton("Leerzeichen:");
|
||||
rbLeerzeile = new RadioButton("Leerzeile:");
|
||||
|
||||
btVerbinden = new Button("Verbinden");
|
||||
|
||||
tgRadios = new ToggleGroup();
|
||||
|
||||
lbErgebnis = new Label("Ergebnis:");
|
||||
|
||||
textverbinder = new Textverbinder("Bitte geben Sie einen Text ein...", "Bitte geben Sie einen Text ein...");
|
||||
|
||||
}
|
||||
|
||||
private void addComponents() {
|
||||
mDatei.getItems().addAll(miExport, new SeparatorMenuItem(), miBeenden);
|
||||
mBearbeiten.getItems().addAll(miZuruecksetzen);
|
||||
menuBar.getMenus().addAll(mDatei, mBearbeiten, mHilfe);
|
||||
|
||||
gpZentrum.add(new Label("Verbinden:"), 0, 0);
|
||||
gpZentrum.add(new Label("Text 1:"), 0, 3);
|
||||
gpZentrum.add(new Label("Text 2:"), 0, 4);
|
||||
|
||||
//gpZentrum.add(new Label("Ergebnis:"), 0, 5);
|
||||
gpZentrum.add(lbErgebnis, 0, 5);
|
||||
GridPane.setValignment(lbErgebnis, VPos.TOP);
|
||||
|
||||
gpZentrum.add(rbDirekt, 1, 0);
|
||||
gpZentrum.add(rbLeerzeichen, 1, 1);
|
||||
gpZentrum.add(rbLeerzeile, 1, 2);
|
||||
|
||||
gpZentrum.add(tfText1, 1, 3);
|
||||
gpZentrum.add(tfText2, 1, 4);
|
||||
gpZentrum.add(taErgebnis, 1, 5);
|
||||
|
||||
gpZentrum.add(btVerbinden, 1, 6);
|
||||
GridPane.setHalignment(btVerbinden, HPos.RIGHT);
|
||||
|
||||
tgRadios.getToggles().addAll(rbDirekt, rbLeerzeichen, rbLeerzeile);
|
||||
tgRadios.selectToggle(rbDirekt);
|
||||
|
||||
setTop(menuBar);
|
||||
setCenter(gpZentrum);
|
||||
|
||||
}
|
||||
|
||||
private void addHandler() {
|
||||
miBeenden.setOnAction(event -> beenden());
|
||||
btVerbinden.setOnAction(event -> verbinden());
|
||||
miZuruecksetzen.setOnAction(event -> zuruecksetzen());
|
||||
miExport.setOnAction(event -> export());
|
||||
}
|
||||
|
||||
// ---------------------- handlers -----------------------
|
||||
|
||||
private void export()
|
||||
{
|
||||
FileChooser fc = new FileChooser();
|
||||
File selected = fc.showSaveDialog(null);
|
||||
if (selected != null)
|
||||
{
|
||||
String pfadDateiName = selected.getAbsolutePath();
|
||||
try {
|
||||
textverbinder.texteExport(pfadDateiName);
|
||||
Main.showAlert(AlertType.CONFIRMATION, "Die Texte wurden in die Datei " + pfadDateiName + " exportiert");
|
||||
|
||||
} catch (TextException e) {
|
||||
Main.showAlert(AlertType.CONFIRMATION, e.getMessage());
|
||||
}
|
||||
}
|
||||
else
|
||||
Main.showAlert(AlertType.CONFIRMATION, "Es wurde keine Datei fuer den Export der Texte ausgewaehlt");
|
||||
}
|
||||
|
||||
private void beenden() {
|
||||
Platform.exit();
|
||||
}
|
||||
|
||||
private void verbinden() {
|
||||
|
||||
//String text1 = tfText1.getText(); // z.B. "Java"
|
||||
//String text2 = tfText2.getText(); // z.B. "FX"
|
||||
|
||||
//String verbinder = ""; // bei Standard-Auswahl der Radios "Direkt"
|
||||
String ergebnis = "";
|
||||
|
||||
try {
|
||||
textverbinder.setText1(tfText1.getText());
|
||||
textverbinder.setText2(tfText2.getText());
|
||||
|
||||
if (rbDirekt.isSelected())
|
||||
ergebnis = textverbinder.texteVerbindenDirekt();
|
||||
|
||||
if (rbLeerzeichen.isSelected())
|
||||
{
|
||||
ergebnis = textverbinder.texteVerbindenLeerZeichen();
|
||||
}
|
||||
|
||||
if (rbLeerzeile.isSelected())
|
||||
{
|
||||
ergebnis = textverbinder.texteVerbindenLeerZeile();
|
||||
}
|
||||
|
||||
taErgebnis.setText(ergebnis);
|
||||
taErgebnis.setDisable(false);
|
||||
miExport.setDisable(false);
|
||||
}
|
||||
catch (TextException e)
|
||||
{
|
||||
Main.showAlert(AlertType.CONFIRMATION, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void zuruecksetzen()
|
||||
{
|
||||
//rbDirekt.setSelected(true);
|
||||
tgRadios.selectToggle(rbDirekt);
|
||||
tfText1.clear();
|
||||
tfText2.clear();
|
||||
taErgebnis.clear();
|
||||
taErgebnis.setDisable(true);
|
||||
miExport.setDisable(true);
|
||||
}
|
||||
|
||||
}
|
12
src/model/TextException.java
Normal file
12
src/model/TextException.java
Normal file
@ -0,0 +1,12 @@
|
||||
package model;
|
||||
|
||||
public class TextException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public TextException (String message)
|
||||
{
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
121
src/model/Textverbinder.java
Normal file
121
src/model/Textverbinder.java
Normal file
@ -0,0 +1,121 @@
|
||||
package model;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Textverbinder {
|
||||
|
||||
private String text1, text2;
|
||||
|
||||
public Textverbinder(String text1, String text2) throws TextException {
|
||||
setText1(text1);
|
||||
setText2(text2);
|
||||
}
|
||||
|
||||
public void setText1(String text1) throws TextException {
|
||||
if (text1 != null)
|
||||
this.text1 = text1;
|
||||
else
|
||||
throw new TextException("Fehler bei setText1(): null-Referenz erhalten");
|
||||
}
|
||||
|
||||
public void setText2(String text2) throws TextException {
|
||||
if (text2 != null)
|
||||
this.text2 = text2;
|
||||
else
|
||||
throw new TextException("Fehler bei setText2(): null-Referenz erhalten");
|
||||
}
|
||||
|
||||
public String getText1() {
|
||||
return text1;
|
||||
}
|
||||
|
||||
public String getText2() {
|
||||
return text2;
|
||||
}
|
||||
|
||||
public String texteVerbindenDirekt() throws TextException {
|
||||
if (text1 != null && text2 != null)
|
||||
return texteVerbinden("");
|
||||
else
|
||||
throw new TextException(
|
||||
"Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
|
||||
}
|
||||
|
||||
public String texteVerbindenLeerZeichen() throws TextException {
|
||||
if (text1 != null && text2 != null)
|
||||
return texteVerbinden(" ");
|
||||
else
|
||||
throw new TextException(
|
||||
"Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
|
||||
}
|
||||
|
||||
public String texteVerbindenLeerZeile() throws TextException {
|
||||
if (text1 != null && text2 != null)
|
||||
return texteVerbinden("\n");
|
||||
else
|
||||
throw new TextException(
|
||||
"Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
|
||||
}
|
||||
|
||||
private String texteVerbinden(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);
|
||||
}
|
||||
|
||||
public void texteExport(String pfadDateiName) throws TextException {
|
||||
if (pfadDateiName != null && !pfadDateiName.isEmpty()) {
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(pfadDateiName))) {
|
||||
bw.write(toStringCsv());
|
||||
} catch (IOException e) {
|
||||
throw new TextException(
|
||||
"Ein-/Ausgabe-Fehler bei texteExport() mit Datei " + pfadDateiName + ": " + e.getMessage());
|
||||
}
|
||||
} else
|
||||
throw new TextException("Fehler bei texteExport(): pfadDateiName ist ungueltig (" + pfadDateiName + ")");
|
||||
}
|
||||
|
||||
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")))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("text1=");
|
||||
builder.append(text1);
|
||||
builder.append(", text2=");
|
||||
builder.append(text2);
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public String toStringCsv() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(text1);
|
||||
builder.append(";");
|
||||
builder.append(text2);
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
63
src/test/TestTextverbinder.java
Normal file
63
src/test/TestTextverbinder.java
Normal file
@ -0,0 +1,63 @@
|
||||
package test;
|
||||
|
||||
import model.TextException;
|
||||
import model.Textverbinder;
|
||||
|
||||
public class TestTextverbinder {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Textverbinder tv01 = new Textverbinder(null, null); // Fehler null
|
||||
// Textverbinder tv02 = new Textverbinder("", null); // Fehler null
|
||||
// Textverbinder tv03 = new Textverbinder("", ""); // Fehler ungueltig
|
||||
|
||||
Textverbinder tv0 = new Textverbinder(" ", " "); // ok
|
||||
System.out.println(tv0);
|
||||
System.out.println();
|
||||
|
||||
Textverbinder tv1 = new Textverbinder("Java", "FX"); // ok
|
||||
System.out.println(tv1);
|
||||
System.out.println();
|
||||
|
||||
Textverbinder tv2 = new Textverbinder("kein", "Problem"); // ok
|
||||
System.out.println(tv2);
|
||||
System.out.println();
|
||||
|
||||
Textverbinder tv3 = new Textverbinder("heute", "morgen"); // ok
|
||||
System.out.println(tv3);
|
||||
System.out.println();
|
||||
|
||||
System.out.println("\"" + tv0.texteVerbindenDirekt() + "\""); // " " (Anm.: 2x Leerzeichen)
|
||||
System.out.println("\"" + tv0.texteVerbindenLeerZeichen() + "\""); // " " (Anm.: 3x Leerzeichen)
|
||||
System.out.println("\"" + tv0.texteVerbindenLeerZeile() + "\""); // " " (Anm.: 1x Leerzeichen, 1x Leerzeile, 1x Leerzeichen)
|
||||
|
||||
System.out.println("\"" + tv1.texteVerbindenDirekt() + "\""); // "JavaFX"
|
||||
System.out.println("\"" + tv2.texteVerbindenLeerZeichen() + "\""); // "kein Problem"
|
||||
System.out.println("\"" + tv3.texteVerbindenLeerZeile() + "\""); // "heute", dann Leerzeile, dann "morgen")
|
||||
|
||||
System.out.println(tv1.toStringCsv()); // "Java;FX"
|
||||
System.out.println();
|
||||
|
||||
System.out.println(tv2.toStringCsv()); // "kein;Problem"
|
||||
System.out.println();
|
||||
|
||||
System.out.println(tv3.toStringCsv()); // "heute;morgen"
|
||||
System.out.println();
|
||||
|
||||
// tv1.texteExport(null); // Fehler pfadDateiName ist ungueltig (null)
|
||||
// tv1.texteExport("x:\\scratch\\texteExport.csv"); // Fehler Das System kann den angegebenen Pfad nicht finden
|
||||
|
||||
String pfadDateiName = "/home/andre/Documents/EclipseExports/texteExport.csv";
|
||||
tv1.texteExport(pfadDateiName); // ok
|
||||
System.out.println("Texte wurden in Datei " + pfadDateiName + " geschrieben");
|
||||
|
||||
}
|
||||
catch (TextException e)
|
||||
{
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user