mercredi 16 mai 2018

How to refresh a JavaFX ListView using a JFXSlider?

I want to refresh two JFXListView. The first one is a JFXListView of JFXButtons. The idea is to use a JFXSlider to get a refresh rate (time between each refresh). This is the first one :

@FXML
private JFXListView li_se;

@FXML
public void getServices(ActionEvent e) {
    try {
        li_se.getItems().clear();
        for (Object service : R.ListeService()) {
            JFXButton b = new JFXButton("" + service);
            b.setStyle("-fx-rippler-fill:black;-fx-text-fill:black;-fx-font-size:16px;-fx-background-color:orange;-fx-background-radius:5px;");
            b.setOnAction((ActionEvent event) -> {
                try {
                    VBox vBox = new VBox(10);
                    JFXListView li_par = new JFXListView();
                    JFXTextField[] fields = new JFXTextField[22];
                    TextFieldEntier[] fEntiers = new TextFieldEntier[22];
                    TextFieldReel[] fRéels = new TextFieldReel[22];
                    ChoiceBox<String> choiceBox = new ChoiceBox<>();
                    choiceBox.getStylesheets().add(getClass().getResource("/choise-box.css").toExternalForm());
                    choiceBox.getItems().addAll("true", "false");
                    ArrayList<Object> tab = new ArrayList<>();
                    JFXButton folder = new JFXButton("Ouvrir le répertoire");
                    folder.setDisable(true);
                    ObservableList<String> ListParam = FXCollections.observableArrayList(R.paramService(b.getText()));
                    JFXDialogLayout content = new JFXDialogLayout();
                    content.setHeading(new Text(b.getText()));
                    content.setBody(vBox);
                    JFXDialog dialog = new JFXDialog(st_dl, content, JFXDialog.DialogTransition.CENTER);
                    k = 0;
                    ListParam.stream().map((st) -> {
                        if (st.equals("double")) {
                            fRéels[k] = new TextFieldReel();
                            fRéels[k].setPadding(new Insets(11));
                            Label lb = new Label("Argument ( Réel ) : ");
                            lb.setPadding(new Insets(7));
                            lb.setStyle("-fx-min-Width:178;-fx-text-fill:white;-fx-background-color:orange;-fx-background-radius:5px;");
                            HBox hb = new HBox(10);
                            hb.getChildren().add(lb);
                            hb.getChildren().add(fRéels[k]);
                            li_par.getItems().add(hb);
                        }
                        return st;
                    }).map((st) -> {
                        if (st.equals("java.lang.String")) {
                            fields[k] = new JFXTextField();
                            fields[k].setMinHeight(35);
                            fields[k].setMinWidth(157);
                            Label lb = new Label("Argument ( Texte ) : ");
                            lb.setMinWidth(178);
                            lb.setPadding(new Insets(7));
                            lb.setStyle("-fx-text-fill:white;-fx-background-color:orange;-fx-background-radius:5px;");
                            HBox hb = new HBox(10);
                            hb.getChildren().add(lb);
                            hb.getChildren().add(fRéels[k]);
                            li_par.getItems().add(hb);
                        }
                        return st;
                    }).map((st) -> {
                        if (st.equals("int")) {
                            fEntiers[k] = new TextFieldEntier();
                            fEntiers[k].setPadding(new Insets(11));
                            Label lb = new Label("Argument ( Entier ) : ");
                            lb.setPadding(new Insets(7));
                            lb.setStyle("-fx-min-Width:178;-fx-text-fill:white;-fx-background-color:orange;-fx-background-radius:5px;");
                            HBox hb = new HBox(10);
                            hb.getChildren().add(lb);
                            hb.getChildren().add(fEntiers[k]);
                            li_par.getItems().add(hb);
                        }
                        return st;
                    }).map((st) -> {
                        if (st.equals("boolean")) {
                            Label lb = new Label("Argument ( " + st + " ) : ");
                            lb.setMinWidth(178);
                            lb.setPadding(new Insets(7));
                            lb.setStyle("-fx-text-fill:white;-fx-background-color:orange;-fx-background-radius:5px;");
                            HBox hb = new HBox(10);
                            hb.getChildren().add(lb);
                            hb.getChildren().add(choiceBox);
                            li_par.getItems().add(hb);
                        }
                        return st;
                    }).map((st) -> {
                        if (st.equals("java.io.File")) {
                            fields[k] = new JFXTextField();
                            fields[k].setMinHeight(35);
                            fields[k].setMinWidth(157);
                            Label lb = new Label("Argument ( Fichier ) : ");
                            lb.setMinWidth(178);
                            lb.setPadding(new Insets(7));
                            lb.setStyle("-fx-text-fill:white;-fx-background-color:orange;-fx-background-radius:5px;");
                            HBox hb = new HBox(10);
                            hb.getChildren().add(lb);
                            hb.getChildren().add(fields[k]);
                            li_par.getItems().add(hb);
                            int x = k;
                            fields[k].addEventHandler(MouseEvent.MOUSE_CLICKED, (javafx.scene.input.MouseEvent e1) -> {
                                FileChooser fc = new FileChooser();
                                selectedFile = fc.showOpenDialog(null);
                                if (selectedFile != null) {
                                    fields[x].setText(selectedFile.getAbsolutePath());
                                }
                            });
                        }
                        return st;
                    }).forEachOrdered((_item) -> {
                        k++;
                    });
                    JFXButton exe = new JFXButton("Exécuter");
                    WeatherIconView weatherIcon = new WeatherIconView(WeatherIcon.CLOUD);
                    weatherIcon.setSize("30");
                    weatherIcon.setFill(Color.DODGERBLUE);
                    exe.setGraphic(weatherIcon);
                    exe.setStyle("-fx-text-fill:DODGERBLUE;-fx-background-color:white;-fx-background-radius:5px;");
                    exe.setPadding(new Insets(10));
                    JFXTextField res = new JFXTextField();
                    res.setMinWidth(150);
                    Label re = new Label("Résultat");
                    HBox hb = new HBox(10);
                    hb.getChildren().add(re);
                    hb.getChildren().add(res);
                    hb.setAlignment(Pos.CENTER_RIGHT);
                    hb.getChildren().add(folder);
                    li_par.setMaxHeight(180);
                    li_par.setMinWidth(384);
                    vBox.getChildren().add(li_par);
                    vBox.setAlignment(Pos.CENTER_RIGHT);
                    vBox.getChildren().add(exe);
                    vBox.getChildren().add(hb);
                    exe.setOnAction((ActionEvent e2) -> {
                        tab.clear();
                        try {
                            int k1 = 0;
                            for (String st : ListParam) {
                                if (st.equals("int")) {
                                    tab.add(Integer.valueOf(fEntiers[k1].getText()));
                                }
                                if (st.equals("double")) {
                                    tab.add(Double.valueOf(fRéels[k1].getText()));
                                }
                                if (st.equals("boolean")) {
                                    tab.add(Boolean.valueOf(choiceBox.getValue()));
                                }
                                if (st.equals("java.lang.String")) {
                                    tab.add(fields[k1].getText());
                                }
                                if (st.equals("java.io.File")) {
                                    Fichier f = new Fichier(selectedFile.getName());
                                    tab.add(f);
                                    p1 = (int) ((Math.random() * (9000 - 5000)) + 5000);
                                    R.uploadToCloud(p1, selectedFile.getName());
                                    Socket s = new Socket(ip.IPserveur(), p1);
                                    ObjectOutputStream out;
                                    FileInputStream inf = new FileInputStream(new File(selectedFile.getAbsolutePath()));
                                    out = new ObjectOutputStream(s.getOutputStream());
                                    byte buf[] = new byte[1024];
                                    int n;
                                    while ((n = inf.read(buf)) != -1) {
                                        out.write(buf, 0, n);
                                    }
                                    out.close();
                                }
                                k1++;
                            }
                            if (R.retourService(b.getText()).equals("int") || R.retourService(b.getText()).equals("double") || R.retourService(b.getText()).equals("boolean") || R.retourService(b.getText()).equals("class java.lang.String")) {
                                R.envoieDemande(b.getText(), "", 0, tab.toArray());
                                res.setText("" + R.getResult());
                            }
                            if (R.retourService(b.getText()).equals("class java.io.File")) {
                                p1 = (int) ((Math.random() * (9000 - 5000)) + 5000);
                                LocateRegistry.createRegistry(p1);
                                Utilisateur_MethodsPAIRIMPL U = new Utilisateur_MethodsPAIRIMPL();
                                Naming.bind("rmi://" + InetAddress.getLocalHost().getHostAddress() + ":" + p1 + "/" + R.getEmail(), U);
                                R.envoieDemande(b.getText(), InetAddress.getLocalHost().getHostAddress(), p1, tab.toArray());
                                res.setText(urep + R.getResfile());
                                folder.setDisable(false);
                            }
                        } catch (RemoteException | InvocationTargetException ex) {
                            Alert alert = new Alert(Alert.AlertType.ERROR);
                            alert.setTitle("Erreur");
                            alert.setHeaderText("Erreur");
                            alert.setContentText("Veuillez choisir une option");
                            ButtonType btn_annuler = new ButtonType("Annuler");
                            ButtonType btn_réessayer = new ButtonType("Réessayer");
                            alert.setGraphic(new ImageView("icon/erreur.png"));
                            alert.getButtonTypes().setAll(btn_réessayer, btn_annuler);
                            StringWriter sw = new StringWriter();
                            PrintWriter pw = new PrintWriter(sw);
                            ex.printStackTrace(pw);
                            TextArea area = new TextArea(sw.toString());
                            alert.getDialogPane().setExpandableContent(area);
                            Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
                            stage.getIcons().add(new Image("icon/erreur.png"));
                            Optional<ButtonType> res1 = alert.showAndWait();
                            if (res1.get() == btn_réessayer) {
                                alert.close();
                            }
                            if (res1.get() == btn_annuler) {
                                dialog.close();
                                alert.close();
                            }
                            //Logger.getLogger(UtilisateurController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException | AlreadyBoundException ex) {
                            Logger.getLogger(UtilisateurController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    });
                    JFXButton button = new JFXButton("Terminer " + b.getText());
                    button.setOnAction((ActionEvent evt) -> {
                        dialog.close();
                    });
                    content.setActions(button);
                    dialog.setOverlayClose(false);
                    dialog.show();
                    dialog.setOnDialogOpened((evt) -> {
                        bp.set(true);
                        pn_li.setDisable(true);
                        pn_us.setDisable(true);
                    });
                    dialog.setOnDialogClosed((evt) -> {
                        pn_li.setDisable(false);
                        pn_us.setDisable(false);
                        bp.set(false);
                    });
                    folder.setOnAction((ActionEvent evt) -> {
                        try {
                            Desktop.getDesktop().open(new File(urep));
                        } catch (IOException ex) {
                            Logger.getLogger(UtilisateurController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    });
                } catch (RemoteException ex) {
                    Logger.getLogger(UtilisateurController.class.getName()).log(Level.SEVERE, null, ex);
                }
            });
            li_se.getItems().add(b);
        }
        if (!li_se.isExpanded()) {
            li_se.setExpanded(true);
            li_se.depthProperty().set(1);
        } else {
            /*li_se.setExpanded(false);
            li_se.depthProperty().set(0);*/
        }
        lb_re.setDisable(false);
        refresh.setDisable(false);
        //bp.bind(property.isEqualTo(1));
    } catch (RemoteException ex) {
        Logger.getLogger(UtilisateurController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

The second JFXListView is a list of JFXCheckBoxes. this list is refreshed by a Button click and when refreshed i want to keep the previous CheckedProperties of the JFXCheckBoxes. This is the code :

@FXML
private void Services(ActionEvent e) {
    try {
        p1 = (int) ((Math.random() * (9000 - 5000)) + 5000);
        LocateRegistry.createRegistry(p1);
        Participant_MethodsPAIRIMPL P = new Participant_MethodsPAIRIMPL();
        Naming.bind("rmi://" + localip + ":" + p1 + "/" + R.getEmail(), P);
        if (k == 0) {
            for (int i = 0; i < R.OffrirService().size(); i++) {
                JFXCheckBox s = new JFXCheckBox(R.OffrirService().get(i));
                R.serviceDepartager(R.getEmail(), localip, s.getText());
                File fi = new File(prep + s.getText() + ".class");
                fi.delete();
                s.setStyle("-fx-rippler-fill:white;-fx-text-fill:white;-fx-font-size:16px;-fx-background-color:#1A33D5;-fx-background-radius:5px;-jfx-checked-color:orange;-jfx-unchecked-color:dodgerblue;");
                s.selectedProperty().addListener((ObservableValue<? extends Boolean> obs, Boolean wasOn, Boolean isNowOn) -> {
                    if (wasOn == false) {
                        try {
                            p2 = (int) ((Math.random() * (9000 - 5000)) + 5000);
                            R.servicePartager(R.getEmail(), localip, p1, s.getText());
                            sv = new ServerSocket(p2);
                            Thread t = new Thread(new serverSocket(sv, s.getText() + ".class", prep));
                            t.start();
                            R.EnvoieService(s.getText(), localip, p2);
                            sep.add(s.getText());
                            d++;
                            if (d == R.OffrirService().size()) {
                                pr_al.setSelected(true);
                            }
                        } catch (RemoteException ex) {
                            Logger.getLogger(ParticipantController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException ex) {
                            Logger.getLogger(ParticipantController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        sp_pr.toFront();
                    } else {
                        try {
                            R.serviceDepartager(R.getEmail(), localip, s.getText());
                            fi.delete();
                            sep.remove(s.getText());
                            d--;
                            b = false;
                            pr_al.setSelected(false);
                            b = true;
                            if (d == 0) {
                                sp_pr.toBack();
                            }
                        } catch (RemoteException ex) {
                            Logger.getLogger(ParticipantController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    //System.out.println(s.getText() + " changed on state from " + wasOn + " to " + isNowOn);
                });
                pr_al.setDisable(false);
                li_se.getItems().add(s);
                se.add(s.getText());
                pr_al.selectedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
                    if (pr_al.isSelected() == true) {
                        pr_al.setText("Annuler tout");
                        try {
                            for (String OffrirService : R.OffrirService()) {
                                s.setSelected(true);
                                d = R.OffrirService().size();
                            }
                        } catch (RemoteException ex) {
                            Logger.getLogger(ParticipantController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        sp_pr.toFront();
                    } else {
                        pr_al.setText("Héberger tout");
                        try {
                            for (String OffrirService : R.OffrirService()) {
                                if (b == true) {
                                    s.setSelected(false);
                                    sp_pr.toBack();
                                    d = 0;
                                }
                            }
                        } catch (RemoteException ex) {
                            Logger.getLogger(ParticipantController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                });
            }
        }
        if (k == 1) {
            List<String> sev = R.OffrirService().stream().filter(elem -> !se.contains(elem)).collect(Collectors.toList());
            for (int i = 0; i < sev.size(); i++) {
                JFXCheckBox s = new JFXCheckBox(sev.get(i));
                R.serviceDepartager(R.getEmail(), localip, s.getText());
                File fi = new File(prep + s.getText() + ".class");
                fi.delete();
                s.setStyle("-fx-rippler-fill:white;-fx-text-fill:white;-fx-font-size:20px;-fx-background-color:#1A33D5;-fx-background-radius:5px;-jfx-checked-color:orange;-jfx-unchecked-color:dodgerblue;");
                s.getStylesheets().add(getClass().getResource("/CheckBox.css").toExternalForm());
                s.selectedProperty().addListener((ObservableValue<? extends Boolean> obs, Boolean wasOn, Boolean isNowOn) -> {
                    if (wasOn == false) {
                        try {
                            p2 = (int) ((Math.random() * (9000 - 5000)) + 5000);
                            R.servicePartager(R.getEmail(), localip, p1, s.getText());
                            sv = new ServerSocket(p2);
                            Thread t = new Thread(new serverSocket(sv, s.getText() + ".class", prep));
                            t.start();
                            R.EnvoieService(s.getText(), localip, p2);
                            sp_pr.toFront();
                            sep.add(s.getText());
                            d++;
                            if (d == R.OffrirService().size()) {
                                pr_al.setSelected(true);
                            }
                        } catch (RemoteException ex) {
                            Logger.getLogger(ParticipantController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException ex) {
                            Logger.getLogger(ParticipantController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    } else {
                        try {
                            R.serviceDepartager(R.getEmail(), localip, s.getText());
                            fi.delete();
                            sep.remove(s.getText());
                            d--;
                            b = false;
                            pr_al.setSelected(false);
                            b = true;
                            if (d == 0) {
                                sp_pr.toBack();
                            }
                        } catch (RemoteException ex) {
                            Logger.getLogger(ParticipantController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    //System.out.println(s.getText() + " changed on state from " + wasOn + " to " + isNowOn);
                });
                li_se.getItems().add(s);
                se.add(s.getText());
            }
            ArrayList<String> os = R.OffrirService();
            List<String> seup = se.stream().filter(elem -> !os.contains(elem)).collect(Collectors.toList());
            for (int x = 0; x < seup.size(); x++) {
                System.out.println(li_se.getItems().indexOf(x));
                //li_se.getItems().remove(seup.get(x));
            }
            for (int j = 0; j < sep.size(); j++) {
                File p = new File(prep + sep.get(j) + ".class");
                p.delete();
                p2 = (int) ((Math.random() * (9000 - 5000)) + 5000);
                R.servicePartager(R.getEmail(), localip, p1, sep.get(j));
                sv = new ServerSocket(p2);
                Thread t = new Thread(new serverSocket(sv, sep.get(j) + ".class", prep));
                t.start();
                R.EnvoieService(sep.get(j), localip, p2);
            }
        }
        k = 1;
    } catch (RemoteException | AlreadyBoundException | MalformedURLException ex) {
        Logger.getLogger(ParticipantController.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ParticipantController.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (!li_se.isExpanded()) {
        li_se.setExpanded(true);
        li_se.depthProperty().set(1);
    } else {
        /*li_se.setExpanded(false);
            li_se.depthProperty().set(0);*/
    }
    Timeline refreshWonder = new Timeline(new KeyFrame(Duration.millis(800), (ActionEvent event) -> {
        try {
            if (R.log() == false) {
                pr_al.setSelected(false);
                pr_al.setDisable(true);
                sp_pr.toBack();
                li_se.getItems().clear();
                k = 0;
            }
        } catch (RemoteException ex) {
            Logger.getLogger(ParticipantController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }));
    refreshWonder.setCycleCount(Timeline.INDEFINITE);
    refreshWonder.play();
}

as you can see i tried to do a refresh when k == 1 but it's not fully working. Please how can i do a refresh for this two JFXListView you can modify the code to adapt it ? If there is a proper way to code these two methods please let me know.




Aucun commentaire:

Enregistrer un commentaire