Aufgabe 4 ausgeführt.

This commit is contained in:
romanschenk37 2022-03-30 11:17:18 +02:00
parent f0995fbf27
commit dce3387032
8 changed files with 226 additions and 29 deletions

View File

@ -0,0 +1,18 @@
package ch.zhaw.prog2.fxmlcalculator;
/**
* 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);
}

View File

@ -0,0 +1,13 @@
package ch.zhaw.prog2.fxmlcalculator;
/**
* Most basic interface for beeing an observer
* @author bles
*
*/
public interface IsObserver {
/**
* This method is always called when an observed object
* changes
*/
void update();
}

View File

@ -5,6 +5,9 @@ import javafx.fxml.FXMLLoader;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.io.IOException;
/** /**
* Main-Application. Opens the first window (MainWindow) and the common ValueHandler * Main-Application. Opens the first window (MainWindow) and the common ValueHandler
* @author * @author
@ -14,10 +17,21 @@ public class Main extends Application {
private ValueHandler valueHandler; private ValueHandler valueHandler;
private static final String INFO = """
Enter valid values to
- Initial amount (> 0)
- Return in % (can be +/- or 0)
- Annual Costs (> 0)
- Number of years (> 0)
Calculate displays the annual balance development!";
""";
public static void main(String[] args) { public static void main(String[] args) {
launch(args); launch(args);
} }
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
valueHandler = new ValueHandler(); valueHandler = new ValueHandler();
@ -27,16 +41,17 @@ public class Main extends Application {
private void mainWindow(Stage primaryStage) { private void mainWindow(Stage primaryStage) {
//load main window //load main window
try { try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindow.fxml"));
Pane rootNode = loader.load();
MainWindowController mainWindowController = loader.getController();
Scene scene = new Scene(rootNode); FXMLLoader mainWindowLoader = new FXMLLoader(getClass().getResource("MainWindow.fxml"));
Pane rootNode = mainWindowLoader.load();
primaryStage.setScene(scene); MainWindowController mainWindowController = mainWindowLoader.getController();
mainWindowController.setValueHandler(new ValueHandler());
mainWindowController.setHelpText(INFO);
Scene mainWindowScene = new Scene(rootNode);
primaryStage.setScene(mainWindowScene);
primaryStage.show(); primaryStage.show();
} catch(Exception e) { } catch(Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -1,24 +1,27 @@
package ch.zhaw.prog2.fxmlcalculator; package ch.zhaw.prog2.fxmlcalculator;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem; import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea; import javafx.scene.control.TextArea;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.layout.*; import javafx.scene.layout.*;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.event.ActionEvent; import javafx.scene.Scene;
import javafx.fxml.FXML;
import javax.xml.transform.Result;
import java.io.IOException;
/**
* Controller for the MainWindow. One controller per mask (or FXML file)
* Contains everything the controller has to reach in the view (controls)
* and all methods the view calls based on events.
* @author
* @version 1.0
*/
public class MainWindowController { public class MainWindowController {
ValueHandlerDecorator valueHandlerDecorator;
ValueHandler valueHandler;
String helpText;
@FXML @FXML
private TextField annualCost; private TextField annualCost;
@ -48,12 +51,79 @@ public class MainWindowController {
@FXML @FXML
void calculate(ActionEvent event) { void calculate(ActionEvent event) {
valueHandlerDecorator.checkValuesAndCalculateResult(initialAmount.getText(), returnInPercent.getText(), annualCost.getText(), numberOfYears.getText());
}
@FXML
void clearResults(ActionEvent event) {
valueHandlerDecorator.clearResult();
}
@FXML
void clearValues(ActionEvent event) {
if(clearAnnualCosts.isSelected()){
annualCost.clear();
}
if(clearReturnInPercent.isSelected()){
returnInPercent.clear();
}
if(clearNumberOfYears.isSelected()){
numberOfYears.clear();
}
if(clearInitialAmount.isSelected()){
initialAmount.clear();
}
} }
@FXML @FXML
void close(ActionEvent event) { void close(ActionEvent event) {
Platform.exit();
}
@FXML
void openResultWindow(ActionEvent event) {
FXMLLoader resultWindowLoader = new FXMLLoader(getClass().getResource("ResultWindow.fxml"));
Pane resultNode = new StackPane();
try {
resultNode = resultWindowLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
ResultWindowController resultWindowController = resultWindowLoader.getController();
resultWindowController.setValueHandler(valueHandler, valueHandlerDecorator);
Scene resultScene = new Scene(resultNode);
// New window (Stage)
Stage resultStage = new Stage();
resultStage.setTitle("Results");
resultStage.setScene(resultScene);
resultStage.show();
valueHandlerDecorator.informListener();
}
@FXML
void showHelp(ActionEvent event) {
valueHandlerDecorator.showHelp(helpText);
}
public void setValueHandler(ValueHandler valueHandler){
this.valueHandler = valueHandler;
valueHandlerDecorator = new ValueHandlerDecorator(valueHandler);
valueHandlerDecorator.addListener(new IsObserver() {
@Override
public void update() {
results.setText(valueHandler.getResultBound());
}
});
}
public void setHelpText(String helpText){
this.helpText = helpText;
} }
} }

View File

@ -1,5 +1,6 @@
package ch.zhaw.prog2.fxmlcalculator; package ch.zhaw.prog2.fxmlcalculator;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea; import javafx.scene.control.TextArea;
import javafx.stage.Stage; import javafx.stage.Stage;
@ -11,15 +12,26 @@ import javafx.stage.Stage;
* @version 1.0 * @version 1.0
*/ */
public class ResultWindowController { public class ResultWindowController {
// add datafields ValueHandler valueHandler;
ValueHandlerDecorator valueHandlerDecorator;
//@FXML @FXML
private TextArea results; private TextArea results;
//@FXML @FXML
private void closeWindow() { private void closeWindow() {
Stage stage = (Stage) results.getScene().getWindow(); Stage stage = (Stage) results.getScene().getWindow();
stage.close(); stage.close();
} }
public void setValueHandler(ValueHandler valueHandler, ValueHandlerDecorator valueHandlerDecorator){
this.valueHandler = valueHandler;
valueHandlerDecorator.addListener(new IsObserver() {
@Override
public void update() {
results.setText(valueHandler.getResultBound());
}
});
}
} }

View File

@ -0,0 +1,60 @@
package ch.zhaw.prog2.fxmlcalculator;
import ch.zhaw.prog2.fxmlcalculator.IsObservable;
import ch.zhaw.prog2.fxmlcalculator.IsObserver;
import ch.zhaw.prog2.fxmlcalculator.ValueHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Adds observable functionality to one Value Handler.
* The decorator uses the original methods of the WordModel-object.
* @author bles
*
*/
public class ValueHandlerDecorator implements IsObservable {
private final ValueHandler valueHandler;
private List<IsObserver> listener = new ArrayList<>();
public ValueHandlerDecorator(ValueHandler valueHandler) {
this.valueHandler = valueHandler;
}
@Override
public void addListener(IsObserver observer) {
listener.add(observer);
}
@Override
public void removeListener(IsObserver observer) {
listener.remove(observer);
}
public void checkValuesAndCalculateResult(String initialAmount, String returnInPercent, String annualCost, String numberOfYears) {
valueHandler.checkValuesAndCalculateResult(initialAmount, returnInPercent, annualCost, numberOfYears);
informListener();
}
public void showHelp(String text) {
valueHandler.setResultBound(text);
informListener();
}
public void clearResult(){
valueHandler.clearResult();
informListener();
}
public void informListener() {
for(IsObserver observer : listener) {
observer.update();
}
}
}

View File

@ -10,6 +10,7 @@
<?import javafx.scene.control.SeparatorMenuItem?> <?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.TextArea?> <?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?> <?import javafx.scene.control.TextField?>
<?import javafx.scene.input.KeyCodeCombination?>
<?import javafx.scene.layout.BorderPane?> <?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?> <?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?> <?import javafx.scene.layout.GridPane?>
@ -17,7 +18,7 @@
<?import javafx.scene.layout.RowConstraints?> <?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<BorderPane prefHeight="400.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.prog2.fxmlcalculator.MainWindowController"> <BorderPane prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.prog2.fxmlcalculator.MainWindowController">
<top> <top>
<MenuBar BorderPane.alignment="CENTER"> <MenuBar BorderPane.alignment="CENTER">
<menus> <menus>
@ -28,14 +29,17 @@
<CheckMenuItem fx:id="clearAnnualCosts" mnemonicParsing="false" text="Annual Cost" /> <CheckMenuItem fx:id="clearAnnualCosts" mnemonicParsing="false" text="Annual Cost" />
<CheckMenuItem fx:id="clearNumberOfYears" mnemonicParsing="false" text="Number of years" /> <CheckMenuItem fx:id="clearNumberOfYears" mnemonicParsing="false" text="Number of years" />
<SeparatorMenuItem mnemonicParsing="false" /> <SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Clear values" /> <MenuItem mnemonicParsing="false" onAction="#clearValues" text="Clear values" />
<SeparatorMenuItem mnemonicParsing="false" /> <SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Clear results" /> <MenuItem mnemonicParsing="false" onAction="#clearResults" text="Clear results" />
</items> </items>
</Menu> </Menu>
<Menu mnemonicParsing="false" text="?"> <Menu mnemonicParsing="false" text="?">
<items> <items>
<MenuItem mnemonicParsing="false" text="Show help F1" /> <MenuItem mnemonicParsing="false" onAction="#showHelp" text="Show help F1">
<accelerator>
<KeyCodeCombination alt="ANY" code="F1" control="ANY" meta="ANY" shift="ANY" shortcut="ANY" />
</accelerator></MenuItem>
</items> </items>
</Menu> </Menu>
</menus> </menus>
@ -97,6 +101,11 @@
<Insets bottom="5.0" left="10.0" right="10.0" top="5.0" /> <Insets bottom="5.0" left="10.0" right="10.0" top="5.0" />
</HBox.margin> </HBox.margin>
</Button> </Button>
<Button mnemonicParsing="false" onAction="#openResultWindow" text="Open result window">
<HBox.margin>
<Insets bottom="5.0" left="10.0" right="10.0" top="5.0" />
</HBox.margin>
</Button>
</children> </children>
<BorderPane.margin> <BorderPane.margin>
<Insets /> <Insets />

View File

@ -4,12 +4,12 @@
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?> <?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?> <?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.*?> <?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?> <?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="250.0" minWidth="400.0" prefHeight="250.0" prefWidth="400.0" <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="250.0" minWidth="400.0" prefHeight="250.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.prog2.fxmlcalculator.ResultWindowController">
xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="ch.zhaw.prog2.fxmlcalculator.ResultWindowController">
<center> <center>
<VBox minWidth="300.0" BorderPane.alignment="TOP_CENTER"> <VBox minWidth="300.0" BorderPane.alignment="TOP_CENTER">
<children> <children>
@ -35,7 +35,7 @@
<bottom> <bottom>
<HBox alignment="BOTTOM_RIGHT" BorderPane.alignment="CENTER"> <HBox alignment="BOTTOM_RIGHT" BorderPane.alignment="CENTER">
<children> <children>
<Button fx:id="closeForm" mnemonicParsing="false" onMouseClicked="#closeWindow" text="Close"> <Button fx:id="closeForm" mnemonicParsing="false" onAction="#closeWindow" text="Close">
<HBox.margin> <HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</HBox.margin> </HBox.margin>