Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Hierarchical Container of different classes

Bart Van Remortele
9 years ago Apr 18, 2012 5:48pm
Petrus Viljoen
9 years ago Apr 19, 2012 8:42am

You dont need a special Container for this. But your itemID's must have proper comparator / equals methods.

com.vaadin.ui.Tree tree = new com.vaadin.ui.Tree();
        tree.setItemCaptionMode(com.vaadin.ui.Tree.ITEM_CAPTION_MODE_EXPLICIT);
        final java.util.Map<Integer, Product> prodMap = new java.util.HashMap<Integer, Product>(100);
        for (Product prod : api().select(Product.class, "link to (Sale where clientNumber = %d) order by description", client.getObjKey())) {
            prodMap.put(prod.getObjKey(), prod);
            tree.addItem(prod);
            tree.setItemCaption(prod, prod.getDescription());
        }
        for (Sale sale : api().select(Sale.class, "where clientNumber = %d order by productNumber , description", client.getObjKey())) {
             tree.addItem(sale);
             tree.setParent(sale, prodMap.get(sale.getProductNumber()));
             tree.setItemCaption(sale, sale.getDescription());
             tree.setItemIcon(sale, getIcon(sale.getStatus()));
        }

As you can see from example above I use the bean object as the ID itself.

Same work for TreeTable. But if the objects dont have overlaping property ID's you should display all your columns with GeneratedColumns or by poplating the propertiy Items in the container manually.

TreeTable treeTable = new TreeTable();
        treeTable.setItemCaptionMode(com.vaadin.ui.TreeTable.ITEM_CAPTION_MODE_EXPLICIT);
        treeTable.addContainerProperty("Period", String.class, "");
        treeTable.addContainerProperty("Invoiced", Double.class, 0.0);
        treeTable.addContainerProperty("Service Cost", Double.class, 0.0);
        treeTable.addContainerProperty("Product Cost", Double.class, 0.0);
        treeTable.setColumnAlignment("Invoiced", TreeTable.ALIGN_RIGHT);
        treeTable.setColumnAlignment("Service Cost", TreeTable.ALIGN_RIGHT);
        treeTable.setColumnAlignment("Product Cost", TreeTable.ALIGN_RIGHT);

        int curPeriod = UtilityFactory.dateToPeriod(startDate);
        int endPeriod = UtilityFactory.dateToPeriod(endDate);
        do {
            SaleBillingInfo billing = new SaleBillingInfo(); // Simple bean to show summary for the month.
            billing.setPeriod(curPeriod);
            billing.setSaleCost(saleSys.cost(sale, curPeriod, true));
            billing.setProductCost(saleSys.cost(sale, curPeriod, false));

            final java.util.List<DocumentLine> debitLines;
            final java.util.List<DocumentLine> creditLines;
            debitLines = app().api().select(DocumentLine.class, "where  (saleNumber = %d or saleNumber = -%d) link to (Document where type in ('RESELLER_INVOICE','INVOICE') and billingPeriod = %d )", sale.getObjKey(), sale.getObjKey(), curPeriod);
            creditLines = app().api().select(DocumentLine.class, "where (saleNumber = %d or saleNumber = -%d) link to (Document where type in ('CREDITNOTE') and billingPeriod = %d )", sale.getObjKey(), sale.getObjKey(), curPeriod);
            billing.setInvoiced(tallyLines(debitLines, creditLines));
            // Populate the item with the required properties. , default Container will generate own itemID.  
            treeTable.addItem(new Object[]{String.format("Period  - %d", curPeriod), billing.getInvoiced(), billing.getSaleCost(), billing.getProductCost()}, billing);
            treeTable.setItemCaption(billing, Integer.toString(curPeriod));
            treeTable.setChildrenAllowed(billing, true);
            for (DocumentLine line : debitLines) {
                treeTable.addItem(new Object[]{String.format("AIV-%d - %s", line.getDocumentNumber(), line.getDescription()), line.getTransactionAmount(), 0, 0}, line);
                treeTable.setParent(line, billing);
            }
            for (DocumentLine line : creditLines) {
                treeTable.addItem(new Object[]{String.format("ACN-%d - %s", line.getDocumentNumber(), line.getDescription()), -line.getTransactionAmount(), 0, 0}, line);
                treeTable.setParent(line, billing);
            }
            allLines.addAll(debitLines);
            allLines.addAll(creditLines);

            curPeriod = UtilityFactory.billingPeriodAdd(curPeriod, 1);
        } while (curPeriod < endPeriod);

I would suggest you add an additional property ID ( that conains the source object though ) , in my case this was not needed.