tree view attempt: display not working as expected

multiple-captors
Nicolas FRANCO 2 years ago
parent ac4ab8f5ba
commit bc6c5596ea

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Spinner?>
<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">
<children>
<TreeView fx:id="treeView" layoutX="129.0" layoutY="27.0" prefHeight="347.0" prefWidth="343.0" />
<Spinner fx:id="spinnerTemp"></Spinner>
</children>
</AnchorPane>

@ -1,13 +1,18 @@
package launcher;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.stage.Stage;
import model.*;
import view.CapteurView;
import view.ImageCtrlView;
import view.ThermostatView;
import view.TreeViewCtrl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Launch extends Application {
@ -17,40 +22,45 @@ public class Launch extends Application {
@Override
public void start(Stage primaryStage) throws IOException, InterruptedException {
// Creation des capteurs
UnitCapteur captorC = new UnitCapteur(1,new SimpleStringProperty("Capteur CPU"),new StrategyCPU());
UnitCapteur captorR = new UnitCapteur(2,new SimpleStringProperty("Capteur Random"),new StrategyRandom());
CapteurVirtuel captorV = new CapteurVirtuel(5,new SimpleStringProperty("Capteur Virtuel"));
UnitCapteur captorI = new UnitCapteur(2,"Capteur Intervalle",new StrategyCPU());
UnitCapteur captorR = new UnitCapteur(3,"Capteur Realiste", new StrategyCPU());
UnitCapteur captorC = new UnitCapteur(4,"Capteur CPU",new StrategyCPU());
CapteurVirtuel captorV = new CapteurVirtuel(5,"CapteurVirtuel");
captorV.addCapteur(captorI,1);
// Ajout des capteurs dans la collections du capteur virtuel
captorV.addCapteur(captorC,1);
captorV.addCapteur(captorR,1);
// FXML NOT WORKING
Stage thermostatStage = new Stage();
CapteurView capteurView = new ImageCtrlView(captorV,"view/Image.fxml","mon titre");
for (Map.Entry<CapteurAbstrait, Integer> entry : captorV.getCapteurs().entrySet()) {
CapteurAbstrait c = entry.getKey();
c.genTemp();
}
ThermostatView thermostatView = new ThermostatView(captorV,"view/Thermostat.fxml","mon titre");
captorV.genTemp();
ArrayList<CapteurAbstrait> capteurs = new ArrayList<>();
capteurs.add(captorC);
capteurs.add(captorR);
capteurs.add(captorV);
//Random random = new Random();
// FXML NOT WORKING
//Stage thermostatStage = new Stage();
//CapteurView capteurView = new ImageCtrlView(captorV,"view/Image.fxml","mon titre");
//ThermostatView thermostatView = new ThermostatView(captorV,"view/Thermostat.fxml","mon titre");
TreeViewCtrl treeViewCtrl = new TreeViewCtrl(capteurs,"view/TreeView.fxml","treeView");
Thread t = new Thread() {
public void run() {
while (true) {
for (Map.Entry<CapteurAbstrait, Integer> entry : captorV.getCapteurs().entrySet()) {
CapteurAbstrait c = entry.getKey();
c.genTemp();
}
captorV.genTemp();
try {
//((ImageCtrlView) capteurView).changeImage();
System.out.println(captorV.getNom() + ": " + captorV.getTemp().getValue() + " °C");
for (Map.Entry<CapteurAbstrait, Integer> entry : captorV.getCapteurs().entrySet()) {
CapteurAbstrait c = entry.getKey();
c.genTemp();
System.out.println(c.getNom() + ": " + c.getTemp().getValue() + " °C");
System.out.println(c.getNom() + ": " + c.getTemp().getValue() + " °C");
}
sleep(4000);
} catch (InterruptedException e) {
throw new RuntimeException(e);

@ -2,13 +2,15 @@ package model;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public abstract class CapteurAbstrait {
private int id;
private String nom;
private StringProperty nom;
private ObjectProperty<Double> temp = new SimpleObjectProperty<>();
CapteurAbstrait(int id, String nom){
CapteurAbstrait(int id, StringProperty nom){
this.id = id;
this.nom = nom;
this.temp.setValue(0.0);
@ -24,11 +26,11 @@ public abstract class CapteurAbstrait {
this.id = id;
}
public String getNom() {
return nom;
public StringProperty getNom() {
return this.nom;
}
public void setNom(String nom) {
public void setNom(StringProperty nom) {
this.nom = nom;
}
@ -40,4 +42,6 @@ public abstract class CapteurAbstrait {
this.temp.setValue(pTemp);
}
public abstract <T> T accept (Visitor<T> v);
}

@ -2,6 +2,7 @@ package model;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.StringProperty;
import java.util.HashMap;
import java.util.List;
@ -11,18 +12,22 @@ public class CapteurVirtuel extends CapteurAbstrait {
private Map<CapteurAbstrait, Integer> capteurs;
public CapteurVirtuel(int id, String nom){
public CapteurVirtuel(int id, StringProperty nom){
super(id, nom);
this.capteurs = new HashMap<>();
}
public void addCapteur(CapteurAbstrait c, int p){
public void addCapteur(UnitCapteur c, int p){
this.capteurs.put(c,p);
}
public void removeCapteur(CapteurAbstrait c){
public void removeCapteur(UnitCapteur c){
this.capteurs.remove(c);
}
public <T> T accept(Visitor<T> v){
return v.visit(this);
}
public Map<CapteurAbstrait, Integer> getCapteurs(){return this.capteurs;}
// calcule la temperature moyenne des capteurs sa collection de capteurs

@ -8,6 +8,6 @@ public class StrategyRandom implements StrategyCaptor {
@Override
public double genTemp() {
Random random = new Random();
return random.nextDouble(30);
return random.nextDouble(30) - 30;
}
}

@ -1,9 +1,11 @@
package model;
import javafx.beans.property.StringProperty;
public class UnitCapteur extends CapteurAbstrait{
private StrategyCaptor strategie;
public UnitCapteur(int id, String nom,StrategyCaptor st) {
public UnitCapteur(int id, StringProperty nom, StrategyCaptor st) {
super(id, nom);
this.strategie = st;
}
@ -12,4 +14,9 @@ public class UnitCapteur extends CapteurAbstrait{
public void genTemp() {
this.setTemp(strategie.genTemp());
}
@Override
public <T> T accept(Visitor<T> v) {
return v.visit(this);
}
}

@ -26,7 +26,7 @@ public class ImageCtrlView extends CapteurView{
changeImage();
}
private void changeImage() throws IOException {
public void changeImage() throws IOException {
if (this.capteur.getTemp().getValue() > 25){
input = clazz.getResourceAsStream("/images/sun.png");
imageview.setImage(new Image(input));
@ -46,11 +46,13 @@ public class ImageCtrlView extends CapteurView{
Platform.exit();
}
public void initialize() {
/*try {
changeImage();
} catch (IOException e) {
throw new RuntimeException(e);
}*/
public void initialize(CapteurAbstrait capteur) {
/*capteur.getTemp().addListener((observable, oldValue, newValue) -> {
try {
changeImage();
} catch (IOException e) {
throw new RuntimeException(e);
}
});*/
}
}

@ -22,13 +22,11 @@ public class ThermostatView extends CapteurView{
this.capteur=capteur;
this.spin.setValueFactory(new SpinnerValueFactory.DoubleSpinnerValueFactory(0d,100d));
this.spin.getValueFactory().valueProperty().bindBidirectional(this.capteur.getTemp());
SpinnerValueFactory valueFactory = new SpinnerValueFactory.DoubleSpinnerValueFactory(capteur.getTemp().getValue(),capteur.getTemp().getValue(), capteur.getTemp().getValue());
spin.setValueFactory(valueFactory);
myBtn.setOnAction(e -> Platform.exit());
}
public void initialize() {
}
}

@ -0,0 +1,74 @@
package view;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import model.CapteurAbstrait;
import model.CapteurVirtuel;
import model.TreeItemFactoryVisitor;
public class TreeViewCtrl extends FXMLView {
@FXML
private TreeView<CapteurAbstrait> treeView;
@FXML
private Spinner<Double> spinnerTemp;
private ArrayList<CapteurAbstrait> capteurAbstraits;
public TreeViewCtrl(ArrayList<CapteurAbstrait> capteurs, String url, String title) throws IOException {
super(url,title);
this.capteurAbstraits = capteurs;;
TreeItem<CapteurAbstrait> root = new TreeItem<>();
root.setExpanded(true);
treeView.setShowRoot(false);
TreeItemFactoryVisitor treeItemFactoryVisitor = new TreeItemFactoryVisitor();
treeView.setVisible(true);
for(CapteurAbstrait capteur: capteurAbstraits){
TreeItem<CapteurAbstrait> item = capteur.accept(treeItemFactoryVisitor);
root.getChildren().add(item);
}
this.initializeCapteur();
}
public void initializeCapteur(){
treeView.setCellFactory(param -> new TreeCell<CapteurAbstrait>(){
@Override
protected void updateItem(CapteurAbstrait capteur, boolean empty){
super.updateItem(capteur,empty);
if(empty){
textProperty().unbind();
setText("");
} else {
textProperty().bind(capteur.getNom());
}
}
});
spinnerTemp.setVisible(true);
CapteurAbstrait capteur = capteurAbstraits.get(0);
capteur.getTemp().addListener((observable, oldValue, newValue) -> {
System.out.println("Changement de température: " + oldValue + " est passé à "
+ newValue);
});
SpinnerValueFactory<Double> spinnerValueFactory = new SpinnerValueFactory.DoubleSpinnerValueFactory(-150,1000,capteur.getTemp().getValue(),0.5);
spinnerTemp.setValueFactory(spinnerValueFactory);
spinnerTemp.getValueFactory().valueProperty().bindBidirectional(capteur.getTemp());
capteur.genTemp();
capteur.setTemp(capteur.getTemp().getValue());
System.out.println("initialize spinner");
System.out.println(spinnerTemp.getValueFactory().getValue());
};
}
Loading…
Cancel
Save