ItemClickListener Gantt chart

I have tried to get the ItemClickListener in GnattChart. I got the Item but can’t get selected segment. Here is my test code:

weekGanttchart.addGraphItemClickListener(event -> {
    List<DataItem> items = dataProvider.getItems();
    DataItem dataItem = items.get(event.getItemIndex());

    ArrayList<MapDataItem> segments = (ArrayList<MapDataItem>) dataItem.getValue("segments");
    MapDataItem s = segments.get(event.getItemIndex());

    showNotification("" + s.getValue("uuid") );
    UUID uuid = (UUID) s.getValue("uuid");
    Assignment assignment = loadAssignmentById(uuid);
    assignmentDs.setItem(assignment);
    assignmentDs.refresh();
    assignmentFieldGroup.setDatasource(assignmentDs);
    //showNotification("This don't work according to bug. Bug is reported. Waiting for fix");
    });

How can I get the segment i select in the Gantt Chart?

Hi, Kjell

Currently, chart events don’t work properly if a chart hasn’t a datasource (see youtrack issue on the right). In your case, you can use not persistent entities instead of DataItems directly. For example:

Entities:


import com.haulmont.chile.core.annotations.MetaClass;
import com.haulmont.chile.core.annotations.MetaProperty;
import java.util.List;
import com.haulmont.cuba.core.entity.AbstractNotPersistentEntity;

@MetaClass(name = "demo$TaskSpan")
public class TaskSpan extends AbstractNotPersistentEntity {
    private static final long serialVersionUID = 2350551494642402397L;

    @MetaProperty
    protected String category;

    @MetaProperty
    protected List<Segment> segments;

    public void setCategory(String category) {
        this.category = category;
    }

    public String getCategory() {
        return category;
    }

    public void setSegments(List<Segment> segments) {
        this.segments = segments;
    }

    public List<Segment> getSegments() {
        return segments;
    }
}

import com.haulmont.chile.core.annotations.MetaClass;
import com.haulmont.chile.core.annotations.MetaProperty;
import com.haulmont.cuba.core.entity.AbstractNotPersistentEntity;

@MetaClass(name = "demo$Segment")
public class Segment extends AbstractNotPersistentEntity {
    private static final long serialVersionUID = -6805397973094591173L;

    @MetaProperty
    protected Integer start;

    @MetaProperty
    protected Integer duration;

    @MetaProperty
    protected String color;

    @MetaProperty
    protected String task;

    @MetaProperty
    protected TaskSpan taskSpan;

    public void setTaskSpan(TaskSpan taskSpan) {
        this.taskSpan = taskSpan;
    }

    public TaskSpan getTaskSpan() {
        return taskSpan;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    public Integer getStart() {
        return start;
    }

    public void setDuration(Integer duration) {
        this.duration = duration;
    }

    public Integer getDuration() {
        return duration;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public void setTask(String task) {
        this.task = task;
    }

    public String getTask() {
        return task;
    }
}

Note: Segment entity has the link to parent TaskSpan, because in the case of entities GraphItemClickEvent returns segment entity instead of category entity (for GanttChart only).

In a screen descriptor, define a collectionDatasource with refreshMode=“NEVER”


<collectionDatasource id="taskSpansDs"
                              class="com.company.demo.entity.TaskSpan"
                              refreshMode="NEVER"/>

In screen controller, fill the datasource programmatically:


private void generateGanttData() {
        taskSpansDs.refresh();

        taskSpansDs.includeItem(taskSpan("John",
                segment(7, 2, "#7B742C", "Task #1"),
                segment(null, 2, "#7E585F", "Task #2"),
                segment(null, 2, "#CF794A", "Task #3")
        ));
        taskSpansDs.includeItem(taskSpan("Smith",
                segment(10, 2, "#7E585F", "Task #2"),
                segment(null, 1, "#CF794A", "Task #3"),
                segment(null, 4, "#7B742C", "Task #1")
        ));
        ...
    }

    private TaskSpan taskSpan(String category, Segment... segments) {
        TaskSpan taskSpan = new TaskSpan();
        taskSpan.setCategory(category);
        taskSpan.setSegments(Arrays.stream(segments)
                .peek(segment -> segment.setTaskSpan(taskSpan))
                .collect(Collectors.toList()));
        return taskSpan;
    }

    private Segment segment(Integer start, Integer duration, String color, String task) {
        Segment segment = new Segment();
        segment.setStart(start);
        segment.setDuration(duration);
        segment.setColor(color);
        segment.setTask(task);
        return segment;
    }

I’ve uploaded a little demo to github

Regards,

Gleb

:ticket: See the following issue in our bug tracker:

https://youtrack.cuba-platform.com/issue/PL-7655

A post was split to a new topic: Gantt Chart item click listener