Hierarchical Container of different classes


For the past few days I have been trying to figure out how I can create a hierarchical container which consists of 3 different types of classes.

The first one is a line which has an item and a quantity. The item belongs to a certain category.
I would like to list all orderlines that have items which belong to the same category in a treetable and show them in a hierarchical way.

  • Post is a property of the Category

  • Omschrijving is the description of the category if its a category, otherwise the article inside the line

  • Eenheid is a bean property of the item

  • Aankoopprijs is a property of the item

  • In stock is a property of the line

Is there a way to accomplish this? Or should I create intermediate classes to represent this in the table?


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();
        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.setItemCaption(prod, prod.getDescription());
        for (Sale sale : api().select(Sale.class, "where clientNumber = %d order by productNumber , description", client.getObjKey())) {
             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.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.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);

            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.