ajout du tableau (finalement) et du button on/off

master
Nicolas FRANCO 2 years ago
parent 095602d809
commit 7ab49665f7

@ -6,6 +6,10 @@
<?import javafx.scene.control.TextField?> <?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ComboBox?> <?import javafx.scene.control.ComboBox?>
<?import javafx.collections.FXCollections?>
<?import java.lang.String?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.ToggleButton?>
<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>
@ -14,7 +18,17 @@
<Label fx:id="nameLabel" layoutX="400.0" layoutY="57.0" text="Name: " /> <Label fx:id="nameLabel" layoutX="400.0" layoutY="57.0" text="Name: " />
<Label fx:id="temperatureLabel" layoutX="400.0" layoutY="87.0" text="Temperature: " /> <Label fx:id="temperatureLabel" layoutX="400.0" layoutY="87.0" text="Temperature: " />
<TextField fx:id="nameTextField" layoutX="400.0" layoutY="107.0" /> <TextField fx:id="nameTextField" layoutX="400.0" layoutY="107.0" />
<ComboBox fx:id="strategyOptions" layoutX="400.0" layoutY="147.0"/> <ToggleButton fx:id="arretGenTemp" text="On/Off" visible="false" layoutX="400.0" layoutY="147.0"/>
<ComboBox fx:id="strategyOptions" layoutX="400.0" layoutY="177.0">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="CPU"></String>
<String fx:value="Random"></String>
</FXCollections>
</items>
</ComboBox>
<TableView fx:id="sousCapteurs" visible="false" prefHeight="300" prefWidth="300" layoutY="200.0" layoutX="400.0"
/>
</children> </children>
</AnchorPane> </AnchorPane>

@ -49,13 +49,13 @@ public class Launch extends Application {
while (true) { while (true) {
Platform.runLater(() -> { Platform.runLater(() -> {
capteurs.forEach(c -> { capteurs.forEach(c -> {
if(c instanceof UnitCapteur uc){ if(c instanceof UnitCapteur uc && uc.isGenTemp()){
uc.genTemp(); uc.genTemp();
} }
}); });
capteurs.forEach(c -> { capteurs.forEach(c -> {
if(c instanceof UnitCapteur uc){ if(c instanceof CapteurVirtuel vc){
uc.genTemp(); vc.genTemp();
}}); }});
}); });
try { try {

@ -2,11 +2,15 @@ package model;
import javafx.beans.property.*; import javafx.beans.property.*;
import java.util.HashMap;
import java.util.Map;
public abstract class CapteurAbstrait { public abstract class CapteurAbstrait {
private Property<String> id; private Property<String> id;
private StringProperty nom; private StringProperty nom;
private StringProperty displayNom; private StringProperty displayNom;
private ObjectProperty<Double> temp = new SimpleObjectProperty<Double>(); private ObjectProperty<Double> temp = new SimpleObjectProperty<Double>();
private Boolean isGenTemp;
CapteurAbstrait(Property<String> id, StringProperty nom){ CapteurAbstrait(Property<String> id, StringProperty nom){
this.id = id; this.id = id;
@ -14,6 +18,7 @@ public abstract class CapteurAbstrait {
this.displayNom = new SimpleStringProperty(); this.displayNom = new SimpleStringProperty();
this.displayNom.bind(this.nom); this.displayNom.bind(this.nom);
this.temp.setValue(0.0); this.temp.setValue(0.0);
this.isGenTemp = true;
} }
public abstract void genTemp(); public abstract void genTemp();
@ -51,4 +56,10 @@ public abstract class CapteurAbstrait {
public abstract <T> T accept (Visitor<T> v); public abstract <T> T accept (Visitor<T> v);
public Map<CapteurAbstrait, Integer> getCapteurs(){
return new HashMap<>();
};
public Boolean isGenTemp() {return this.isGenTemp;};
public void setIsGenTemp(boolean b) {this.isGenTemp = b;};
} }

@ -26,4 +26,8 @@ public class UnitCapteur extends CapteurAbstrait{
public StrategyCaptor getStrategie() { public StrategyCaptor getStrategie() {
return strategie; return strategie;
} }
public void setStrategy(StrategyCaptor strategie) {
this.strategie = strategie;
}
} }

@ -7,13 +7,21 @@ import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import javafx.beans.binding.Bindings; import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
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.IntegerStringConverter;
import javafx.util.converter.NumberStringConverter; import javafx.util.converter.NumberStringConverter;
import model.*; import model.*;
@ -32,8 +40,11 @@ public class TreeViewCtrl extends FXMLView {
@FXML @FXML
private Label temperatureLabel; private Label temperatureLabel;
@FXML private TableView sousCapteurs;
@FXML @FXML
private ComboBox strategyOptions; private ComboBox strategyOptions;
@FXML
private ToggleButton arretGenTemp;
private ArrayList<CapteurAbstrait> capteurAbstraits; private ArrayList<CapteurAbstrait> capteurAbstraits;
@ -66,8 +77,6 @@ public class TreeViewCtrl extends FXMLView {
} }
public void initializeCapteur() { public void initializeCapteur() {
strategyOptions.getItems().addAll("CPU", "Random");
treeView.setCellFactory(param -> new TreeCell<CapteurAbstrait>() { treeView.setCellFactory(param -> new TreeCell<CapteurAbstrait>() {
@Override @Override
protected void updateItem(CapteurAbstrait capteur, boolean empty) { protected void updateItem(CapteurAbstrait capteur, boolean empty) {
@ -93,7 +102,12 @@ public class TreeViewCtrl extends FXMLView {
} }
} }
}); });
}
public void initialize(){
strategyOptions.setVisible(false); strategyOptions.setVisible(false);
sousCapteurs.setVisible(false);
arretGenTemp.setVisible(false);
treeView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { treeView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (oldValue != null) { if (oldValue != null) {
CapteurAbstrait capteur = oldValue.getValue(); CapteurAbstrait capteur = oldValue.getValue();
@ -103,6 +117,8 @@ public class TreeViewCtrl extends FXMLView {
if(capteur instanceof UnitCapteur){ if(capteur instanceof UnitCapteur){
strategyOptions.setVisible(false); strategyOptions.setVisible(false);
} }
sousCapteurs.getItems().clear();
sousCapteurs.getColumns().clear();
} }
if (newValue != null) { if (newValue != null) {
CapteurAbstrait capteur = newValue.getValue(); CapteurAbstrait capteur = newValue.getValue();
@ -124,6 +140,90 @@ public class TreeViewCtrl extends FXMLView {
}); });
if(capteur instanceof UnitCapteur){ if(capteur instanceof UnitCapteur){
strategyOptions.setVisible(true); strategyOptions.setVisible(true);
sousCapteurs.setVisible(false);
arretGenTemp.setVisible(true);
strategyOptions.setOnAction(event ->{
if(strategyOptions.getValue().equals("CPU")){
((UnitCapteur) newValue.getValue()).setStrategy(new StrategyCPU());
}
else if (strategyOptions.getValue().equals("Random")){
((UnitCapteur) newValue.getValue()).setStrategy(new StrategyRandom());
}
if(!newValue.getValue().isGenTemp()){
arretGenTemp.setSelected(true);
};
initializeCapteur();
});
arretGenTemp.setOnAction(event -> {
newValue.getValue().setIsGenTemp(!arretGenTemp.isSelected());
});
} else if(capteur instanceof CapteurVirtuel){
strategyOptions.setVisible(false);
sousCapteurs.setVisible(true);
arretGenTemp.setVisible(false);
ObservableList<CapteurAbstrait> capteurs = FXCollections.observableArrayList(newValue.getValue().getCapteurs().keySet());
sousCapteurs.setItems(capteurs);
TableColumn<CapteurAbstrait, Object> iconColumn = new TableColumn<>("Icone");
iconColumn.setCellFactory(column -> {
return new TableCell<CapteurAbstrait, Object>() {
@Override
protected void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
if (getTableRow() != null){
CapteurAbstrait capteur = (CapteurAbstrait) getTableRow().getItem();
if (capteur instanceof CapteurVirtuel) {
ImageView capVirt = new ImageView("images/virtual.png");
capVirt.setFitWidth(25);
capVirt.setFitHeight(25);
setGraphic(capVirt);
} else if (capteur instanceof UnitCapteur) {
UnitCapteur unitCaptor = (UnitCapteur) capteur;
if (unitCaptor.getStrategie() instanceof StrategyCPU) {
ImageView capCpu = new ImageView("images/cpu.png");
capCpu.setFitWidth(25);
capCpu.setFitHeight(25);
setGraphic(capCpu);
} else if (unitCaptor.getStrategie() instanceof StrategyRandom) {
ImageView capRand = new ImageView("images/random.png");
capRand.setFitWidth(25);
capRand.setFitHeight(25);
setGraphic(capRand);
}
}
}
}
}
};
});
sousCapteurs.getColumns().add(iconColumn);
TableColumn<CapteurAbstrait, Integer> idColumn = new TableColumn<>("id");
idColumn.setCellValueFactory(cellData -> new SimpleIntegerProperty(Integer.parseInt(cellData.getValue().getId().getValue())).asObject());
sousCapteurs.getColumns().add(idColumn);
TableColumn<CapteurAbstrait, String> NameColumn = new TableColumn<>("Nom");
NameColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getNom().getValue()));
sousCapteurs.getColumns().add(NameColumn);
NameColumn.setEditable(true);
TableColumn<CapteurAbstrait, Integer> poidsColumn = new TableColumn<>("Poids");
poidsColumn.setCellValueFactory(cellData -> {
CapteurAbstrait captor = cellData.getValue();
if (captor != null && newValue.getValue().getCapteurs().get(captor) != null) {
return new SimpleIntegerProperty(newValue.getValue().getCapteurs().get(captor)).asObject();
} else {
return new SimpleObjectProperty<>(null);
}
});
sousCapteurs.getColumns().add(poidsColumn);
poidsColumn.setEditable(true);
poidsColumn.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
poidsColumn.setOnEditCommit(event -> {
CapteurAbstrait capteur2 = event.getRowValue();
newValue.getValue().getCapteurs().put(capteur2, event.getNewValue());
});
} }
} }
}); });

Loading…
Cancel
Save