0

I have creating this treetable treetable

Now I want to sum up th children values and show the result in the parent cell under the related column. For example for Function 7 in column 2 and row 2 I want to right 2.0, and for Function 11 column 4 row 4 right 1.0 (function 12 + function 13)

Here is the code which produces the treetable.

    root.setExpanded(true);
    Set<String> combinedKeys = new HashSet<>(dc.getCombiFunc().keySet());
    Set<String> funcAllKeys = new HashSet<>(dc.getSortedfuncAll().keySet());
    funcAllKeys.removeAll(dc.getCombiFunc().keySet());
    for (List<String> value : dc.getCombiFunc().values()) {
        funcAllKeys.removeAll(value);
    }
    for (String valueremained : funcAllKeys) {
        ArrayList<String> tempNameId = new ArrayList<>();
        tempNameId.add(dc.getSortedfuncAll().get(valueremained));
        // all elements which are not in combined functions (They are all
        // orphan)
        root.getChildren().add(new TreeItem<String>(tempNameId.get(0)));
    }

    // Getting Keys that have children//////
    Set<String> keyFromcombined = new HashSet<>();
    List<String> valueOfCombined = new ArrayList<String>();
    for (Entry<String, List<String>> ent : dc.getCombiFunc().entrySet()) {
        for (int i = 0; i < ent.getValue().size(); i++)
            valueOfCombined.add(ent.getValue().get(i));
    }
    List<String> rootKeyList = new ArrayList<>();
    for (String key : combinedKeys) {

        if (!valueOfCombined.contains((key))) {

            keyFromcombined.add(dc.getFuncAll().get(key));
            rootKeyList.add(key);
        }
    }
    String[] rootKeys = rootKeyList.toArray(new String[rootKeyList.size()]);

    // ////////////////treetable////////////////////////////

    treeTable.setRoot(root);
    Arrays.stream(rootKeys).forEach(
            rootKey -> root.getChildren().add(
                    createTreeItem(dc.getCombiFunc(), rootKey)));


    // ////////////////First column/////////////////////////

    TreeTableColumn<String, String> firstColumn = new TreeTableColumn<>("");
    treeTable.getColumns().add(firstColumn);// Tree column
    firstColumn.setPrefWidth(50);
    firstColumn
            .setCellValueFactory(new Callback<CellDataFeatures<String, String>, ObservableValue<String>>() {
                public ObservableValue<String> call(
                        CellDataFeatures<String, String> p) {
                    return new ReadOnlyStringWrapper(p.getValue()
                            .getValue());
                }
            });

    // //////////////////Rest Columns////////////////////////

    for (Entry<String, String> ent : dc.getSortedAssignedOrg().entrySet()) {

        TreeTableColumn<String, ArrayList<String>> col = new TreeTableColumn<>();
        Label label = new Label(ent.getValue());
        col.setGraphic(label);
        label.setTooltip(new Tooltip(label.getText()));// tooltip for column
                                                        // headers
        col.setPrefWidth(45);
        // cell Value Factory////////////////////////
        col.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<String, ArrayList<String>>, ObservableValue<ArrayList<String>>>() {
            @Override
            public ObservableValue<ArrayList<String>> call(
                    CellDataFeatures<String, ArrayList<String>> param) {
                TreeMap<String, List<String>> temp = (TreeMap<String, List<String>>) dc
                        .getFuncTypeOrg().clone();
                ArrayList<String> result = new ArrayList<>();
                for (int i = 0; i < dc.getFuncTypeOrg().size(); i++) {
                    List<String> list = temp.firstEntry().getValue();

                    String key = temp.firstEntry().getKey();

                    if (list.get(1).equals(param.getValue().getValue())
                            && !list.get(5).equals(label.getText())) {
                        result.add("white");
                    }
                    if (!root.isLeaf()) {

                        result.add("parent");
                    }
                    if (list.get(1).equals(param.getValue().getValue())
                            && list.get(5).equals(label.getText())) {
                        result.add(0, list.get(2));// weight

                        if (list.size() > 6) {
                            result.add(1, list.get(list.size() - 1));// color
                            result.add(2, list.get(6));// App component
                        }

                        else
                            // result.add("white");
                            result.add("noOrg");

                    } else
                        temp.remove(key);

                }

                return new ReadOnlyObjectWrapper<ArrayList<String>>(result);
            }
        }); // end cell Value Factory

        // //////////////cellfactory/////////////////////////
        col.setCellFactory(new Callback<TreeTableColumn<String, ArrayList<String>>, TreeTableCell<String, ArrayList<String>>>() {
            @Override
            public TreeTableCell<String, ArrayList<String>> call(
                    TreeTableColumn<String, ArrayList<String>> param) {
                return new TreeTableCell<String, ArrayList<String>>() {
                    public void updateItem(ArrayList<String> item,
                            boolean empty) {
                        super.updateItem(item, empty);

                        if (item == null || empty) {
                            setStyle("");
                            setText("");
                        } else if (item.contains("Green")) {
                            float weightInt = Float.parseFloat(item.get(0));
                            float res = weightInt * 1;
                            String resString = Float.toString(res);
                            this.setStyle("-fx-background-color:green");
                            setTooltip(new Tooltip(item.get(2)));
                            setText(resString);
                        } else if (item.contains("yellow")) {
                            this.setStyle("-fx-background-color:yellow");
                            setTooltip(new Tooltip(item.get(2)));
                            setText("0");
                        } else if (item.contains("white")) {
                            this.setStyle("-fx-background-color:linear-gradient(black, white); ");

                            // setText("DD");
                        } else if (item.contains("parent")) {
                            for (int i = 0; i < dc.getFuncTypeOrg().size(); i++) {

                            }

                            String text = param.getCellData(root).get(0);
                            // setText(text);
                        }
                    }
                };
            };

        });// end cell factory


        treeTable.getColumns().add(col);
    }//end for loop col

TreeMap temp clones dc.getFuncTypeOrg(). In this TreeMap I have value for each child (color and the number). then in cellfactory i multiply value in color ( green = 1 and yellow = 0). Outside the loop I thought to make a treemap containg each parent as key and all it's children as value. Then I can sum up children values together and make a treemap in which first key is parent and as value the required value(or just string ArrayList ). After that I can check the name of cell in cellFactory and if it is a parent just right the value in the cell. I have been told how i can get treeitem values, and i am now here :

//after col loop ends
TreeMap<String, List<TreeItem<String>>> mytreemap = new TreeMap<>(); 
    TreeMap<String, List<String>> parChild = new TreeMap<>();

    for(TreeItem node: root.getChildren()){
        if(!node.isLeaf())
            mytreemap.put(node.getValue().toString(), node.getChildren());
        }

        for(Entry<String, List<TreeItem<String>>> ent: mytreemap.entrySet()){

            for(TreeItem myItem : ent.getValue()){
                // how can i fill parChild with parent as key and all its children as value?
                System.out.println(ent.getKey()+"  "+myItem.getValue());
            }
        }

    treeTable.setPrefWidth(1200);
    treeTable.setPrefHeight(500);
    treeTable.setShowRoot(false);
    treeTable.setTableMenuButtonVisible(true);
    return treeTable; }

Here at setCellFactory

     else if (item.contains("parent")) {
                            for (int i = 0; i < dc.getFuncTypeOrg().size(); i++) {

                            }

i can get the roots. Is there a way to do a recursion (up to the number of children and subchildren for that root cell) and add their value together and setText the parent cell to that value?

Iman
  • 769
  • 1
  • 13
  • 51
  • so if I understand it right (its kinda hard to read your code) you just want to sum up some values of your function treeitems. Why do you even try to add all parents and childs to a map instead of just loop with recursion through all nodes and sum up the values? – NDY Mar 12 '15 at 11:34
  • yes. because i though in this way i check in cellfactory and if the cell is a parent i just go and get its value from the treemap. the TreeTable can get very big (400 x 200) and i am not sure if recursive is a good idea. Besides I do not know how to get the number of children in cellfactory and sum them up together. When i get to parent cell i should check the cell under it which is it's child, but at this time this child cell is still empty! I am kind of lost i guess – Iman Mar 12 '15 at 11:48
  • But if i make the treemap i can add the children before cellfactory for each parent together. I have a TreeMap like this {AUF_1425938599751_127,=[ Function 1, 1.0, Green], AUF_1425938607217_128=[, Function 2, 1.0,Green], and other TreeMap which gives relation between parent and child. For example function 3 = [function 2, function 1] – Iman Mar 12 '15 at 11:52
  • then i can add value of function 2 to value of function 1 (1.0 + 1.0) and make a function 3 = 2.0 treemap. When I check in cellfactroy that i have a parent i come and get the value from here – Iman Mar 12 '15 at 11:54
  • How can check in cellfactory that the current cell is parent? At the time i am doing by comparison of datastructure out of the treetable Like : if (list.get(1).equals(param.getValue().getValue()) && !list.get(5).equals(label.getText())) { result.add("white"); } – Iman Mar 12 '15 at 12:04

1 Answers1

1

You can use onEditCommit method to add all childern values and show them in parent cell. For example

column1.setOnEditCommit((evt) -> {
            //finalsing value of the cell
            evt.getRowValue().getValue().setCellValue((evt.getNewValue()));
            //Returns all the sibblings of the current cell
            ObservableList<TreeItem> children = evt.getRowValue().getParent().getChildren();
            int parentValue = 0;
            for (TreeItem<> child : children) {
                parentValue = parentValue + Integer.valueOf(child.getValue().getCellValue());
            }
            evt.getRowValue().getParent().getValue().setCellValue(parentValue);
        });
Harshita Sethi
  • 2,035
  • 3
  • 24
  • 46