Step 10: Show task list

Add new part for tasks

We have implemented Gantt-chart visualization of all tasks. Gantt chart helps to detect the trucks that are too busy or, on the contrary, spend too much time idle.

Now we are going to display all tasks in table form. In that format, tasks can be exported to an Excel file and be further analyzed outside our application.

Go to the Model class and add one more method that returns all created transportation tasks:

public List<TransportationTask> getTransportationTasks() {
    return dispatcher.getTransportationTasks();
}

public void addTaskStateChangeHandler(Consumer<TransportationTask> handler) {
    dispatcher.addTaskStateChangeHandler(handler);
}

public void clearTaskStateChangeHandlers() {
    dispatcher.clearTaskStateChangeHandlers();
}

Go to the '…​application' bundle again and create a new TasksPart class in its 'parts.simulation' package:

TasksPart.java
package com.company.tutorial3.application.parts.simulation;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.function.Function;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.eclipse.swt.widgets.Composite;

import com.amalgamasimulation.desktop.utils.MessageManager;
import com.amalgamasimulation.desktop.ui.views.TableView;
import com.amalgamasimulation.utils.format.Formats;
import com.amalgamasimulation.viewupdater.service.IViewUpdaterService;
import com.company.tutorial3.application.utils.Topics;
import com.company.tutorial3.simulation.model.Model;
import com.company.tutorial3.simulation.model.TransportationTask;
import com.company.tutorial3.simulation.model.Truck;

public class TasksPart {

    @Inject
    private MessageManager messageManager;

    @Inject
    private IViewUpdaterService viewUpdaterService;

    private Model model;
    private TableView<TransportationTask> tableView;

    @PostConstruct
    public void createComposite(Composite parent) {
        tableView = new TableView<>(parent, Collections.emptyList());
        Function<LocalDateTime, String> labelExtractor = v -> v == null ? "" : Formats.getDefaultFormats().dayMonthLongYearHoursMinutes(v);
        tableView.addColumn("Id", t -> t.getId());
        tableView.addColumn("Truck", t -> t.getTruck())
                    .setLabelExtractorNullable(Truck::getName);
        tableView.addColumn("Source", t -> t.getRequest().getSourceAsset().getName());
        tableView.addColumn("Destination", t -> t.getRequest().getDestAsset().getName());
        tableView.addColumn("Created", t -> model.timeToDate(t.getRequest().getCreatedTime()))
                    .setLabelExtractor(labelExtractor);
        tableView.addColumn("Started", t -> model.timeToDate(t.getBeginTime()))
                    .setLabelExtractor(labelExtractor);
        tableView.addColumn("Deadline", t -> model.timeToDate(t.getRequest().getDeadlineTime()))
                    .setLabelExtractor(labelExtractor);
        tableView.addColumn("Completed",
                        t -> t.getRequest().isCompleted() ? model.timeToDate(t.getRequest().getCompletedTime()) : null)
                    .setLabelExtractor(labelExtractor);
        tableView.addColumn("Status", 200, t -> t.getStatus().toString().replace("_", " "));

        messageManager.subscribe(Topics.SHOW_MODEL, this::onShowModel, true);
        viewUpdaterService.getDefaultUpdater().addView(tableView);
    }

    private void onShowModel(Model model) {
        this.model = model;
        tableView.setData(model.getTransportationTasks());
    }
}

Update the 'Application.e4xmi' file

Our application does not use this new class yet. We need to mention this part class in the 'Application.e4xmi' file.

Get to the '…​application' bundle and open the 'Application.e4xmi' file. This file describes the composition of UI elements.

Select the 'Part Stack' element that contains 'Trucks' and 'Gantt Chart' parts. In the right side of the window, click the 'Add' button.

Adding a new part to the application

A new part is created.

Update the 'Label' and the 'Class URI' properties of the new part, as shown in the following screen:

Tasks part properties

'Class URI' in this example is: bundleclass://com.company.tutorial3.application/com.company.tutorial3.application.parts.simulation.TasksPart.

You do not need to type in the Class URI manually - just press the 'Find' button and find the TasksPart class using the dialog window.

Check the result

Start the simulation. A list of transportation tasks is displayed in the new 'Tasks' part:

Simulation tasks

Here are the key features of this table available via its context menu:

  1. Contents can be copied to clipboard.

  2. Contents can be opened in Excel.

  3. In-place table row filtering by specific column value is also supported.