Initial commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Gradle build configuration for specific lab module / exercise
|
||||
*/
|
||||
// the Java plugin is added by default in the main lab configuration
|
||||
plugins {
|
||||
id 'java'
|
||||
// Apply the application plugin to add support for building a CLI application.
|
||||
id 'application'
|
||||
// Adding JavaFX support and dependencies
|
||||
id 'org.openjfx.javafxplugin' version '0.0.12'
|
||||
}
|
||||
|
||||
// Project/Module information
|
||||
description = 'Lab02 FXML WordCloud solution'
|
||||
group = 'ch.zhaw.prog2'
|
||||
version = '2022.1'
|
||||
|
||||
// Dependency configuration
|
||||
// repositories to download dependencies from
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
// required dependencies
|
||||
dependencies {
|
||||
|
||||
}
|
||||
|
||||
// Configuration for Application plugin
|
||||
application {
|
||||
// Define the main class for the application.
|
||||
mainClass = 'ch.zhaw.prog2.application.App'
|
||||
}
|
||||
|
||||
// Configuration for JavaFX plugin
|
||||
javafx {
|
||||
version = '17'
|
||||
modules = [ 'javafx.controls', 'javafx.fxml' ]
|
||||
}
|
||||
|
||||
// Java plugin configuration
|
||||
java {
|
||||
// By default the Java version of the gradle process is used as source/target version.
|
||||
// This can be overridden, to ensure a specific version. Enable only if required.
|
||||
// sourceCompatibility = JavaVersion.VERSION_17 // ensure Java source code compatibility
|
||||
// targetCompatibility = JavaVersion.VERSION_17 // version of the created byte-code
|
||||
|
||||
// Java compiler specific options
|
||||
compileJava {
|
||||
// source files should be UTF-8 encoded
|
||||
options.encoding = 'UTF-8'
|
||||
// for more options see https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* This Java source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
package ch.zhaw.prog2.application;
|
||||
|
||||
import javafx.application.Application;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
Application.launch(MainWindow.class, args);
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package ch.zhaw.prog2.application;
|
||||
/**
|
||||
* Most basic interface for observing an object
|
||||
* @author bles
|
||||
*
|
||||
*/
|
||||
public interface IsObservable {
|
||||
/**
|
||||
* Add an observer that listens for updates
|
||||
* @param observer
|
||||
*/
|
||||
void addListener(IsObserver observer);
|
||||
/**
|
||||
* Remove an observer from the list
|
||||
* @param observer
|
||||
*/
|
||||
void removeListener(IsObserver observer);
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ch.zhaw.prog2.application;
|
||||
/**
|
||||
* Most basic interface for beeing an observer
|
||||
* @author bles
|
||||
*
|
||||
*/
|
||||
public interface IsObserver {
|
||||
/**
|
||||
* This method is always called when an observed object
|
||||
* changes
|
||||
*/
|
||||
void update();
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package ch.zhaw.prog2.application;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.beans.InvalidationListener;
|
||||
import javafx.beans.Observable;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.layout.Pane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class MainWindow extends Application {
|
||||
|
||||
private WordModel wordModel = new WordModel();
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
openMainWindow(primaryStage);
|
||||
}
|
||||
|
||||
private void openMainWindow(Stage stage) {
|
||||
try {
|
||||
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindow.fxml"));
|
||||
|
||||
Pane rootNode = loader.load();
|
||||
|
||||
MainWindowController mainWindowController = loader.getController();
|
||||
mainWindowController.connectProperties();
|
||||
mainWindowController.setWordModel(wordModel);
|
||||
|
||||
Scene scene = new Scene(rootNode);
|
||||
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package ch.zhaw.prog2.application;
|
||||
|
||||
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.control.TextField;
|
||||
|
||||
public class MainWindowController {
|
||||
private WordModelDecorator wordModelDecorator;
|
||||
|
||||
@FXML
|
||||
private TextArea textHistory;
|
||||
|
||||
@FXML
|
||||
private TextField textEingabe;
|
||||
|
||||
@FXML
|
||||
private Label labelTitel;
|
||||
|
||||
public void connectProperties() {
|
||||
//binding properties
|
||||
labelTitel.textProperty().bind(textEingabe.textProperty());
|
||||
}
|
||||
|
||||
public void setWordModel(WordModel wordModel) {
|
||||
wordModelDecorator = new WordModelDecorator(wordModel);
|
||||
wordModelDecorator.addListener(new IsObserver() {
|
||||
@Override
|
||||
public void update() {
|
||||
textHistory.setText(wordModel.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void hinzufuegenText(ActionEvent event) {
|
||||
for(String word : textEingabe.getText().split(" ")) {
|
||||
wordModelDecorator.addWord(word.toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void leerenTextEingabe(ActionEvent event) {
|
||||
textEingabe.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package ch.zhaw.prog2.application;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
/**
|
||||
* A WordModel object holds a map of words (String) with the actual count
|
||||
* @author bles
|
||||
*
|
||||
*/
|
||||
public class WordModel {
|
||||
private Map<String, Integer> words = new HashMap<>();
|
||||
|
||||
public void addWord(String word) {
|
||||
int count = words.getOrDefault(word, 0);
|
||||
words.put(word, ++count);
|
||||
}
|
||||
|
||||
public void removeWord(String word) {
|
||||
int count = words.getOrDefault(word, 1);
|
||||
if (count == 1) {
|
||||
words.remove(word);
|
||||
} else {
|
||||
words.put(word, --count);
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(Map.Entry<String, Integer> entry : words.entrySet()) {
|
||||
sb.append(entry.getKey()).append(" - ").append(entry.getValue());
|
||||
sb.append(System.lineSeparator());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package ch.zhaw.prog2.application;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
/**
|
||||
* Adds observable functionality to one WordModel-object.
|
||||
* The decorator uses the original methods of the WordModel-object.
|
||||
* @author bles
|
||||
*
|
||||
*/
|
||||
public class WordModelDecorator implements IsObservable {
|
||||
private final WordModel wordModel;
|
||||
private List<IsObserver> listener = new ArrayList<>();
|
||||
|
||||
public WordModelDecorator(WordModel wordModel) {
|
||||
this.wordModel = wordModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(IsObserver observer) {
|
||||
listener.add(observer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(IsObserver observer) {
|
||||
listener.remove(observer);
|
||||
}
|
||||
|
||||
public void addWord(String word) {
|
||||
wordModel.addWord(word);
|
||||
informListener();
|
||||
}
|
||||
|
||||
public void removeWord(String word) {
|
||||
wordModel.removeWord(word);
|
||||
informListener();
|
||||
}
|
||||
|
||||
private void informListener() {
|
||||
for(IsObserver observer : listener) {
|
||||
observer.update();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.geometry.*?>
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import java.lang.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
|
||||
<AnchorPane prefHeight="480.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.prog2.application.MainWindowController">
|
||||
<children>
|
||||
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" spacing="10.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
|
||||
<children>
|
||||
<Label fx:id="labelTitel" text="Label" />
|
||||
<TextField fx:id="textEingabe" maxWidth="300.0" />
|
||||
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="10.0">
|
||||
<children>
|
||||
<Button mnemonicParsing="false" onAction="#hinzufuegenText" text="Hinzufügen Text" />
|
||||
<Button mnemonicParsing="false" onAction="#leerenTextEingabe" text="Löschen Eingabefeld" />
|
||||
</children>
|
||||
</HBox>
|
||||
<TextArea fx:id="textHistory" editable="false" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
|
||||
</children>
|
||||
<padding>
|
||||
<Insets top="10.0" />
|
||||
</padding>
|
||||
</VBox>
|
||||
</children>
|
||||
<padding>
|
||||
<Insets bottom="20.0" />
|
||||
</padding>
|
||||
</AnchorPane>
|
||||
Reference in New Issue
Block a user