mardi 14 novembre 2017

Enabling the indeterminate state while using CheckBoxTreeTableCell in JavaFX

I am building a treetable where the first two columns are Strings and the next column are checkboxes. All the entries have root-children relationships and hence the use of the TreeTableView. The checkboxes need to cycle through the three states (true,false and indeterminate). While it's easy to render a 2-state checkbox using CheckBoxTreeTableCell , I haven't been able to set it to the 3-state configuration. Is it possible at all? If not what could be an alternative? Attaching the relevant code.

 package fend.session.node.qcTable;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventType;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.CheckBoxTreeItem;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableCell;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.control.cell.CheckBoxTreeTableCell;
import javafx.scene.control.cell.TreeItemPropertyValueFactory;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 *
 * @author 
 */



public class QcTableController extends Stage {
    QcTableModel model;
    QcTableNode node;
    @FXML
    private TreeTableView<QcTableSequences> treetable;

    void setModel(QcTableModel lsm) {
        this.model=lsm;
        TreeTableColumn<QcTableSequences,Long> sequenceNumber=new TreeTableColumn<>("Sequence");
        sequenceNumber.setCellValueFactory(new TreeItemPropertyValueFactory<>("sequenceNumber"));
        TreeTableColumn<QcTableSequences,String> subsurface=new TreeTableColumn<>("Subsurface");
        subsurface.setCellValueFactory(new TreeItemPropertyValueFactory<>("subsurface"));
        List<QcTypeModel> qctypes=model.getQctypes();                     

        List<TreeItem<QcTableSequences>> treeSeq=new ArrayList<>();
        List<QcTableSequences> qcTableSequences=model.getQcTableSequences();
        for(QcTableSequences qcTableSequence: qcTableSequences){

            //CheckBoxTreeItem<QcTableSequences> seqroot=new CheckBoxTreeItem<>(qcTableSequence);
            System.out.println("fend.session.node.qcTable.QcTableController.setModel(): creating treeItem seq: "+qcTableSequence.getSequence().getSequenceNumber());
            TreeItem<QcTableSequences> seqroot=new TreeItem<>(qcTableSequence);
          //  qcTableSequence.setQcfields(qctypes);
            List<QcTableSubsurfaces> qcsubs=qcTableSequence.getQcSubs();
            for(QcTableSubsurfaces qcsub:qcsubs){
               // CheckBoxTreeItem<QcTableSequences> subItem=new CheckBoxTreeItem<>(qcsub);
               System.out.println("fend.session.node.qcTable.QcTableController.setModel(): creating subtreeItem seq: "+qcsub.getSub().getSequenceNumber() +" sub: "+qcsub.getSub().getSubsurface());
                TreeItem<QcTableSequences> subItem=new TreeItem<>(qcsub);
                seqroot.getChildren().add(subItem);

            }
            treeSeq.add(seqroot);
        }


         List<TreeTableColumn<QcTableSequences,Boolean>> cols=new ArrayList<>();
        //List<TreeTableColumn<QcTableSequences,QcTypeModel>> cols=new ArrayList<>();





        for(int i=0;i<qctypes.size();i++){
            QcTypeModel qctype=qctypes.get(i);
            //TreeTableColumn<QcTableSequences,Boolean> qctypeCol=new TreeTableColumn<>(qctype.getName());
           //TreeTableColumn<QcTableSequences,QcTypeModel> qcval=new TreeTableColumn<>(qctype.getName());
            final int iii=i;

            System.out.println("fend.session.node.qcTable.QcTableController.setModel(): Column name : "+qctype.getName());
            TreeTableColumn<QcTableSequences,Boolean> qctypeCol=new TreeTableColumn<>();//  

            qctypeCol.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<QcTableSequences, Boolean>, ObservableValue<Boolean>>() {
                @Override
                public ObservableValue<Boolean> call(TreeTableColumn.CellDataFeatures<QcTableSequences, Boolean> param) {
                     SimpleBooleanProperty checkUncheck=new SimpleBooleanProperty();
                     SimpleBooleanProperty fail=new SimpleBooleanProperty();
                     QcTableSequences qseq=param.getValue().getValue();

                     if(qseq instanceof QcTableSubsurfaces){
                     qctypeCol.setText(((QcTableSubsurfaces)qseq).getQctypes().get(iii).getName());
                     checkUncheck= (SimpleBooleanProperty) qseq.getQctypes().get(iii).getCheckUncheckProperty();
                     fail=(SimpleBooleanProperty) qseq.getQctypes().get(iii).getFailProperty();
                     }


                     checkUncheck.addListener(new ChangeListener<Boolean>(){
                         @Override
                         public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                             if(qseq instanceof QcTableSubsurfaces){
                                ((QcTableSubsurfaces)qseq).getQctypes().get(iii).setCheckUncheckProperty(newValue);
                               }

                         }

                     });
                     return checkUncheck;
                }
            });

// the following line gives a 2-state checkbox. Is a 3-state possible (indeterminate enabled)?
     qctypeCol.setCellFactory(CheckBoxTreeTableCell.forTreeTableColumn(qctypeCol));         


            cols.add(qctypeCol);

        }
        treetable.getColumns().add(sequenceNumber);
        treetable.getColumns().add(subsurface);
        treetable.getColumns().addAll(cols);


        CheckBoxTreeItem<QcTableSequences> root=new CheckBoxTreeItem<>();
        root.getChildren().addAll(treeSeq);
        root.setIndependent(true);
        treetable.setRoot(root);
        treetable.setShowRoot(false);
        treetable.setEditable(true);

    }

    void setView(QcTableNode aThis) {
        node=aThis;
        this.setTitle("QC Table");
        this.setScene(new Scene(node));
        this.showAndWait();
    }




}




Aucun commentaire:

Enregistrer un commentaire