diff --git a/src/main/java/fr/univlille/iut/r304/thermo/Main.java b/src/main/java/fr/univlille/iut/r304/thermo/Main.java
index 9ab0f10c6f1bcbd671d681229e4934137cea4279..d920feb1223916fec7e9e535c2983bdf744d790a 100644
--- a/src/main/java/fr/univlille/iut/r304/thermo/Main.java
+++ b/src/main/java/fr/univlille/iut/r304/thermo/Main.java
@@ -1,20 +1,28 @@
package fr.univlille.iut.r304.thermo;
import fr.univlille.iut.r304.thermo.model.Thermostat;
+import fr.univlille.iut.r304.thermo.view.SliderView;
import fr.univlille.iut.r304.thermo.view.TextView;
import javafx.application.Application;
+
import javafx.stage.Stage;
+
+import java.awt.*;
+
public class Main extends Application {
+ Thermostat thermo = new Thermostat();
+
public static void main(String[] args) {
launch(args);
}
+
@Override
public void start(Stage primaryStage) throws Exception {
- Thermostat thermo = new Thermostat();
new TextView(thermo);
+ new SliderView(thermo);
}
}
diff --git a/src/main/java/fr/univlille/iut/r304/thermo/model/Thermostat.java b/src/main/java/fr/univlille/iut/r304/thermo/model/Thermostat.java
index 9d86f764c0563d05a0e479f4ddbe891165ad9758..e83cdbc65d0a054b64f9fa2fa44d6bcad90f9241 100644
--- a/src/main/java/fr/univlille/iut/r304/thermo/model/Thermostat.java
+++ b/src/main/java/fr/univlille/iut/r304/thermo/model/Thermostat.java
@@ -1,29 +1,27 @@
package fr.univlille.iut.r304.thermo.model;
+import fr.univlille.iut.r304.utils.*;
-public class Thermostat implements ITemperature {
+public class Thermostat extends ConnectableProperty implements ITemperature{
+
+ private double temperature;
@Override
public void setTemperature(double d) {
- // TODO Auto-generated method stub
-
+ this.temperature = d;
}
@Override
public Double getTemperature() {
- // TODO Auto-generated method stub
- return null;
+ return this.temperature;
}
@Override
public void incrementTemperature() {
- // TODO Auto-generated method stub
-
+ this.setTemperature(this.getTemperature() + 1);
}
@Override
public void decrementTemperature() {
- // TODO Auto-generated method stub
-
+ this.setTemperature(this.getTemperature() - 1);
}
-
}
diff --git a/src/main/java/fr/univlille/iut/r304/thermo/view/SliderView.java b/src/main/java/fr/univlille/iut/r304/thermo/view/SliderView.java
new file mode 100644
index 0000000000000000000000000000000000000000..a5a86d66a080adebaf814cc6b338d8d47ceaf15c
--- /dev/null
+++ b/src/main/java/fr/univlille/iut/r304/thermo/view/SliderView.java
@@ -0,0 +1,97 @@
+package fr.univlille.iut.r304.thermo.view;
+
+import fr.univlille.iut.r304.thermo.model.Thermostat;
+import fr.univlille.iut.r304.utils.ConnectableProperty;
+import fr.univlille.iut.r304.utils.Observable;
+import fr.univlille.iut.r304.utils.Observer;
+import javafx.geometry.Orientation;
+import javafx.geometry.Pos;
+import javafx.scene.Scene;
+import javafx.scene.control.Button;
+import javafx.scene.control.Slider;
+import javafx.scene.control.TextField;
+import javafx.scene.layout.HBox;
+import javafx.scene.layout.VBox;
+import javafx.scene.text.Text;
+import javafx.stage.Stage;
+
+public class SliderView extends Stage implements ITemperatureView, Observer{
+
+ private static final String TITLE = "Thermostat";
+ protected Thermostat model;
+ protected boolean wasUpdated = false;
+ Slider slider = new Slider(0, 100, 0);
+
+ public SliderView(Thermostat model) {
+ this.model = model;
+ model.attach(this);
+
+
+
+ Stage primaryStage = new Stage();
+ VBox root = new VBox();
+ Scene scene = new Scene(root, 300, 200);
+ primaryStage.setScene(scene);
+ primaryStage.setTitle(TITLE);
+
+ Button incrementButton = new Button("+");
+ Button decrementButton = new Button("-");
+
+ slider.setOrientation(Orientation.VERTICAL);
+ slider.setShowTickLabels(true);
+
+
+ slider.valueProperty().addListener((observable, oldValue, newValue) -> {
+ newValue = (int) Math.round(newValue.doubleValue());
+ model.setTemperature(newValue.doubleValue());
+ model.notifyObservers(model.getTemperature());
+ });
+
+ incrementButton.setOnAction(e -> {
+ model.setTemperature(model.getTemperature() + 5);
+ model.notifyObservers(model.getTemperature());
+ });
+
+ decrementButton.setOnAction(e -> {
+ model.setTemperature(model.getTemperature() -5);
+ model.notifyObservers(model.getTemperature());
+ });
+
+ root.getChildren().addAll(incrementButton, slider, decrementButton);
+ root.setAlignment(Pos.CENTER);
+
+ primaryStage.show();
+ }
+
+ @Override
+ public double getDisplayedValue() {
+ return model.getTemperature();
+ }
+
+ @Override
+ public void incrementAction() {
+ model.incrementTemperature();
+ }
+
+ @Override
+ public void decrementAction() {
+ model.decrementTemperature();
+ }
+
+ public void update(Observable o) {
+ if (o instanceof Thermostat) {
+ wasUpdated = true;
+ }
+ }
+
+ public void update(Observable o, Object arg) {
+ if (o instanceof Thermostat) {
+ wasUpdated = true;
+ if (arg instanceof Double) {
+ System.out.println("Temperature : " + arg);
+ slider.setValue((double) arg);
+ }
+ }
+ }
+
+}
diff --git a/src/main/java/fr/univlille/iut/r304/thermo/view/TextView.java b/src/main/java/fr/univlille/iut/r304/thermo/view/TextView.java
index 4faa759f6764a99144422ba1e6c9d45da0a15810..5828a6e31b9ec8e4c440c2e4095634dd9e28e6b3 100644
--- a/src/main/java/fr/univlille/iut/r304/thermo/view/TextView.java
+++ b/src/main/java/fr/univlille/iut/r304/thermo/view/TextView.java
@@ -1,32 +1,100 @@
package fr.univlille.iut.r304.thermo.view;
import fr.univlille.iut.r304.thermo.model.Thermostat;
+import fr.univlille.iut.r304.utils.ConnectableProperty;
+import fr.univlille.iut.r304.utils.Observable;
+import fr.univlille.iut.r304.utils.Observer;
+import javafx.geometry.Pos;
+import javafx.scene.Scene;
+import javafx.scene.control.Button;
+import javafx.scene.control.TextField;
+import javafx.scene.layout.HBox;
+import javafx.scene.layout.VBox;
+import javafx.scene.text.Text;
import javafx.stage.Stage;
-public class TextView extends Stage implements ITemperatureView {
+public class TextView extends Stage implements ITemperatureView, Observer{
+
+ private static final String TITLE = "Thermostat";
+ protected Thermostat model;
+ protected boolean wasUpdated = false;
+ protected TextField temperatureField = new TextField();
public TextView(Thermostat model) {
- //simplement pour avoir qqch qui s'affiche à la prise en main du dépôt
- this.setTitle("Thermostat");
- this.show();
+ this.model = model;
+ model.attach(this);
+
+ Stage primaryStage = new Stage();
+ VBox root = new VBox();
+ HBox hbox = new HBox();
+ Scene scene = new Scene(root, 300, 200);
+ primaryStage.setScene(scene);
+ primaryStage.setTitle(TITLE);
+
+ Text text = new Text("Température en Celsius");
+ temperatureField.setText(this.getDisplayedValue() + "°C");
+ Button incrementButton = new Button("+");
+ Button decrementButton = new Button("-");
+
+ temperatureField.setOnAction(e -> {
+ try {
+ double newTemp = Double.parseDouble(temperatureField.getText().substring(0, temperatureField.getText().length() - 2));
+ model.setTemperature(newTemp);
+ temperatureField.setText(this.getDisplayedValue() + "°C");
+ model.notifyObservers(model.getTemperature());
+ } catch (NumberFormatException ex) {
+ temperatureField.setText("0°C");
+ }
+ });
+
+ incrementButton.setOnAction(e -> {
+ model.incrementTemperature();
+ temperatureField.setText(this.getDisplayedValue() + "°C");
+ model.notifyObservers(model.getTemperature());
+ });
+
+ decrementButton.setOnAction(e -> {
+ model.decrementTemperature();
+ temperatureField.setText(this.getDisplayedValue() + "°C");
+ model.notifyObservers(model.getTemperature());
+ });
+
+ hbox.getChildren().addAll(decrementButton, temperatureField, incrementButton);
+ hbox.setAlignment(Pos.CENTER);
+ root.getChildren().addAll(text, hbox);
+ root.setAlignment(Pos.CENTER);
+ primaryStage.show();
}
@Override
public double getDisplayedValue() {
- // TODO Auto-generated method stub
- return 0;
+ return model.getTemperature();
}
@Override
public void incrementAction() {
- // TODO Auto-generated method stub
-
+ model.incrementTemperature();
}
@Override
public void decrementAction() {
- // TODO Auto-generated method stub
-
+ model.decrementTemperature();
+ }
+
+ public void update(Observable o) {
+ if (o instanceof Thermostat) {
+ wasUpdated = true;
+ }
+ }
+
+ public void update(Observable o, Object arg) {
+ if (o instanceof Thermostat) {
+ wasUpdated = true;
+ if (arg instanceof Double) {
+ System.out.println("Temperature : " + arg);
+ temperatureField.setText(arg + "°C");
+ }
+ }
}
}
diff --git a/src/main/java/fr/univlille/iut/r304/utils/Observable.java b/src/main/java/fr/univlille/iut/r304/utils/Observable.java
index c2445c7c1e20b885949f0fd1677288d4c3cbf07f..361ccc358bf7f31110b635b1af87c69c6ef7f64b 100644
--- a/src/main/java/fr/univlille/iut/r304/utils/Observable.java
+++ b/src/main/java/fr/univlille/iut/r304/utils/Observable.java
@@ -22,7 +22,7 @@ public abstract class Observable {
}
}
- protected void notifyObservers(Object data) {
+ public void notifyObservers(Object data) {
this.updateList();
for (Observer o : attached) {
o.update(this, data);