changes on the thread, temp is updating in the background

multiple-captors
Nicolas FRANCO 2 years ago
parent bcce3deeac
commit 07f91b1b1f

@ -1,12 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TreeView?> <?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Spinner?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1"> prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children> <children>
<TreeView fx:id="treeView" layoutX="129.0" layoutY="27.0" prefHeight="347.0" prefWidth="343.0" /> <TreeView fx:id="treeView" layoutX="20.0" layoutY="27.0" prefHeight="347.0" prefWidth="343.0" />
<Label fx:id="idLabel" layoutX="400.0" layoutY="27.0" text="ID: " />
<Label fx:id="nameLabel" layoutX="400.0" layoutY="57.0" text="Name: " />
<Label fx:id="temperatureLabel" layoutX="400.0" layoutY="87.0" text="Temperature: " />
</children> </children>
</AnchorPane> </AnchorPane>

@ -1,8 +1,10 @@
package launcher; package launcher;
import javafx.application.Application; import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.scene.control.TreeItem;
import javafx.stage.Stage; import javafx.stage.Stage;
import model.*; import model.*;
import view.CapteurView; import view.CapteurView;
@ -23,9 +25,9 @@ public class Launch extends Application {
@Override @Override
public void start(Stage primaryStage) throws IOException, InterruptedException { public void start(Stage primaryStage) throws IOException, InterruptedException {
// Creation des capteurs // Creation des capteurs
UnitCapteur captorC = new UnitCapteur(1,new SimpleStringProperty("Capteur CPU"),new StrategyCPU()); UnitCapteur captorC = new UnitCapteur(new SimpleStringProperty("1"),new SimpleStringProperty("Capteur CPU"),new StrategyCPU());
UnitCapteur captorR = new UnitCapteur(2,new SimpleStringProperty("Capteur Random"),new StrategyRandom()); UnitCapteur captorR = new UnitCapteur(new SimpleStringProperty("2"),new SimpleStringProperty("Capteur Random"),new StrategyRandom());
CapteurVirtuel captorV = new CapteurVirtuel(5,new SimpleStringProperty("Capteur Virtuel")); CapteurVirtuel captorV = new CapteurVirtuel(new SimpleStringProperty("3"),new SimpleStringProperty("Capteur Virtuel"));
// Ajout des capteurs dans la collections du capteur virtuel // Ajout des capteurs dans la collections du capteur virtuel
captorV.addCapteur(captorC,1); captorV.addCapteur(captorC,1);
@ -40,26 +42,20 @@ public class Launch extends Application {
capteurs.add(captorR); capteurs.add(captorR);
capteurs.add(captorV); capteurs.add(captorV);
// FXML NOT WORKING
Stage thermostatStage = new Stage(); Stage thermostatStage = new Stage();
TreeViewCtrl treeViewCtrl = new TreeViewCtrl(capteurs,"view/TreeView.fxml","treeView"); TreeViewCtrl treeViewCtrl = new TreeViewCtrl(capteurs,"view/TreeView.fxml","treeView");
treeViewCtrl.show();
Thread t = new Thread() { Thread t = new Thread() {
public void run() { public void run() {
while (true) { while (true) {
for (Map.Entry<CapteurAbstrait, Integer> entry : captorV.getCapteurs().entrySet()) { Platform.runLater(() -> {
CapteurAbstrait c = entry.getKey(); capteurs.forEach(c -> {
c.genTemp(); if(c instanceof UnitCapteur uc){
uc.genTemp();
} }
captorV.genTemp(); });
});
try { try {
/*((ImageCtrlView) capteurView).changeImage(); sleep(1000);
System.out.println(captorV.getNom() + ": " + captorV.getTemp().getValue() + " °C");
for (Map.Entry<CapteurAbstrait, Integer> entry : captorV.getCapteurs().entrySet()) {
CapteurAbstrait c = entry.getKey();
System.out.println(c.getNom() + ": " + c.getTemp().getValue() + " °C");
}*/
sleep(4000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -67,6 +63,7 @@ public class Launch extends Application {
} }
}; };
t.start(); t.start();
treeViewCtrl.show();
} }
} }

@ -1,16 +1,13 @@
package model; package model;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.*;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public abstract class CapteurAbstrait { public abstract class CapteurAbstrait {
private int id; private Property<String> id;
private StringProperty nom; private StringProperty nom;
private ObjectProperty<Double> temp = new SimpleObjectProperty<>(); private ObjectProperty<Double> temp = new SimpleObjectProperty<Double>();
CapteurAbstrait(int id, StringProperty nom){ CapteurAbstrait(Property<String> id, StringProperty nom){
this.id = id; this.id = id;
this.nom = nom; this.nom = nom;
this.temp.setValue(0.0); this.temp.setValue(0.0);
@ -18,11 +15,11 @@ public abstract class CapteurAbstrait {
public abstract void genTemp(); public abstract void genTemp();
/* Getters - Setters */ /* Getters - Setters */
public int getId() { public Property<String> getId() {
return id; return id;
} }
public void setId(int id) { public void setId(Property<String> id) {
this.id = id; this.id = id;
} }

@ -1,6 +1,7 @@
package model; package model;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.ObjectProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
@ -12,7 +13,7 @@ public class CapteurVirtuel extends CapteurAbstrait {
private Map<CapteurAbstrait, Integer> capteurs; private Map<CapteurAbstrait, Integer> capteurs;
public CapteurVirtuel(int id, StringProperty nom){ public CapteurVirtuel(Property<String> id, StringProperty nom){
super(id, nom); super(id, nom);
this.capteurs = new HashMap<>(); this.capteurs = new HashMap<>();
} }

@ -15,10 +15,14 @@ public class TreeItemFactoryVisitor implements Visitor<TreeItem<CapteurAbstrait>
root.setExpanded(true); root.setExpanded(true);
for(CapteurAbstrait c : cv.getCapteurs().keySet()){ for(CapteurAbstrait c : cv.getCapteurs().keySet()){
TreeItem<CapteurAbstrait> item = new TreeItem<>(); TreeItem<CapteurAbstrait> item = new TreeItem<>(c);
if(c.getClass() == CapteurVirtuel.class){
item = c.accept(this);
} else {
item = new TreeItem<>(c);
}
root.getChildren().add(item); root.getChildren().add(item);
} }
return root; return root;
} }
} }

@ -1,11 +1,12 @@
package model; package model;
import javafx.beans.property.Property;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
public class UnitCapteur extends CapteurAbstrait{ public class UnitCapteur extends CapteurAbstrait{
private StrategyCaptor strategie; private StrategyCaptor strategie;
public UnitCapteur(int id, StringProperty nom, StrategyCaptor st) { public UnitCapteur(Property<String> id, StringProperty nom, StrategyCaptor st) {
super(id, nom); super(id, nom);
this.strategie = st; this.strategie = st;
} }

@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import javafx.beans.binding.Bindings;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
@ -13,12 +14,22 @@ import javafx.scene.control.*;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import javafx.util.converter.NumberStringConverter;
import model.*; import model.*;
import javax.swing.text.LabelView;
public class TreeViewCtrl extends FXMLView { public class TreeViewCtrl extends FXMLView {
@FXML @FXML
private TreeView<CapteurAbstrait> treeView; private TreeView<CapteurAbstrait> treeView;
@FXML
private Label idLabel;
@FXML
private Label nameLabel;
@FXML
private Label temperatureLabel;
private ArrayList<CapteurAbstrait> capteurAbstraits; private ArrayList<CapteurAbstrait> capteurAbstraits;
@ -75,5 +86,18 @@ public class TreeViewCtrl extends FXMLView {
} }
} }
}); });
treeView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
CapteurAbstrait capteur = newValue.getValue();
idLabel.textProperty().bindBidirectional(capteur.getId());
nameLabel.textProperty().bindBidirectional(capteur.getNom());
temperatureLabel.textProperty().bind(Bindings.format("%.2f",capteur.getTemp()));
}
});
}
public TreeView<CapteurAbstrait> getTreeView() {
return treeView;
} }
} }

Loading…
Cancel
Save