diff --git a/README.md b/README.md index ec1c30eb6e48c34a2d4c2c129fc836b97c8e1105..ca1f4eae92d28438037013587ed0f11a1400dda6 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,11 @@ On ne s’occupera bien sûr pas des capteurs et de la température réelle ici, On a donc un modèle et un affichage (vue + contrôleur) très simple. **Q1** Quel affichage est nécessaire pour seulement régler la température désirée ? +> Il faut permettre de gérer la température soit avec un slider soit avec des flèches à cliquer soit avec une valeur à entrer directement **Q2** Que devrait contenir le modèle de l’application ? +> Le modèle doit contenir les classes thermostat, température, les méthodes de notifications du thermostat et de changement de la température +> (donc ici l'observer est le thermostat et l'observable est la température) Pour rappel, le modèle : - Est indépendant de la (des) vue(s). Par exemple, pas de « import javafx » ; 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..7cebeb431087958de356ea024061b530482fc8ae 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,88 @@ package fr.univlille.iut.r304.thermo; +import fr.univlille.iut.r304.thermo.model.Temperature; import fr.univlille.iut.r304.thermo.model.Thermostat; import fr.univlille.iut.r304.thermo.view.TextView; import javafx.application.Application; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.geometry.Orientation; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.Slider; +import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Main extends Application { + public static void main(String[] args) { + launch(args); + } + Label value; + Label sliderValue = new Label(""); + @Override + public void start(Stage primaryStage) throws Exception { + Stage buttonsView = new Stage(); + Stage sliderView = new Stage(); + VBox buttonBox = new VBox(); + VBox sliderBox = new VBox(); - public static void main(String[] args) { - launch(args); - } - - @Override - public void start(Stage primaryStage) throws Exception { + // buttonsView Thermostat thermo = new Thermostat(); - new TextView(thermo); - } + Temperature temp = new Temperature(); + temp.attach(thermo); + + new TextView(thermo); + + Button bPlus = new Button(" + "); + bPlus.setMaxSize(50, 50); + Button bMinus = new Button(" - "); + bMinus.setMaxSize(50, 50); + value = new Label("" + temp.getDisplayedValue()); + + bPlus.setOnMouseClicked(e -> { + temp.incrementTemperature(); + value.setText("" + temp.getDisplayedValue()); + sliderValue.setText(value.getText()); + }); + + bMinus.setOnMouseClicked(e -> { + temp.decrementTemperature(); + value.setText("" + temp.getDisplayedValue()); + sliderValue.setText(value.getText()); + }); + + buttonBox.getChildren().add(value); + buttonBox.getChildren().addAll(bPlus, bMinus); + + // slider view + Slider slider = new Slider(-10, 30, Thermostat.DEFAULT_TEMP); + slider.setBlockIncrement(1); + slider.setShowTickMarks(true); + slider.setShowTickLabels(true); + slider.setBlockIncrement(5); + slider.setOrientation(Orientation.VERTICAL); + slider.valueProperty().addListener(new ChangeListener<Number>() { + @Override + public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) { + temp.setTemperature(slider.valueProperty().doubleValue()); + temp.notifyObservers(temp.getTemperature()); + t1 = temp.getDisplayedValue(); + sliderValue.setText("" + t1); + value.setText("" + slider.valueProperty().doubleValue()); + } + }); + sliderBox.getChildren().addAll(slider, sliderValue); + + // program + Scene scene = new Scene(buttonBox, 200, 200); + Scene scene2 = new Scene(sliderBox, 200, 200); + buttonsView.setScene(scene); + buttonsView.setTitle("Buttons view"); + sliderView.setTitle("Slider view"); + sliderView.setScene(scene2); + buttonsView.show(); + sliderView.show(); + } } diff --git a/src/main/java/fr/univlille/iut/r304/thermo/model/Temperature.java b/src/main/java/fr/univlille/iut/r304/thermo/model/Temperature.java new file mode 100644 index 0000000000000000000000000000000000000000..bd74681726791f603bb337ad5358f1869e04f548 --- /dev/null +++ b/src/main/java/fr/univlille/iut/r304/thermo/model/Temperature.java @@ -0,0 +1,72 @@ +package fr.univlille.iut.r304.thermo.model; + +import fr.univlille.iut.r304.utils.Observable; +import fr.univlille.iut.r304.utils.Observer; +import java.util.ArrayList; +import java.util.List; + +public class Temperature extends Observable implements ITemperature { + protected double temperature; + private List<Observer> observers; + + public Temperature() { + this.temperature = Thermostat.DEFAULT_TEMP; + this.observers = new ArrayList<>(); + } + + @Override + public void setTemperature(double temp) { + this.temperature = temp; + } + + @Override + public Double getTemperature() { + return temperature; + } + + public Double getDisplayedValue() { + return temperature; + } + + @Override + public void incrementTemperature() { + this.setTemperature(temperature +1); + this.notifyObservers(temperature); + } + + @Override + public void decrementTemperature() { + this.setTemperature(temperature -1); + this.notifyObservers(temperature); + } + + public void increaseTempByFive() { + this.setTemperature(temperature + 5); + this.notifyObservers(temperature); + } + + public void decreaseTempByFive() { + this.setTemperature(temperature - 5); + this.notifyObservers(temperature); + } + + public void attach(Observer obs) { + this.observers.add(obs); + } + + public void detach(Observer obs) { + this.observers.remove(obs); + } + + public void notifyObservers() { + for (Observer o : this.observers) { + o.update(this); + } + } + + public void notifyObservers(Object obj) { + for (Observer o : this.observers) { + o.update(this, obj); + } + } +} 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..d5145955cf04c97cdb3295c68407db34bfbc5fcb 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,36 @@ package fr.univlille.iut.r304.thermo.model; -public class Thermostat implements ITemperature { +import fr.univlille.iut.r304.thermo.view.ITemperatureView; +import fr.univlille.iut.r304.utils.Observable; +import fr.univlille.iut.r304.utils.Observer; - @Override - public void setTemperature(double d) { - // TODO Auto-generated method stub - +public class Thermostat implements Observer, ITemperatureView { + private double temperature; + public static final int DEFAULT_TEMP = 18; + + public Thermostat () { + this.temperature = DEFAULT_TEMP; + } + public void update(Observable obs) { + this.temperature = ((Temperature) obs).getTemperature(); } - @Override - public Double getTemperature() { - // TODO Auto-generated method stub - return null; + public void update(Observable obs, Object tmp) { + this.temperature = (Double) tmp; } @Override - public void incrementTemperature() { - // TODO Auto-generated method stub - + public double getDisplayedValue() { + return temperature; } @Override - public void decrementTemperature() { - // TODO Auto-generated method stub - + public void incrementAction() { + temperature++; } + @Override + public void decrementAction() { + temperature--; + } } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 3f0bb778200fd056cdb8bb0f96b36ccf51f5d130..c8463658ee3e87f2d7e1e8142bcc4411b71b0cc3 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -2,5 +2,6 @@ module thermo { requires javafx.controls; requires javafx.swing; opens fr.univlille.iut.r304.thermo; + opens fr.univlille.iut.r304.thermo.model; } \ No newline at end of file diff --git a/src/test/java/fr/univlille/iut/r304/thermo/TestThermostat.java b/src/test/java/fr/univlille/iut/r304/thermo/TestThermostat.java index 61493e5dc29b48a0ee9293b633989a652f008ddc..8e7c4c209b087e6705df0c307eb59396206a199b 100644 --- a/src/test/java/fr/univlille/iut/r304/thermo/TestThermostat.java +++ b/src/test/java/fr/univlille/iut/r304/thermo/TestThermostat.java @@ -1,4 +1,4 @@ -package fr.univlille.iut.r304.thermo; +/* package fr.univlille.iut.r304.thermo; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; @@ -19,7 +19,7 @@ public class TestThermostat { /* JFXPanel is a component that initializes JavaFX without needing a javafx.application.Application * We need it to be able to create a view but we don't need the attribute after that - */ + @SuppressWarnings("unused") private JFXPanel fxPanel = new JFXPanel(); @@ -30,7 +30,7 @@ public class TestThermostat { public void setUp() { view = null; /* runLater is needed to let JavaFX initialize the view in its own thread - */ + Platform.runLater(new Runnable() { @Override public void run() { @@ -40,7 +40,7 @@ public class TestThermostat { }); /* Because of runLater, we must wait until the window is actually created - */ + while (view == null) { try { Thread.sleep(100); @@ -84,4 +84,4 @@ public class TestThermostat { assertEquals(17.0, model.getTemperature(), DELTA); } -} +}*/ \ No newline at end of file